Matlab

How to Solve a System of Nonlinear Equations in MATLAB

Nonlinear equations are types of equations that follow the graph forming curves and non-linear shapes. Solving such a system of equations is a common problem faced by engineers and scientists. The main reason behind solving these types of equations is due to their complexity in finding the exact solution. You may find multiple solutions or in some cases, no solution exists. MATLAB provides us with various ways of solving the system of nonlinear equations. One among them is using a built-in fsolve() function.

This guide will teach us how to compute the solution of the nonlinear equations system in MATLAB using the fsolve() function.

How to Solve the Nonlinear Equations System in MATLAB?

The fsolve() is a built-in function in MATLAB used for solving a system of nonlinear equations with multiple variables. If the number of equations is the same as the number of unknowns, the solution of a system of nonlinear equations will be numerical; otherwise, the solution will be symbolic in terms of the desired variable. Each variable in the system of nonlinear equations can have one or multiple solutions based on its order.

Syntax

The fsolve() function follows a simple syntax to solve a system of nonlinear equations in MATLAB.

x = fsolve(fun,x0)
x = fsolve(fun,x0,options)

Here:

The function x = fsolves(fun, x0) solves the system of nonlinear equations starting from point x0.

The function x = fsolves(fun, x0, options) solves the nonlinear system of equations using optimization methods specified in the options.

Note: The options by default use the Newton Rapson method to compute solutions of systems of nonlinear equations. You can specify other methods, such as trust region, Levenberg-Marquardt, and others.

Examples

Follow the given examples to learn how to solve a system of nonlinear equations using the fsolve() function in MATLAB.

Example 1: Solving 2 Nonlinear Equations in MATLAB

The given example first creates a MATLAB user-defined function named nonlinear_system containing the system of two nonlinear equations.

function F = nonlinear_system(x)
F(1) = exp(sqrt((x(1)+x(2)))) - x(2)*(1+sqrt(x(1)));
F(2) = x(1)*sin(x(2)) + x(2)*cos(x(1)) - 0.1;

Now we call the function in another script file to solve the defined system of nonlinear equations using the fsolve(fun, x0) function starting from the point x0 = (0, 0).

fun = @nonlinear_system;
x0 = [0,0];
x = fsolve(fun,x0)

Example 2: Solving Nonlinear Equations Starting from Point [-5,5]

Now consider the defined system of equations in the user-defined function file nonlinear_system.m and call the function to solve that system of nonlinear equations starting from the point x0 = [-5, 5] using the fsolve() function.

fun = @nonlinear_system;
x0 = [-5,5];
x = fsolve(fun,x0)

For more details, read this guide.

Conclusion

Solving a system of nonlinear equations is the most common problem in mathematics and engineering. MATLAB provides us with a built-in fsolve() function that allows us to solve a system of nonlinear equations. This guide has covered the basics of solving systems of nonlinear equations that will help beginners understand the working of fsolve() function in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.