How can I solve system of nonlinear equations in Matlab?

Hello,
I need some help solving analytically system of non linear equations in matlab. I try "fsolve", but had this error message "Not enough input arguments". Can you please help?
function F = fun(x)
F =[(x(3)+x(2)-a)*x(1)+(x(3)-a)*x(2);
(x(3)+x(2))*x(1)+(x(3)-b)*x(2)-b*x(3);
(x(3)+x(2)-c)*x(1)+x(3)*x(2)-c*x(3)];
x = fsolve(@fun)
end
Thanks,

1 Comment

Note: you would not call fsolve() passing in fun while you are in the function fun itself: that would lead to recursion. This is why I switched from a separate function for fun to using an anonymous function: no danger of recursion by accidentally placing the fsolve() call at the wrong place.

Sign in to comment.

Answers (2)

Supply an initial guess:
function main
x0 = [0 0 0];
a=...;
b=...;
c=...;
y=...;
x=fsolve(@(x)fun(x,a,b,c,y),x0);
function F = fun(x,a,b,c,y)
F =[(x(3)+x(2)-a)*x(1)+(x(3)-a)*x(2); (x(3)+x(2))*x(1)+(x(3)-b)*y-b*x(3); (x(3)+x(2)-c)*x(1)+x(3)*y-c*x(3)];
Best wishes
Torsten.
a=42.8; b=-1.234563; c=pi; %as appropriate
fun = @(x) [(x(3)+x(2)-a)*x(1)+(x(3)-a)*x(2); (x(3)+x(2))*x(1)+(x(3)-b)*y-b*x(3); (x(3)+x(2)-c)*x(1)+x(3)*y-c*x(3)];
X0 = randn(1,3)*50; %some starting point
X= fsolve(fun, X0)

4 Comments

Note that fsolve is only for numeric solutions not for analytic solutions. If you need analytic solutions you will need the symbolic toolbox and solve()
Thank you for your answer, I tried solve({(x(3)+x(2)-a)*x(1)+(x(3)-a)*x(2); (x(3)+x(2))*x(1)+(x(3)-b)*x(2)-b*x(3); (x(3)+x(2)-c)*x(1)+x(3)*x(2)-c*x(3)}) And I had this error massage:"Undefined function 'x' for input arguments of type 'double'." Sorry, I'm a beginneer with Matlab, so what is the toolbox?
Thanks,
Here are explanations and examples:
Best wishes
Torsten.
a=42.8; b=-1.234563; c=pi, y=-55.5, z = sqrt(3); %as appropriate
syms x1 x2 x3 x
x = [x1 x2 x3];
sol = solve([(x(3)+x(2)-a)*x(1)+(x(3)-a)*x(2); (x(3)+x(2))*x(1)+(x(3)-b)*y-b*x(3); (x(3)+x(2)-c)*x(1)+x(3)*y-c*x(3)], [x1 x2 x3]);
sol.x1, sol.x2, sol.x3
Note: the system of equations turns out to be a cubic polynomial, so there are three solutions

Sign in to comment.

Categories

Asked:

on 15 Dec 2015

Commented:

on 15 Dec 2015

Community Treasure Hunt

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

Start Hunting!