Is it possible to solve simultaneous nonlinear equations with MATLAB?

66 views (last 30 days)
I would like to know if I can solve simultaneous nonlinear equations with MATLAB.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Oct 2013
You can solve simultaneous nonlinear equations with MATLAB using the FSOLVE function in the Optimization Toolbox. Information about this function can be found in the following link (or by typing "doc fsolve" into the MATLAB command prompt if you have installed the Optimization Toolbox):
For general information about solving systems of nonlinear equations in MATLAB, see the link below:
The example below shows how to find the intersection of the curves
y = sin(x)
y^2 = cos(x)
using FSOLVE.
1. Write the system as F(x,y) = 0. The system becomes:
y - sin(x) = 0
y^2 - cos(x) = 0
2. Write a function which accepts a vector 'V = [x; y]':
F = @(V) [V(2)-sin(V(1)); V(2).^2-cos(V(1))];
Note that if you MATLAB version prior to MATLAB 7.0 (R14), you will need to create an inline function:
F = inline('[V(2)-sin(V(1)); V(2).^2-cos(V(1))]', 'V');
3. Pass this function to FSOLVE, along with an initial guess for X and Y and an options structure, if you want to specify any options. In the code below we specify that we want to see information about the optimization at each iteration by setting the 'Display' parameter to 'iter':
InitialGuess = [1;1];
Options = optimset('Display','iter');
XY = fsolve(F, InitialGuess, Options);
4. Verify that the value returned is in fact an intersection and that the values returned satisfy F(x, y) = 0:
ShouldBeZero = F(XY)
ezplot('y = sin(x)');
hold on
ezplot('y^2 = cos(x)');
plot(XY(1),XY(2),'ro');
In this case, the entries of ShouldBeZero are not exactly zero, but they are within the default function value tolerance (TolFun) for the FSOLVE function. Likewise, the point XY is not exactly the intersection of the two curves, but it is within the default FSOLVE tolerance for X values (TolX) of the true intersection.
  2 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!