필터 지우기
필터 지우기

Matlab returns different values with two equivalent functions

조회 수: 1 (최근 30일)
Let's say I have two functions:
f = @(x) (1.01.*exp(4.*x) - 4.62.*exp(3.*x) - 3.11.*exp(2.*x) + 12.2.*exp(x) - 1.99)
F = @(x) (((1.01.*exp(x) - 4.62).*exp(x)-3.11).*exp(x) + 12.2).*exp(x) - 1.99
These two functions are equivalent but when I want to execute with MATLAB it gives me different results with the same input. (for example with 0.925)
I would like to ask why cause this situation and which one has should I trust.
Thanks in advance!
  댓글 수: 2
darova
darova 2020년 3월 8일
>> f(0.925)
ans =
-24.2489
>> F(0.925)
ans =
-24.2489
Works as always
Marcos Baños
Marcos Baños 2020년 3월 8일
ans =
-24.2489252551835
ans =
-24.2489252551834
as you see it's not the same, and with 1.53 the difference is bigger

댓글을 달려면 로그인하십시오.

채택된 답변

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 3월 8일
편집: Thiago Henrique Gomes Lobato 2020년 3월 8일
The difference between then in my computer with your example is -3.197442310920451e-14 (depending of your processor may be slightely different but still extremely low). This difference is negletible and can be regarded as numerical/approximation error between taking the exp of a number and multiplying two (to four, in your case) exp's. You can reproduce it easily with an easier example:
exp(3.245*2)-exp(3.245)*exp(3.245)
ans =
-1.136868377216160e-13
The results are then actually the same and you can trust both of them with the given accuracy, which should be enough for almost any application you may think.
  댓글 수: 6
Stephen23
Stephen23 2020년 3월 8일
편집: Stephen23 2020년 3월 8일

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by