In: Advanced Math
Use Newton's method to find all solutions of the equation correct to eight decimal places. Start by drawing a graph to find initial approximations. (Enter your answers as a comma-separated list.)
−2x7 − 4x4 + 8x3 + 6 = 0
MATLAB Script:
close all
clear
clc
syms x
f = -2*x.^7 - 4*x.^4 + 8*x.^3 + 6; % f(x)
df = diff(f, x); % f'(x)
x_vals = -2:0.001:2;
plot(x_vals, subs(f,x_vals)), xlabel('x'), ylabel('f(x)'), grid
on
disp('Approximate Solutions (From Graph) are: -1.593, -0.833 and
1.293')
title('f(x) = -2x^7 - 4x^4 + 8x^3 + 6')
fprintf('Solutions (Newton''s Method) are:\n\t')
tol = 1e-8; % Error tolerance
% Initial Condition, x0 = -2
xc = newton(f, df, -2, tol); fprintf('%.8f\n\t', xc)
% Initial Condition, x0 = -0.8
xc = newton(f, df, -0.8, tol); fprintf('%.8f\n\t', xc)
% Initial Condition, x0 = 1.5
xc = newton(f, df, 1.5, tol); fprintf('%.8f\n', xc)
function xc = newton(f, df, x0, tol)
xc = x0;
while true
xc_prev = xc; % Save previous iteration's result
xc = double(xc - subs(f,xc)/subs(df,xc)); % Newton update
if abs(xc - xc_prev) < tol % Termination condition
break;
end
end
end
Plot:
Output:
Approximate Solutions (From Graph) are: -1.593, -0.833 and
1.293
Solutions (Newton's Method) are:
-1.59333009
-0.83331079
1.29348432