Is there a way to vpasolve elementwise? (2024)

3 views (last 30 days)

Show older comments

John Moseley on 29 Aug 2015

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise

Commented: Star Strider on 30 Aug 2015

Accepted Answer: Star Strider

Open in MATLAB Online

Hello,

I am hoping someone can point me in the right direction with regards to the following problem. I have an equation with two variables, x and y, and an unknown c, and I'd like to solve the equation for c over a range of x and y values. The equation cannot be solved for c analytically. Eventually, I'd like to plot the solutions for c as function of x and y in a contour plot. Below is the code that I thought would work for a much simplified example of the actual problem; this may help define the problem.

x = 1:5;

y = 1:5;

[X,Y] = meshgrid(x,y);

syms c

sol = vpasolve(X + c*Y - 3 == 0,c);

Obviously, this did not work. I'm sure I'm thinking about this wrong way. Any helpful ideas are welcome.

Thank you, John.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Star Strider on 29 Aug 2015

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#answer_190712

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#answer_190712

Open in MATLAB Online

The contour plot is actually the solution. Solving a two-variable problem like that is one of its hidden benefits.

I’m not quite sure what you want to do, but I solved for ‘c’ here, and then had contour provide the (x,y) coordinates for that over the grid you gave it. Here, I asked it for the coordinates corresponding to c=0:

x = 1:5;

y = 1:5;

[X,Y] = meshgrid(x,y);

F = (3-X)./Y;

sol = contour(X, Y, F, [0 0]);

xy = sol(:,2:end)

The ‘xy’ matrix are the coordinates, x = xy(1,:) and y = xy(2,:). I refer you to the documentation on contour for the details. Substitute other values for the fourth argument to define other values of ‘c’.

6 Comments

Show 4 older commentsHide 4 older comments

John Moseley on 30 Aug 2015

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307081

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307081

Thank you very much for your fast response. This definitely helps. My issue is that in the actual equation I'm using with x, y, and c, there is not an analytical solution for c. So, I was trying to use vpasolve.

John

Star Strider on 30 Aug 2015

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307086

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307086

My pleasure.

I figured that this must be a proxy for a different problem. If you have one equation in two unknowns, while you cannot have a unique solution for ‘c’, you can obviously get a range of solutions for ‘c’ if you define ranges for the other two variables.

A problem arises if powers of ‘c’ are involved, because there will be as many values of ‘c’ for every (X,Y) pair as there are powers of ‘c’, and they may not all be real.

John Moseley on 30 Aug 2015

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307095

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307095

For a given x,y pair, there are three solutions for c. There is only one positive solution, and that's the one I need, so I can write that in the code easy enough. My trouble is generating all the positive solutions for c over the [X,Y] mesh.

Star Strider on 30 Aug 2015

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307097

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307097

Open in MATLAB Online

Without knowing your actual equation, the only other option I can offer is to loop over the individual elements in the mesh:

fn = @(c,X,Y) X + c*Y - 3;

x = 1:5;

y = 1:5;

for k1 = 1:length(x)

for k2 = 1:length(y)

C(k1,k2) = fsolve(@(c) fn(c,x(k1),y(k2)), 1);

end

end

You can also use fzero instead of fsolve.

John Moseley on 30 Aug 2015

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307098

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307098

Perfect, Thanks!

Star Strider on 30 Aug 2015

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307099

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/237814-is-there-a-way-to-vpasolve-elementwise#comment_307099

My pleasure!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsContour Plots

Find more on Contour Plots in Help Center and File Exchange

Tags

  • vpasolve
  • elementwise
  • contour plot

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.


Is there a way to vpasolve elementwise? (9)

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

Contact your local office

Is there a way to vpasolve elementwise? (2024)
Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5685

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.