problem about precision in matlab
13 次查看(过去 30 天)
显示 更早的评论
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
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
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
eps(0)
negligibleNumber = 1e-20
isThisNonzero = negligibleNumber > 0 % true
x = 1 + negligibleNumber % 1e-20 is smaller than eps(1) so x is exactly 1
x == 1 % true
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]
[maxValue, maxIndex] = max(a)
0 个评论
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!