- Please format your code to make it more readable
- Why do you want to display the iterations? But either way, just add a disp or fprintf statement in your loop, after updating xi. (Or even just leave off the semicolon.)
- To plot the functions, make a vector of x values in [-10,10] (try linspace), then evaluate the functions at these x values. If f is a function handle, you can simply evaluate f(x).

# Newton Raphson method

145 views (last 30 days)

Show older comments

Given f(x) = x^3-8*x^2-200*x+1000

Locate the maximum of f(x) for x[-10,10]. The maximum must be located by finding the root of derivative of f(x).Use Newton Raphson method to perform root finding.

The question asks us to select the initial guess buy ourself after looking at the f(x) graphically. The solution must have a precision of 0.01%. Then, plot of f'(x) and the root must b done on the same figure.

I have the function for Newton Raphson method,but I think I'm missing an importance sentence because I can't get the iterations printed on the command window. Plus,I do not know how to plot the f'(x) and the root. I googled for hours but nothing helped.

Here is my function:

function root = newraph(f,df,xi,precision)

% comments on input and output

%Calculate f(xi) and f'(xi)

fxi=f(xi);

dfxi=df(xi);

%Iteration for Newton Raphson method begins

while abs(fxi)>precision

%Calculate the new estimated root,x_i_+_1

xi=xi-fxi/dfxi;

fxi=f(xi); %recalculate f(x_i_+_1)

dfxi =df(xi); %recalculate f'(x_i_+_1)

end

%The final xr value is the root

root = xi;

I hope someone can help me fix the mistakes. Any help is much appreciated. Thanks :)

##### 0 Comments

### Answers (3)

Matt Tearle
on 19 Apr 2011

##### 0 Comments

kalyan m
on 15 Feb 2022

function[]=Newton_Raphson(f,df,x0)

% INPUT f and df as inline functions

% where df is the dirivative of f

Maxit = 100;

x = zeros(1,Maxit);

x(1) = x0;

for i = 1:Maxit

if abs(df(x(i)))<10^-15

fprintf('The derivative is zero at x = %f\n',x(i))

break

end

x(i+1) = x(i)-f(x(i))/df(x(i));

err = abs(x(i+1)-x(i));

if err<10^-6

fprintf('\n The Number of iteration is %d, the root of function is x = %f\n',i,x(i+1))

break

end

end

if i == Maxit

fprintf('The Newton - Raphson method does not converge after %d iteration\n',i)

end

if err<10^-7

tt = -1:0.001:5;

y = f(tt);

plot(tt,y,'k',x(i+1),0,'*')

grid on

legend('function','Approximate root')

end

end

harmonics

x=[45 90 135 180 225 270 315 360];

y=[4 3.8 2.4 2 -1.5 0 2.8 3.4 ]

x=(pi/180)*x

syms t

T=2*pi;

w=2*pi/T;

h=2; % number of hormonics

a0=2*mean(y);

HS=a0/2;

for i=1:h

a(i)=2*mean(y.*cos(i*w*x));

b(i)=2*mean(y.*sin(i*w*x));

HS=HS+a(i)*cos(i*w*t)+b(i)*sin(i*w*t);

end

HS=vpa(HS,4);

disp(HS)

plot(x,y,'*')

hold on

t=linspace(x(1),x(end),1000);

y1=eval(HS);

plot(t,y1,'r')

function[] = Gauss_Seidal22(A,B)

[m,n]=size(A);

f=0;

%%% Diagonally Dominant

for j = 1:m

if(sum(abs(A(j,:)))-abs(A(j,j)))>abs(A(j,j))

f = 1;

fprintf('The system is not diagonally dominant\n')

end

end

if f~=1

fprintf('The given system is diagonally dominant\n')

%Gauss-Seidal Iteration Method

tol = 1e-6;

maxit = 100;

x = zeros(n,1); % Initial value As a column vector

for k = 1:maxit

xold = x;

for i = 1:n

Num = B(i)-A(i,1:i-1)*x(1:i-1)-A(i,i+1:n)*x(i+1:n);

x(i) = Num/A(i,i);

end

err = abs(x-xold);

iferr<tol

fprintf('Gauss-Seidal method is convergent\n')

break

end

end

fprintf('The %d iterations are required to get \n x=',k)

fprintf('\t\t%f\n',x)

end

end

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!