- You define the fprintf format with %s instead of some numeric format.
- disp(fprintf(...)) is very weird: fprintf returns the number of bytes printed. Why do you want to display this?
- You define
Can Matlab make calculation errors?
8 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Stephen23
on 12 Apr 2017
Edited: Stephen23
on 12 Apr 2017
Your code has many obvious bugs or "features" in it:
function [rhol,rhog,mug,mul] = prop
but call it with the incorrect output order:
[rhog, rhol] = prop();
which would clearly explain all/some of the differences that you are seeing.
No doubt there are more bugs/features, but basically this is a classic example of a beginner writing a huge amount of code without testing every line as they write it, and then not having a clue what the code is really doing. Sorry, but one line of correct code is more useful than fifty lines of untested and buggy code that no-one (not even its author) knows what it is doing.
The title is also rather telling: "Can Matlab make calculation errors?" Yes, it can. But it is highly unlikely that you will have discovered bugs in MATLAB by doing some very basic multiplications and divisions. MATLAB has been extensively tested and used by millions of people, who check its behavior and report bugs.
Writing a huge block of untested code is a waste of time. Write one line: test it, check that it does exactly what you need it to do. Check it by hand. Only then should you move on to the next line. Collect test cases to use on your code. Write comments (where are the comments, help, and input and output descriptions in your code?).
2 Comments
Stephen23
on 12 Apr 2017
Edited: Stephen23
on 12 Apr 2017
"what is the correct way to have multiple numerical answers (with variable names) displayed?"
fprintf is fine, but you do not need to disp as well! Note that fprintf is very versatile, which means that it has many options. Read its help carefully. If you want to display the variable name as well then do not end the line with a semi-colon, e.g.:
Cg
will display the name and value/s of variable Cg.
More Answers (0)
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!