%Program that approximates the function ln(x) about x=1 by a 1st order, 2nd
%order, and a 3rd order Taylor approximation. Written by Philip Shaw,
%Fordham University, 2018.
clear all
h=-.8:.01:1;
tic %start a timer for the i loop below
for i=1:length(h)
fxx(i,1)=log(1+h(i));
fxhn1(i,1)=h(i);
fxhn2(i,1)=h(i)-h(i)^2/2;
fxhn3(i,1)=h(i)-h(i)^2/2+(h(i)^3)/3;
end
tloop=toc %stop the timer for the i loop above and store the result
plot(h,fxx,h,fxhn1,h,fxhn2,h,fxhn3,'linewidth',2)
legend('ln(x)','f(x+h) n=1','f(x+h) n=2','f(x+h) n=3')
title('Taylor Approximation of ln(x) About x=1')
xlabel('h')
ylabel('f(1+h)')
display('Average Absolute Deviation of 1st Order Approx')
mean(abs(fxx-fxhn1))
display('Average Absolute Deviation of 2nd Order Approx')
mean(abs(fxx-fxhn2))
display('Average Absolute Deviation of 3rd Order Approx')
mean(abs(fxx-fxhn3))