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.)
4e-x2 sin(x) = x2 − x + 1
MATLAB Script:
close all
clear
clc
syms x
f = 4*exp(-x^2)*sin(x) - x^2 + x - 1; % f(x)
fd = diff(f); % f'(x)
tol = 1e-8;
% Plotting f(x)
xx = -2:0.01:2;
ff = subs(f, xx);
plot(xx,ff), xlabel('x'), ylabel('f(x)')
ic = 0.22;
[result] = my_newton(f,fd,ic,tol);
fprintf('Solution, x: %.8f\n', result)
ic = 1.08;
[result] = my_newton(f,fd,ic,tol);
fprintf('Solution, x: %.8f\n', result)
function [res] = my_newton(func,func_d,ic,tol)
res = ic;
while true
x_ = res;
res = res - subs(func, res)/subs(func_d, res);
if abs(x_ - res) < tol
break;
end
end
end
Plot:
Output:
Solution, x: 0.21916368
Solution, x: 1.08422462