How to use loop with vpasolve ??

조회 수: 22 (최근 30일)
Eman S
Eman S 2018년 5월 8일
댓글: Image Analyst 2020년 2월 1일
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
Image Analyst
Image Analyst 2020년 2월 1일
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 ??

댓글을 달려면 로그인하십시오.

답변 (2개)

John D'Errico
John D'Errico 2018년 5월 8일
편집: John D'Errico 2018년 5월 8일
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
John D'Errico
John D'Errico 2018년 5월 8일
편집: John D'Errico 2018년 5월 8일
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.
Eman S
Eman S 2018년 5월 8일
편집: Eman S 2018년 5월 8일
OK. I modified the code as the following. I removed indexing from r.
syms 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/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/0.5)).^(alpha(i)*mio));
mqam_part4(i)=((sqrt(3)-(r/0.5)).^-alpha(i));
mqam_part5(i)=((2*sqrt(3))-(r/0.5)).^-alpha(i);
mqam_part6(i)=mqam_part4(i)+mqam_part5(i);
mqam_part7(i)=2*((3-(r/0.5)).^-alpha(i));
mqam_part8(i)=6*(((r/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,[0 Inf]);
end
After running this code, it gives the following errors:
Error using subsasgn
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in sym/privsubsasgn (line 997)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 834)
C = privsubsasgn(L,R,inds{:});
Error in tetra_v3 (line 23)
sol_positive(i) = vpasolve(eqn1(i),r,[0 Inf]);

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2018년 5월 8일
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
Walter Roberson
Walter Roberson 2018년 5월 8일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by