 function [ret,x0,str,ts,xts]=inv_pend(t,x,u,flag);
%INV_PEND	is the M-file description of the SIMULINK system named INV_PEND.
%	The block-diagram can be displayed by typing: INV_PEND.
%
%	SYS=INV_PEND(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 INV_PEND 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 INV_PEND with a FLAG of zero:
%	[SIZES]=INV_PEND([],[],[],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
%		SIZES(5) number of roots (currently unsupported)
%		SIZES(6) direct feedthrough flag
%		SIZES(7) number of sample times
%
%	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',[166,167,878,586])
     open_system(sys)
end;
set_param(sys,'BackGround',0)
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 Workspace1'])
set_param([sys,'/','To Workspace1'],...
		'mat-name','t',...
		'buffer','[1000 1 0.1]',...
		'position',[605,347,655,363])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'position',[550,345,570,365])


%     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/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])

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/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])
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'],...
		'ForeGround',2,...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[405,340,507,391])


%     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/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])

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/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])
set_param([sys,'/','More Info'],...
		'Mask Display','?')


%     Finished composite block 'More Info'.

set_param([sys,'/','More Info'],...
		'ForeGround',2,...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[340,340,367,365])

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',[189,380,194,385])

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


%     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/Inport',[sys,'/','Discrete State Estimator/in_2'])
set_param([sys,'/','Discrete State Estimator/in_2'],...
		'Port','2',...
		'position',[15,85,35,105])

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_1'])
set_param([sys,'/','Discrete State Estimator/in_1'],...
		'position',[15,60,35,80])

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/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_line([sys,'/','Discrete State Estimator'],[40,95;50,95])
add_line([sys,'/','Discrete State Estimator'],[240,85;255,85])
add_line([sys,'/','Discrete State Estimator'],[40,70;50,70])
add_line([sys,'/','Discrete State Estimator'],[85,85;110,85])
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 calc
ulation 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'],...
		'ForeGround',1,...
		'BackGround',3,...
		'position',[540,227,570,278])

add_block('built-in/Saturation',[sys,'/','Saturation'])
set_param([sys,'/','Saturation'],...
		'hide name',0,...
		'Lower Limit','-10',...
		'Upper Limit','10',...
		'position',[390,195,415,215])

add_block('built-in/Sum',[sys,'/','Sum3'])
set_param([sys,'/','Sum3'],...
		'hide name',0,...
		'position',[95,195,115,215])

add_block('built-in/Constant',[sys,'/','Constant'])
set_param([sys,'/','Constant'],...
		'Value','-0.1089',...
		'position',[20,250,70,270])

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',[20,183,65,217])


%     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/Mux',[sys,'/',['Limited ',13,'Integrator/Mux']])
set_param([sys,'/',['Limited ',13,'Integrator/Mux']],...
		'inputs','2',...
		'position',[85,116,115,149])

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/Outport',[sys,'/',['Limited ',13,'Integrator/Out_1']])
set_param([sys,'/',['Limited ',13,'Integrator/Out_1']],...
		'position',[720,125,740,145])

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/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/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_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])
add_line([sys,'/',['Limited ',13,'Integrator']],[525,135;535,135])
add_line([sys,'/',['Limited ',13,'Integrator']],[120,135;145,135])
add_line([sys,'/',['Limited ',13,'Integrator']],[45,140;80,140])
add_line([sys,'/',['Limited ',13,'Integrator']],[680,135;715,135])
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']],...
		'ForeGround',6,...
		'position',[260,183,295,217])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'hide name',0,...
		'inputs','-+',...
		'position',[150,190,170,210])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'inputs','+++-',...
		'position',[350,182,370,228])

add_block('built-in/Gain',[sys,'/',['Integral',13,'gain']])
set_param([sys,'/',['Integral',13,'gain']],...
		'ForeGround',6,...
		'Gain','0.06',...
		'position',[195,182,240,218])

add_block('built-in/Gain',[sys,'/','Proportional gain'])
set_param([sys,'/','Proportional gain'],...
		'ForeGround',6,...
		'Gain','-9.4',...
		'position',[210,127,255,163])

add_block('built-in/Transfer Fcn',[sys,'/','Feedforward gain'])
set_param([sys,'/','Feedforward gain'],...
		'ForeGround',3,...
		'Numerator','[7.12 0]',...
		'Denominator','[1 2]',...
		'position',[200,252,255,288])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'inputs','3',...
		'position',[535,99,565,131])


%     Subsystem  'Animation'.

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

add_block('built-in/Inport',[sys,'/','Animation/in_1'])
set_param([sys,'/','Animation/in_1'],...
		'position',[75,55,95,75])

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_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'],...
		'BackGround',6,...
		'position',[620,90,675,140])

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',[186,335,191,340])


%     Subsystem  'Pendulum'.

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

add_block('built-in/Outport',[sys,'/','Pendulum/out_2'])
set_param([sys,'/','Pendulum/out_2'],...
		'Port','2',...
		'position',[470,105,490,125])

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

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

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

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/Integrator1'])
set_param([sys,'/','Pendulum/Integrator1'],...
		'position',[385,90,405,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/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/Mux1'])
set_param([sys,'/','Pendulum/Mux1'],...
		'hide name',0,...
		'inputs','3',...
		'position',[140,84,170,116])

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

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_line([sys,'/','Pendulum'],[410,100;465,100])
add_line([sys,'/','Pendulum'],[255,185;300,185])
add_line([sys,'/','Pendulum'],[175,185;205,185])
add_line([sys,'/','Pendulum'],[175,100;205,100])
add_line([sys,'/','Pendulum'],[255,100;300,100])
add_line([sys,'/','Pendulum'],[330,100;380,100])
add_line([sys,'/','Pendulum'],[330,185;380,185])
add_line([sys,'/','Pendulum'],[347,185;347,150;85,150;85,100;135,100])
add_line([sys,'/','Pendulum'],[40,90;45,175;135,175])
add_line([sys,'/','Pendulum'],[40,90;135,90])
add_line([sys,'/','Pendulum'],[410,185;450,185;450,115;465,115])
add_line([sys,'/','Pendulum'],[420,185;420,130;100,130;100,110;135,110])
add_line([sys,'/','Pendulum'],[355,185;355,215;105,215;105,185;135,185])
add_line([sys,'/','Pendulum'],[430,185;430,235;75,235;75,195;135,195])
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'],...
		'ForeGround',1,...
		'BackGround',4,...
		'Drop Shadow',4,...
		'position',[435,177,465,228])

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','y',...
		'buffer','[1000 1 0.1]',...
		'position',[625,166,660,184])

add_block('built-in/Note',[sys,'/','Inverted Pendulum on Cart'])
set_param([sys,'/','Inverted Pendulum on Cart'],...
		'Font Name','helvetica',...
		'Font Weight','bold',...
		'Font Angle','oblique',...
		'Font Size',18,...
		'position',[360,20,365,25])
add_line(sys,[420,205;430,205])
add_line(sys,[470,215;515,215;515,265;535,265])
add_line(sys,[470,190;525,190;535,240])
add_line(sys,[575,255;590,255])
add_line(sys,[470,190;470,115;135,115;145,195])
add_line(sys,[120,205;145,205])
add_line(sys,[300,200;345,200])
add_line(sys,[665,255;675,255;675,305;330,305;330,220;345,220])
add_line(sys,[70,200;90,200])
add_line(sys,[75,260;75,210;90,210])
add_line(sys,[120,205;120,270;195,270])
add_line(sys,[260,270;315,270;315,210;345,210])
add_line(sys,[260,145;315,145;315,190;345,190])
add_line(sys,[375,205;385,205])
add_line(sys,[175,200;175,145;205,145])
add_line(sys,[245,200;255,200])
add_line(sys,[175,200;190,200])
add_line(sys,[120,205;120,105;530,105])
add_line(sys,[470,190;485,190;485,115;530,115])
add_line(sys,[470,215;505,215;505,125;530,125])
add_line(sys,[570,115;615,115])
add_line(sys,[570,115;580,115;580,175;620,175])
add_line(sys,[575,355;600,355])

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
else
	drawnow % Flash up the model and execute load callback
end
