필터 지우기
필터 지우기

[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