% Updated 200918 by Wei-Cheng Wang % What is the purpose of this program? % Which one of f1, f2 is a more accurate approximation of % exp(x) - cos(x) - x near x=0? why? % floating point algorithm in matlab is double precision by default. % Nevertheless, newer versions of matlab contains vpa, % Vaiable Precision Arithmetic, % which is based on symbolic calculation so you can specify almost % any degree of precision upon numerical evaluation. % It is probably built in some newer versions of octave as well clf a = 3.e-8; % 3.0*10^(-8) h = a/16; x = -a:h:a; f1 = exp(x) - cos(x) - x; f2 = x.^2 + x.^3/6; x_more_accurate = vpa(x,24); f_more_accurate = exp(x_more_accurate)-cos(x_more_accurate) - x_more_accurate; f_double_precision = double(f_more_accurate); figure(1), plot(x,f_double_precision,x,f1,'x',x,f2,'o')