How to use loop with vpasolve ?? (2024)

9 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Eman S am 8 Mai 2018

  • Verknüpfen

    Direkter Link zu dieser Frage

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

  • Verknüpfen

    Direkter Link zu dieser Frage

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

Kommentiert: Image Analyst am 1 Feb. 2020

In MATLAB Online öffnen

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 Kommentar

-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Image Analyst am 1 Feb. 2020

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

In MATLAB Online öffnen

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

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Antworten (2)

John D'Errico am 8 Mai 2018

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

Bearbeitet: John D'Errico am 8 Mai 2018

In MATLAB Online öffnen

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 Kommentare

3 ältere Kommentare anzeigen3 ältere Kommentare ausblenden

Eman S am 8 Mai 2018

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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 am 8 Mai 2018

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Bearbeitet: John D'Errico am 8 Mai 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 am 8 Mai 2018

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Bearbeitet: Eman S am 8 Mai 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 am 8 Mai 2018

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Bearbeitet: John D'Errico am 8 Mai 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 am 8 Mai 2018

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Bearbeitet: Eman S am 8 Mai 2018

In MATLAB Online öffnen

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

Melden Sie sich an, um zu kommentieren.

Walter Roberson am 8 Mai 2018

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

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 Kommentar

-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Walter Roberson am 8 Mai 2018

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Kategorien

MATLABLanguage FundamentalsLoops and Conditional Statements

Mehr zu Loops and Conditional Statements finden Sie in Help Center und 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!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


Translated by How to use loop with vpasolve ?? (11)

How to use loop with vpasolve ?? (12)

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

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

Europa

  • 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)

Asien-Pazifik

Kontakt zu Ihrer lokalen Niederlassung

How to use loop with vpasolve ?? (2024)

References

Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6417

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.