problem about precision in matlab

13 次查看(过去 30 天)
nasrin
nasrin 2023-7-26
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 个评论
James Tursa
James Tursa 2023-7-26
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.

请先登录,再进行评论。

回答(2 个)

Steven Lord
Steven Lord 2023-7-26
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 2023-7-26
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.

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by