March 10

# fsolve in MATLAB: What You Need to Know

Abdul Mannan

## 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?

### Definition

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.

### Description

Let’s suppose:

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:

MATLAB Code:

x = 0:pi/100:2*pi;

y = sin(x);

plot(y)

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:

1. 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

% F = [2*X(1) – X(2) – exp(-X(1)); -X(1).^2 + 2*X(2))];

End

• 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);

disp(X);

• 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:

## Conclusion

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.

http://www.mathworks.com

Abdul Mannan is an Electrical Power Engineer with specialization in High Voltage. He's the founder and former president at Youth Entrepreneurship Society (YES), University of Engineering & Technology Taxila Campus. He is the leading contributor at "Right to Write". Connect with him about Entrepreneurship, startup ideas, creative writing, business strategies via linked in.

Abdul Mannan