covert data as matrix

Hi
need help .... to CONVERT data 5*15 ,that I get it from the following program ....as MATRIX contain row i=5 and column j =15
this is the program:
Q =(unifrnd(0,1,5,15))
save Q
if there is alot of number ... that is difficult to write matrix by using ; in every row.
WHAT I WRITE in the same program???? IF ANY PROF. KNOW...thanks alot

 Risposta accettata

Q =(unifrnd(0,1,5,15))
Q = 5×15
0.6639 0.4524 0.1932 0.7793 0.4019 0.0852 0.7317 0.5443 0.6400 0.9254 0.4015 0.3222 0.5093 0.5372 0.8024 0.1512 0.8782 0.8470 0.3862 0.3625 0.4341 0.9869 0.7286 0.4532 0.8665 0.5559 0.6165 0.0237 0.7311 0.2724 0.5689 0.8799 0.7683 0.6209 0.7952 0.0832 0.7453 0.1717 0.6402 0.4705 0.7399 0.6973 0.7990 0.9779 0.6402 0.6130 0.2077 0.8421 0.2649 0.0269 0.1636 0.5503 0.0439 0.2371 0.0030 0.7877 0.7767 0.4297 0.3760 0.0445 0.0602 0.1058 0.4839 0.0780 0.9417 0.1768 0.0868 0.8750 0.1341 0.3844 0.3747 0.5078 0.4752 0.1798 0.6737
save('Q.txt', 'Q', '-ascii', '-double')
!cat Q.txt
6.6392079079920463e-01 4.5235571596635848e-01 1.9316148221534890e-01 7.7929796267166485e-01 4.0187340004345129e-01 8.5151634550524102e-02 7.3169640609405273e-01 5.4429391635246049e-01 6.3998783353082334e-01 9.2536320087337998e-01 4.0149054447634047e-01 3.2223963473914463e-01 5.0930643031449951e-01 5.3716002761190185e-01 8.0235908232593778e-01 1.5121990903952298e-01 8.7824242681845399e-01 8.4704362397489430e-01 3.8621816305805401e-01 3.6246145933476215e-01 4.3414004021693398e-01 9.8688763144956315e-01 7.2858731426382739e-01 4.5318793660766010e-01 8.6646738130811141e-01 5.5589156556919395e-01 6.1649313001817796e-01 2.3700376853586835e-02 7.3108013038230801e-01 2.7240432119647917e-01 5.6888971954278122e-01 8.7987852225658303e-01 7.6834295200565705e-01 6.2087530894804055e-01 7.9523156025077957e-01 8.3189592572391668e-02 7.4527204071029118e-01 1.7168914014464565e-01 6.4024339383210793e-01 4.7051120988032114e-01 7.3991134708839257e-01 6.9727632429789299e-01 7.9901163482237803e-01 9.7786053731897582e-01 6.4020663985588955e-01 6.1299751870622943e-01 2.0769354912309579e-01 8.4212549598643649e-01 2.6494012680566281e-01 2.6923822357603600e-02 1.6360315270545678e-01 5.5026885098899259e-01 4.3926305010117850e-02 2.3709963300152437e-01 3.0286948911313338e-03 7.8774144114460520e-01 7.7669296695025258e-01 4.2969450904609452e-01 3.7597209698334411e-01 4.4524513343272121e-02 6.0167559639795587e-02 1.0578433110097485e-01 4.8392347456417129e-01 7.8019304296682912e-02 9.4166940478339789e-01 1.7681457887673979e-01 8.6762820992417611e-02 8.7495394112995439e-01 1.3413795755485414e-01 3.8441577935736448e-01 3.7470793006034464e-01 5.0775182098339389e-01 4.7524575666372515e-01 1.7981150219493136e-01 6.7370366631287792e-01

35 Commenti

hasan s
hasan s il 23 Feb 2021
thanks a lot prof. Walter for your help...
what mean '-ascii', and '-double'???
and not appear in my program as matrix.
please write it as matrix in first step to show it .
then save it in another step
Walter Roberson
Walter Roberson il 23 Feb 2021
save() can save data either as binary files (not readable by humans) or as text files. The -ascii option tells it to save as text, and the -double option says to use double precision when it does that.
I am not sure what you mean by not appearing in your program as a matrix? The assignment to Q makes Q a matrix holding the result, and you can display that to the command window the way I did or by using disp(Q)
hasan s
hasan s il 23 Feb 2021
Modificato: Walter Roberson il 27 Feb 2021
thank you for your reply...
I write display Q in the program , but not appear any thing :
Q =(unifrnd(0,1,5,15));
save('Q.txt', 'Q', '-ascii', '-double')
display Q
I need to show this data as matrix.. and I want to use this matrix name in another program...what is the name of matrix,???
Q = unifrnd(0,1,5,15);
save('Q.txt', 'Q', '-ascii', '-double')
disp(Q)
The name of the matrix is Q
Note: it is not necessary to save to the text file in order to display the matrix or pass it on to another part of the program. I only put in the save to file because your original code had a call to save()
hasan s
hasan s il 23 Feb 2021
pardon..you mean ...I can deal data as matrix ...not need to convert or add anything???
After the line
Q = unifrnd(0,1,5,15);
then Q is already the matrix of data. You can
disp(Q)
0.4793 0.8762 0.9996 0.9766 0.1466 0.5964 0.1777 0.9023 0.9839 0.7071 0.8686 0.4813 0.8447 0.1321 0.4197 0.8243 0.4757 0.0873 0.4524 0.3155 0.6068 0.1987 0.0429 0.4452 0.7281 0.2872 0.0883 0.7353 0.0966 0.9672 0.1782 0.7150 0.8373 0.8565 0.8762 0.6715 0.0130 0.8911 0.7683 0.0246 0.3806 0.5532 0.8728 0.1065 0.3088 0.3164 0.9974 0.8146 0.8010 0.7779 0.1290 0.6169 0.6383 0.7632 0.1078 0.5700 0.6541 0.3456 0.7388 0.9914 0.3927 0.3446 0.4765 0.3013 0.5837 0.6138 0.8402 0.1154 0.0497 0.2661 0.1674 0.3935 0.6073 0.8192 0.7342
if you need to, or pass it to something else, like
std(Q)
ans = 1×15
0.2425 0.2714 0.3643 0.2868 0.3069 0.2224 0.3449 0.4151 0.3637 0.3319 0.2742 0.2157 0.2147 0.3668 0.3114
hasan s
hasan s il 23 Feb 2021
Modificato: hasan s il 23 Feb 2021
Your information is great ..
but why when I want Q(i,j) in another program ,(i=1:5, j=1:15), in loops ,the program not read it....???
Q = unifrnd(0,1,3,5);
RST(Q)
Q(1,1) = 0.57049 Q(1,2) = 0.97833 Q(1,3) = 0.62962 Q(1,4) = 0.15659 Q(1,5) = 0.43139 Q(2,1) = 0.15080 Q(2,2) = 0.11099 Q(2,3) = 0.30354 Q(2,4) = 0.95066 Q(2,5) = 0.53604 Q(3,1) = 0.37450 Q(3,2) = 0.04654 Q(3,3) = 0.27853 Q(3,4) = 0.08982 Q(3,5) = 0.22130
function RST(Q)
for i = 1 : size(Q,1)
for j = 1 : size(Q,2)
fprintf('Q(%d,%d) = %.5f\n', i, j, Q(i,j));
end
end
end
(Note: I used a smaller matrix to get shorter output for demonstration purposes.)
hasan s
hasan s il 24 Feb 2021
Modificato: hasan s il 24 Feb 2021
thanks a lot prof.walter
THE last program not run in my matlab
Error in RST (line 2)
for i = 1 : size(Q,1)
..what mean size (Q,1), 1 refer to what ??
I think you tried to run RST without passing it any values. Notice in my example above I assign to Q and then I have the line of code
RST(Q)
which invokes RST passing in the value of Q.
size(Q,1) computes the number of rows that Q has. size(Q,2) is the number of columns that Q has.
thank you very much for your reply.
After your valuable information prof. Walter ,
I take
Q1 = unifrnd(0,1,10,1000);
RST(Q1)
but , take the data Q1, in the another program , alot of outputs is NAN. That is another program
sum1=0;sum2=0;sum3=0;sum4=0;sum5=0;sum6=0;sum7=0;sum8=0;sum9=0;
sum10=0;sum11=0;sum12=0;sum13=0;sum14=0;
err=10;
A=1;
B=2;
G=3;
Q=Q1;
while(err>[0.00001;0.00005;0.00002])
for j=1:1000
for i=1:10
sum1=sum1+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
sum2=sum2+(Q(i,j));
sum3=sum3+(G(j)*((Q(i,j))^(G(j)-1)))/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
sum4=sum4+(Q(i,j))^(G(j));
sum5=sum5+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))));
sum6=sum6+((Q(i,j))^(G(j)))*log(Q(i,j));
sum7=sum7+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))))^2;
sum8=sum8+(G(j)*((Q(i,j))^(G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum9=sum9+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum10=sum10+(((G(j))^2)*((Q(i,j))^(2*G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum11=sum11+((((G(j))*log(Q(i,j)))*((Q(i,j))^(2*(G(j)-1))))+((Q(i,j))^(2*(G(j)-1))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum12=sum12+(((G(j)*(log(Q(i,j)))^2)*((Q(i,j))^(G(j)-1)))+((2*log(Q(i,j)))*((Q(i,j))^(G(j)-1))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))));
sum13=sum13+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j)-1)))^2/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum14=sum14+(log(Q(i,j)))^2*((Q(i,j))^(G(j)));
end
x=[sum1-sum2;sum3-sum4;B(j)*sum5-B(j)*sum6];
y1=(-sum1);
y2=(-sum8);
y3=(-B(j))*sum9;
y4=(-sum8);
y5=(-sum10);
y6=((-B(j))*(G(j)))*sum11+sum5-sum6;
y7=(-B(j))*sum9;
y8=(-(B(j)*G(j)))*sum11+sum5-sum6;
y9=(B(j))*sum12-((B(j))^2)*sum13-(B(j))*sum14;
d=[y1,y2,y3;y4,y5,y6;y7,y8,y9];
d=real(d);
x=real(x);
z0=[A(j);B(j);G(j)]
p=inv(d)*x
z=z0-p;
err=abs(z-z0);
A(j+1)=z(1);
B(j+1)=z(2);
G(j+1)=z(3);
z0=z;
end
end
A
I dont know the reason why ???what I change please to get all values of A are real, not NAN???
I must take column1, column 2,..., column 1000
I must take the first column of matrix to get first value of A
and take the second column of matix to find second value of A
and so on to get 1000 value of A
Can you help me ???
while(err>[0.00001;0.00005;0.00002])
should be
while any(err>[0.00001;0.00005;0.00002])
You have a while loop based upon err. Inside the while loop, you always do 1000 iterations. Are you sure that is what you want to do? It would be common instead to want to loop until error is satisfied but also have a maximum number of iterations
p=inv(d)*x
Caution: using inv(d)*x is not as accurate as using d\x . Also, you will find that d can become singular, leading you to warning messages.
When you reach the maximum number of iterations, if the error is not satisfied, you go back and redo the 1000 iterations again, writing on top of all of the A, B, G locations you wrote to before. The conditions are not exactly the same for each iteration, as you do not initialize the sum* variables in the loop, so each time you are adding more to the accumulated sum. Are you sure that is what you want to do? You have no limits on the number of while loop iterations you will do.
hasan s
hasan s il 24 Feb 2021
thank you very much for your help..
I want A and B and G , and error err=abs(z-z0).
but tried with A now.
I put while loop since I need to calculate the above error ..can I delete it ???or it is nescesarry??
I need take the first column of matrix to get first value of A
and take the second column of matix to find second value of A
and so on to get 1000 value of A.
I put sum to evaluate the row for each column I take.
sum1=sum1+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
Is that sum only intended to take into account the current column? You do not set sum1 to zero for each new column, so at the moment the sum* variables are effectively becoming cumulative from the first column.
Using a while loop on err would be something you would do if you want to stop executing something when the error becomes small enough. I do not see anything in your code that is doing that at the moment.
hasan s
hasan s il 24 Feb 2021
thank you very much for your help..
I want A and B and G , and error err=abs(z-z0).
but tried with A now.
I put while loop since I need to calculate the above error ..can I delete it ???or it is nescesarry??
I need take the first column of matrix to get first value of A
and take the second column of matix to find second value of A
and so on to get 1000 value of A.
I put sum to evaluate the row for each column I take.
d/x, or x/d instead of p=inv(d)*x???
if I print / to express to divission instead of \ is possible? or there is different
hasan s
hasan s il 24 Feb 2021
Modificato: hasan s il 24 Feb 2021
what I do about your words "Is that sum only intended to take into account the current column? You do not set sum1 to zero for each new column, so at the moment the sum* variables are effectively becoming cumulative from the first column."
i =1:10 , I put sum to evaluate the rows for each new column I take.
How I set sum1 to zero for each new column,??PLEASE
((( I have :
z0=[A(j);B(j);G(j)]
p=inv(d)*x
z=z0-p;
will becoming cumulative in every loop, since will take :
z0=z;
is right ????
N = 2;
data = rand(10,N);
fprintf('what you have now\n');
what you have now
sum1 = 0;
for j = 1 : N
for i = 1 : 10
sum1 = sum1 + data(i,j);
end
sum1
end
sum1 = 4.0624
sum1 = 9.2656
Value of sum1 for j = 2 includes everything derived from the sum1 for j = 1
fprintf('\nbut did you mean\n');
but did you mean
for j = 1 : N
sum1 = 0;
for i = 1 : 10
sum1 = sum1 + data(i,j);
end
sum1
end
sum1 = 4.0624
sum1 = 5.2032
Here the value of sum1 for j = 2 only depends upon the information for this column, and does not include values calculated in previous columns
hasan s
hasan s il 25 Feb 2021
Modificato: hasan s il 25 Feb 2021
yes..prof. Walter
I need , as you say >> the value of sum1 for j = 2 only depends upon the information for this column, and does not include values calculated in previous column.
I change it ,
B=2;
G=3;
Q=Q1;
for j=1:1000
sum1=0;sum2=0;sum3=0;sum4=0;sum5=0;sum6=0;sum7=0;sum8=0;sum9=0;
sum10=0;sum11=0;sum12=0;sum13=0;sum14=0;
for i=1:10
sum1=sum1+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
sum2=sum2+(Q(i,j));
sum3=sum3+(G(j)*((Q(i,j))^(G(j)-1)))/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
sum4=sum4+(Q(i,j))^(G(j));
sum5=sum5+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))));
sum6=sum6+((Q(i,j))^(G(j)))*log(Q(i,j));
sum7=sum7+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))))^2;
sum8=sum8+(G(j)*((Q(i,j))^(G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum9=sum9+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum10=sum10+(((G(j))^2)*((Q(i,j))^(2*G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum11=sum11+((((G(j))*log(Q(i,j)))*((Q(i,j))^(2*(G(j)-1))))+((Q(i,j))^(2*(G(j)-1))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum12=sum12+(((G(j)*(log(Q(i,j)))^2)*((Q(i,j))^(G(j)-1)))+((2*log(Q(i,j)))*((Q(i,j))^(G(j)-1))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))));
sum13=sum13+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j)-1)))^2/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum14=sum14+(log(Q(i,j)))^2*((Q(i,j))^(G(j)));
end
sum1
sum2
sum3
sum4
sum5
sum6
sum7
sum8
sum9
sum10
sum11
sum12
sum13
sum14
x=[sum1-sum2;sum3-sum4;B(j)*sum5-B(j)*sum6];
y1=(-sum1);
y2=(-sum8);
y3=(-B(j))*sum9;
y4=(-sum8);
y5=(-sum10);
y6=((-B(j))*(G(j)))*sum11+sum5-sum6;
y7=(-B(j))*sum9;
y8=(-(B(j)*G(j)))*sum11+sum5-sum6;
y9=(B(j))*sum12-((B(j))^2)*sum13-(B(j))*sum14;
d=[y1,y2,y3;y4,y5,y6;y7,y8,y9];
d=real(d);
x=real(x);
z0=[A(j);B(j);G(j)]
p=inv(d)*x
z=z0-p;
err=abs(z-z0);
A(j+1)=z(1);
B(j+1)=z(2);
G(j+1)=z(3);
z0=z;
end
A;
When I run the program , all sum is NAN ...except sum 2...since it is the only which contain data(i,j) only
but other sums contain A(j), B(j), G(j),log(Q(i,j));
the NAN of sums give all the 1000 - value of A is NAN
What I do ??? please
I change it again to the following..but also most of values of A is NAN.
A=1;
B=2;
G=3;
Q=Q1;
for j=1:1000
s1=0;sum1=0;
for i=1:n
s1=s1+1
sum1=sum1+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
end
sum1
s2=0;sum2=0;
for i=1:n
s2=s2+1;
sum2=sum2+(Q(i,j));
end
sum2;
s3=0;sum3=0;
for i=1:n
s3=s3+1 ;
sum3=sum3+(G(j)*((Q(i,j))^(G(j)-1)))/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
end
sum3;
s4=0;sum4=0;
for i=1:n
s4=s4+1;
sum4=sum4+(Q(i,j))^(G(j));
end
sum4;
s5=0;sum5=0;
for i=1:n
s5=s5+1 ;
sum5=sum5+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))));
end
sum5;
s6=0;sum6=0;
for i=1:n
s6=s6+1 ;
sum6=sum6+((Q(i,j))^(G(j)))*log(Q(i,j));
end
sum6;
s7=0;sum7=0;
for i=1:n
s7=s7+1 ;
sum7=sum7+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))))^2;
end
sum7;
s8=0;sum8=0;
for i=1:n
s8=s8+1 ;
sum8=sum8+(G(j)*((Q(i,j))^(G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
end
sum8;
s9=0;sum9=0;
for i=1:n
s9=s9+1;
sum9=sum9+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
end
sum9;
s10=0;sum10=0;
for i=1:n
s10=s10+1;
sum10=sum10+(((G(j))^2)*((Q(i,j))^(2*G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
end
sum10;
s11=0;sum11=0;
for i=1:n
s11=s11+1 ;
sum11=sum11+((((G(j))*log(Q(i,j)))*((Q(i,j))^(2*(G(j)-1))))+((Q(i,j))^(2*(G(j)-1))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
end
sum11
s12=0;sum12=0;
for i=1:n
s12=s12+1 ;
sum12=sum12+(((G(j)*(log(Q(i,j)))^2)*((Q(i,j))^(G(j)-1)))+((2*log(Q(i,j)))*((Q(i,j))^(G(j)-1))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))));
end
sum12
s13=0;sum13=0;
for i=1:n
s13=s13+1 ;
sum13=sum13+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j)-1)))^2/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
end
sum13
s14=0;sum14=0;
for i=1:n
s14=s14+1 ;
sum14=sum14+(log(Q(i,j)))^2*((Q(i,j))^(G(j)));
end
sum14
x=[sum1-sum2;sum3-sum4;B(j)*sum5-B(j)*sum6];
y1=(-sum1);
y2=(-sum8);
y3=(-B(j))*sum9;
y4=(-sum8);
y5=(-sum10);
y6=((-B(j))*(G(j)))*sum11+sum5-sum6;
y7=(-B(j))*sum9;
y8=(-(B(j)*G(j)))*sum11+sum5-sum6;
y9=(B(j))*sum12-((B(j))^2)*sum13-(B(j))*sum14;
d=[y1,y2,y3;y4,y5,y6;y7,y8,y9];
d=real(d);
x=real(x);
z0=[A(j);B(j);G(j)]
p=d\x;
z=z0-p;
err=abs(z-z0);
A(j+1)=z(1);
B(j+1)=z(2);
G(j+1)=z(3);
z0=z;
end
A
what I change ???
rng(123456)
digits(32)
N = 1000;
Q1 = rand(10,N);
A = zeros(1,N+1,'sym');
B = zeros(1,N+1,'sym');
G = zeros(1,N+1,'sym');
A(1) = 1;
B(1) = 2;
G(1) = 3;
Q = vpa(Q1);
for j=1:1000
sum1=0;sum2=0;sum3=0;sum4=0;sum5=0;sum6=0;sum7=0;sum8=0;sum9=0;
sum10=0;sum11=0;sum12=0;sum13=0;sum14=0;
for i=1:10
sum1=sum1+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
sum2=sum2+(Q(i,j));
sum3=sum3+(G(j)*((Q(i,j))^(G(j)-1)))/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))));
sum4=sum4+(Q(i,j))^(G(j));
sum5=sum5+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))));
sum6=sum6+((Q(i,j))^(G(j)))*log(Q(i,j));
sum7=sum7+1/(A(j)+(G(j)*B(j)*((Q(i,j))^(G(j)-1))))^2;
sum8=sum8+(G(j)*((Q(i,j))^(G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum9=sum9+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum10=sum10+(((G(j))^2)*((Q(i,j))^(2*G(j)-1)))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum11=sum11+((((G(j))*log(Q(i,j)))*((Q(i,j))^(2*(G(j)-1))))+((Q(i,j))^(2*(G(j)-1))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum12=sum12+(((G(j)*(log(Q(i,j)))^2)*((Q(i,j))^(G(j)-1)))+((2*log(Q(i,j)))*((Q(i,j))^(G(j)-1))))/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))));
sum13=sum13+(((G(j)*log(Q(i,j)))*((Q(i,j))^(G(j)-1)))+((Q(i,j))^(G(j)-1)))^2/(A(j)+(B(j)*G(j)*((Q(i,j))^(G(j)-1))))^2;
sum14=sum14+(log(Q(i,j)))^2*((Q(i,j))^(G(j)));
end
% disp(sum1)
% disp(sum2)
% disp(sum3)
% disp(sum4)
% disp(sum5)
% disp(sum6)
% disp(sum7)
% disp(sum8)
% disp(sum9)
% disp(sum10)
% disp( sum11)
% disp(sum12)
% disp(sum13)
% disp(sum14)
x=[sum1-sum2;sum3-sum4;B(j)*sum5-B(j)*sum6];
y1=(-sum1);
y2=(-sum8);
y3=(-B(j))*sum9;
y4=(-sum8);
y5=(-sum10);
y6=((-B(j))*(G(j)))*sum11+sum5-sum6;
y7=(-B(j))*sum9;
y8=(-(B(j)*G(j)))*sum11+sum5-sum6;
y9=(B(j))*sum12-((B(j))^2)*sum13-(B(j))*sum14;
d=[y1,y2,y3;y4,y5,y6;y7,y8,y9];
d=real(d);
x=real(x);
z0=[A(j);B(j);G(j)];
if rank(d) < size(d,1)
fprintf('Early termination at j = %d due to rank failure\n', j);
break;
end
p=d\x;
z=z0-p;
err=abs(z-z0);
A(j+1)=z(1);
B(j+1)=z(2);
G(j+1)=z(3);
z0=z;
end
Early termination at j = 20 due to rank failure
disp(vpa(A(1:j),5))
disp(vpa(B(1:j),5))
disp(vpa(G(1:j),5))
With this particular rng seed and digits(50), the output will be
Early termination at j = 29 due to rank failure
[1.0, 1.0349, 0.86581, 0.99576, 1.0175, -0.18171, -5.6105, -5.8035, -6.7836, -29.428, -2.7838, -0.10434, 0.44927, 1.2216, 1.5661, 1.4736, 1.3823, 1.7725, 1.6324, 1.4034, 1.7778, 2.0341, 2.0359, 1.7352, 1.9228, 2.0852, 2.1461, 1.7618, 2.0146]
[2.0, 1.801, 2.8963, 1.3459, 2.3176, 5.4269, 22.873, 29.808, 46.481, 116.8, 340.77, 1660.9, 1538.6, 1379.8, 1382.8, 1427.6, 1745.9, 1747.5, 1736.8, 1706.3, 1707.5, 1704.6, 1701.4, 1685.4, 1684.3, 1684.5, 1673.8, 1665.6, 1664.2]
[3.0, 2.9351, 2.8183, 2.4457, 3.3412, 0.52283, 1.9538, 2.1644, 2.4915, 6.3134, 15.635, 55.825, 69.911, 79.92, 82.984, 112.25, 194.11, 197.78, 248.55, 291.33, 293.3, 306.1, 319.32, 387.32, 398.36, 401.89, 435.72, 550.72, 567.52]
What is happening? Well, once G gets large enough then since Q < 1, Q^G will be small and most of your sums get small but the first one stays comparatively large. This leads to small x(2) and especially small x(3) which leads to large responses on the p\x and that leads to even larger G values . Eventually everything in d except the first element vanishes compared to the first element, giving you a matrix of rank 1 (or 2, depending on how much arithmetic error you allowed for.)
You can increase digits() to postpone the exact place the rank becomes a problem, but you don't get far.
hasan s
hasan s il 26 Feb 2021
Modificato: hasan s il 27 Feb 2021
Thank you very much for your help
A, B , and G is the initial value ,,can change it to any value (greater than zero.)
I change it alot but also get NAN -values.
hasan s
hasan s il 26 Feb 2021
Modificato: hasan s il 27 Feb 2021
What I do ,please?
Is this file has no solution in j=1000??
this is newton raphson of non linear equations method ..but data is matrix.
is the algorithm is false ??
Walter Roberson
Walter Roberson il 27 Feb 2021
Yes the algorithm is not valid for that purpose. To do that solving on a matrix, reshape from a matrix into a vector and do newton raphson on the vector. For example for a 10 by 1000 array representing 10 states, then reshape to 10000 states (that happen to calculate in 10 different ways.)
hasan s
hasan s il 27 Feb 2021
Modificato: hasan s il 27 Feb 2021
thank you very very much for your great idea.
in the end of this idea, How many values of A can I obtain??
in this case I donot use Q(i,j), in the program?? I will use Q(i)?? thats right??
hasan s
hasan s il 27 Feb 2021
Modificato: hasan s il 27 Feb 2021
I change it : Is that right?? is the algorithm right for newton raphson of non linear equations??
N = 1000;
Q1 = rand(10,N);
Q=reshape(Q1,1,[]); % convert matrix to row vector
A(1) = 1;
B(1) = 2;
G(1) = 3;
for i=1:2
sum1=0;sum2=0;sum3=0;sum4=0;sum5=0;sum6=0;sum7=0;sum8=0;sum9=0;
sum10=0;sum11=0;sum12=0;sum13=0;sum14=0;
for j=1:10000
sum1=sum1+1/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))));
sum2=sum2+(Q(j));
sum3=sum3+(G(i)*((Q(j))^(G(i)-1)))/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))));
sum4=sum4+(Q(j))^(G(i));
sum5=sum5+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))));
sum6=sum6+((Q(j))^(G(i)))*log(Q(j));
sum7=sum7+1/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))))^2;
sum8=sum8+(G(i)*((Q(j))^(G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum9=sum9+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum10=sum10+(((G(i))^2)*((Q(j))^(2*G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum11=sum11+((((G(i))*log(Q(j)))*((Q(j))^(2*(G(i)-1))))+((Q(j))^(2*(G(i)-1))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum12=sum12+(((G(i)*(log(Q(j)))^2)*((Q(j))^(G(i)-1)))+((2*log(Q(j)))*((Q(j))^(G(i)-1))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))));
sum13=sum13+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i)-1)))^2/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum14=sum14+(log(Q(j)))^2*((Q(j))^(G(i)));
end
% disp(sum1)
% disp(sum2)
% disp(sum3)
% disp(sum4)
% disp(sum5)
% disp(sum6)
% disp(sum7)
% disp(sum8)
% disp(sum9)
% disp(sum10)
% disp( sum11)
% disp(sum12)
% disp(sum13)
% disp(sum14)
x=[sum1-sum2;sum3-sum4;B(i)*sum5-B(i)*sum6];
y1=(-sum1);
y2=(-sum8);
y3=(-B(i))*sum9;
y4=(-sum8);
y5=(-sum10);
y6=((-B(i))*(G(i)))*sum11+sum5-sum6;
y7=(-B(i))*sum9;
y8=(-(B(i)*G(i)))*sum11+sum5-sum6;
y9=(B(i))*sum12-((B(i))^2)*sum13-(B(i))*sum14;
d=[y1,y2,y3;y4,y5,y6;y7,y8,y9];
d=real(d);
x=real(x);
z0=[A(i);B(i);G(i)]
p=d\x;
z=z0-p;
err=abs(z-z0);
A(i+1)=z(1);
B(i+1)=z(2);
G(i+1)=z(3);
z0=z;
end
A
B
G
when run ,I get
A =
1.0000 1.2030 1.2808 1.3323 1.3486 1.3537 1.3557 1.3565 1.3569 1.3570 1.3571
B =
2.0000 2.6133 2.7633 2.7282 2.7355 2.7333 2.7333 2.7331 2.7331 2.7330 2.7330
G =
3.0000 4.7106 6.1480 6.9308 7.2657 7.3095 7.3186 7.3223 7.3240 7.3247 7.3250
but ...why the out put of A,B,G have ten output ,while I determinant i=2 , ??
thanks a lot for your help prof. Walter
hasan s
hasan s il 27 Feb 2021
Is
A = zeros(1,N+1,'sym');
B = zeros(1,N+1,'sym');
G = zeros(1,N+1,'sym');
add in the last program ?? or not
A = zeros(1,N+1,'sym');
was something I added in the last version I posted. You were running into problems with p\x so I moved everything to symbolic since symbolic work can be at higher precision (digits) and using higher digits often postpones the point at which you can no longer do meaningful calculations.
hasan s
hasan s il 27 Feb 2021
Modificato: hasan s il 27 Feb 2021
ok prof. thanks for reply
what about the program??is right?
N = 1000;
Q1 = rand(10,N);
Q=reshape(Q1,1,[]); % convert matrix to row vector
A(1) = 1;
B(1) = 2;
G(1) = 3;
for i=1:2
sum1=0;sum2=0;sum3=0;sum4=0;sum5=0;sum6=0;sum7=0;sum8=0;sum9=0;
sum10=0;sum11=0;sum12=0;sum13=0;sum14=0;
for j=1:10000
sum1=sum1+1/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))));
sum2=sum2+(Q(j));
sum3=sum3+(G(i)*((Q(j))^(G(i)-1)))/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))));
sum4=sum4+(Q(j))^(G(i));
sum5=sum5+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))));
sum6=sum6+((Q(j))^(G(i)))*log(Q(j));
sum7=sum7+1/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))))^2;
sum8=sum8+(G(i)*((Q(j))^(G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum9=sum9+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum10=sum10+(((G(i))^2)*((Q(j))^(2*G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum11=sum11+((((G(i))*log(Q(j)))*((Q(j))^(2*(G(i)-1))))+((Q(j))^(2*(G(i)-1))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum12=sum12+(((G(i)*(log(Q(j)))^2)*((Q(j))^(G(i)-1)))+((2*log(Q(j)))*((Q(j))^(G(i)-1))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))));
sum13=sum13+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i)-1)))^2/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum14=sum14+(log(Q(j)))^2*((Q(j))^(G(i)));
end
% disp(sum1)
% disp(sum2)
% disp(sum3)
% disp(sum4)
% disp(sum5)
% disp(sum6)
% disp(sum7)
% disp(sum8)
% disp(sum9)
% disp(sum10)
% disp( sum11)
% disp(sum12)
% disp(sum13)
% disp(sum14)
x=[sum1-sum2;sum3-sum4;B(i)*sum5-B(i)*sum6];
y1=(-sum1);
y2=(-sum8);
y3=(-B(i))*sum9;
y4=(-sum8);
y5=(-sum10);
y6=((-B(i))*(G(i)))*sum11+sum5-sum6;
y7=(-B(i))*sum9;
y8=(-(B(i)*G(i)))*sum11+sum5-sum6;
y9=(B(i))*sum12-((B(i))^2)*sum13-(B(i))*sum14;
d=[y1,y2,y3;y4,y5,y6;y7,y8,y9];
d=real(d);
x=real(x);
z0=[A(i);B(i);G(i)]
p=d\x;
z=z0-p;
err=abs(z-z0);
A(i+1)=z(1);
B(i+1)=z(2);
G(i+1)=z(3);
z0=z;
end
A
B
G
when run ,I get
A =
1.0000 1.2030 1.2808 1.3323 1.3486 1.3537 1.3557 1.3565 1.3569 1.3570 1.3571
B =
2.0000 2.6133 2.7633 2.7282 2.7355 2.7333 2.7333 2.7331 2.7331 2.7330 2.7330
G =
3.0000 4.7106 6.1480 6.9308 7.2657 7.3095 7.3186 7.3223 7.3240 7.3247 7.3250
why the out put of A,B,G have ten output ,while I determinant i=2 , ??
N = 1000;
Q1 = rand(10,N);
Q=reshape(Q1,1,[]); % convert matrix to row vector
A(1) = 1;
B(1) = 2;
G(1) = 3;
for i=1:2
sum1=0;sum2=0;sum3=0;sum4=0;sum5=0;sum6=0;sum7=0;sum8=0;sum9=0;
sum10=0;sum11=0;sum12=0;sum13=0;sum14=0;
for j=1:10000
sum1=sum1+1/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))));
sum2=sum2+(Q(j));
sum3=sum3+(G(i)*((Q(j))^(G(i)-1)))/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))));
sum4=sum4+(Q(j))^(G(i));
sum5=sum5+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))));
sum6=sum6+((Q(j))^(G(i)))*log(Q(j));
sum7=sum7+1/(A(i)+(G(i)*B(i)*((Q(j))^(G(i)-1))))^2;
sum8=sum8+(G(i)*((Q(j))^(G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum9=sum9+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum10=sum10+(((G(i))^2)*((Q(j))^(2*G(i)-1)))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum11=sum11+((((G(i))*log(Q(j)))*((Q(j))^(2*(G(i)-1))))+((Q(j))^(2*(G(i)-1))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum12=sum12+(((G(i)*(log(Q(j)))^2)*((Q(j))^(G(i)-1)))+((2*log(Q(j)))*((Q(j))^(G(i)-1))))/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))));
sum13=sum13+(((G(i)*log(Q(j)))*((Q(j))^(G(i)-1)))+((Q(j))^(G(i)-1)))^2/(A(i)+(B(i)*G(i)*((Q(j))^(G(i)-1))))^2;
sum14=sum14+(log(Q(j)))^2*((Q(j))^(G(i)));
end
% disp(sum1)
% disp(sum2)
% disp(sum3)
% disp(sum4)
% disp(sum5)
% disp(sum6)
% disp(sum7)
% disp(sum8)
% disp(sum9)
% disp(sum10)
% disp( sum11)
% disp(sum12)
% disp(sum13)
% disp(sum14)
x=[sum1-sum2;sum3-sum4;B(i)*sum5-B(i)*sum6];
y1=(-sum1);
y2=(-sum8);
y3=(-B(i))*sum9;
y4=(-sum8);
y5=(-sum10);
y6=((-B(i))*(G(i)))*sum11+sum5-sum6;
y7=(-B(i))*sum9;
y8=(-(B(i)*G(i)))*sum11+sum5-sum6;
y9=(B(i))*sum12-((B(i))^2)*sum13-(B(i))*sum14;
d=[y1,y2,y3;y4,y5,y6;y7,y8,y9];
d=real(d);
x=real(x);
z0=[A(i);B(i);G(i)]
p=d\x;
z=z0-p;
err=abs(z-z0);
A(i+1)=z(1);
B(i+1)=z(2);
G(i+1)=z(3);
z0=z;
end
z0 = 3×1
1 2 3
z0 = 3×1
1.1665 2.6140 4.6185
A
A = 1×3
1.0000 1.1665 1.2490
B
B = 1×3
2.0000 2.6140 2.7217
G
G = 1×3
3.0000 4.6185 5.9808
Looks like 2+1 = 3 outputs to me.
As you do not initialize all of A, you are counting on A not existing already, but your A did exist, from a previous run in which your for i limit was 10 instead of 2.
Walter Roberson
Walter Roberson il 27 Feb 2021
I think it is unlikely that your revised program is correct, but you have not shown us your equations, so the best I can say is "Maybe?". There are equations for which that program might be suitable... I just think it unlikely that those are the equations you need to implement.
hasan s
hasan s il 27 Feb 2021
maybe....I have a problem with the program Matlab ... previously, I run the program, but the output contain ten element...when close matlab and open it again and run the program the output is true as you write...and run it again the output are 5 values.. I dont know what the reason
this is homework with this equations , when y1,..., y9 are given ..and must use while...that I deleted it
Yes, sure, in the exam there will be other equations that I do not know yet.
Because the questions change in the exam. ...
thank you very very much...
I hope that this algorithm is suitable for this above equations at least for the time being
hasan s
hasan s il 27 Feb 2021
thanks a lot
Walter Roberson
Walter Roberson il 19 Mar 2021
Sorry, I do not have the resources to assist you with any additional questions for a while.
hasan s
hasan s il 21 Mar 2021
thanks alot

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by