How to use fsolve to solve this system of equations?
258 views (last 30 days)
Show older comments
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 Comment
Matt
on 16 Feb 2018
Edited: Matt
on 16 Feb 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
Answers (1)
Matt
on 16 Feb 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 Comments
Walter Roberson
on 16 Feb 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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!