You are on page 1of 2

% Newton Raphson Method

clear all
close all
clc
% Change here for different functions
f=@(x) exp(x)-5*sin(2.5)-10
%this is the derivative of the above function
df=@(x) exp(x)-5*cos(2.5)
% Change lower limit 'a' and upper limit 'b'
a=0; b=1;
x=a;
for i=1:1:100
x1=x-(f(x)/df(x));
x=x1;
end
sol=x;
fprintf('Approximate Root is %.15f',sol)
a=0;b=1;
x=a;
er(5)=0;
for i=1:1:5
x1=x-(f(x)/df(x));
x=x1;
er(i)=x1-sol;
end
plot(er)
xlabel('Number of iterations')
ylabel('Error')
title('Error Vs. Number of iterations')

f=

@(x)exp(x)-5*sin(2.5)-10

df =

@(x)exp(x)-5*cos(2.5)

Approximate Root is 2.564361547852220>>


% Newton Raphson Method
clear all
close all
clc
% Change here for different functions
f=@(x) 3*sin(x)-3*cos(x)-1
%this is the derivative of the above function
df=@(x) 3*cos(1)+3*sin(1)
% Change lower limit 'a' and upper limit 'b'
a=0; b=1;
x=a;
for i=1:1:100
x1=x-(f(x)/df(x));
x=x1;
end
sol=x;
fprintf('Approximate Root is %.15f',sol)
a=0;b=1;
x=a;
er(5)=0;
for i=1:1:5
x1=x-(f(x)/df(x));
x=x1;
er(i)=x1-sol;
end
plot(er)
xlabel('Number of iterations')
ylabel('Error')
title('Error Vs. Number of iterations')

f=

@(x)3*sin(x)-3*cos(x)-1

df =

@(x)3*cos(1)+3*sin(1)

Approximate Root is 1.023339288227657>>

You might also like