How to use loop with vpasolve ?? (2024)

9 views (last 30 days)

Show older comments

Eman S on 8 May 2018

  • Link

    Direct link to this question

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

  • Link

    Direct link to this question

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

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

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

  • Link

    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

  • Link

    Direct link to this answer

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

  • Link

    Direct link to this answer

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

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

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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    Direct link to this answer

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

  • Link

    Direct link to this answer

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

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

  • Link

    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.


How to use loop with vpasolve ?? (11)

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

How to use loop with vpasolve ?? (2024)

FAQs

How do you find multiple solutions to an equation in Matlab? ›

An equation or a system of equations can have multiple solutions. To find these solutions numerically, use the function vpasolve . For polynomial equations, vpasolve returns all solutions. For nonpolynomial equations, vpasolve returns the first solution it finds.

How to do a for loop in matlab? ›

Direct link to this answer
  1. For loop repeat itself for a given number of input. The syntax for “For Loop Matlab” is. for variable = expression. ...
  2. Initial value : Final value. for x = 1:10. fprintf('value of x: %d\n', x); ...
  3. Initial value : Step : Final value. for x = 1:2:10. ...
  4. Value Array. for x = [1 4 6 8 90]
Mar 4, 2012

What is the difference between solve and Vpasolve? ›

solve solves equations and inequalities that contain parameters. vpasolve does not solve inequalities, nor does it solve equations that contain parameters. solve can return parameterized solutions. vpasolve does not return parameterized solutions.

How does Vpasolve work in Matlab? ›

For polynomial equations, vpasolve returns all solutions. For nonpolynomial equations, there is no general method of finding all solutions and vpasolve returns only one solution by default. To find several different solutions for nonpolynomial, you can set 'Random' to true and use vpasolve repeatedly.

How do you find multiple solutions to a problem? ›

How can you approach problems with multiple solutions?
  1. Identify the problem.
  2. Generate possible solutions.
  3. Evaluate and select solutions.
  4. Implement and monitor solutions.
  5. Adapt to different domains.
  6. Develop your skills.
  7. Here's what else to consider.
Sep 22, 2023

Can systems of equations have multiple answers? ›

A system of linear equations usually has a single solution, but sometimes it can have no solution (parallel lines) or infinite solutions (same line).

What is an example of a loop control statement? ›

for(i=1; i <= 10; i++){ printf(''Enter number %d: '',i); scanf(''%d'',&number); // If user enters negative number, loop is terminated.

How do while loops work in MATLAB? ›

The MATLAB while loop is similar to a do... while loop in other programming languages, such as C and C++. However, while evaluates the conditional expression at the beginning of the loop rather than the end.

How to use the while loop? ›

Let's break it down:
  1. You start the while loop by using the while keyword.
  2. Then, you add a condition which will be a Boolean expression. ...
  3. The condition is followed by a colon, : .
  4. On a new line, you add a level of indentation. ...
  5. Then, the code you want to run goes in the body of the while statement.
Jul 19, 2022

What are the best ode solvers MATLAB? ›

ode45 performs well with most ODE problems and should generally be your first choice of solver. However, ode23 , ode78 , ode89 and ode113 can be more efficient than ode45 for problems with looser or tighter accuracy requirements. Some ODE problems exhibit stiffness, or difficulty in evaluation.

Why do we use Dsolve in MATLAB? ›

dsolve sorts the dependent variables alphabetically, and then assigns the solutions for the variables to output variables or symbolic arrays.

What are different solvers in MATLAB? ›

Solvers for Real-Time Simulation
RealmTypeSolver
Simulink global solverContinuousode1be (Backward Euler)
DiscreteDiscrete (no continuous states)
Simscape local networkContinuousBackward Euler
Trapezoidal Rule
8 more rows

How does MATLAB for loop work? ›

Description. for index = values , statements , end executes a group of statements in a loop for a specified number of times. values has one of the following forms: initVal : endVal — Increment the index variable from initVal to endVal by 1 , and repeat execution of statements until index is greater than endVal .

What is the equivalent of Vpasolve in Python? ›

You closest equivalent to vpasolve would be using mpmath in python. If you instead aim for an exact solution using symbolic computation, sympy would be your choice.

How to get MATLAB to solve an equation? ›

S = solve( eqn , var ) solves the equation eqn for the variable var . If you do not specify var , the symvar function determines the variable to solve for. For example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 for x.

How do I find how many solutions an equation has? ›

If we can solve the equation and get something like x=b where b is a specific number, then we have one solution. If we end up with a statement that's always false, like 3=5, then there's no solution. If we end up with a statement that's always true, like 5=5, then there are infinite solutions..

How can an equation have multiple solutions? ›

An equation can have infinitely many solutions when it should satisfy some conditions. The system of an equation has infinitely many solutions when the lines are coincident, and they have the same y-intercept. If the two lines have the same y-intercept and the slope, they are actually in the same exact line.

How do you know if a system of equations has multiple solutions? ›

A linear system has one solution when the two lines comprising the system intersect once. A linear system has many (infinite) solutions when the two lines are the same (such as y=x+3 and 2y=2x+6 ).

How to check multiple conditions in MATLAB? ›

To verify multiple conditions in a single time step, include verify statements inside if statements, and include multiple if statements in a single test step.

Top Articles
Role of the Prevestarol Microbiome in Gut Health
Marissa Danae Net Worth- Best Guide In 2024
Netronline Taxes
Room Background For Zepeto
Jonathon Kinchen Net Worth
Cooktopcove Com
Craigslist Pets Athens Ohio
Dexter Gomovies
Apus.edu Login
SXSW Film & TV Alumni Releases – July & August 2024
Slope Tyrones Unblocked Games
Booknet.com Contract Marriage 2
Golden Abyss - Chapter 5 - Lunar_Angel
Quest: Broken Home | Sal's Realm of RuneScape
A Man Called Otto Showtimes Near Cinemark University Mall
Getmnapp
Creed 3 Showtimes Near Island 16 Cinema De Lux
Cinema | Düsseldorfer Filmkunstkinos
In hunt for cartel hitmen, Texas Ranger's biggest obstacle may be the border itself (2024)
Lininii
Ugly Daughter From Grown Ups
Loopnet Properties For Sale
Kaiser Infozone
Clearvue Eye Care Nyc
Fandango Pocatello
Microsoftlicentiespecialist.nl - Microcenter - ICT voor het MKB
Kstate Qualtrics
Audi Q3 | 2023 - 2024 | De Waal Autogroep
Darrell Waltrip Off Road Center
Oreillys Federal And Evans
Build-A-Team: Putting together the best Cathedral basketball team
Mydocbill.com/Mr
Mvnt Merchant Services
Frommer's Philadelphia &amp; the Amish Country (2007) (Frommer's Complete) - PDF Free Download
Reese Witherspoon Wiki
Worcester County Circuit Court
Nid Lcms
Sams Gas Price Sanford Fl
Chase Bank Zip Code
15 Best Places to Visit in the Northeast During Summer
Gw2 Support Specter
Nearest Wintrust Bank
Samsung 9C8
Worland Wy Directions
Lesson 5 Homework 4.5 Answer Key
Theatervoorstellingen in Nieuwegein, het complete aanbod.
Dolce Luna Italian Restaurant & Pizzeria
Deshuesadero El Pulpo
Acellus Grading Scale
Generator für Fantasie-Ortsnamen: Finden Sie den perfekten Namen
Island Vibes Cafe Exeter Nh
Volstate Portal
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5887

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.