How to calculate round off error in each step of finite central difference approximation.

6 views (last 30 days)
format long; f = @(x) exp(cos(x)); df = @(x) -exp(cos(x))*sin(x); x = 1; Truedf = df(x); h(1) = 1/2; H(1) = h; D(1) = (f(x+h)-f(x-h))/(2*h); % First approx by central difference E(1) = abs(Truedf-D(1)); % Truncation error at first step e1(1) = f(x-h)-round(f(x-h)); e2(1) = f(x+h)-round(f(x+h)); Roundoff(1) = abs((e2(1)-e1(1))/(2*h)); for i = 2:16 h = h/2; H(i) = h; D(i) = (f(x+h)-f(x-h))/(2*h); E(i) = abs(Truedf-D(i)); e1(i) = f(x-h)-round(f(x-h)); e2(i) = f(x+h)-round(f(x+h)); Roundoff(i) = abs((e2(i)-e1(i))/(2*h)); end A = [H' D' E' Roundoff']
Where I am doing mistake? please help.

Answers (1)

Roger Stafford
Roger Stafford on 11 Mar 2016
The lines
e1(i) = f(x-h)-round(f(x-h));
e2(i) = f(x+h)-round(f(x+h));
Roundoff(i) = abs((e2(i)-e1(i))/(2*h));
do not produce what I would regard as a "roundoff error". The values e1(i) and e2(i) are simply the differences between f(x-h) and its nearest integer, and between f(x+h) and its nearest integer. That has nothing to do with any kind of round-off error that I ever heard of.
When h has become as small as 1/2^16 there is real danger of a serious round-off error in your central difference calculation due to taking the difference between two nearly equal quantities and the fact that matlab's 'double' format has only 53 bits of significand. However, you can only compute the amount of this error by expanding the expression for the central difference in terms of a power series in h and comparing it with the computed central difference.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!