# Newton Raphson on Mathlab

13 views (last 30 days)

Show older comments

##### 0 Comments

### Answers (7)

Torsten
on 3 Nov 2014

p=[0.5 1 -10 14.742];

r=roots(p);

Best wishes

Torsten.

##### 2 Comments

Matt Tearle
on 5 Nov 2014

What do you mean? The roots in r already solve the equation. The real root near -6 is the first element of r:

>> fprintf('%10.6f\n',r(1))

-6.083918

You can use the polyval function to evaluate a polynomial at a given value of x. For example:

x = linspace(-8,6);

y = polyval(p,x);

plot(x,y)

grid on

If you evaluate at x = r(1) you'll see that it solves the equation (to within ~10^-13):

polyval(p,r(1))

Matt Tearle
on 5 Nov 2014

Torsten showed how to find the roots of a polynomial. More generally, to find the zeros of any function:

f = @(x) 0.5*x.^3 + x.^2 - 10*x + 14.742; % define the function

x0 = -4; % initial guess for the solution

xroot = fzero(f,x0) % solve

(If you're interested in the details, according to the doc, "the [fzero] algorithm, created by T. Dekker, uses a combination of bisection, secant, and inverse quadratic interpolation methods")

##### 2 Comments

Matt Tearle
on 5 Nov 2014

Edited: Matt Tearle
on 5 Nov 2014

I don't understand what you mean by "complete this manually". The assignment problem says to solve the equation. One way is to do N-R by hand. The other is "repeat using MATLAB". So I guess the question is what "repeat" entails. Repeat the solving of the equation? Or repeat solving the equation with N-R?

We've shown two ways you can solve the equation in MATLAB: roots (for solving polynomial equations) and fzero (for solving general nonlinear equations), but neither of these use N-R.

If you want to implement Newton-Raphson in MATLAB then that's a bigger issue. That requires knowing the basics of MATLAB programming. Given that this is a homework problem, you'll need to show what you've attempted and ask for some specific pointers.

Torsten
on 13 Nov 2014

As Matt already mentioned, neither MATLAB's "fzero" nor MATLAB's "roots" uses N-R.

So it's difficult to compare the two methods.

You should ask your supervisor what he/she means by

"Repeat using MATLAB and compare the two methods.".

Best wishes

Torsten.

##### 0 Comments

Farhad Sedaghati
on 3 Aug 2015

The following link is the matlab code to perform Newton-Raphson's Method:

##### 0 Comments

sabik EL YATIM
on 27 Jul 2019

hello, Recently, a part of the Matlab code I found on the resolution system of nonlinear equations using the method of Newton-Raphson with the Jacobian matrix (I also left it in my comments). However, although he provides me with the basic code, I can not make it work, no matter how hard I try. I spent many hours trying to present function func, but to no avail, I often did not understand how I used it if you can help me

function [x,F, niter] = newtonsys(Ffun ,Jfun,x0 ,tol ,...

nmax, varargin )

% NEWTONSYS cherche un zéro d’un système non linéaire

% [ZERO ,F, NITER ]= NEWTONSYS(FFUN, JFUN,X0 ,TOL , NMAX)

% tente de trouver le vecteur ZERO, racine d’ un

% système non linéaire défini dans FFUN et dont

% la matrice jacobienne est définie dans la

% fonction JFUN. La racine est cherchée autour

% du vecteur X0.

% La variable F renvoie le résidu dans ZERO

% NITER renvoie le nombre d’ itérations nécessaires

% pour calculer ZERO. FFUN et JFUN sont des fonctions

% MATLAB définies dans des M- files.

niter = 0; err = tol + 1; x = x0;

while err >= tol & niter < nmax

J = feval(Jfun ,x , varargin {:});

F = feval(Ffun ,x , varargin {:});

delta = - J\F;

x = x + delta;

err = norm( delta );

niter = niter + 1;

end

F = norm( feval( Ffun,x, varargin {:}));

if (niter == nmax & err > tol)

fprintf ([’Pas de convergence dans le nombre ’ ,...

’ d’’ iterations imparti \n ’]);

fprintf ([’ La valeur retournée a un résidu ’ ,...

’ relatif de %e\n’],F);

else

fprintf ([’La méthode a convergé à l’’ itération’ ,...

’ %i avec un résidu %e\n’],niter,F);

end

return

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!