Matlab is giving different answers for the same calculation
Show older comments
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
Flow_Rate = 0.000075; %m^3/s
vel = Flow_Rate/(12*A_c)
Re = vel*D_h/v
Nu = 8.24
h = (k*Nu)/D_h
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?
Accepted Answer
More Answers (1)
Dyuman Joshi
on 30 Mar 2024
Edited: Dyuman Joshi
on 30 Mar 2024
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
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
which in the default (short) format is
format default
out
which matches with the output you obtained.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!