function [ret,x0,str]=pendmodl(t,x,u,flag);
%PENDMODL	is the M-file description of the SIMULINK system named PENDMODL.
%	The block-diagram can be displayed by typing: PENDMODL.
%
%	SYS=PENDMODL(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 PENDMODL 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 PENDMODL with a FLAG of zero:
%	[SIZES]=PENDMODL([],[],[],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.2)
if(0 == (nargin + nargout))
     set_param(sys,'Location',[4,42,422,268])
     open_system(sys)
end;
set_param(sys,'algorithm',		'RK-45')
set_param(sys,'Start time',	'0.0')
set_param(sys,'Stop time',		'10')
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',	'')

add_block('built-in/Saturation',[sys,'/','Saturation'])
set_param([sys,'/','Saturation'],...
		'orientation',1,...
		'Lower Limit','-1',...
		'Upper Limit','1',...
		'position',[187,60,213,80])

add_block('built-in/Switch',[sys,'/','Switch'])
set_param([sys,'/','Switch'],...
		'position',[225,94,250,126])


%     Subsystem  'Pendulum Model'.

new_system([sys,'/','Pendulum Model'])
set_param([sys,'/','Pendulum Model'],'Location',[189,74,565,302])

add_block('built-in/Outport',[sys,'/','Pendulum Model/Theta'])
set_param([sys,'/','Pendulum Model/Theta'],...
		'position',[330,55,350,75])

add_block('built-in/Gain',[sys,'/','Pendulum Model/Inertia'])
set_param([sys,'/','Pendulum Model/Inertia'],...
		'Gain','1/inertia',...
		'Mask Display','1/K',...
		'Mask Type','Inertia',...
		'Mask Dialogue','Pendulum Inertia|Inertia',...
		'Mask Translate','inertia=@1;')
set_param([sys,'/','Pendulum Model/Inertia'],...
		'Mask Help','Allows you to enter a constant representing the\nInertia of the pendulum\nI=l*l*m',...
		'Mask Entries','48\/',...
		'position',[185,55,205,75])

add_block('built-in/Integrator',[sys,'/','Pendulum Model/To Omega'])
set_param([sys,'/','Pendulum Model/To Omega'],...
		'position',[230,55,250,75])

add_block('built-in/Integrator',[sys,'/','Pendulum Model/To Theta'])
set_param([sys,'/','Pendulum Model/To Theta'],...
		'Initial','-0.7',...
		'position',[280,55,300,75])

add_block('built-in/Fcn',[sys,'/','Pendulum Model/Gravity Torque'])
set_param([sys,'/','Pendulum Model/Gravity Torque'],...
		'orientation',2,...
		'Expr','m*l*9.80665*sin(u[1])',...
		'Mask Display','m l g sin(theta)',...
		'Mask Type','mlgsin(Theta)',...
		'Mask Dialogue','mlgsin(Theta)|m:|l:')
set_param([sys,'/','Pendulum Model/Gravity Torque'],...
		'Mask Translate','m=@1;l=@2;',...
		'Mask Help','Allows you to enter m and l\nm in kg, l in m\noutput = m*l*g*sin(input) ',...
		'Mask Entries','3\/4\/',...
		'position',[180,116,245,134])

add_block('built-in/Sum',[sys,'/','Pendulum Model/Sum Torques'])
set_param([sys,'/','Pendulum Model/Sum Torques'],...
		'position',[135,55,155,75])

add_block('built-in/Inport',[sys,'/','Pendulum Model/Current'])
set_param([sys,'/','Pendulum Model/Current'],...
		'position',[15,50,35,70])

add_block('built-in/Gain',[sys,'/','Pendulum Model/Motor Torque'])
set_param([sys,'/','Pendulum Model/Motor Torque'],...
		'Gain','motorsize',...
		'Mask Display','K',...
		'Mask Type','Motorsize Gain',...
		'Mask Dialogue','Motorsize Gain|Motorsize',...
		'Mask Translate','motorsize=@1;')
set_param([sys,'/','Pendulum Model/Motor Torque'],...
		'Mask Help','Allows you to enter a constant representing the size\nof the motor driving the pendulum',...
		'Mask Entries','120\/',...
		'position',[65,50,85,70])
add_line([sys,'/','Pendulum Model'],[90,60;125,60])
add_line([sys,'/','Pendulum Model'],[210,65;220,65])
add_line([sys,'/','Pendulum Model'],[160,65;175,65])
add_line([sys,'/','Pendulum Model'],[175,125;115,125;115,70;125,70])
add_line([sys,'/','Pendulum Model'],[305,65;315,65;315,125;255,125])
add_line([sys,'/','Pendulum Model'],[255,65;270,65])
add_line([sys,'/','Pendulum Model'],[40,60;55,60])
add_line([sys,'/','Pendulum Model'],[305,65;320,65])


%     Finished composite block 'Pendulum Model'.

set_param([sys,'/','Pendulum Model'],...
		'position',[270,85,300,135])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'inputs','-+',...
		'position',[60,100,80,120])


%     Subsystem  'PID Controller'.

new_system([sys,'/','PID Controller'])
set_param([sys,'/','PID Controller'],'Location',[0,0,362,244])

add_block('built-in/Inport',[sys,'/','PID Controller/In_1'])
set_param([sys,'/','PID Controller/In_1'],...
		'position',[25,65,45,85])

add_block('built-in/Outport',[sys,'/','PID Controller/Out_1'])
set_param([sys,'/','PID Controller/Out_1'],...
		'position',[290,65,310,85])

add_block('built-in/Derivative',[sys,'/','PID Controller/Derivative'])
set_param([sys,'/','PID Controller/Derivative'],...
		'position',[150,128,190,152])

add_block('built-in/Transfer Fcn',[sys,'/','PID Controller/Integral'])
set_param([sys,'/','PID Controller/Integral'],...
		'Numerator','[I]',...
		'Denominator','[1 0]',...
		'position',[110,57,145,93])

add_block('built-in/Gain',[sys,'/','PID Controller/Proportional'])
set_param([sys,'/','PID Controller/Proportional'],...
		'Gain','P',...
		'position',[120,13,140,37])

add_block('built-in/Gain',[sys,'/','PID Controller/D'])
set_param([sys,'/','PID Controller/D'],...
		'Gain','D',...
		'position',[95,129,115,151])

add_block('built-in/Sum',[sys,'/','PID Controller/Sum'])
set_param([sys,'/','PID Controller/Sum'],...
		'inputs','+++',...
		'position',[245,57,265,93])
add_line([sys,'/','PID Controller'],[145,25;210,25;210,65;235,65])
add_line([sys,'/','PID Controller'],[150,75;235,75])
add_line([sys,'/','PID Controller'],[195,140;215,140;215,85;235,85])
add_line([sys,'/','PID Controller'],[50,75;100,75])
add_line([sys,'/','PID Controller'],[80,75;80,25;110,25])
add_line([sys,'/','PID Controller'],[65,75;65,140;85,140])
add_line([sys,'/','PID Controller'],[270,75;280,75])
add_line([sys,'/','PID Controller'],[120,140;140,140])
set_param([sys,'/','PID Controller'],...
		'Mask Display','PID',...
		'Mask Type','PID Controller',...
		'Mask Dialogue','Enter expressions for proportional, integral, and derivative terms.|Proportional:|Integral|Derivative:')
set_param([sys,'/','PID Controller'],...
		'Mask Translate','P=@1; I=@2; D=@3;')
set_param([sys,'/','PID Controller'],...
		'Mask Help','This block implements a PID controller where paramaters are entered for the Proportional, Integral and Derivitive terms. Unmask this block to see how it works. The derivative term is implemented using a true derivative block.')
set_param([sys,'/','PID Controller'],...
		'Mask Entries','-3\/0\/-1.5\/')


%     Finished composite block 'PID Controller'.

set_param([sys,'/','PID Controller'],...
		'position',[100,174,140,206])


%     Subsystem  ['Fuzzy Controller',13,'Subsytem'].

new_system([sys,'/',['Fuzzy Controller',13,'Subsytem']])
set_param([sys,'/',['Fuzzy Controller',13,'Subsytem']],'Location',[47,243,423,443])

add_block('built-in/Derivative',[sys,'/',['Fuzzy Controller',13,'Subsytem/Derivative']])
set_param([sys,'/',['Fuzzy Controller',13,'Subsytem/Derivative']],...
		'position',[75,65,105,85])

add_block('built-in/S-function',[sys,'/',['Fuzzy Controller',13,'Subsytem/Fuzzy Controller']])
set_param([sys,'/',['Fuzzy Controller',13,'Subsytem/Fuzzy Controller']],...
		'function name','sfuzzym',...
		'Mask Display','',...
		'Mask Type','Fuzzy Controller',...
		'Mask Dialogue','Fuzzy Controller',...
		'Mask Translate','')
set_param([sys,'/',['Fuzzy Controller',13,'Subsytem/Fuzzy Controller']],...
		'Mask Help','Implements a PD fuzzy controller',...
		'position',[210,60,260,80])

add_block('built-in/Outport',[sys,'/',['Fuzzy Controller',13,'Subsytem/out_1']])
set_param([sys,'/',['Fuzzy Controller',13,'Subsytem/out_1']],...
		'position',[335,60,355,80])

add_block('built-in/Inport',[sys,'/',['Fuzzy Controller',13,'Subsytem/in_1']])
set_param([sys,'/',['Fuzzy Controller',13,'Subsytem/in_1']],...
		'position',[15,65,35,85])

add_block('built-in/Mux',[sys,'/',['Fuzzy Controller',13,'Subsytem/Mux']])
set_param([sys,'/',['Fuzzy Controller',13,'Subsytem/Mux']],...
		'inputs','2',...
		'position',[130,50,160,85])

add_block('built-in/Outport',[sys,'/',['Fuzzy Controller',13,'Subsytem/out_2']])
set_param([sys,'/',['Fuzzy Controller',13,'Subsytem/out_2']],...
		'Port','2',...
		'position',[335,105,355,125])
add_line([sys,'/',['Fuzzy Controller',13,'Subsytem']],[40,75;65,75])
add_line([sys,'/',['Fuzzy Controller',13,'Subsytem']],[55,75;55,60;120,60])
add_line([sys,'/',['Fuzzy Controller',13,'Subsytem']],[110,75;120,75])
add_line([sys,'/',['Fuzzy Controller',13,'Subsytem']],[165,70;200,70])
add_line([sys,'/',['Fuzzy Controller',13,'Subsytem']],[265,70;325,70])
add_line([sys,'/',['Fuzzy Controller',13,'Subsytem']],[165,70;165,115;325,115])


%     Finished composite block ['Fuzzy Controller',13,'Subsytem'].

set_param([sys,'/',['Fuzzy Controller',13,'Subsytem']],...
		'position',[105,6,135,59])

add_block('built-in/Saturation',[sys,'/','Saturation1'])
set_param([sys,'/','Saturation1'],...
		'Lower Limit','-1',...
		'Upper Limit','1',...
		'position',[165,180,195,200])

add_block('built-in/Scope',[sys,'/','Error and dError'])
set_param([sys,'/','Error and dError'],...
		'Vgain','1.000000',...
		'Hgain','10.000000',...
		'Vmax','2.000000',...
		'Hmax','20.000000',...
		'Window',[231,286,443,465])
open_system([sys,'/','Error and dError'])
set_param([sys,'/','Error and dError'],...
		'position',[365,52,385,78])

add_block('built-in/Scope',[sys,'/','Fuzzy Current'])
set_param([sys,'/','Fuzzy Current'],...
		'Vgain','1.000000',...
		'Hgain','10.000000',...
		'Vmax','2.000000',...
		'Hmax','20.000000',...
		'Window',[442,109,639,286])
open_system([sys,'/','Fuzzy Current'])
set_param([sys,'/','Fuzzy Current'],...
		'position',[365,7,385,33])

add_block('built-in/Scope',[sys,'/','Theta'])
set_param([sys,'/','Theta'],...
		'Vgain','1.000000',...
		'Hgain','10.000000',...
		'Vmax','2.000000',...
		'Hmax','20.000000',...
		'Window',[0,285,231,464])
open_system([sys,'/','Theta'])
set_param([sys,'/','Theta'],...
		'position',[365,107,385,133])

add_block('built-in/Scope',[sys,'/','PID Current'])
set_param([sys,'/','PID Current'],...
		'Vgain','1.000000',...
		'Hgain','10.000000',...
		'Vmax','2.000000',...
		'Hmax','20.000000',...
		'Window',[443,287,640,464])
open_system([sys,'/','PID Current'])
set_param([sys,'/','PID Current'],...
		'position',[365,177,385,203])

add_block('built-in/Constant',[sys,'/','Setpoint'])
set_param([sys,'/','Setpoint'],...
		'Value','0',...
		'position',[10,95,30,115])

add_block('built-in/Constant',[sys,'/',['Controller',13,'Switch']])
set_param([sys,'/',['Controller',13,'Switch']],...
		'position',[135,100,155,120])
add_line(sys,[305,110;325,110;325,150;45,150;45,115;50,115])
add_line(sys,[35,105;50,105])
add_line(sys,[160,110;215,110])
add_line(sys,[255,110;260,110])
add_line(sys,[325,120;355,120])
add_line(sys,[140,45;290,45;290,65;355,65])
add_line(sys,[140,20;355,20])
add_line(sys,[200,20;200,50])
add_line(sys,[200,85;200,100;215,100])
add_line(sys,[85,110;85,190;90,190])
add_line(sys,[85,110;85,35;95,35])
add_line(sys,[145,190;155,190])
add_line(sys,[200,190;355,190])
add_line(sys,[200,190;200,120;215,120])

% 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]=',sys,'(t,x,u,flag);'])
		else
			eval(['ret =', sys,'(t,x,u,flag);'])
		end
	else
		[ret,x0,str] = feval(sys);
	end
end
