# How do I solve a nonlinear equation in MATLAB?

Many students ask me how do I do this or that in MATLAB.  So I thought why not have a small series of my next few blogs do that.  In this blog, I show you how to solve a nonlinear equation.

The MATLAB program link is here.

The HTML version of the MATLAB program is here.

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical
% procedure.

%% TOPIC
% How do I solve a nonlinear equation?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/integration.m;
% Last Revised : March 28, 2009;
% Abstract: This program shows you how to solve a nonlinear equation.
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to solve’)
disp(‘   a nonlinear equation’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/nonlinearequation.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   April 11, 2009’)
disp(‘ ‘)

%% INPUTS
% Solve the nonlinear equation x^3-15*x^2+47*x-33=0
% Define x as a symbol
syms x
% Assigning the fleft hand side o the equation f(x)=0
f=x^3-15*x^2+47*x-33;
%% DISPLAYING INPUTS

disp(‘INPUTS’)
func=[‘  The equation to be solved is ‘ char(f), ‘=0’];
disp(func)
disp(‘  ‘)

%% THE CODE

% Finding the solution of the nonlinear equation
soln=solve(f,x);
solnvalue=double(soln);

%% DISPLAYING OUTPUTS

disp(‘OUTPUTS’)
for i=1:1:length(solnvalue)
fprintf(‘\nThe solution# %g is %g’,i,solnvalue(i))
end
disp(‘  ‘)

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos

## 0 thoughts on “How do I solve a nonlinear equation in MATLAB?”

1. baldev choudhary says:

nice blog…..

2. baldev choudhary says:

nice blog…..

3. chris says:

Nice …. and thnx….

4. chris says:

Nice …. and thnx….

5. chris says:

Can u please tell me how i can solve a nonlinear equation with a set of constraints in matlab….

thnx.
chris

6. chris says:

Can u please tell me how i can solve a nonlinear equation with a set of constraints in matlab….

thnx.
chris

7. Vahid says:

Hi
Can you help me to solve this equation?

h (d2h/dx2)=4.822h-1.5h3-3.322
Boundary conditions:
X=0 h=0.73
X=-2 dh/dx=0

Regards,
Vahid

8. Vahid says:

Hi
Can you help me to solve this equation?

h (d2h/dx2)=4.822h-1.5h3-3.322
Boundary conditions:
X=0 h=0.73
X=-2 dh/dx=0

Regards,
Vahid

9. siamak says:

i need help with solving nonlinear system of three equations that repeated over 30000000
nodes see the number of nodes a lot and due to saving the function problem i cant use the fsolve please help me

10. siamak says:

i need help with solving nonlinear system of three equations that repeated over 30000000
nodes see the number of nodes a lot and due to saving the function problem i cant use the fsolve please help me

11. siamak says:

i need to solve system of three equations over a 30000000 node my problem is how to save them to use in fsolve you know the node number

12. siamak says:

i need to solve system of three equations over a 30000000 node my problem is how to save them to use in fsolve you know the node number

13. somayeh says:

ydot1 = 20*y(1)*((y(2)-30.6176)/30.6176)-0.03*y(1);
ydot2 = -0.07*y(2)-(28.7776)*((1.4873)^t)*(y(2)^2)/y(1)^0.38-10*y(2)^2*(y(2)-30.6176)^2/30.6176;
and initial conditions are y1(0)=1706115.9
y2(0)=30.75819596
thanx alot

14. somayeh says:

ydot1 = 20*y(1)*((y(2)-30.6176)/30.6176)-0.03*y(1);
ydot2 = -0.07*y(2)-(28.7776)*((1.4873)^t)*(y(2)^2)/y(1)^0.38-10*y(2)^2*(y(2)-30.6176)^2/30.6176;
and initial conditions are y1(0)=1706115.9
y2(0)=30.75819596
thanx alot

15. somayeh says:

i used ode45 for solving it but the answer was NaN!!!!!

16. somayeh says:

i used ode45 for solving it but the answer was NaN!!!!!

1. Nnc says:

I have a similar problem, I want to solve a non linear 2nd order system of 3 equations & when i use ode45 the answer is NAN :s. Have you solved yours ??

17. Subhash Kadiam says:

Nice blog!

18. Subhash Kadiam says:

Nice blog!

19. getahun goshu says:

<a href=" to get detail knowledge about mathlab and soln with their quastions

20. getahun goshu says:

<a href=" to get detail knowledge about mathlab and soln with their quastions

21. getahun goshu says:

22. getahun goshu says:

23. getahun goshu says:

``` ```

24. getahun goshu says:

``` ```

25. Den says:

Thanks man!! That really helped my work.

26. Den says:

Thanks man!! That really helped my work.

27. roshan says:

i want to solve this four equations;
x(3) – ((log(x(1)/ 65792043000 )) -( 0.07+(0.5*(x(2)^2))))/x(2);
x(4) – x(3) + x(2);
8507000337- x(1)*normcdf(x(3)) + 65792043000*exp(-0.07)*normcdf(x(4));
(8507000337* 0.347089018056744) – x(1)*x(2)*normcdf(x(3))];
can u solve?

28. roshan says:

i want to solve this four equations;
x(3) – ((log(x(1)/ 65792043000 )) -( 0.07+(0.5*(x(2)^2))))/x(2);
x(4) – x(3) + x(2);
8507000337- x(1)*normcdf(x(3)) + 65792043000*exp(-0.07)*normcdf(x(4));
(8507000337* 0.347089018056744) – x(1)*x(2)*normcdf(x(3))];
can u solve?

29. mjj says:

Hi sir…
i need a point to solve a nonlinear equation state space in matlab…
AX+Bu=0
[0 1 ] [x1] [0 ]
=[ ]
[x^4 -1] [x2] [sin(t)]

30. mjj says: