9 views (last 30 days)

Show older comments

Eman S on 8 May 2018

Commented: Image Analyst on 1 Feb 2020

Open in MATLAB Online

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 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Image Analyst on 1 Feb 2020

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/399712-how-to-use-loop-with-vpasolve#comment_792592

Open in MATLAB Online

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 ??

Sign in to comment.

Sign in to answer this question.

### Answers (2)

John D'Errico on 8 May 2018

Edited: John D'Errico on 8 May 2018

Open in MATLAB Online

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 Comments Show 3 older commentsHide 3 older comments

Show 3 older commentsHide 3 older comments

Eman S on 8 May 2018

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/399712-how-to-use-loop-with-vpasolve#comment_565729

Thanks for reply. How can I solve this error and how can I solve this equation using loop ??

John D'Errico on 8 May 2018

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/399712-how-to-use-loop-with-vpasolve#comment_565741

⋮

Edited: John D'Errico on 8 May 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?

Eman S on 8 May 2018

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/399712-how-to-use-loop-with-vpasolve#comment_565747

Edited: Eman S on 8 May 2018

I want to solve an equation for multiple times to get the value of r by varying the value of alpha in each time. The values of alpha are 1:0.5:6. With each value of alpha, i want to solve the equation to get the value of r.

John D'Errico on 8 May 2018

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/399712-how-to-use-loop-with-vpasolve#comment_565750

Edited: John D'Errico on 8 May 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.

Eman S on 8 May 2018

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/399712-how-to-use-loop-with-vpasolve#comment_565793

Edited: Eman S on 8 May 2018

Open in MATLAB Online

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]);

Sign in to comment.

Walter Roberson on 8 May 2018

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 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Walter Roberson on 8 May 2018

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/399712-how-to-use-loop-with-vpasolve#comment_565821

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

Sign in to comment.

Sign in to answer this question.

### See Also

### Categories

MATLABLanguage FundamentalsLoops and Conditional Statements

Find more on **Loops and Conditional Statements** in Help Center and File Exchange

### Tags

- vpasolve
- for loop

### Community Treasure Hunt

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

Start Hunting!

**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom(English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

Contact your local office