Professional Documents
Culture Documents
Numerical Analysis HW2
Numerical Analysis HW2
Numerical Analysis HW2
070488
Matlab Assignment 2
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
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