How do you use fsolve to solve simultaneous equations?

15 views (last 30 days)
Hello,
I am new to Matlab and would like to learn how to use fsolve to solve simultaneous equations.
Say you have the following equations:
x+y+z=2
y^2=z+5
x+z^3=2
Zero degrees of freedom, how do you solve for x, y and z using fsolve?
Thank you very much,
M
  2 Comments
Matt J
Matt J on 25 Oct 2012
It's not clear what "zero degrees of freedom" means here. Looks like you have 3 dof.
Mike
Mike on 25 Oct 2012
3 equations, 3 unknowns. There is likely single values of x, y, z that satisfies the equations listed.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 25 Oct 2012
Edited: Matt J on 25 Oct 2012
You need to rewrite as F(X)=0 where X is your vector of unknowns
fun=@(X) [sum(X)-2; X(2)^2 -X(3)-5 ; X(1)+X(3)^3-2];
Then
x = fsolve(fun,x0)
where x0 is your initial guess. There are other input/output arguments that you can use to refine the behavior of FSOLVE (see "doc fsolve").
  2 Comments
Mike
Mike on 25 Oct 2012
what is the purpose of @? when writing sum(X)-2 just to confirm you could also write X(1)+X(2)+X(3)-2? How do you choose the initial guess and if you guess incorrectly will the solution not converge?
Thank you very much for your answer!
M
Matt J
Matt J on 25 Oct 2012
what is the purpose of @?
It's one way of defining a function in MATLAB. See the documentation on Anonymous Functions for details.
when writing sum(X)-2 just to confirm you could also write X(1)+X(2)+X(3)-2?
Yes, that would be equivalent. When you have lots of variables, though, writing things out explicitly that way can be painful.
How do you choose the initial guess and if you guess incorrectly will the solution not converge?
An initial guess is a guess, so it's always at least partly a matter of guess-work, not science. Often, the physical nature of the problem might suggest a good initial guess. The algorithm is really just trying to minimize norm(F(X))^2. If your guess is bad, it can get stuck in a local minimum. Many things can happen, really, depending on the shape of the graph of norm(F(X))^2.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!