Is it possible to solve simultaneous nonlinear equations with MATLAB?

44 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Oct 2022
Edited: MathWorks Support Team on 10 Nov 2022
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 use a release 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.

More Answers (0)

Categories

Find more on Startup and Shutdown 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!