f(x) is different of f(value of x)

1 visualización (últimos 30 días)
undefined undefined
undefined undefined el 11 de En. de 2021
Comentada: undefined undefined el 11 de En. de 2021
Hi,
In my code, I have a function wich receive a lambda function f.
inside this function, I have a variable called newPoint
I stop with a breakpoint in the running, the value of newPoint is 1.0434
and if I write in the console f(newPoint), I get 0, but if I write f(1.0434) i get -5.2815e-05.
so in my function, I can not evaluate precisely f(newPoint)
(My function f is f = @(x) 2 - x^3 -sin(x))
I will very gratefull if you can help me.
  8 comentarios
Walter Roberson
Walter Roberson el 11 de En. de 2021
Editada: Walter Roberson el 11 de En. de 2021
fprintf('%.20g\n', newPoint)
newPoint - 1.0434
undefined undefined
undefined undefined el 11 de En. de 2021
Okay, I see the problem.
Here what I did : The difference is very small between a positive result and 0.
Thank you very much for your help, I hope I didn't bther you too much
K>> format long g
K>> newPoint
newPoint =
1.04338598820955
K>> f(1.04338598820955)
ans =
1.765254609154e-14
K>> f(newPoint)
ans =
0
K>> fprintf('%.20g\n', newPoint)
1.0433859882095546379
K>> f(1.0433859882095546379)
ans =
0
K>>

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 11 de En. de 2021
The value stored in an array is not necessarily the same value that is displayed.
Right now Forbes magazine lists the net worth of Jeff Bezos as $183.1 billion dollars. Is that his actual net worth down to the last penny? No. Is it close enough for purposes of this profile? Yes.
Similarly, when displaying pi in MATLAB in the default display format you don't see the full value of pi.
format
x = pi
x = 3.1416
y = x - 3.1416 % not exactly 0
y = -7.3464e-06
Using x or 3.1416 in a calculation could result in different answers if the answer changes based on the value of y (which is not itself exactly -7.3464e-06.)
z = y - (-7.3464e-06)
z = -1.0207e-11

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by