60 likes | 285 Views
Lecture 24: Numerical differentiation. diffexamp.m. %Example of numerical differentiation f=inline('exp(-x)'); fder=inline('-exp(-x)'); x0=0; hvec=zeros(15); i=1:16; hvec=10.^(-i); for ii=1:16 fdernum=(f(x0+hvec(ii))-f(x0))/(hvec(ii));
E N D
Lecture 24: Numerical differentiation diffexamp.m %Example of numerical differentiation f=inline('exp(-x)'); fder=inline('-exp(-x)'); x0=0; hvec=zeros(15); i=1:16; hvec=10.^(-i); for ii=1:16 fdernum=(f(x0+hvec(ii))-f(x0))/(hvec(ii)); disp(['h=',num2str(hvec(ii)),' Error in forward-difference formula =', num2str(fdernum-fder(x0))]);%display value of function f(x) end for ii=1:16 fdernum=(f(x0+hvec(ii))-f(x0-hvec(ii)))/(2*hvec(ii)); disp(['h=',num2str(hvec(ii)),' Error in three-point centered-difference formula =', num2str(fdernum-fder(x0))]);%display value of function f(x) end
>> diffexamp h=0.1 Error in forward-difference formula =0.048374 h=0.01 Error in forward-difference formula =0.0049834 h=0.001 Error in forward-difference formula =0.00049983 h=0.0001 Error in forward-difference formula =4.9998e-005 h=1e-005 Error in forward-difference formula =5e-006 h=1e-006 Error in forward-difference formula =5.0002e-007 h=1e-007 Error in forward-difference formula =5.0486e-008 h=1e-008 Error in forward-difference formula =6.0775e-009 h=1e-009 Error in forward-difference formula =2.8282e-008 h=1e-010 Error in forward-difference formula =-8.274e-008 h=1e-011 Error in forward-difference formula =-8.274e-008 h=1e-012 Error in forward-difference formula =2.2122e-005 h=1e-013 Error in forward-difference formula =-0.00031095 h=1e-014 Error in forward-difference formula =0.00079928 h=1e-015 Error in forward-difference formula =0.00079928 h=1e-016 Error in forward-difference formula =-0.11022
h=0.1 Error in three-point centered-difference formula =-0.0016675 h=0.01 Error in three-point centered-difference formula =-1.6667e-005 h=0.001 Error in three-point centered-difference formula =-1.6667e-007 h=0.0001 Error in three-point centered-difference formula =-1.6669e-009 h=1e-005 Error in three-point centered-difference formula =-1.2102e-011 h=1e-006 Error in three-point centered-difference formula =2.6755e-011 h=1e-007 Error in three-point centered-difference formula =5.2636e-010 h=1e-008 Error in three-point centered-difference formula =6.0775e-009 h=1e-009 Error in three-point centered-difference formula =-2.7229e-008 h=1e-010 Error in three-point centered-difference formula =-8.274e-008 h=1e-011 Error in three-point centered-difference formula =-8.274e-008 h=1e-012 Error in three-point centered-difference formula =-3.3389e-005 h=1e-013 Error in three-point centered-difference formula =0.00024417 h=1e-014 Error in three-point centered-difference formula =0.00079928 h=1e-015 Error in three-point centered-difference formula =-0.054712 h=1e-016 Error in three-point centered-difference formula =0.44489