Matlab is giving different answers for the same calculation

19 次查看(过去 30 天)
Matlab is showing different results for the same calculation when done with direct values instead of using variables.
The calculation is: h = k*Nu/D_h
k = 0.129, Nu = 8.24, D_h = 0.0019
After running the script, the answer produced is: 562.7917
Doing the same calculation in the command window through the variables produces the same answer,
however the answer produced while doing the calculation in the command window using direct values is different.
Running " 0.129*8.24/0.0019 " in the command window produces the result: 559.4526
Matlab Script:
rho = 916; %kg/m^3
C_p = 1907; %J/kg
k = 0.129; %W/m*K
v = 1.6400e-05; %m^2/s
A_c = 1.2985e-05;
P = 0.0275;
D_h = 4*A_c/P
D_h = 0.0019
Flow_Rate = 0.000075; %m^3/s
vel = Flow_Rate/(12*A_c)
vel = 0.4813
Re = vel*D_h/v
Re = 55.4324
Nu = 8.24
Nu = 8.2400
h = (k*Nu)/D_h
h = 562.7917
Script Results:
Command Window Results:
In case its relevant, the system running Matlab is 64 bit and the version of Matlab being used is:
MATLAB Version: 23.2.0.2515942 (R2023b) Update 7
Operating System: Microsoft Windows 10 Home Single Language Version 10.0 (Build 19045)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
However, the same issue was observed on Matlab online.
The answer for this calculation from other sources is the same as the result when the direct values were used (559.4526).
Why is Matlab producing different results, and can anything be done to resolve this?

采纳的回答

Sam Chak
Sam Chak 2024-3-30
The discrepancy observed can be attributed to numerical precision errors introduced by humans. The scripted answer is the accurate one in this case. By default, the displayed answer is rounded to four decimal places. If you directly input the numerical values from the displayed answer into the formula, the computation accuracy will be compromised as it truncates beyond the displayed precision.
  4 个评论
Sam Chak
Sam Chak 2024-3-30
It's important to acknowledge that human errors are relatively common, especially when working on tasks manually and individually (one-man show), as is the case with experts as well.

请先登录,再进行评论。

更多回答(1 个)

Dyuman Joshi
Dyuman Joshi 2024-3-30
编辑:Dyuman Joshi 2024-3-30
That's because the value of D_h is not 0.0019.
The value stored is not the same as value displayed.
rho = 916; %kg/m^3
C_p = 1907; %J/kg
k = 0.129; %W/m*K
v = 1.6400e-05; %m^2/s
A_c = 1.2985e-05;
P = 0.0275;
%Change the format
format long
D_h = 4*A_c/P
D_h =
0.001888727272727
The value is a terminating irrational value.
Now, let's see the result of manual calculation in limited precision of floating point values-
out = 0.129*8.24/0.001888727272727
out =
5.627916827109015e+02
which in the default (short) format is
format default
out
out = 562.7917
which matches with the output you obtained.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by