Different results for formula in Matlab and Excel

19 vues (au cours des 30 derniers jours)
Osama Anwar
Osama Anwar le 21 Avr 2021
Commenté : Osama Anwar le 21 Avr 2021
I derived a formula using a long calculation and it gives satisfactory answer (1759) when variables are replaced in Matlab
f__prime_c=4000;
kd=44.928;
epsilon_o=2*4000/(57000*sqrt(4000));
epsilon_ct=0.003;
epsilon_c=0.001264;
epsilon_ult=0.003;
z=150;
double((3*f__prime_c*kd*(epsilon_c^3-3*epsilon_c^2*epsilon_o+2*epsilon_o^3))/(500*epsilon_o^2*epsilon_ct)-(9*f__prime_c*kd*(-epsilon_ct^2+epsilon_o*epsilon_ult)*(-z*epsilon_ct^2+2*epsilon_ult+epsilon_o*epsilon_ult*z))/(1000*epsilon_ct*epsilon_ult^2))
But when I bring the same equation in a cell in excel ,answers is different.
I had made sure that replacement is correct but everytime I replace variables with cells or even with numerical values in excel ,answer is different from Matlab.
The Numerical values for variables are same in matlab and excel.
Any apparent reasons for the problem?
Btw answer is 1759 in matlab and -4332 in excel
  6 commentaires
Osama Anwar
Osama Anwar le 21 Avr 2021
The problem is in Bn but I can't figure it out.
I have compared the excel values with matlab values but still unable to figure out the real cause.
Osama Anwar
Osama Anwar le 21 Avr 2021
1st pic is of the cell with variable names (R72) and second one is using cell values (S72)

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 21 Avr 2021
The problem is operator precedence. Here are the rules for operator precedence in Microsoft Excel and here are the rules for MATLAB. In Excel negation (unary minus) takes precedence over exponentiation so -epsilon_ct^2 is evaluated as (-epsilon_ct)^2 while in MATLAB unary minus is at precedence level 4 while exponentiation is at levels 2 and 3 so -epsilon_ct^2 is -(epsilon_ct^2).
epsilon_ct = 0.003;
resultFromMATLAB = -(epsilon_ct^2)
resultFromMATLAB = -9.0000e-06
resultFromExcel = (-epsilon_ct)^2
resultFromExcel = 9.0000e-06
When I modified the formula in Excel to use parentheses like resultFromMATLAB above its answer agreed with the answer from MATLAB. Or you could rewrite that term slightly to reduce the need for parentheses, since subtraction is lower than exponentiation in both Excel and MATLAB.
epsilon_o=2*4000/(57000*sqrt(4000));
epsilon_ct=0.003;
epsilon_ult=0.003;
term1 = epsilon_o*epsilon_ult-epsilon_ct^2
term1 = -2.3426e-06
term2 = -(epsilon_ct^2) + epsilon_o*epsilon_ult
term2 = -2.3426e-06
term3 = (-epsilon_ct)^2 + epsilon_o*epsilon_ult
term3 = 1.5657e-05
  1 commentaire
Osama Anwar
Osama Anwar le 21 Avr 2021
Thanks man. I replaced - by +(-1)* and it worked.

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 21 Avr 2021
It looks, like you have a typo in the code you have typed in Excel.
  4 commentaires
Osama Anwar
Osama Anwar le 21 Avr 2021
Attached.
Osama Anwar
Osama Anwar le 21 Avr 2021
@Image Analyst Yeah I did that

Connectez-vous pour commenter.


Osama Anwar
Osama Anwar le 21 Avr 2021
Excel file

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by