matlab returns different result for same inputs

3 visualizaciones (últimos 30 días)
navi mahmoo
navi mahmoo el 18 de Ag. de 2019
Comentada: Star Strider el 18 de Ag. de 2019
hi
i am writing a code. the result is different when i pass varibles to it and when i pass numbers. for axample the value for x in my code is: -0.0424
now when i run this code:
max(min(((x-a)/(b-a)),((c-x)/(c-b))),0)
ans =
0.3159
and when i replace varible x with its value and run this code:
max(min(((-0.0424-a)/(b-a)),((c+0.0424)/(c-b))),0)
ans =
0.3107
the values for a,b and c are the same in both codes.
how can i fix this?

Respuesta aceptada

Star Strider
Star Strider el 18 de Ag. de 2019
You may believe that the value of ‘x’ and the value of ‘x’ that are displayed are the same. They are likely much different, since ‘x’ exists with full precision internally. If you are only seeing four digits of it, you are definitely not seeing all of it. See the discussion on Floating-Point Numbers and the format function for details.
If you use:
format long
and then copy and paste the value for ‘x’ that you then see displayed, there will be very little if any discrepancy between using ‘x’ and using the displayed value of ‘x’.
  2 comentarios
navi mahmoo
navi mahmoo el 18 de Ag. de 2019
thank you very much. that realy helped me...
Star Strider
Star Strider el 18 de Ag. de 2019
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by