2 equal functions, different outputs

Hi
I have two functions that are the same. The inputs are 10^-1, 10^-2...10^-14
Why does the first one get funny answers after 10^-7?
Thanks

4 comentarios

John BG
John BG el 28 de Mzo. de 2017
have you tried vpa?
vpa(exp(2*x)-(exp(x).*cos(x)).^2)'
ans =
0.012173368402139450594745540001895
0.00010201673337717487299869389971718
0.000001002001667416152486111968755722
0.000000010002000117026454972801730036736
0.00000000010000200667548142519081011414528
0.0000000000010000889005823410116136074066162
0.000000000000010436096431476471479982137680054
0.00000000000000022204460492503130808472633361816
-0.00000000000000022204460492503130808472633361816
0
0
-0.00000000000000022204460492503130808472633361816
0.00000000000000022204460492503130808472633361816
0
>> vpa(exp(2*x)-(exp(2*x).*cos(x)).^2)'
ans =
-0.25555335373862742009976045665098
-0.020505356557536158490506750240456
-0.0020050053356654995440067068557255
-0.00020005000533362959913574741221964
-0.000020000500005057375574324396438897
-0.0000020000049998358804259623866528273
-0.0000002000000498547649385727709159255
-0.000000020000000544584395356650929898024
-0.0000000019999999434361370731494389474392
-0.00000000020000001654807419981807470321655
-0.000000000020000001654807419981807470321655
-0.0000000000019999557565597569919191300868988
-0.00000000000020006218903745320858433842658997
-0.000000000000019984014443252817727625370025635
>>
also, why do you double the x in the 2nd exponential, don't you say the expressions are the same?
John BG
Jan
Jan el 28 de Mzo. de 2017
Editada: Jan el 28 de Mzo. de 2017
For clarification the formulas as code, not as screenshot, with additional parenthesis:
exp(2*x) - (exp(x) .* cos(x)) .^ 2
exp(2*x) - exp(2*x) .* (cos(x) .^ 2)
Calculating the values in double format at first and using vpa only to expand the displayed digits afterwards does not increase the number of significant digits.
Craig Smith
Craig Smith el 29 de Mzo. de 2017
The functions are the same. I double the exponential in the second because that is what happen when you raise exp(x) to the second power.
(exp(x)*cos(x))^2 = exp(2*x)*cos(x)^2
Stephen23
Stephen23 el 29 de Mzo. de 2017
"I have two functions that are the same". Sure, they might be mathematically the same, but when working with finite precision floating point numbers on a computer then you are not working with mathematical operations, but with numeric operations.
"Why does the first one get funny answers..."
They are not funny at all, these are quite reasonable answers for floating point calculations, where the floating point error accumulates with each operation.
And a thousand other pages on the internet which explain floating point numbers.

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de Mzo. de 2017

1 voto

Both versions are correct. When you use floating point numbers, then when you calculate the same expression two different ways, the rounding might come out differently.
Furthermore if you examine the ieee 754 specifications on the required accuracy of each of the floating point operations you will find that for some of the operations the last bit is not required to be correct.
Basically you should consider the last bit of a floating point calculation to generally be random.

La pregunta está cerrada.

Etiquetas

Preguntada:

el 28 de Mzo. de 2017

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by