How do you use fsolve to solve simultaneous equations?

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 commentaires

It's not clear what "zero degrees of freedom" means here. Looks like you have 3 dof.
Mike
Mike le 25 Oct 2012
3 equations, 3 unknowns. There is likely single values of x, y, z that satisfies the equations listed.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 25 Oct 2012
Modifié(e) : Matt J le 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 commentaires

Mike
Mike le 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
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by