Matlab Code:
x = 0:.001:5;
f = @(x) -2 +6 * x - 4 * x .^2 + 0.5 * x .^3;
hold on
plot (x,f(x))
%(a) Newton-Raphson method (three iterations, x_0 = 4.2).
df = @(x) 6 - 2 * 4 * x + 3 * 0.5 * x .^2;
ax = 4.2;
for n = 1:1:23
ax_n = ax - f(ax) ./ df(ax);
ax = ax_n;
end
plot (ax_n, x, 'k')
error_approximatea = (ax_n - ax) ./ ax_n;
%(b) Newton-Raphson method (three iterations, x_0 = 4.43).
bx = 4.43;
for n = 1:1:23
bx_n = bx - f(bx) ./ df(bx);
bx = bx_n;
end
plot (bx_n, x, 'r')
error_approximateb = (bx_n - bx) ./ bx_n;
Red is the approximation to the real root 4.43
Black is the approximation to the real root 4.2
Blue is f(x) = -2 +6x - 4x^2 +.5x^3
EDU>> ax_n
ax_n =
0.4746
EDU>> bx_n
bx_n =
0.4740
EDU>> error_approximatea
error_approximatea =
0
EDU>> error_approximateb
error_approximateb =
0
No comments:
Post a Comment