フィルターのクリア

[HELP] A Classical Numerical Computing Question

1 回表示 (過去 30 日間)
Xiao Tang
Xiao Tang 2012 年 9 月 8 日
f(x) = (exp(x)-1)/x; g(x) = (exp(x)-1)/log(exp(x))
Analytically, f(x) = g(x).
When x is approaching to 0, both f(x) and g(x) are approaching to 1. However, g(x) works better than f(x).
% Compute y against x
for k = 1:15
x(k) = 10^(-k);
f(k) =(exp(x(k))-1)/x(k);
De(k) = log(exp(x(k)));
g(k)= (exp(x(k))-1)/De(k);
end
% Plot y
plot(1:15,f,'r',1:15,g,'b');
f(x) actually diverges when x approaches to 0.But shouldn't them be the same??

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 8 日
編集済み: Matt Fig 2012 年 9 月 8 日
No, they shouldn't be the same at the fringes. This is an example of why we often have to look for more stable ways of doing in floating point arithmetic what is analytically simple. Look how f oscillates:
f = @(x) (exp(x)-1)./x;
g = @(x) (exp(x)-1)./log(exp(x));
x = 0:1e-13:1e-7; % Try with x = 0:2e-14:1e-7;
ax(1) = subplot(1,2,1);
plot(x,f(x),'r')
title('(exp(x)-1)./x')
ax(2) = subplot(1,2,2);
plot(x,g(x),'b');
title('(exp(x)-1)./log(exp(x))')
L = get(gca,{'xlim','ylim'});
axis(ax(1),[L{:}])
  3 件のコメント
Matt Fig
Matt Fig 2012 年 9 月 8 日
I think what we have is a case of near perfect cancellation of errors. Take a look:
x = 1e-12:1e-12:1e-9; % Double values
X = vpa(1/10^12:1/10^12:1/10^9,80); % Symbolic values
syms Z
D = abs(X - log(exp(x)));
E = abs(X-x);
plot(D)
figure
plot(E) % Very little difference
F = abs((exp(x)-1) - subs(exp(Z)-1,X));
figure
plot(F) % Notice similarity to D!
G = F-D;
max(double(G)) % Cancellation of errors.
Xiao Tang
Xiao Tang 2012 年 9 月 9 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by