How to use loop with vpasolve ??
Mostrar comentarios más antiguos
In the following code, I want to use loop with vpasolve. For different values of alpha, I want to solve the equation to get different values of r.
syms alpha mio B r
mio=0.6;
B=2;
alpha=1:0.5:6;
alpha_length=length(alpha);
for i=1:alpha_length
bast(i)=(r(i)/0.5).^(alpha(i)*(mio-1));
mqam_part1(i)=3*B*((sqrt(3)/2).^(alpha(i)*mio));
mqam_part2(i)=((0.5*sqrt(3)).^(-alpha(i)))+((1.5*sqrt(3)).^(-alpha(i)));
mqam_part3(i)=3*(((r(i)/0.5)).^(alpha(i)*mio));
mqam_part4(i)=((sqrt(3)-(r(i)/0.5)).^-alpha(i));
mqam_part5(i)=((2*sqrt(3))-(r(i)/0.5)).^-alpha(i);
mqam_part6(i)=mqam_part4(i)+mqam_part5(i);
mqam_part7(i)=2*((3-(r(i)/0.5)).^-alpha(i));
mqam_part8(i)=6*(((r(i)/0.5)).^(alpha(i)*mio));
mqam_part9(i)=6*B*(2.^-alpha(i));
eqn_LHS(i)=bast(i)/(mqam_part1(i)+mqam_part2(i))+(mqam_part3(i)*(mqam_part6(i)+mqam_part7(i)));
eqn_RHS(i)=B/((mqam_part8(i)*mqam_part6(i))+mqam_part9(i));
eqn1(i)=eqn_LHS(i)==eqn_RHS(i);
sol_positive(i) = vpasolve(eqn1(i),r(i),[0 Inf]);
end
But, After running this code, it shows the following error.
Error using subsref
Index exceeds matrix dimensions.
Error in sym/subsref (line 771)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in tetra_v3 (line 10)
bast(i)=(r(i)/0.5).^(alpha(i)*(mio-1));
So, my question is: What is the cause of these errors and how to solve them ?? and How to use loop with vpasolve ??
1 comentario
Image Analyst
el 1 de Feb. de 2020
Original question by Erman:
In the following code, I want to use loop with vpasolve. For different values of alpha, I want to solve the equation to get different values of r.
syms alpha mio B r
mio=0.6;
B=2;
alpha=1:0.5:6;
alpha_length=length(alpha);
for i=1:alpha_length
bast(i)=(r(i)/0.5).^(alpha(i)*(mio-1));
mqam_part1(i)=3*B*((sqrt(3)/2).^(alpha(i)*mio));
mqam_part2(i)=((0.5*sqrt(3)).^(-alpha(i)))+((1.5*sqrt(3)).^(-alpha(i)));
mqam_part3(i)=3*(((r(i)/0.5)).^(alpha(i)*mio));
mqam_part4(i)=((sqrt(3)-(r(i)/0.5)).^-alpha(i));
mqam_part5(i)=((2*sqrt(3))-(r(i)/0.5)).^-alpha(i);
mqam_part6(i)=mqam_part4(i)+mqam_part5(i);
mqam_part7(i)=2*((3-(r(i)/0.5)).^-alpha(i));
mqam_part8(i)=6*(((r(i)/0.5)).^(alpha(i)*mio));
mqam_part9(i)=6*B*(2.^-alpha(i));
eqn_LHS(i)=bast(i)/(mqam_part1(i)+mqam_part2(i))+(mqam_part3(i)*(mqam_part6(i)+mqam_part7(i)));
eqn_RHS(i)=B/((mqam_part8(i)*mqam_part6(i))+mqam_part9(i));
eqn1(i)=eqn_LHS(i)==eqn_RHS(i);
sol_positive(i) = vpasolve(eqn1(i),r(i),[0 Inf]);
end
But, After running this code, it shows the following error.
Error using subsref
Index exceeds matrix dimensions.
Error in sym/subsref (line 771)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in tetra_v3 (line 10)
bast(i)=(r(i)/0.5).^(alpha(i)*(mio-1));
So, my question is: What is the cause of these errors and how to solve them ?? and How to use loop with vpasolve ??
Respuestas (2)
John D'Errico
el 8 de Mayo de 2018
Editada: John D'Errico
el 8 de Mayo de 2018
What is the cause? READ THE ERROR MESSAGE.
"Index exceeds matrix dimensions."
What are you indexing?
bast(i)=(r(i)/0.5).^(alpha(i)*(mio-1));
What indexing is involved here? alpha seems to be a vector.
How about r?
r is a scalar. When i is greater than 1, r(i) will return an error, because r IS A SCALAR. What is the second element of a scalar?
5 comentarios
Eman S
el 8 de Mayo de 2018
John D'Errico
el 8 de Mayo de 2018
Editada: John D'Errico
el 8 de Mayo de 2018
How can you solve it? r is not a vector. You can't solve it, at least not with the code you have written. r has only one value.
If r has only one value, why are you trying to use multiple values for r? Why do you need to index r at all?
John D'Errico
el 8 de Mayo de 2018
Editada: John D'Errico
el 8 de Mayo de 2018
You are saving the solution in sol_positive(i), NOT in r(i). There is no need to index r. r is just the symbolic variable you are solving for in the equation.
Walter Roberson
el 8 de Mayo de 2018
0 votos
As we explored earlier, your system works out to be a polynomial and vpasolve() is going to return a list of all the solutions under the constraint you give, [0 inf]. You are trying to store that vector into a single location sol_positive(i) .
If you were certain that there would always be the same number of results, you could store to sol_positive(i,:) instead, but I think you would be better off assuming that the number of positive roots might change, so I would recommend assigning to sol_positive{i} . Indeed, my test shows that most of your equations have no solution in that range.
1 comentario
Walter Roberson
el 8 de Mayo de 2018
Correction: this is a different system that is not polynomial.
However, your system does not happen to have solutions at the alpha that end in 0.5 . There are solutions with fractional alpha, but those solutions do not happen be real valued for alpha ending in 1/2 . For example for r = 1/5 there is a solution of alpha about 2.4
Also, because it is not polynomial, vpasolve() is only finding one solution. For alpha = 1, there are three positive solutions in the range 1 to 1.7
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!