Can Matlab make calculation errors?
Mostra commenti meno recenti
Hi,
I am writing a code for Matlab to solve some simple engineering equations, so I am using numerous scripts for inputs calculations of different variables etc and referencing between each other.
The issue I came across is that Matlab sometimes returns incorrect result of simple calculations. My question is, if just the display is wrong, if there are issues with my referencing or it is just wrong? How can this be avoided? Here is some examples:
EXAMPLE 1:
function [rhol,rhog,mug,mul] = prop
rhol = 1000 ; %[kg/m3]
rhog = 1 ; %[kg/m3]
mug = 1.8E-5 ; %[Pa.s=kg/m.s]
mul = 1E-3 ; %[Pa.s=kg/m.s]
end
function [Y1,Y] = getY
[D, theta] = geom();
Mg = flow();
[rhog, rhol] = prop();
[Reg] = getRe() ;
g = 9.18;
R = D/2;
A = 3.14*R^2;
ug = Mg/(rhog*A);
fg = 16/Reg ;
dpdlg = rhog*(ug^2)*fg/D ;
Y1 = (rhol-rhog) ;
Y = ( (rhol-rhog)*g*sin(theta) ) / dpdlg;
disp(fprintf('%s fg.\n',fg));
disp(fprintf('%s dpdlg.\n',dpdlg));
disp(fprintf('%s Y1.\n',Y1));
end
WRONG ANSWER: >> getY
4.069437e-01 fg. 17
1.304460e-05 dpdlg. 20
-9.990000e+02 Y1. 18
ans =
-999
This states that Y1=rhol-rhog= - 999 (negative) but as rhol =1000 and rhog=1, Y1 should be 999 (positive).
I tried not using reference to prop.m script as it seems to be causing issues and defining rho values again in getYs.m script.
function [Y1,Y] = getY
[D, theta] = geom();
Mg = flow();
[Reg] = getRe() ;
g = 9.18;
%[rhog, rhol] = prop();
rhol = 1000 ; %[kg/m3]
rhog = 1 ; %[kg/m3]
R = D/2;
A = 3.14*R^2;
ug = Mg/(rhog*A);
fg = 16/Reg ;
dpdlg = rhog*(ug^2)*fg/D ;
Y1 = (rhol-rhog) ;
Y = ( (rhol-rhog)*g*sin(theta) ) / dpdlg;
disp(fprintf('%s fg.\n',fg));
disp(fprintf('%s dpdlg.\n',dpdlg));
disp(fprintf('%s Y1.\n',Y1));
end
ANSWER NOT DISPLAYING RIGHT: >> getY 4.069437e-01 fg. 17
1.304460e-02 dpdlg. 20
ϧ Y1. 6
EXAMPLE 2:
function [rhol,rhog,mug,mul] = prop
rhol = 1000 ; %[kg/m3]
rhog = 1 ; %[kg/m3]
mug = 1.8E-5 ; %[Pa.s=kg/m.s]
mul = 1E-3 ; %[Pa.s=kg/m.s]
end
function [X] = getX
[rhog, rhol] = prop();
[Mg, Ml] = flow();
[Reg, Rel] = getRe();
[n,m,Cl,Cg] = getblas();
X=sqrt((Reg^m/Rel^n)*(Cl/Cg)*((Ml/Mg)^2)*(rhog/rhol)) ;
disp(fprintf('%s X.\n',X));
end
WRONG ANSWER: >> getX
6.999997e+03 X. 16
ans =
7.0000e+03
(EXPECTED ANSWER =7.0) So I took out the reference to prop.m script and defined rhog and rhol in the same sheet, which gave a correct answer. But why does this referencing not work??
function [X] = getX
%[rhog, rhol] = prop();
rhol = 1000 ; %[kg/m3]
rhog = 1 ; %[kg/m3]
[Mg, Ml] = flow();
[Reg, Rel] = getRe();
[n,m,Cl,Cg] = getblas();
X=sqrt((Reg^m/Rel^n)*(Cl/Cg)*((Ml/Mg)^2)*(rhog/rhol)) ;
disp(fprintf('%s X.\n',X));
end
CORRECT ANSWER >> getX 6.999997e+00 X. 16 ans =
7.0000
EXAMPLE 3:
function [n,m,Cl,Cg] = getblas()
[Reg, Rel] = getRe();
if (Rel>=2000) && (Reg>=2000)
n = 0.2 ;
m = 0.2 ;
Cl = 0.046 ;
Cg = 0.046 ;
elseif (Rel<=1000) && (Reg>=2000)
n = 1 ;
m = 0.2 ;
Cl = 16 ;
Cg = 0.046 ;
elseif (Rel>=2000) && (Reg<=1000)
n = 1 ;
m = 0.2 ;
Cl = 16 ;
Cg = 0.046 ;
elseif (Rel<=1000) && (Reg<=1000)
n=1 ;
m=1 ;
Cl=16 ;
Cg=16 ;
else
disp('Unexpected Re value')
end
disp(fprintf('%s Reg.\n', Reg));
disp(fprintf('%s Rel.\n', Rel));
disp(fprintf('%s Parameter n.\n',n));
disp(fprintf('%s Parameter m.\n',m));
disp(fprintf('%s Parameter Cl.\n',Cl));
disp(fprintf('%s Parameter Cg.\n',Cg));
end
ANSWER NOT DISPLAYING RIGHT: 3.931748e+01 Reg. 18
6.242038e+02 Rel. 18
Parameter n. 15
Parameter m. 15
Parameter Cl. 16
Parameter Cg. 16
ans =
1
(EXPECTING: n=1 ; m=1 ; Cl=16 ; Cg=16 ; )
This has been quite frustrating as I don't know how many more mistakes like this there might be in my calculations.
Any help will be highly appreciated!
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Surrogate Optimization in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!