You are on page 1of 11

We have used MATLAB for writing our code.

The reasons behind writing a particular thing in the


code will be mentioned in the code of Newton-Raphson method written for example 1.

1) Bisection method
Example1:-
Code:-
clear all
a=0; b=1; err=1e-6;
iteration=0;
fa=a-cos(a);
fb=b-cos(b);
while b-a>err
c=(a+b)/2;
fc=c-cos(c);
if fa*fc<0
b=c;
elseif fb*fc<0
a=c;
end
iteration = iteration + 1;
sprintf('After iteration %2.0d the x is %3.10f',iteration,c)
end
x=linspace(0.7,0.8);
fx=x-cos(x);
figure
plot(fx,x), xlabel('f(x)'), ylabel('x');
grid on
grid minor

Result:-
After iteration 1 the x is 0.5000000000
After iteration 2 the x is 0.7500000000
After iteration 3 the x is 0.6250000000
After iteration 4 the x is 0.6875000000
After iteration 5 the x is 0.7187500000
After iteration 6 the x is 0.7343750000
After iteration 7 the x is 0.7421875000
After iteration 8 the x is 0.7382812500
After iteration 9 the x is 0.7402343750
After iteration 10 the x is 0.7392578125
After iteration 11 the x is 0.7387695313
After iteration 12 the x is 0.7390136719
After iteration 13 the x is 0.7391357422
After iteration 14 the x is 0.7390747070
After iteration 15 the x is 0.7391052246
After iteration 16 the x is 0.7390899658
After iteration 17 the x is 0.7390823364
After iteration 18 the x is 0.7390861511
After iteration 19 the x is 0.7390842438
After iteration 20 the x is 0.7390851974

Graph:-

Solution got converged at 16th iteration if we consider the correctness of the answer up to 4
decimal places. Interchanging the values of a and b havent affected the number of iterations.
From the graph, we can get that solution of the equation, on Y axis, at f(x)=0 on X axis. If we
consider the range of x from -10 to 10 then we can get that variation on f(x) is like an inclined
wavy pattern. If n is number of iterations then,
n = ln((b-a)/err)/ln(2) =~ 20
Example2:-
Code:-
a=1; b=2; err=1e-6;
iteration=0;
fa=(a.^6)-a-1;
fb=(b.^6)-b-1;
while b-a>err
c=(a+b)/2;
fc=(c.^6)-c-1;
if fa*fc<0
b=c;
elseif fb*fc<0
a=c;
end
iteration = iteration + 1;
sprintf('After iteration %2.0d the x is %3.10f',iteration,c)
end
x=linspace(1,1.2);
fx=(x.^6-x-1);
figure
plot(fx,x), xlabel('f(x)'), ylabel('x');
grid on
grid minor

Result:-
After iteration 1 the x is 1.5000000000
After iteration 2 the x is 1.2500000000
After iteration 3 the x is 1.1250000000
After iteration 4 the x is 1.1875000000
After iteration 5 the x is 1.1562500000
After iteration 6 the x is 1.1406250000
After iteration 7 the x is 1.1328125000
After iteration 8 the x is 1.1367187500
After iteration 9 the x is 1.1347656250
After iteration 10 the x is 1.1337890625
After iteration 11 the x is 1.1342773438
After iteration 12 the x is 1.1345214844
After iteration 13 the x is 1.1346435547
After iteration 14 the x is 1.1347045898
After iteration 15 the x is 1.1347351074
After iteration 16 the x is 1.1347198486
After iteration 17 the x is 1.1347274780
After iteration 18 the x is 1.1347236633
After iteration 19 the x is 1.1347255707
After iteration 20 the x is 1.1347246170

Graph:-

Solution got converged at 14th iteration if we consider the correctness of the answer up to 4
decimal places. Interchanging the values of a and b havent affected the number of iterations.
From the graph, we can get that solution of the equation, on Y axis, at f(x)=0 on X axis. If we
take the range of x like -5 to 5 or -10 to 10 or -15 to 15 or any other similar range then the
curve of the graph will take the C shape.
2) Newton-Raphson method
Example1:-
Code:-
In this code we will be mentioning the reasons behind writing a particular thing in the code.
clear all
x=0.5; %Initial guess.
err=1e-6; %Maximum allowable error.
iteration=0; %To set the number of iterations.
fx=x-cos(x); %Function under consideration.
while abs(fx)>err %Condition for carrying out iterations.
fx=x-cos(x);
dfx= 1+sin(x); %Derivative of the given function.
x1=x-(fx/dfx); %Formula of the Newton-Raphson method.
x=x1;
iteration = iteration + 1; %It will repeat the iterations.
sprintf('After iteration %2.0d the x is %3.10f',iteration,x)
%This will print the result.
end
x=linspace(0.7,0.8); %This generates linearly spaced vectors.
fx=x-cos(x);
figure %This creates new window for the graph.
plot(fx,x),xlabel('f(x)'),ylabel('x');%This will plot the
graph.
grid on %This displays gridlines.
grid minor %This displays minor gridlines.

Result:-
After iteration 1 the x is 0.7552224171
After iteration 2 the x is 0.7391416661
After iteration 3 the x is 0.7390851339
After iteration 4 the x is 0.7390851332

Graph:-
Solution got converged at 3rd iteration itself if we consider the correctness of the answer up to 5
decimal places. Changing the initial value will have unpredictable effect on the number of
iterations. For example if we change the initial guess from 0.5 to 2.5 still we can get the solution
with required correctness by carrying out only 5 iterations but if we change the initial guess to
3.5 then number of iterations will drastically raise to 247 and contrary to this, changing the initial
guess to 4.5 will require 24 iterations. This means that we cannot correlate initial gues with
number of iterations required.
Example2:-
Code:-
clear all
x=1.5;
err=1e-6;
iteration=0;
fx=x.^6-x-1;
while abs(fx)>err
fx=x.^6-x-1;
dfx= 6*x.^5-1;
x1=x-(fx/dfx);
x=x1;
iteration = iteration + 1;
sprintf('After iteration %2.0d the x is %3.10f',iteration,x)
end
x=linspace(1,1.2);
fx=(x.^6-x-1);
figure
plot(fx,x), xlabel('f(x)'), ylabel('x');
grid on
grid minor

Results:-

After iteration 1 the x is 1.3004908836


After iteration 2 the x is 1.1814804164
After iteration 3 the x is 1.1394555903
After iteration 4 the x is 1.1347776252
After iteration 5 the x is 1.1347241453
After iteration 6 the x is 1.1347241384

Graph:-

Solution got converged at 4th iteration itself if we consider the correctness of the answer up to 4
decimal places. A different behavior has been observed for this example as compared to previous
one. We can directly have relationship between initial guess and number of iterations required.
With initial guess varying from 10.5, 50.5 and 100.5, the number of iterations required will
change from 17, 26 and 29 respectively. The numbers of iterations required are found to be
increasing very slowly.
3) Secant method:-
Example1:-
Code:-
clear all
x=0;x1=1;
err=1e-6;
iteration=0;
fx=x-cos(x);
fx1=x1-cos(x1);
while abs(fx)>err
fx=x-cos(x);
fx1=x1-cos(x1);
x=x1-(fx1*(x1-x)/(fx1-fx));
iteration = iteration + 1;
sprintf('After iteration %2.0d the x is %3.10f',iteration,x)
end
x=linspace(0.7,0.8);
fx=(x-cos(x));
figure
plot(fx,x), xlabel('f(x)'), ylabel('x');
grid on
grid minor

Result:-
After iteration 1 the x is 0.6850733573
After iteration 2 the x is 0.7362989976
After iteration 3 the x is 0.7389453560
After iteration 4 the x is 0.7390781309
After iteration 5 the x is 0.7390847824
After iteration 6 the x is 0.7390851156

Graph:-
Solution got converged at 4th iteration if we consider the correctness of the answer up to 4
decimal places. Interchanging the values of x and x1 have resulted in rise in the number of
iterations required from 6 to 10. Rest of the things like variation of graph are same as that of
Bisection method.

Example2:-
Code:-
clear all
x=2;x1=1;
err=1e-6;
iteration=0;
fx=x.^6-x-1;
fx1=x1.^6-x1-1;
while abs(fx)>err
fx=x.^6-x-1;
fx1=x1.^6-x1-1;
x=x1-(fx1*(x1-x)/(fx1-fx));
iteration = iteration + 1;
sprintf('After iteration %2.0d the x is %3.10f',iteration,x)
end
x=linspace(1,1.2);
fx=(x.^6-x-1);
figure
plot(fx,x), xlabel('f(x)'), ylabel('x');
grid on
grid minor

Result:-
After iteration 1 the x is 1.0161290323
After iteration 2 the x is 1.1905777687
After iteration 3 the x is 1.1149814256
After iteration 4 the x is 1.1425921894
After iteration 5 the x is 1.1317252002
After iteration 6 the x is 1.1358873047
After iteration 7 the x is 1.1342760040
After iteration 8 the x is 1.1348972393
After iteration 9 the x is 1.1346573414
After iteration 10 the x is 1.1347499243
After iteration 11 the x is 1.1347141857
After iteration 12 the x is 1.1347279801
After iteration 13 the x is 1.1347226555
After iteration 14 the x is 1.1347247108
After iteration 15 the x is 1.1347239175
After iteration 16 the x is 1.1347242237
After iteration 17 the x is 1.1347241055

Graph:-
Solution got converged at 10th iteration if we consider the correctness of the answer up to 4
decimal places. Interchanging the values of x and x1 has resulted in drastic rise in the number
of iterations required from 17 to 93. Rest of the things like variation of graph is same as that of
Bisection method.

You might also like