1

I am having some issues implementing multiple functions in the same m file. The following code includes three functions which are called in the function HeatTransferModel. If I set up the m file exactly like the following, it is unable to recognize the functions RadOnly,analytica and RadConv. However, if I create separate m file for RadOnly,analytica and RadConv, the code works. Do you know why? How can I integrate all these functions in one m file? Sorry about all the unnecessary information.

function HeatTransferModel
global e rho sigma c V Arad Twall Ti tend h Tinf
e = 0.87;
rho = 770; %kg/m3
sigma = 5.67E-08; %Stefan Boltzman constant
c = 1900; %Heat capacity of white oak wood
Lavg = 0.3686; 
Wavg = 0.08382;
Havg = 0.05715;
V = 10*Lavg*Wavg*Havg; %Volume of the lumped wood
Arad =2*(Lavg*3*Havg) + 2*(3*Havg*3*Wavg) + Lavg*3*Wavg ; %Surface area of the lump that is exposed to radiation
Twall = 755; %Wall temperature of the furnace
Ti = 300; %Initial temperature of the wood when it is thrown in the furnace
tend = 500; %Seconds
h = 10;
Tinf = 500; %Temperature of the incoming air

[timeODE,TODE] = ode45('RadOnly',[0:1:tend],Ti);
[timeRadConv,TRadConv] = ode45('RadConv',[0,tend],Ti);
timeanalytical = analytical(TODE);
plot(timeODE,TODE,timeanalytical,TODE,timeRadConv,TRadConv);
legend('ODE Solver','Analytical Solution','RadConv');
title('Lumped Capacitance Model')
xlabel('Time [s]')
ylabel('Bulk Wood Temperature [K]')

end

function RadiationODE = RadOnly(t,T)
global e rho sigma c V Arad Twall h Tinf 
RadiationODE = -e*Arad*sigma*(T^4 - Twall^4)/(rho*V*c);
end

function time = analytical(T)
global e rho sigma c V Arad Twall Ti
time = ((rho*V*c)./(4*e*Arad*sigma*Twall^3)).*( log( abs((Twall + T)./(Twall-T))) - log(abs((Twall + Ti)./(Twall-Ti))) + 2*( atan(T./Twall) - atan(Ti/Twall)));
end

function RadConvODE = RadConv(t,T)
global e rho sigma c V Arad Twall h Tinf
RadConvODE = -e*Arad*sigma*(T.^4 - Twall^4)./(rho*V*c) + (h*Arad.*(T - Tinf))./(rho*V*c);
end
l3win
  • 235
  • 3
  • 10
  • Are you saying that the HeatTransferModel function does not work as written above? The end statements at the end of the functions are superfluous, but I don't think that would make it not see the subfunctions. – dustincarr Apr 11 '14 at 17:09
  • Looks like a duplicate question. Your answer can be found [here](http://stackoverflow.com/questions/3569933/is-it-possible-to-define-more-than-one-function-per-file-in-matlab). Short answer: you can't. – PDiracDelta Apr 11 '14 at 17:12

1 Answers1

0

You can't pass subfunction handles to Ode45, because that is in a different workspace than the primary function.

dustincarr
  • 1,365
  • 1
  • 9
  • 14
  • That might be the reason. My error is on the ODE45. It says:Error using feval Undefined function 'RadOnly' for input arguments of type 'double'. Error in odearguments (line 88) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0. Error in ode45 (line 114) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ... Error in HeatTransferModel (line 18) [timeODE,TODE] = ode45('RadOnly',[0:1:tend],Ti); – l3win Apr 11 '14 at 17:28