Effacer les filtres
Effacer les filtres

How to use fsolve to solve this system of equations?

116 vues (au cours des 30 derniers jours)
Thien Son Phan
Thien Son Phan le 16 Fév 2018
Commenté : Walter Roberson le 16 Fév 2018
So I need to use fsolve on this to solve the following system of four equations:
2x=-2kx
2y=-ky
2z=-k
2-(x^2)-(1/2)(y^2)-z=0.
I therefore assigned x(1) to x, x(2) to y, x(3) to z, and x(4) to k. This is what I typed:
x0=[0,0,2,0];
fsolve(@(x)[2.*x(1)+2.*x(2).*x(4);2.*x(2)+x(2).*x(4);2.*x(3)+x(4);2-(x(1).^2)-0.5.*(x(2).^2)-x(3)],[x0])
Apparently, there should be five solutions, but matlab is only returning one:
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ans =
-0.0000 0 2.0000 -4.0000
How can I get matlab to compute and display the other solutions?
  1 commentaire
Matt
Matt le 16 Fév 2018
Modifié(e) : Matt le 16 Fév 2018
Hi Thien,
The fsolve function will give you a solution to your equations, but it's an optimization type function. So it tries to find a minimum around the initial guess you provide it. For instance, if you change it to x0 = [-1,-1,-1,-1], you will get a different solution.
Matt

Connectez-vous pour commenter.

Réponses (1)

Matt
Matt le 16 Fév 2018
If you have the Symbolic Math Toolbox in MATLAB, you can get all 5 solutions exactly.
syms k x y z
eqns = [2*x == -2*k*x,...
2*y == -k*y,...
2*z == -k,...
2-(x^2)-(1/2)*(y^2)-z == 0];
vars = [k,x,y,z];
[solk,solx,soly,solz] = solve(eqns,vars);
solutions = [solk,solx,soly,solz]
  2 commentaires
Thien Son Phan
Thien Son Phan le 16 Fév 2018
These codes don't seem to use fsolve though...
Walter Roberson
Walter Roberson le 16 Fév 2018
It is never possible to get fsolve() to display more than one solution. The closest you can get is to run fsolve on different equations or on different starting points, hoping that you manage to find all of the solutions.

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by