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

10 vues (au cours des 30 derniers jours)
Sreeraj T
Sreeraj T le 13 Juil 2021
Modifié(e) : Sreeraj T le 14 Juil 2021
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
Walter Roberson
Walter Roberson le 13 Juil 2021
Can you attach a mat with Array1 and Array2 for testing ?
Sreeraj T
Sreeraj T le 14 Juil 2021
@Walter Roberson sure. I have appended that.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 13 Juil 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:
@(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
Walter Roberson
Walter Roberson le 14 Juil 2021
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.

Catégories

En savoir plus sur Language Support dans Help Center et File Exchange

Produits


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by