the value of h is monotonically decreasing so that the err (calculated-real value) should also decrease
I have the following code to calculate d/dx sin(0.5)) using first derivative principle and compare it with the real value. Why does err starts increasing again?
2 ビュー (過去 30 日間)
古いコメントを表示
clc
n=30;
x=0.5;
h=zeros(n,1);
y=zeros(n,1);
err=zeros(n,1);
h(1)=1;
for i=1:n-1
y(i)=(sin(x+h(i))-sin(x))/h(i)
err(i)=abs(cos(x)-y(i))
h(i+1)=h(i)/4;
end
採用された回答
John D'Errico
2021 年 9 月 16 日
編集済み: John D'Errico
2021 年 9 月 16 日
What are you doing? You used a simple forward difference to compute the derivative, as an approximation to the derivative of sin(x). You did so while decreasing the step size, by a factor of 4 after every iteration.
At some point, the error actually starts to increase. Why? Look at what happened.
n=30;
x=0.5;
h=zeros(n,1);
y=zeros(n,1);
err=zeros(n,1);
h(1)=1;
for i=1:n-1
y(i)=(sin(x+h(i))-sin(x))/h(i);
err(i)=abs(cos(x)-y(i));
h(i+1)=h(i)/4;
end
Now, I'll just look at some of the steps in that loop.
ind = [1 5 10 15 20 25:30]';
format long g
[ind,h(ind),err(ind)]
So, for the first 15 iterations, the error is getting smaller. But when h gets smaller than roughly 1e-8 or so, things start to get bad. The error actually gets worse, and steadily so.
What you need to understand is that for small values of h, you are subtracting two values that are VERY close to each other. For example, look t what happens when h is as small as 1e-12.
dx = 1e-12;
x0 = 0.5;
[sin(x0),sin(x0+dx)]
Now subtract them.
sin(x0 + dx) - sin(x0)
Finally, divide the difference by dx.
(sin(x0 + dx) - sin(x0))/dx
And compare that to cos(x0)
cos(x0)
You should see the two results are starting to deviate after around 4 digits or so there.
The problem is, when dx gets very, very, very tiny, that difference between sin(x+dx) and sin(x) starts to get less accurate. Double precision arithmetic starts to run out of room to compute that difference exactly.
In the case of this problem, the optimal value for dx should be roughly sqrt(eps).
eps
sqrt(eps)
So go back, and look very carefully at what happened. These two values:
[sin(x0),sin(x0 + dx)]
Are the same, almost identically, out for the first 12 digits or so. So now, when you subtract them, the result has only roughly 4 significant digits that mean anything.
sin(x0 + dx) - sin(x0)
Even though we see more than 4 digits in the result, after roughly 4 digits there, those digits are complete garbage.
This is usually called massive subtractive cancellation. Remember that a double precision number is only capable of carrying roughly 16 decimal digits or so.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!