% script err.m % illustrate absolute and relative errors % using sin(x) and an approximation x - x^3/6 % evaluate for x = 0 to x = 0.25 in steps of 0.05 for x = 0 : 0.05 : 0.25 exact = sin(x); approx = x - x^3/6; abs_err = abs(exact - approx); if x == 0 rel_err = 0.0; else rel_err = abs_err / abs(x); end fprintf('%8.2f%17.9e%17.9e%17.9e%17.9e\n', ... x, exact, approx, abs_err, rel_err); end