Numerical Analysis HW2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Numerical Analaysis Hamza Abu Ajamia

070488
Matlab Assignment 2

Newton-Raphson Method Implementations:


Use the Newton-Raphson algorithm to find solutions accurate to 10 -7 for the following
problems:
 x 3−x−1=0 on [1,2].
x
 e +2 + 2cos x −6=0
−x
on [1,2].
by using the following code in Matlab, one can find solutions:
f=inline('x*x*x-x-1');
fprime=inline('3*x*x-1');
e=1E-7; P0=1.5; N0=20;
str = sprintf(' Ni P0 P
F(P)');
disp(str);
for i=1:N0
P = P0- f(P0)/fprime(P0);
str = sprintf('%3d %E %E %E',i,P0,P,f(P));
disp(str);
if (abs(P-P0)<e)
break
end

P0=P;
end
if(i>=N0)
str =sprintf('The Method Failed after %d iterations',i);
disp(str);
end
Newton Method Code (Traditional) - given both F and F' and 1 point
f=inline('<Function Syntax>');
e=1E-7; P0=1.2; P1=1.7; Q0=f(P0); Q1=f(P1); N0=20;
str = sprintf(' Ni P0 P1
F(P)');
disp(str);
for i=2:N0
P = P1- ((Q1*(P1-P0))/(Q1-Q0));
str = sprintf('%3d %E %E %E',i,P0,P1,f(P));
disp(str);
if (abs(P-P1)<e)
break
end
P0=P1; Q0=Q1; P1=P; Q1=f(P);
end

if(i>=N0)
str =sprintf('The Method Failed after %d iterations',i);
disp(str);
else
str =sprintf('Solution is %E',P);
disp(str);
end
Newton Method Code (Secant Implementation) - given both F and 2 points
Algorithm Output:
 x 3−x−1=0 on [1,2].
Traditional Algorithm:
having inserting the following into our code:
f=inline('x*x*x-x-1');
fprime=inline('3*x*x-1');
P0=1.5;
Ni P0 P F(P)
1 1.500000E+000 1.347826E+000 1.006822E-001
2 1.347826E+000 1.325200E+000 2.058362E-003
3 1.325200E+000 1.324718E+000 9.243778E-007
4 1.324718E+000 1.324718E+000 1.865175E-013
5 1.324718E+000 1.324718E+000 2.220446E-016
The Solution is 1.324718E+000

Secant Implementation Algorithm:


having inserting the following into our code:

f=inline('x*x*x-x-1');
fprime=inline('3*x*x-1');
P0=1.2;
P1=1.7;
Ni P0 P1 F(P)
2 1.200000E+000 1.700000E+000 -1.516948E-001
3 1.700000E+000 1.287896E+000 -4.386414E-002
4 1.287896E+000 1.314332E+000 1.570713E-003
5 1.314332E+000 1.325086E+000 -1.530088E-005
6 1.325086E+000 1.324714E+000 -5.248550E-009
7 1.324714E+000 1.324718E+000 1.709743E-014
The Solution is 1.324718E+000

 e x +2−x + 2cos x −6=0 on [1,2].


Traditional Algorithm:
having inserting the following into our code:

f=inline('2.7182818.^(x) + 2.^(-x) + (2* cos(x))- 6');


fprime=inline('2.7182818.^(x) - 2.^(-x) - (2* sin(x))');
P0=1.5;
Ni P0 P F(P)
1 1.500000E+000 1.979706E+000 6.989381E-001
2 1.979706E+000 1.844042E+000 6.086351E-002
3 1.844042E+000 1.829261E+000 -5.035326E-004
4 1.829261E+000 1.829386E+000 1.088549E-005
5 1.829386E+000 1.829384E+000 -2.340596E-007
6 1.829384E+000 1.829384E+000 5.033331E-009
The Solution is 1.829384E+000
Secant Implementation Algorithm:
having inserting the following into our code:
f=inline('2.7182818.^(x) + 2.^(-x) + (2* cos(x))- 6');
P0=1;
P1=2;
Ni P0 P1 F(P)
2 1.000000E+000 2.000000E+000 -5.456738E-001
3 2.000000E+000 1.678309E+000 -8.573866E-002
4 1.678309E+000 1.808103E+000 1.198455E-002
5 1.808103E+000 1.832298E+000 -2.150279E-004
6 1.832298E+000 1.829331E+000 -5.247848E-007
7 1.829331E+000 1.829384E+000 2.306511E-011
8 1.829384E+000 1.829384E+000 8.881784E-016
Solution is 1.829384E+000

You might also like