function [ret,x0,str,ts,xts]=thermo(t,x,u,flag);
%THERMO	is the M-file description of the SIMULINK system named THERMO.
%	The block-diagram can be displayed by typing: THERMO.
%
%	SYS=THERMO(T,X,U,FLAG) returns depending on FLAG certain
%	system values given time point, T, current state vector, X,
%	and input vector, U.
%	FLAG is used to indicate the type of output to be returned in SYS.
%
%	Setting FLAG=1 causes THERMO to return state derivatives, FLAG=2
%	discrete states, FLAG=3 system outputs and FLAG=4 next sample
%	time. For more information and other options see SFUNC.
%
%	Calling THERMO with a FLAG of zero:
%	[SIZES]=THERMO([],[],[],0),  returns a vector, SIZES, which
%	contains the sizes of the state vector and other parameters.
%		SIZES(1) number of states
%		SIZES(2) number of discrete states
%		SIZES(3) number of outputs
%		SIZES(4) number of inputs.
%	For the definition of other parameters in SIZES, see SFUNC.
%	See also, TRIM, LINMOD, LINSIM, EULER, RK23, RK45, ADAMS, GEAR.

% Note: This M-file is only used for saving graphical information;
%       after the model is loaded into memory an internal model
%       representation is used.

% the system will take on the name of this mfile:
sys = mfilename;
new_system(sys)
simver(1.3)
if (0 == (nargin + nargout))
     set_param(sys,'Location',[11,59,573,394])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '1000000')
set_param(sys,'Min step size', '0.0001')
set_param(sys,'Max step size', '10')
set_param(sys,'Relative error','1e-3')
set_param(sys,'Return vars',   '')
set_param(sys,'Load callback','thermdat;');

add_block('built-in/Gain',[sys,'/',['Dollar',13,'Gain']])
set_param([sys,'/',['Dollar',13,'Gain']],...
		'Gain','cost',...
		'position',[420,15,440,35])


%     Subsystem  ['Temp',13,'Convert 3'].

new_system([sys,'/',['Temp',13,'Convert 3']])
set_param([sys,'/',['Temp',13,'Convert 3']],'Location',[577,197,810,324])

add_block('built-in/Gain',[sys,'/',['Temp',13,'Convert 3/Gain1']])
set_param([sys,'/',['Temp',13,'Convert 3/Gain1']],...
		'Gain','5/9',...
		'position',[125,17,160,53])

add_block('built-in/Outport',[sys,'/',['Temp',13,'Convert 3/Celsius',13,'out']])
set_param([sys,'/',['Temp',13,'Convert 3/Celsius',13,'out']],...
		'position',[180,25,200,45])

add_block('built-in/Inport',[sys,'/',['Temp',13,'Convert 3/Fahrenheit',13,'in']])
set_param([sys,'/',['Temp',13,'Convert 3/Fahrenheit',13,'in']],...
		'position',[15,15,35,35])

add_block('built-in/Constant',[sys,'/',['Temp',13,'Convert 3/Constant']])
set_param([sys,'/',['Temp',13,'Convert 3/Constant']],...
		'Value','32',...
		'position',[35,70,55,90])

add_block('built-in/Sum',[sys,'/',['Temp',13,'Convert 3/Sum']])
set_param([sys,'/',['Temp',13,'Convert 3/Sum']],...
		'inputs','+-',...
		'position',[85,15,105,50])
add_line([sys,'/',['Temp',13,'Convert 3']],[165,35;175,35])
add_line([sys,'/',['Temp',13,'Convert 3']],[110,35;120,35])
add_line([sys,'/',['Temp',13,'Convert 3']],[40,25;80,25])
add_line([sys,'/',['Temp',13,'Convert 3']],[60,80;70,80;80,40])
set_param([sys,'/',['Temp',13,'Convert 3']],...
		'Mask Display','F2C')


%     Finished composite block ['Temp',13,'Convert 3'].

set_param([sys,'/',['Temp',13,'Convert 3']],...
		'position',[280,122,310,158])

add_block('built-in/Scope',[sys,'/','Indoor Temp.'])
set_param([sys,'/','Indoor Temp.'],...
		'Vgain','100.000000',...
		'Hgain','43200.000000',...
		'Vmax','200.000000',...
		'Hmax','86400.000000',...
		'Window',[542,171,776,296])
open_system([sys,'/','Indoor Temp.'])
set_param([sys,'/','Indoor Temp.'],...
		'position',[510,72,530,98])

add_block('built-in/Constant',[sys,'/',['Avg Outdoor',13,'Temp']])
set_param([sys,'/',['Avg Outdoor',13,'Temp']],...
		'Value','50',...
		'position',[165,120,185,140])

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'position',[230,120,250,155])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'inputs','+-',...
		'position',[135,50,155,85])

add_block('built-in/Note',[sys,'/','Tout'])
set_param([sys,'/','Tout'],...
		'position',[345,120,350,125])


%     Subsystem  'House'.

new_system([sys,'/','House'])
set_param([sys,'/','House'],'Location',[282,316,666,552])

add_block('built-in/Integrator',[sys,'/','House/Integrator'])
set_param([sys,'/','House/Integrator'],...
		'Initial','TinIC',...
		'position',[220,35,240,55])

add_block('built-in/Gain',[sys,'/','House/1//Req'])
set_param([sys,'/','House/1//Req'],...
		'orientation',2,...
		'Gain','1/Req',...
		'position',[95,115,125,145])

add_block('built-in/Sum',[sys,'/','House/Sum2'])
set_param([sys,'/','House/Sum2'],...
		'orientation',2,...
		'inputs','-+',...
		'position',[155,114,175,141])

add_block('built-in/Gain',[sys,'/','House/1//Mc'])
set_param([sys,'/','House/1//Mc'],...
		'Gain','1/(M*c)',...
		'position',[160,27,190,63])

add_block('built-in/Sum',[sys,'/','House/Sum1'])
set_param([sys,'/','House/Sum1'],...
		'position',[110,27,130,58])

add_block('built-in/Inport',[sys,'/',['House/Heater',13,'QDot',13,'In']])
set_param([sys,'/',['House/Heater',13,'QDot',13,'In']],...
		'position',[45,25,65,45])

add_block('built-in/Outport',[sys,'/',['House/Indoor Temp',13,'Tin']])
set_param([sys,'/',['House/Indoor Temp',13,'Tin']],...
		'position',[305,35,325,55])

add_block('built-in/Inport',[sys,'/',['House/Outdoor Temp',13,'Tout']])
set_param([sys,'/',['House/Outdoor Temp',13,'Tout']],...
		'Port','2',...
		'position',[50,160,70,180])

add_block('built-in/Note',[sys,'/',['House/Thermodynamic Model',13,'for the House']])
set_param([sys,'/',['House/Thermodynamic Model',13,'for the House']],...
		'position',[290,145,295,150])
add_line([sys,'/','House'],[245,45;300,45])
add_line([sys,'/','House'],[270,45;270,120;180,120])
add_line([sys,'/','House'],[195,45;215,45])
add_line([sys,'/','House'],[90,130;85,130;85,50;105,50])
add_line([sys,'/','House'],[150,130;130,130])
add_line([sys,'/','House'],[135,45;155,45])
add_line([sys,'/','House'],[70,35;105,35])
add_line([sys,'/','House'],[75,170;210,170;210,135;180,135])
set_param([sys,'/','House'],...
		'Mask Display','plot(0,0,100,100,[20,80,80,20,20],[20,20,70,70,20],[15,50,85,15],[70,90,70,70],[42,42,29,29],[20,44,44,20],[52,68,68,52,52],[55,55,44,44,55])')


%     Finished composite block 'House'.

set_param([sys,'/','House'],...
		'Drop Shadow',4,...
		'position',[350,59,400,106])

add_block('built-in/Gain',[sys,'/',['Heater ',13,'Blower']])
set_param([sys,'/',['Heater ',13,'Blower']],...
		'Gain','Mdot*ha',...
		'position',[275,60,295,80])


%     Subsystem  'Thermostat'.

new_system([sys,'/','Thermostat'])
set_param([sys,'/','Thermostat'],'Location',[342,132,538,256])

add_block('built-in/Relay',[sys,'/','Thermostat/Relay1'])
set_param([sys,'/','Thermostat/Relay1'],...
		'On_switch_value','5*(5/9)',...
		'Off_switch_value','-5*(5/9)',...
		'position',[65,27,100,63])

add_block('built-in/Outport',[sys,'/',['Thermostat/Blower',13,'switch']])
set_param([sys,'/',['Thermostat/Blower',13,'switch']],...
		'position',[135,35,155,55])

add_block('built-in/Inport',[sys,'/','Thermostat/Terr'])
set_param([sys,'/','Thermostat/Terr'],...
		'position',[10,35,30,55])
add_line([sys,'/','Thermostat'],[105,45;130,45])
add_line([sys,'/','Thermostat'],[35,45;60,45])
set_param([sys,'/','Thermostat'],...
		'Mask Display','plot(0,0,100,100,[20,80,80,20,20],[20,20,80,80,20],[70,65,50,35,30],[20,40,50,40,20],[45,40,43],[47,59,45],[30,30],[60,60],[40,40],[65,65],[50,50],[70,70],[60,60],[65,65],[70,70],[60,60])')


%     Finished composite block 'Thermostat'.

set_param([sys,'/','Thermostat'],...
		'Drop Shadow',4,...
		'position',[185,51,230,89])

add_block('built-in/Constant',[sys,'/','Set Point'])
set_param([sys,'/','Set Point'],...
		'Value','70',...
		'position',[25,50,45,70])

add_block('built-in/Scope',[sys,'/','Floating Scope'])
set_param([sys,'/','Floating Scope'],...
		'Vgain','90.000000',...
		'Hgain','18000.000000',...
		'Vmax','180.000000',...
		'Hmax','36000.000000',...
		'Window',[0,0,280,220],...
		'position',[480,142,500,168])

add_block('built-in/Sine Wave',[sys,'/',['Daily Temp',13,'Variation']])
set_param([sys,'/',['Daily Temp',13,'Variation']],...
		'amplitude','15',...
		'frequency','2*pi/(24*3600)',...
		'position',[165,170,185,190])


%     Subsystem  ['Temp',13,'Convert 2'].

new_system([sys,'/',['Temp',13,'Convert 2']])
set_param([sys,'/',['Temp',13,'Convert 2']],'Location',[577,197,817,328])

add_block('built-in/Gain',[sys,'/',['Temp',13,'Convert 2/Gain']])
set_param([sys,'/',['Temp',13,'Convert 2/Gain']],...
		'Gain','9/5',...
		'position',[60,7,95,43])

add_block('built-in/Outport',[sys,'/',['Temp',13,'Convert 2/Fahrenheit',13,'out']])
set_param([sys,'/',['Temp',13,'Convert 2/Fahrenheit',13,'out']],...
		'position',[180,25,200,45])

add_block('built-in/Inport',[sys,'/',['Temp',13,'Convert 2/Celsius',13,'in']])
set_param([sys,'/',['Temp',13,'Convert 2/Celsius',13,'in']],...
		'position',[15,15,35,35])

add_block('built-in/Constant',[sys,'/',['Temp',13,'Convert 2/Constant']])
set_param([sys,'/',['Temp',13,'Convert 2/Constant']],...
		'Value','32',...
		'position',[65,70,85,90])

add_block('built-in/Sum',[sys,'/',['Temp',13,'Convert 2/Sum']])
set_param([sys,'/',['Temp',13,'Convert 2/Sum']],...
		'position',[130,15,150,50])
add_line([sys,'/',['Temp',13,'Convert 2']],[40,25;55,25])
add_line([sys,'/',['Temp',13,'Convert 2']],[100,25;125,25])
add_line([sys,'/',['Temp',13,'Convert 2']],[155,35;175,35])
add_line([sys,'/',['Temp',13,'Convert 2']],[90,80;105,80;105,40;125,40])
set_param([sys,'/',['Temp',13,'Convert 2']],...
		'Mask Display','C2F')


%     Finished composite block ['Temp',13,'Convert 2'].

set_param([sys,'/',['Temp',13,'Convert 2']],...
		'position',[445,68,475,102])


%     Subsystem  ['Temp',13,'Convert 1'].

new_system([sys,'/',['Temp',13,'Convert 1']])
set_param([sys,'/',['Temp',13,'Convert 1']],'Location',[577,197,810,324])

add_block('built-in/Sum',[sys,'/',['Temp',13,'Convert 1/Sum']])
set_param([sys,'/',['Temp',13,'Convert 1/Sum']],...
		'inputs','+-',...
		'position',[85,15,105,50])

add_block('built-in/Constant',[sys,'/',['Temp',13,'Convert 1/Constant']])
set_param([sys,'/',['Temp',13,'Convert 1/Constant']],...
		'Value','32',...
		'position',[35,70,55,90])

add_block('built-in/Inport',[sys,'/',['Temp',13,'Convert 1/Fahrenheit',13,'in']])
set_param([sys,'/',['Temp',13,'Convert 1/Fahrenheit',13,'in']],...
		'position',[15,15,35,35])

add_block('built-in/Outport',[sys,'/',['Temp',13,'Convert 1/Celsius',13,'out']])
set_param([sys,'/',['Temp',13,'Convert 1/Celsius',13,'out']],...
		'position',[180,25,200,45])

add_block('built-in/Gain',[sys,'/',['Temp',13,'Convert 1/Gain1']])
set_param([sys,'/',['Temp',13,'Convert 1/Gain1']],...
		'Gain','5/9',...
		'position',[125,17,160,53])
add_line([sys,'/',['Temp',13,'Convert 1']],[60,80;70,80;80,40])
add_line([sys,'/',['Temp',13,'Convert 1']],[40,25;80,25])
add_line([sys,'/',['Temp',13,'Convert 1']],[110,35;120,35])
add_line([sys,'/',['Temp',13,'Convert 1']],[165,35;175,35])
set_param([sys,'/',['Temp',13,'Convert 1']],...
		'Mask Display','F2C')


%     Finished composite block ['Temp',13,'Convert 1'].

set_param([sys,'/',['Temp',13,'Convert 1']],...
		'position',[70,42,100,78])

add_block('built-in/Integrator',[sys,'/','Integrator'])
set_param([sys,'/','Integrator'],...
		'position',[360,15,380,35])

add_block('built-in/Note',[sys,'/',['House Thermodynamics',13,'(Double click on the "?" for more info)']])
set_param([sys,'/',['House Thermodynamics',13,'(Double click on the "?" for more info)']],...
		'position',[130,240,135,245])

add_block('built-in/Note',[sys,'/','Terr'])
set_param([sys,'/','Terr'],...
		'position',[170,30,175,35])

add_block('built-in/Note',[sys,'/',['blower',13,'cmd']])
set_param([sys,'/',['blower',13,'cmd']],...
		'position',[255,30,260,35])

add_block('built-in/Note',[sys,'/',['heater',13,'QDot']])
set_param([sys,'/',['heater',13,'QDot']],...
		'position',[310,30,315,35])

add_block('built-in/Note',[sys,'/',['To start and stop the simulation, use the "Start//Stop"',13,'selection in the "Simulation" pull-down menu']])
set_param([sys,'/',['To start and stop the simulation, use the "Start//Stop"',13,'selection in the "Simulation" pull-down menu']],...
		'position',[144,285,149,290])

add_block('built-in/Note',[sys,'/','Tin'])
set_param([sys,'/','Tin'],...
		'position',[395,165,400,170])


%     Subsystem  'More Info'.

new_system([sys,'/','More Info'])
set_param([sys,'/','More Info'],'Location',[409,70,773,213])

add_block('built-in/Note',[sys,'/',['More Info/This system models the thermodynamics of a house.',13,'The variables are initialized by a file ',13,'called thermdat.m.']])
set_param([sys,'/',['More Info/This system models the thermodynamics of a house.',13,'The variables are initialized by a file ',13,'called thermdat.m.']],...
		'position',[177,25,182,30])

add_block('built-in/Note',[sys,'/',['More Info/Examine the file thermdat.m to see the ',13,'simulation variables you can change at will.',13,'']])
set_param([sys,'/',['More Info/Examine the file thermdat.m to see the ',13,'simulation variables you can change at will.',13,'']],...
		'position',[185,95,190,100])
set_param([sys,'/','More Info'],...
		'Mask Display','?')


%     Finished composite block 'More Info'.

set_param([sys,'/','More Info'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[270,245,297,270])


%     Subsystem  'More Info1'.

new_system([sys,'/','More Info1'])
set_param([sys,'/','More Info1'],'Location',[0,0,364,143])

add_block('built-in/Note',[sys,'/',['More Info1/Examine the file "thermo_script.m to',13,'get a feel for the wide range of variables at play here.']])
set_param([sys,'/',['More Info1/Examine the file "thermo_script.m to',13,'get a feel for the wide range of variables at play here.']],...
		'position',[185,95,190,100])

add_block('built-in/Note',[sys,'/',['More Info1/This system models the thermodynamics of a house.',13,'The variables are initialized by a file ',13,'called "thermo_script".']])
set_param([sys,'/',['More Info1/This system models the thermodynamics of a house.',13,'The variables are initialized by a file ',13,'called "thermo_script".']],...
		'position',[177,25,182,30])
set_param([sys,'/','More Info1'],...
		'Mask Display','Show Script\nListing',...
		'Mask Dialogue','eval(''type thermdat'')')


%     Finished composite block 'More Info1'.

set_param([sys,'/','More Info1'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[457,285,542,324])


%     Subsystem  'More Info2'.

new_system([sys,'/','More Info2'])
set_param([sys,'/','More Info2'],'Location',[0,56,364,199])

add_block('built-in/Note',[sys,'/',['More Info2/This system models the thermodynamics of a house.',13,'The variables are initialized by a file ',13,'called "thermo_script".']])
set_param([sys,'/',['More Info2/This system models the thermodynamics of a house.',13,'The variables are initialized by a file ',13,'called "thermo_script".']],...
		'position',[177,25,182,30])

add_block('built-in/Note',[sys,'/',['More Info2/Examine the file "thermo_script.m to',13,'get a feel for the wide range of variables at play here.']])
set_param([sys,'/',['More Info2/Examine the file "thermo_script.m to',13,'get a feel for the wide range of variables at play here.']],...
		'position',[185,95,190,100])
set_param([sys,'/','More Info2'],...
		'Mask Display','Load Data',...
		'Mask Dialogue','eval(''thermdat'')')


%     Finished composite block 'More Info2'.

set_param([sys,'/','More Info2'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[457,236,541,269])


%     Subsystem  'More Info3'.

new_system([sys,'/','More Info3'])
set_param([sys,'/','More Info3'],'Location',[128,280,487,453])

add_block('built-in/Note',[sys,'/',['More Info3/This system models a double pendulum system where',13,'the rest state has both pendulums hanging vertically.']])
set_param([sys,'/',['More Info3/This system models a double pendulum system where',13,'the rest state has both pendulums hanging vertically.']],...
		'position',[168,20,173,25])

add_block('built-in/Note',[sys,'/',['More Info3/Associated with the demo is an animation function that',13,'will automatically open a figure window and display to it.']])
set_param([sys,'/',['More Info3/Associated with the demo is an animation function that',13,'will automatically open a figure window and display to it.']],...
		'position',[174,65,179,70])

add_block('built-in/Note',[sys,'/',['More Info3/Try experimenting with different initial conditions for',13,'alpha and gamma to see the unusal coupling effects.']])
set_param([sys,'/',['More Info3/Try experimenting with different initial conditions for',13,'alpha and gamma to see the unusal coupling effects.']],...
		'position',[175,115,180,120])
set_param([sys,'/','More Info3'],...
		'Mask Display','Double click\nhere for\nSIMULINK Help',...
		'Mask Dialogue','eval(''[cs,cb]=get_param;str=[''''simcad '''' cs];eval(str);'')')


%     Finished composite block 'More Info3'.

set_param([sys,'/','More Info3'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[325,260,427,311])

add_block('built-in/Scope',[sys,'/','Heat Cost ($)'])
set_param([sys,'/','Heat Cost ($)'],...
		'Vgain','15.000000',...
		'Hgain','43200.000000',...
		'Vmax','30.000000',...
		'Hmax','86400.000000',...
		'Window',[545,435,775,567])
open_system([sys,'/','Heat Cost ($)'])
set_param([sys,'/','Heat Cost ($)'],...
		'position',[490,12,510,38])
add_line(sys,[445,25;485,25])
add_line(sys,[385,25;415,25])
add_line(sys,[315,140;325,140;325,95;345,95])
add_line(sys,[255,140;275,140])
add_line(sys,[105,60;130,60])
add_line(sys,[50,60;65,60])
add_line(sys,[405,85;440,85])
add_line(sys,[415,85;415,225;115,225;115,75;130,75])
add_line(sys,[190,180;210,180;210,145;225,145])
add_line(sys,[190,130;225,130])
add_line(sys,[160,70;180,70])
add_line(sys,[235,70;270,70])
add_line(sys,[480,85;505,85])
add_line(sys,[300,70;345,70])
add_line(sys,[330,70;330,25;355,25])

drawnow

% Return any arguments.
if (nargin | nargout)
	% Must use feval here to access system in memory
	if (nargin > 3)
		if (flag == 0)
			eval(['[ret,x0,str,ts,xts]=',sys,'(t,x,u,flag);'])
		else
			eval(['ret =', sys,'(t,x,u,flag);'])
		end
	else
		[ret,x0,str,ts,xts] = feval(sys);
	end
end
