function [ret,x0,str,ts,xts]=penddemo(t,x,u,flag);
%PENDDEMO	is the M-file description of the SIMULINK system named PENDDEMO.
%	PENDDEMO has a total of 2 states, 6 discrete states, 0 outputs and 0 inputs.
%	The block-diagram can be displayed by typing: PENDDEMO.
%
%	SYS=PENDDEMO(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 PENDDEMO 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 PENDDEMO with a FLAG of zero:
%	[SIZES]=PENDDEMO([],[],[],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',[448,114,1132,470])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0')
set_param(sys,'Stop time',     '10000')
set_param(sys,'Min step size', '0.0001')
set_param(sys,'Max step size', '5')
set_param(sys,'Relative error','1e-3')
set_param(sys,'Return vars',   '')

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','y',...
		'buffer','[1000 1 0.1]',...
		'position',[615,96,650,114])


%     Subsystem  'Pendulum'.

new_system([sys,'/','Pendulum'])
set_param([sys,'/','Pendulum'],'Location',[97,238,630,546])

add_block('built-in/Fcn',[sys,'/','Pendulum/Fcn2'])
set_param([sys,'/','Pendulum/Fcn2'],...
		'hide name',0,...
		'Expr','(-u[1]*cos(u[3])/m + (M+m)*g*sin(u[3])/m - l*power(u[2],2)*sin(u[3])*cos(u[3]))/(l*(M/m + power(sin(u[3]),2)))',...
		'position',[210,175,250,195])

add_block('built-in/Integrator',[sys,'/','Pendulum/Integrator4'])
set_param([sys,'/','Pendulum/Integrator4'],...
		'position',[305,90,325,110])

add_block('built-in/Mux',[sys,'/','Pendulum/Mux1'])
set_param([sys,'/','Pendulum/Mux1'],...
		'hide name',0,...
		'inputs','3',...
		'position',[140,84,170,116])

add_block('built-in/Fcn',[sys,'/','Pendulum/Fcn1'])
set_param([sys,'/','Pendulum/Fcn1'],...
		'hide name',0,...
		'Expr','(u[1]/m - g*sin(u[3])*cos(u[3]) + l*power(u[2],2)*sin(u[3]))/(M/m + power(sin(u[3]),2))',...
		'position',[210,90,250,110])

add_block('built-in/Mux',[sys,'/','Pendulum/Mux'])
set_param([sys,'/','Pendulum/Mux'],...
		'hide name',0,...
		'inputs','3',...
		'position',[140,169,170,201])

add_block('built-in/Integrator',[sys,'/','Pendulum/Integrator1'])
set_param([sys,'/','Pendulum/Integrator1'],...
		'position',[385,90,405,110])

add_block('built-in/Integrator',[sys,'/','Pendulum/Integrator3'])
set_param([sys,'/','Pendulum/Integrator3'],...
		'position',[305,175,325,195])

add_block('built-in/Integrator',[sys,'/','Pendulum/Integrator2'])
set_param([sys,'/','Pendulum/Integrator2'],...
		'position',[385,175,405,195])

add_block('built-in/Inport',[sys,'/','Pendulum/in_1'])
set_param([sys,'/','Pendulum/in_1'],...
		'position',[15,80,35,100])

add_block('built-in/Outport',[sys,'/','Pendulum/out_1'])
set_param([sys,'/','Pendulum/out_1'],...
		'position',[470,90,490,110])

add_block('built-in/Outport',[sys,'/','Pendulum/out_2'])
set_param([sys,'/','Pendulum/out_2'],...
		'Port','2',...
		'position',[470,105,490,125])
add_line([sys,'/','Pendulum'],[410,185;450,185;450,115;465,115])
add_line([sys,'/','Pendulum'],[430,185;430,235;75,235;75,195;135,195])
add_line([sys,'/','Pendulum'],[330,185;380,185])
add_line([sys,'/','Pendulum'],[355,185;355,215;105,215;105,185;135,185])
add_line([sys,'/','Pendulum'],[420,185;420,130;100,130;100,110;135,110])
add_line([sys,'/','Pendulum'],[40,90;135,90])
add_line([sys,'/','Pendulum'],[40,90;45,175;135,175])
add_line([sys,'/','Pendulum'],[347,185;347,150;85,150;85,100;135,100])
add_line([sys,'/','Pendulum'],[330,100;380,100])
add_line([sys,'/','Pendulum'],[255,100;300,100])
add_line([sys,'/','Pendulum'],[175,100;205,100])
add_line([sys,'/','Pendulum'],[175,185;205,185])
add_line([sys,'/','Pendulum'],[255,185;300,185])
add_line([sys,'/','Pendulum'],[410,100;465,100])
set_param([sys,'/','Pendulum'],...
		'Mask Display','',...
		'Mask Type','Inverted pendulum on cart')
set_param([sys,'/','Pendulum'],...
		'Mask Dialogue','Unmask and double-click to see dynamics|Mass of cart (kg):|Mass of pendulum (kg):|Distance to pendulum center of mass(m):|Acceleration of gravity (m/s^2):')
set_param([sys,'/','Pendulum'],...
		'Mask Translate','M=@1; m=@2; l=@3; g=@4;',...
		'Mask Help','Nonlinear dynamics of inverted pendulum on cart.',...
		'Mask Entries','0.455\/0.21\/0.61/2\/9.8\/')


%     Finished composite block 'Pendulum'.

set_param([sys,'/','Pendulum'],...
		'position',[425,107,455,158])

add_block('built-in/Note',[sys,'/',['Inverted pendulum on cart with Animation',13,'(Double click on the "?" for more info)']])
set_param([sys,'/',['Inverted pendulum on cart with Animation',13,'(Double click on the "?" for more info)']],...
		'position',[176,265,181,270])


%     Subsystem  'Animation'.

new_system([sys,'/','Animation'])
set_param([sys,'/','Animation'],'Location',[445,59,728,223])

add_block('built-in/S-Function',[sys,'/','Animation/S-function'])
set_param([sys,'/','Animation/S-function'],...
		'function name','pendan',...
		'parameters','RefBlock',...
		'position',[120,53,175,77])

add_block('built-in/Inport',[sys,'/','Animation/in_1'])
set_param([sys,'/','Animation/in_1'],...
		'position',[75,55,95,75])
add_line([sys,'/','Animation'],[100,65;115,65])
set_param([sys,'/','Animation'],...
		'Mask Display','plot([-10 10],[0 0],[-2 -2 2 2],[0 1 1 0],[0 3],[1 5])',...
		'Mask Type','Pendulum plot',...
		'Mask Dialogue','Pendulum plot|DC reference signal block name:')
set_param([sys,'/','Animation'],...
		'Mask Translate','RefBlock = @1;',...
		'Mask Help','Pendulum animation function using Handle graphics.',...
		'Mask Entries','''Constant''\/')


%     Finished composite block 'Animation'.

set_param([sys,'/','Animation'],...
		'position',[610,20,665,70])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'inputs','3',...
		'position',[525,29,555,61])

add_block('built-in/Transfer Fcn',[sys,'/','Feedforward gain'])
set_param([sys,'/','Feedforward gain'],...
		'Numerator','[7.12 0]',...
		'Denominator','[1 2]',...
		'position',[190,182,245,218])

add_block('built-in/Gain',[sys,'/','Proportional gain'])
set_param([sys,'/','Proportional gain'],...
		'Gain','-9.4',...
		'position',[200,57,245,93])

add_block('built-in/Gain',[sys,'/',['Integral',13,'gain']])
set_param([sys,'/',['Integral',13,'gain']],...
		'Gain','0.06',...
		'position',[185,112,230,148])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'inputs','+++-',...
		'position',[340,112,360,158])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'hide name',0,...
		'inputs','-+',...
		'position',[140,120,160,140])


%     Subsystem  ['Limited ',13,'Integrator'].

new_system([sys,'/',['Limited ',13,'Integrator']])
set_param([sys,'/',['Limited ',13,'Integrator']],'Location',[0,0,791,233])

add_block('built-in/Saturation',[sys,'/',['Limited ',13,'Integrator/Saturation']])
set_param([sys,'/',['Limited ',13,'Integrator/Saturation']],...
		'Lower Limit','lb',...
		'Upper Limit','ub',...
		'position',[630,121,675,149])

add_block('built-in/Fcn',[sys,'/',['Limited ',13,'Integrator/Fcn']])
set_param([sys,'/',['Limited ',13,'Integrator/Fcn']],...
		'Expr','u[2]*((((u[1]>lb)+(u[2]>=0))>0) * (((u[1]<ub)+(u[2]<=0))>0))',...
		'position',[150,116,520,154])

add_block('built-in/Inport',[sys,'/',['Limited ',13,'Integrator/In_1']])
set_param([sys,'/',['Limited ',13,'Integrator/In_1']],...
		'position',[20,130,40,150])

add_block('built-in/Outport',[sys,'/',['Limited ',13,'Integrator/Out_1']])
set_param([sys,'/',['Limited ',13,'Integrator/Out_1']],...
		'position',[720,125,740,145])

add_block('built-in/Integrator',[sys,'/',['Limited ',13,'Integrator/Integrator']])
set_param([sys,'/',['Limited ',13,'Integrator/Integrator']],...
		'Initial','xi',...
		'position',[540,126,585,144])

add_block('built-in/Mux',[sys,'/',['Limited ',13,'Integrator/Mux']])
set_param([sys,'/',['Limited ',13,'Integrator/Mux']],...
		'inputs','2',...
		'position',[85,116,115,149])
add_line([sys,'/',['Limited ',13,'Integrator']],[680,135;715,135])
add_line([sys,'/',['Limited ',13,'Integrator']],[45,140;80,140])
add_line([sys,'/',['Limited ',13,'Integrator']],[120,135;145,135])
add_line([sys,'/',['Limited ',13,'Integrator']],[525,135;535,135])
add_line([sys,'/',['Limited ',13,'Integrator']],[590,135;625,135])
add_line([sys,'/',['Limited ',13,'Integrator']],[600,135;600,35;55,35;55,125;80,125])
set_param([sys,'/',['Limited ',13,'Integrator']],...
		'Mask Display','plot(-1,-0.2,3.5,1.2,[0.05,1,2,2.9],[0,0,1,1]); 1/s    ',...
		'Mask Type','Limited integrator.')
set_param([sys,'/',['Limited ',13,'Integrator']],...
		'Mask Dialogue','Limited integrator|Lower bound:|Upper bound:|Initial condition',...
		'Mask Translate','lb = @1; ub = @2; xi = @3;')
set_param([sys,'/',['Limited ',13,'Integrator']],...
		'Mask Help','Implements a limited integrator with \nan expression of the form:\n\nif(x<=lb and u<0) or (x>=ub and u>0)\n\txdot=0\nelse\n\txdot=u.',...
		'Mask Entries','-5\/5\/0\/')


%     Finished composite block ['Limited ',13,'Integrator'].

set_param([sys,'/',['Limited ',13,'Integrator']],...
		'position',[250,113,285,147])

add_block('built-in/Signal Generator',[sys,'/','Signal Gen.'])
set_param([sys,'/','Signal Gen.'],...
		'hide name',0,...
		'Peak','0.000000',...
		'Peak Range','Inf',...
		'Freq','0.400000',...
		'Freq Range','2.000000',...
		'Wave','Sqr',...
		'Units','Rads',...
		'position',[10,113,55,147])

add_block('built-in/Constant',[sys,'/','Constant'])
set_param([sys,'/','Constant'],...
		'Value','-5.099',...
		'position',[10,180,60,200])

add_block('built-in/Sum',[sys,'/','Sum3'])
set_param([sys,'/','Sum3'],...
		'hide name',0,...
		'position',[85,125,105,145])

add_block('built-in/Saturation',[sys,'/','Saturation'])
set_param([sys,'/','Saturation'],...
		'hide name',0,...
		'Lower Limit','-10',...
		'Upper Limit','10',...
		'position',[380,125,405,145])


%     Subsystem  'Discrete State Estimator'.

new_system([sys,'/','Discrete State Estimator'])
set_param([sys,'/','Discrete State Estimator'],'Location',[380,440,840,612])

add_block('built-in/Discrete State-Space',[sys,'/','Discrete State Estimator/Dis. State-space'])
set_param([sys,'/','Discrete State Estimator/Dis. State-space'],...
		'A','zeros(2)',...
		'B','-eye(2)/Ts',...
		'C','[0 0; 1 0; 0 0; 0 1]',...
		'D','[1 0; 1/Ts 0; 0 1; 0 1/Ts]',...
		'Sample time','Ts',...
		'position',[115,65,235,105])

add_block('built-in/Mux',[sys,'/','Discrete State Estimator/Mux2'])
set_param([sys,'/','Discrete State Estimator/Mux2'],...
		'hide name',0,...
		'inputs','2',...
		'position',[55,55,80,110])

add_block('built-in/Inport',[sys,'/','Discrete State Estimator/in_1'])
set_param([sys,'/','Discrete State Estimator/in_1'],...
		'position',[15,60,35,80])

add_block('built-in/Outport',[sys,'/','Discrete State Estimator/out_1'])
set_param([sys,'/','Discrete State Estimator/out_1'],...
		'position',[260,75,280,95])

add_block('built-in/Inport',[sys,'/','Discrete State Estimator/in_2'])
set_param([sys,'/','Discrete State Estimator/in_2'],...
		'Port','2',...
		'position',[15,85,35,105])
add_line([sys,'/','Discrete State Estimator'],[85,85;110,85])
add_line([sys,'/','Discrete State Estimator'],[40,70;50,70])
add_line([sys,'/','Discrete State Estimator'],[240,85;255,85])
add_line([sys,'/','Discrete State Estimator'],[40,95;50,95])
set_param([sys,'/','Discrete State Estimator'],...
		'Mask Display','',...
		'Mask Type','Pendulum Discrete state estimator',...
		'Mask Dialogue','Pendulum discrete state estimator|Sample time',...
		'Mask Translate','Ts = @1;')
set_param([sys,'/','Discrete State Estimator'],...
		'Mask Help','The states of the pendulum and cart system are the pendulum angle and angular velocity and cart position and velocity.  Since the sensors output the pendulum angle and cart position, this state estimator just does a one step difference calculation to get the velocities.')
set_param([sys,'/','Discrete State Estimator'],...
		'Mask Entries','0.02\/')


%     Finished composite block 'Discrete State Estimator'.

set_param([sys,'/','Discrete State Estimator'],...
		'position',[530,157,560,208])

add_block('built-in/State-Space',[sys,'/','Matrix gain.'])
set_param([sys,'/','Matrix gain.'],...
		'hide name',0,...
		'A','[]',...
		'B','[]',...
		'C','[]',...
		'D','K',...
		'Mask Display','LQR',...
		'Mask Type','Matrix gain',...
		'Mask Dialogue','Matrix Gain:|Gain matrix:')
set_param([sys,'/','Matrix gain.'],...
		'Mask Translate','K = @1;',...
		'Mask Help','Multiplies input vector by entered matrix to produce output vector.',...
		'Mask Entries','[0 -18 -166.5 -15.2]\/',...
		'position',[585,169,650,201])

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',[179,310,184,315])


%     Subsystem  'More Info'.

new_system([sys,'/','More Info'])
set_param([sys,'/','More Info'],'Location',[127,169,475,352])

add_block('built-in/Note',[sys,'/',['More Info/This system simulates a cart carrying an inverted',13,'pendulum and displays an animation.']])
set_param([sys,'/',['More Info/This system simulates a cart carrying an inverted',13,'pendulum and displays an animation.']],...
		'position',[160,15,165,20])

add_block('built-in/Note',[sys,'/',['More Info/The animation is created using MATLAB''s ',13,'Handle Graphics. The animation block is ',13,'just a masked S-function.']])
set_param([sys,'/',['More Info/The animation is created using MATLAB''s ',13,'Handle Graphics. The animation block is ',13,'just a masked S-function.']],...
		'position',[160,60,165,65])

add_block('built-in/Note',[sys,'/',['More Info/For more information on what the S-function ',13,'does, look at the functions pendan.m, pend0.m, ',13,'and pendsets.m.']])
set_param([sys,'/',['More Info/For more information on what the S-function ',13,'does, look at the functions pendan.m, pend0.m, ',13,'and pendsets.m.']],...
		'position',[160,115,165,120])
set_param([sys,'/','More Info'],...
		'Mask Display','?')


%     Finished composite block 'More Info'.

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


%     Subsystem  'More Info2'.

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

add_block('built-in/Note',[sys,'/',['More Info2/This system models a double pendulum system where',13,'the rest state has both pendulums hanging vertically.']])
set_param([sys,'/',['More Info2/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 Info2/Associated with the demo is an animation function that',13,'will automatically open a figure window and display to it.']])
set_param([sys,'/',['More Info2/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 Info2/Try experimenting with different initial conditions for',13,'alpha and gamma to see the unusal coupling effects.']])
set_param([sys,'/',['More Info2/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 Info2'],...
		'Mask Display','Double click\nhere for\nSIMULINK Help',...
		'Mask Dialogue','eval(''[cs,cb]=get_param;str=[''''simcad '''' cs];eval(str);'')')


%     Finished composite block 'More Info2'.

set_param([sys,'/','More Info2'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[395,270,497,321])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'position',[540,275,560,295])

add_block('built-in/To Workspace',[sys,'/','To Workspace1'])
set_param([sys,'/','To Workspace1'],...
		'mat-name','t',...
		'buffer','[1000 1 0.1]',...
		'position',[595,277,645,293])
add_line(sys,[410,135;420,135])
add_line(sys,[460,145;505,145;505,195;525,195])
add_line(sys,[460,120;515,120;525,170])
add_line(sys,[565,185;580,185])
add_line(sys,[460,120;460,45;125,45;135,125])
add_line(sys,[110,135;135,135])
add_line(sys,[290,130;335,130])
add_line(sys,[655,185;665,185;665,235;320,235;320,150;335,150])
add_line(sys,[60,130;80,130])
add_line(sys,[65,190;65,140;80,140])
add_line(sys,[110,135;110,200;185,200])
add_line(sys,[250,200;305,200;305,140;335,140])
add_line(sys,[250,75;305,75;305,120;335,120])
add_line(sys,[365,135;375,135])
add_line(sys,[165,130;165,75;195,75])
add_line(sys,[235,130;245,130])
add_line(sys,[165,130;180,130])
add_line(sys,[110,135;110,35;520,35])
add_line(sys,[475,120;475,45;520,45])
add_line(sys,[495,145;495,55;520,55])
add_line(sys,[560,45;605,45])
add_line(sys,[570,45;570,105;610,105])
add_line(sys,[565,285;590,285])

% 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
