How can I solve system of nonlinear equations in Matlab?
Show older comments
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
Walter Roberson
on 15 Dec 2015
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.
Answers (2)
Torsten
on 15 Dec 2015
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.
Walter Roberson
on 15 Dec 2015
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
Walter Roberson
on 15 Dec 2015
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()
RIM chtourou
on 15 Dec 2015
Torsten
on 15 Dec 2015
Here are explanations and examples:
Best wishes
Torsten.
Walter Roberson
on 15 Dec 2015
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
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!