Matlab treating numbers smaller than 10^(-10) as zero
Afficher commentaires plus anciens
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 commentaires
dpb
le 13 Juil 2021
Dunno how you get 1.9E-10 ans answer, but I don't get underflow even with the direct calculation as you've written it--
>> (5*Nr*mp^2)/(Dr*qCharge^2)
ans =
8.740610427341364e-11
>>
I'd probably write
5*Nr*mp/qCharge*mp/qCharge/Dr
to avoid the direct squaring of the small exponents but it really doesn't matter; the two results are the same until the last digit.
Sreeraj T
le 13 Juil 2021
dpb
le 13 Juil 2021
The values of Dr and Nr above don't match those used by Walter is why the answer isn't same. Not sure where that discrepency came from at the moment but since seem to have gotten desired result not going to try to find what changed where...
Walter Roberson
le 13 Juil 2021
The code values for the variable differed from the text description of what they should be.
dpb
le 13 Juil 2021
Ah, so. Figured something like that, but you had come up with "the right stuff" ...
Sreeraj T
le 13 Juil 2021
Walter Roberson
le 13 Juil 2021
Can you attach a mat with Array1 and Array2 for testing ?
Sreeraj T
le 14 Juil 2021
Réponses (1)
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 commentaires
Calculating using the values you gave, and substituting in the Nr and Dr from your text description instead of from your code:
format long
mp=1.6726*10^(-27);
qCharge=1.602*10^(-19);
Nr=11791924
Dr = 33.6273003
numer = (5*Nr*mp^2)
denom = (Dr*qCharge^2)
numer/denom
This is not approximated as 0: it is the value you indicate that you need.
Sreeraj T
le 13 Juil 2021
Walter Roberson
le 13 Juil 2021
My answer was in response to your original question, in which you had posted code and said that you expected a particular output for that code, and my answer demonstrated that when you evaluate to high precision that the expected value should be quite different than what you said. It turned out that you had a difference between your code and your descriptive text.
It also turns out that your actual problem is probably quite different than what you described. But for us to debug your actual problem you will need to attach your two vectors, as I asked before.
Walter Roberson
le 14 Juil 2021
Your arrays are single precision. You need to double() the value before you use them in the calculation.
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!