View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Non-linear equations system solver (Newton Raphson)

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.2 | 10 ratings Rate this file 89 Downloads (last 30 days) File Size: 47 KB File ID: #33332 Version: 1.7
image thumbnail

Non-linear equations system solver (Newton Raphson)


Felipe Mendes (view profile)


18 Oct 2011 (Updated )

Solves a non-linear system with iterative Newton-Raphson. Very easy and powerfull!!

| Watch this File

File Information

This code solves a system of non-linear equations.
It is practical to use since there's no need to enter the derivation matrix (the code calculates itself).
Also, there's an indetermined number of variables (no restriction). You can put 1 million equations! :)
Inside the code there are instructions of how to use it, and also a folder with a simple exemple.
Hope it's going to be usefull for you! Comments and debugs are deeply enjoyed.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (22)
26 Apr 2016 Muhammad Umar Farooq

Thank you for your work Mr. Felipe Mendes. It has solved my equations time and time again but it is stuck at my 4 equations and always provide a NaN value no matter what I do. Can you tell me what is the solution of this problem?

Comment only
25 Apr 2016 Mahmoud Y  
28 Mar 2016 Juan Abuabara

Hello, thank you for your great work...

at the moment, I'm getting an error when I try to to solve a 3 system equations. The error is

>> NewtonRaphson
Undefined function or variable 'nls_aleatory_initial_values'.

Error in NewtonRaphson (line 81)

08 Jul 2015 Carl Witthoft

My apologies - I failed to unzip all files. The main file has all the 'readme' info I was after.

08 Jul 2015 Carl Witthoft

Code looks good but it really needs a solid ReadMe or Help file to clarify how to set up the inputs

27 Oct 2014 Ching-Chen  

Good day people, I am new to MATLAB and I currently have a system of theree coupled nonlinear equation to solve. i will like to implement newton raphson iteration to solve the system of equation but I donot know how to go about this. ANy form of help will be appreciated. Thank you

Comment only
12 Aug 2014 paolo

paolo (view profile)

thanxs man great work

17 Oct 2012 Felipe Mendes

Felipe Mendes (view profile)

Thanks Danny! I've also spent a couple of hours trying to figure out how to solve this, but I couldn't. If you find it, please share with us! And if it is a bug I'll be very greatful with you for correcting it!
Thanks! Good luck!

Comment only
17 Oct 2012 Danny MA

Felipe, thank you all the same, i will try to solve it by myself. if i have the ansewer, i will send it to you for sharing.

Comment only
16 Oct 2012 Felipe Mendes

Felipe Mendes (view profile)

You should try to reformulate your system. Maybe it's not even possible to solve by newton-raphson!
(Sorry can't really help you) :/
Best regards,

Comment only
15 Oct 2012 Danny MA

Hi Felipe,

thanks for your work. When i use your code to solve five nonlinear equations, the answers are NaN, and the message is: Warning: Matrix is singular, close to singular or badly scaled.
Results may be inaccurate. RCOND = NaN.
Could you give me some suggestions on how to solve it? thanks

15 Oct 2012 Danny MA

here are my code:
%%%%% 1 Reading of the parameters of the solution:

%maximum number of iterations


%parameter to establish the convergenvce criteria


%number of equations in the non-linear system to solve


%%%%% 2)Reading of the (nsys)equations

%Standard form of definition of the equations:
%F1 = @(x1,x2,x3) x1+x2-x3+b*a;
% *The equations are defined as an anonymous function;
% *They must be numbered sequentially: F1,F2,F3,... ,Fnsys;
% *The function array must be filled with all the F(nsys) functions of
% the system: F={F1,F2,F3,... F(nsys)}
% *Variables must be numbered like: x1,x2,x3,... ,xnsys;
% *Number of equations must be equal to the number of variables and
% equal to (nsys)
%Equation 1: (F1)

F1 = @(x) Np*x(1)-Np*x(2)*(exp(Voc/Ns/x(5))-1)-Np*Voc/Ns/x(4);

%Equation 2: (F2)

F2 = @(x) Np*x(1)-Np*x(2)*(exp(Isc*x(3)/Np/x(5))-1)-Isc*x(3)/x(4)-Isc;
F3 = @(x) Np*x(1)-Np*x(2)*(exp((Vm/Ns+Im*x(3)/Np)/x(5))-1)-Np/x(4)*(Vm/Ns+Im*x(3)/Np)-Im;
F4 = @(x) (Np*x(2)*exp((Vm+Im*Ns*x(3)/Np)/Ns/x(5))/Ns/x(5)+Np/Ns/x(4))/(1+x(3)*x(2)*exp((Vm+Im*Ns*x(3)/Np)/Ns/x(5))/x(5)+x(3)/x(4))-Im/Vm;
F5 = @(x) (Np*x(2)*exp(Isc*Ns*x(3)/Np/Ns/x(5))/Ns/x(5)+Np/Ns/x(4))/(1+x(3)*x(2)*exp(Isc*Ns*x(3)/Np/Ns/x(5))/x(5)+x(3)/x(4))-1/x(4);

%%%%% Function array


%%%%% Reading of the initial condition of all the (nsys) variables


%The values could instead be inputed one by one like done bellow:

%%%%% Verification of the input data:

%(to be created)

%Function array has an order equivalent to nsys

%(to be created)

%%%%% 3)Iterative calculations


while nloop<maxiter

%%%%% Assemblage of the matrix of derivative functions


for i=1:1:nsys
for j=1:1:nsys

%%%%% Calculation of the vector with the values of each function

for i=1:1:nsys

%%%%% Calculation of the error


%%%%% Adding the error to the x to obtain the new iteration


%%%%% Parameter to determine the end of the while loop

%%%% While loop counter

%%%% Converging criteria (error < epsilon)


if maxdx<epsilon

%%%%% 4)Presentation and store of the result values


%%% The values of the variables x(1), x(2),... x(nsys) can now be atributed
%%% to their respective destines

Comment only
04 Oct 2012 Felipe Mendes

Felipe Mendes (view profile)

Thanks for the debugging. Update version already corrected.

Comment only
01 Oct 2012 Nello Troccia

Probably this is the correct version:


if maxdx<epsilon

Comment only
01 Oct 2012 Nello Troccia

thanks for your work!
I think that the part of the "convergin criteria" it's wrong. You write:


if abs(maxdx)<epsilon

Probably this is the correct version:


if maxdx<epsilon

Are you agree with me?
Thanks again!

19 Aug 2012 Felipe Mendes

Felipe Mendes (view profile)

No Zhazira, it will always converge to the solution that is the nearest to the initial values! What you could do if you want to explore a system with multiple solutions could be assign a score function at the end of the main routine. After that you could use a genetic algorithm to explore all the range of variation of the set of parameters and choose the solution that best fits the criteria you want to fulfill!
Good luck!

Comment only
30 Jul 2012 Zhazira

Hi there!
If a system has several solutions, is it possible to get all real solutions at once using your solver?

13 Jun 2012 Felipe Mendes

Felipe Mendes (view profile)

you're welcome!!

Comment only
13 Jun 2012 Yaan Yee Choy

Thanks of ur work.

14 Nov 2011 Felipe Mendes

Felipe Mendes (view profile)

Hi Houssemus!
Exactly! "nconverg" is the number of iterations done by the routine until the maximum error (creteria of convergence) was reached!
If you have a very big non-linear system, it might give you some information about how near your initial values are!
Good luck!

Comment only
14 Nov 2011 Houssemus

hi man,
thanx for this work,, but i'm wondering if 'nconverg' is the number of iterations done during the calculation,
waiting for ur answer,

Comment only
08 Jun 2012 1.1

A better functional description.

09 Oct 2012 1.7

Correction of small bug!

Contact us