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

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

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.
k. Thanks.
dpb
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...
The code values for the variable differed from the text description of what they should be.
dpb
dpb le 13 Juil 2021
Ah, so. Figured something like that, but you had come up with "the right stuff" ...
@dpb Thanks a lot for your time. I have just modified the question.
Can you attach a mat with Array1 and Array2 for testing ?
@Walter Roberson sure. I have appended that.

Connectez-vous pour commenter.

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)
Q = function_handle with value:
@(v)sym(v)
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 = 
double(result)
ans =
8.74061042734137e-11

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
Nr =
11791924
Dr = 33.6273003
Dr =
33.627300300000002
numer = (5*Nr*mp^2)
numer =
1.649448881251112e-46
denom = (Dr*qCharge^2)
denom =
8.630123799912121e-37
numer/denom
ans =
1.911269084306656e-10
This is not approximated as 0: it is the value you indicate that you need.
Dear @Walter Roberson many thanks for your answer. I have gone through your answer, but it seems like your is answer for a specific case, but mine is kind of general one. I have updated the question. Kindly have a look into it.
Thanks once again
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.
Your arrays are single precision. You need to double() the value before you use them in the calculation.
Sreeraj T
Sreeraj T le 14 Juil 2021
Modifié(e) : Sreeraj T le 14 Juil 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.

Connectez-vous pour commenter.

Produits

Version

R2016a

Question posée :

le 13 Juil 2021

Modifié(e) :

le 14 Juil 2021

Community Treasure Hunt

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

Start Hunting!

Translated by