problem about precision in matlab

12 visualizaciones (últimos 30 días)
nasrin
nasrin el 26 de Jul. de 2023
Respondida: Steven Lord el 26 de Jul. de 2023
hello
i am working with very small numbers near ordder 10^-20 in my calculations. in the program i wrote, matlab must calculate four numbers four me and find the maximum of them. but due to very smallness of these numbers , it considers them zero and can't find the maximum. what should i do? this is the code i wrote for comparison: prob1,prob2,prob3,prob4 are very small numbers
i want to find maximum of them after calculation with matlab.
ans=max([prob1,prob2,prob3,prob4]);
if (ans=prob1)
ans=1
if (ans=prob2)
ans=2
if (ans=prob3)
ans=3
if (ans=prob4)
ans=4
...................
it gives 4 because it considers prob1,prob2,prob3,prob4 as 0. please help me.
  1 comentario
James Tursa
James Tursa el 26 de Jul. de 2023
MATLAB does not consider 10^-20 to be 0, and the code you show is not valid MATLAB syntax. Please post an exact copy of the code you are running, along with the exact inputs to your code, and then post the MATLAB results including any error messages in their entirety.

Iniciar sesión para comentar.

Respuestas (2)

Steven Lord
Steven Lord el 26 de Jul. de 2023
i am working with very small numbers near ordder 10^-20 in my calculations. in the program i wrote, matlab must calculate four numbers four me and find the maximum of them. but due to very smallness of these numbers , it considers them zero
No, it almost certainly doesn't, not unless they're smaller than realmin (or really eps(0).) If you later add them to something much larger (relatively speaking) than they may be negligible but that's different from considering the numbers themselves zero. Take a look at the documentation page for the eps function for more information.
realmin
ans = 2.2251e-308
eps(0)
ans = 4.9407e-324
negligibleNumber = 1e-20
negligibleNumber = 1.0000e-20
isThisNonzero = negligibleNumber > 0 % true
isThisNonzero = logical
1
x = 1 + negligibleNumber % 1e-20 is smaller than eps(1) so x is exactly 1
x = 1
x == 1 % true
ans = logical
1
and can't find the maximum. what should i do? this is the code i wrote for comparison: prob1,prob2,prob3,prob4 are very small numbers
You don't need your if / elseif / else / end block. Just use the second output of max.
format longg
a = [1e-20 3e-40 5e-6 7e-89]
a = 1×4
1.0e+00 * 1e-20 3e-40 5e-06 7e-89
[maxValue, maxIndex] = max(a)
maxValue =
5e-06
maxIndex =
3

Star Strider
Star Strider el 26 de Jul. de 2023
If the numbers are all greater than zero, calculate the log of each and compare that. Remember that subtracting logaritms is essentially a division operation, so dividing them and then comparing those results is another option.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by