problem about precision in matlab

조회 수: 14 (최근 30일)
nasrin
nasrin 2023년 7월 26일
답변: Steven Lord 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