problem about precision in matlab
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
James Tursa
am 26 Jul. 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.
Antworten (2)
Steven Lord
am 26 Jul. 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
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 Kommentare
Star Strider
am 26 Jul. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Performance and Memory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!