How do you use fsolve to solve simultaneous equations?
15 views (last 30 days)
Show older comments
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
on 25 Oct 2012
It's not clear what "zero degrees of freedom" means here. Looks like you have 3 dof.
Accepted Answer
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
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!