Matlab treating numbers smaller than 10^(-10) as zero

34 views (last 30 days)
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:
format long
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.
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 :) )

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 13 Jul 2021
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)
Q = function_handle with value:
mp = Q(16726)/10000*10^(-27)
mp = 
qCharge = Q(1602)/1000*10^(-19)
qCharge = 
Nr = Q(73331415)/10000000*1e+06
Nr = 
Dr = Q(457275009)/10000000
Dr = 
double([mp, qCharge, Nr, Dr])
ans = 1×4
1.6726e-27 1.602e-19 7333141.5 45.7275009
result = (5*Nr*mp^2)/(Dr*qCharge^2)
result = 
ans =
Sreeraj T
Sreeraj T on 14 Jul 2021
Edited: Sreeraj T on 14 Jul 2021
@Walter Roberson, yes. Now I got it. Sorry to drag this for a long time. If I had included the Array1.mat and Array2.mat in the initial time itself, this misunderstanding would not have happenned and you could have sorted this well earlier. The moral that I learned is to attach the relevent vectors in the question. Once again sorry, and thanks. Also thanks to @dpb.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!