Matlab treating numbers smaller than 10^(-10) as zero
10 次查看(过去 30 天)
显示 更早的评论
I have a program which does some calculations. At some point, one has to take two values, lets say Nr and Dr having values 7.3331415e+06 and 45.7275009, respectively. It has to be multiplied by (5*MassOfProton^2)/(Charge^2). For this set of parameters, the correct answer is 8.74061*10^-11. But Matlab approximates to zero. I need that particular value. How can I do this?
The code is given as below:
clc
format long
mp=1.6726*10^(-27);
qCharge=1.602*10^(-19);
Nr=7.3331415e+06
Dr=45.7275009
(5*Nr*mp^2)/(Dr*qCharge^2)
I have looked into various solution given here and here, but did not get any solution. Any help will be deeply appreciated! Thanks in advance
**************************************************
Edit 1:
After reading the comments and answers, I realised something strange.
Well, the values of Nr and Dr is 26th element two different 1x72 single array, which I have loaded into MATLAB workspace from a CDF file.
i.e.
Nr=Array1(26);
Dr= Array2(26);
When I am using this instead of the exact value that is seen at the specific location (i.e. 26th column), I am getting zero. But If I am using the numbers which I had copied from that exact location (just like as I shown in my earlier code), I am getting the answer 8.74061*10^-11, as mentioned earlier. Further, in the long run, I am planning to replace iterate over so that 26 will be replaced by i.
One more information (not sure whether useful or not): I went and looked into CDF file and found that there is a column titled ‘FORMATS’. In it, it is shown that the FORMAT corresponding to Nr is 'E12.2' and that of Dr is 'E8.2' (not pretty sure what they are). Further, 7.3331415e+06- Array1(26) gives me zero, while 45.7275009- Array2(26) gives me a small finite value “-1.5527341e-08”
Edit 2:
Appending 'Array1' and 'Array2' mat file for testing (Sorry, didn't knew earlier that there was a provision to save the variables :) )
8 个评论
回答(1 个)
Walter Roberson
2021-7-13
Assuming that every floating point number you gave is an exact decimal fraction (which is not realistic), then the result of the calculation is an order of magnitude different than you indicate.
format long g
Q = @(v) sym(v)
mp = Q(16726)/10000*10^(-27)
qCharge = Q(1602)/1000*10^(-19)
Nr = Q(73331415)/10000000*1e+06
Dr = Q(457275009)/10000000
double([mp, qCharge, Nr, Dr])
result = (5*Nr*mp^2)/(Dr*qCharge^2)
double(result)
5 个评论
Walter Roberson
2021-7-14
Your arrays are single precision. You need to double() the value before you use them in the calculation.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Language Support 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!