- What is ‘fsolve’ in MATLAB?
- What is a System of Non-linear Equations?
- How to Solve Non-linear Equations in MATLAB Using fsolve Command
What is ‘fsolve’ in MATLAB?
‘fsolve’ is a built-in function in MATLAB to solve nxn system of non-linear equation without showing iterations. Function to be solved must be a continuous function and ‘fsolve’ only gives one root.
What is a System of Non-linear Equations?
A system of ‘n’ (n>0) number of equations having ‘n’ number of variables in which at least one equation is not linear i.e. Its solution must not plot a linear graph. In other words, standard form of linear equation is:
C1x + C2y + C3 = 0
Any equation that cannot be written in this form will be a non-linear equation.
y=f(x) % ‘y’ is a function of ’x’.
In non-linear equation, ‘x’ is not directly proportional to ‘y’ and the graph is not linear. It can be a parabola, circle, hyperbola, curve etc.
Take an example:
x = 0:pi/100:2*pi;
y = sin(x);
A sine wave can be seen in fig.1 which is not linear.
How to Solve Non-linear Equations in MATLAB using ‘fsolve’ Command?
We can solve nxn non linear system in MATLAB using a built-in command “fsolve” without showing iterations.
Steps to Solve Non-linear Equations in MATLAB
Following are the steps to solve any non-linear equation using ‘fsolve’ command in MATLAB:
- Take an example:
F(x,y) = 2x – y – e-x
G(x,y) = 2y – x2 – e-y
- Make a function of the equation in editor of MATLAB and save it in the known location.
function F = ftn_fsolve_example(X)
x = X(1);
y = X(2);
f = 2*x-y-exp(-x);
g = 2*x-x.^2-exp(-y);
F = [f;g];
% Alternatively, you could do the following. It’s more compact , but harder
% to read;
% F = [2*X(1) – X(2) – exp(-X(1)); -X(1).^2 + 2*X(2))];
- Write a code calling the ‘fsolve’ function in the new script.
% script fsolve example
a = @ftn_fsolve_example;
X0 = [1;1]; % initial guess
X = fsolve(a,X0);
- Output will be shown as:
- If we do not want the output in descriptive form, we can introduce a third variable in MATLAB code named “options”.
options = optimset(‘display’,’off’);
- Now the output will show like this:
The ‘fsolve’ command in MATLAB is quite useful in solving the roots of non-linear equations without iterations. The function to be solved must be a continuous function and ‘fsolve’ only gives one root.