How to calculate round off error in each step of finite central difference approximation.
조회 수: 7 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
Roger Stafford
2016년 3월 11일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!