How do I get my codes to work together?

2 views (last 30 days)
Ugo Mbakwe
Ugo Mbakwe on 3 May 2014
Moved: Sabin on 24 Mar 2024
I wanted to test out a simulation of a Stirling engine I ran across on an online Appendix. My problem is I cant seem to get the code to work. I have attached the code in an attachment. My question is how do I put the code into Matlab in order to plot the outputs. Are these separate codes that I need to put all in one or do I need to use only one of them and just save the rest of them in a certain directory?
*main.m*
global d_displacer;
global l_displacer;
Ti = 0:.01:.99;
W = enginefinal2(ddisplacer, ldisplacer, Ti);
*enginefinal2.m*
function W = enginefinal2(d_displacer, ldisplacer, Ti)
global T;
global dpower;
global lpower;
dpower = 0.625;
l_power = 0.875;
form= 1:1: 1 00
T m/100;
Q1l(m) = quad((enginenum2,0,2*pi);
Q2(m) = quad((engineden2,0,2*pi);
W(m)=2*pi*Q 1 (m)./Q2(m);
end
plot (Ti, W);
title('Normalized Engine Work per Cycle vs. Ratio of Cold to Hot side Temperature');
xlabel('Tc/Th');
ylabel('W');
*enginenum2.m*
function num= enginenum2(x)
global T
global ddisplacer
global ldisplacer
global d_power
global l_power
d_heatcyl = d displacer + 0.125;
d_powercyl = dpower + 0.035;
Vhmax - pi*dheatcylA2/4*(3.510-1.960);
Vh_min pi*dheatcyl^2/4*(3.510-1.960-.5);
Vc_max = pi*dpowercyl2/4*(.25-(l_power-.875)) + pi*dheatcyl^2/4*(.5+(1.960-
l_displacer));
Vc_min = pi*d_powercylA2/4*(.25-(lpower-.875)) + pi*d heatcylA2/4*(1.960-
ldisplacer);
Vc = (Vcmax+Vc_min)/2; %Average volume of engine cold side
Vh = (Vhmax+Vh_min)/2; %Average volueme of engine hot side
vp = pi*d_power^2/4*.5; %Change in engine volume from the piston
vd = pi*ddisplacer^2/4*.5; %Change in hot and cold engine volume from the displacer
piston
p = pi/2; %phi= phase difference between the displacer and power piston
num = (cos(x).*(Vh + vd*sin(x+p) + (Vc + vp*sin(x) - vd*sin(x+p))*T))./((Vc + Vh +
vp*sin(x)).2);
*engineden2.m*
function den = engineden2(x)
global T
global d_displacer
global l_displacer
global dpower
global l_power
dheatcyl = d displacer + 0.125;
d_powercyl = dpower + 0.035;
Vh_max pi*dheatcylA2/4*(3.510-1.960);
Vh_min pi*d heatcylA2/4*(3.510-1.960-.5);
Vc_max pi*d_powercylA2/4*(.25-(l_power-.875)) + pi*dheatcylA2/4*(.5+(1.960-
l_displacer));
Vc_min = pi*d_powercyl2/4*(.25-(lpower-.875)) + pi*dheatcylA2/4*(1.960-
l_displacer);
Vc = (Vc max+Vc_min)/2; %Average volume of engine cold side
Vh = (Vh max+Vh_min)/2; %Average volueme of engine hot side
vp = pi*d_powerA2/4*.5; %Change in engine volume from the piston
vd = pi*ddisplacerA2/4*.5; %Change in hot and cold engine volume from the displacer
piston
p = pi/2; %phi= phase difference between the displacer and power piston
den- ((Vh + vd*sin(x+p) + (Vc + vp*sin(x) -vd*sin(x+p))*T))./((Vc + Vh +
vp*sin(x)).2);

Answers (1)

Geoff Hayes
Geoff Hayes on 3 May 2014
Moved: Sabin on 24 Mar 2024
Hi Ugo,
From the above code, there should be four files: main.m, enginefinal2.m, enginenum2.m, and engineden2.m. Just look for these file names in the above (where each is enclosed within two stars) and place the blocks that follow in separate files named as shown. All four files can (and should) be saved to the same directory.
However, in the main.m block of code, there are two global variables d_displacer and l_displacer that are not set and then are passed immediately to the enginefinal2 function. Should they have been set somewhere else? Also there look to be several other bugs: a global variable l_power is then set as lpower, or maybe they are two different variables? Same with ddisplacer and d_displacer - same or different? Code written as d displacer + 0.125 when it should probably be d_displacer + 0.125. (This is in a couple of places.) There may be other bugs too that will be revealed once you start running the code.
Geoff

Categories

Find more on Graphics Performance 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!