%%% This code solves Ex1.1.17 syms x format long %%% define function f f = log(x^2+2) %%% graph the function on interval [0, 2] ezplot(f,[0,2]) %%%(a) %%%% get the 3rd Taylor polynomial "tay_p3" at expansion point 1.0 tay_p3 = taylor(f,4,1.0) %%% (b) %%% we define f - the 3rd Taylor polynomial err = f - tay_p3 %%% Now use extreme value Thm on function "err" der_f = diff(err, x) root = fzero(char(der_f), [0, 1]) subs(err, x, root) left = subs(err, x, 0.0) right = subs(err, x, 1.0) %%%% Answer: The Maximum error |f(x) - P3(x)| for 0<= x <= 1 %%%% is at the point 0.0. The max error = 0.026633657323934 %%% (c) %%%% get the 3rd Maclaurin polynomial "tmac_p3" %%%% Note: Maclaurin polynomial is a special Taylor polynomial, which %%%% expands at point 0.0 mac_p3 = taylor(f,4) %%% (d) %%% we define f - the 3rd Maclaurin polynomial err_mac = f - mac_p3 %%% Now use extreme value Thm on function "err_mac" der_f_mac = diff(err_mac, x) root = fzero(char(der_f_mac), [0, 1]) subs(err_mac, x, root) left_mac = subs(err_mac, x, 0.0) right_mac = subs(err_mac, x, 1.0) %%% Answer: The Max error is at point 1.0 %%% The max error = 0.094534891891836 (do not forget to take the abs. value) %%% (e) %%% Compute the relative error for Maclaurin polynomial approximation err = subs(mac_p3, x, 1) - subs(f, x, 1) relative_err = err/subs(f, x, 1) %%% Compute the relative error for Taylot polynomial approximation err_tay = subs(tay_p3, x, 0.0) - subs(f, x, 0.0) relative_err_tay = err_tay/subs(f, x, 0) %%%% Answer: Since the relative error of Taylor polynomial approximation is %%%% smaller than that of the Maclaurin polynomial approximation. P3(0) %%%% approximation is better. %%%%