Subject:
Is it possible to solve simultaneous non-linear equations with MATLAB?
Problem Description:
I would like to know if I can solve simultaneous non-linear equations with MATLAB.
Solution:
You can solve simultaneous non-linear equations with MATLAB using the FSOLVE or LSQNONLIN functions in the Optimization Toolbox. For more information on the Optimization Toolbox, point your web browser to the following URL. This includes more generic examples of how to go about using these functions or your application:
http://www.mathworks.com/products/optimization/
If you have the Optimization Toolbox installed, for more information on these functions type 'help function_name' at the MATLAB Command Prompt. If you have installed the documentation, type 'doc function_name' to see the documentation for this function. If you do not have the documentation installed, you can find it here:
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/fsolve.shtml
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnonlin.html
As a simple example using FSOLVE, if you wanted to find the intersection of the curves:
y = sin(x)
y^2 = cos(x)
You can use the following steps:
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, using the Display option, that we want to see information about the optimization at each iteration:
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.