function [ret,x0,str,ts,xts]=bounce(t,x,u,flag);
%BOUNCE	is the M-file description of the SIMULINK system named BOUNCE.
%	The block-diagram can be displayed by typing: BOUNCE.
%
%	SYS=BOUNCE(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 BOUNCE 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 BOUNCE with a FLAG of zero:
%	[SIZES]=BOUNCE([],[],[],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',[60,184,507,554])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-23')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '20')
set_param(sys,'Min step size', '0.0001')
set_param(sys,'Max step size', '.01')
set_param(sys,'Relative error','1e-3')
set_param(sys,'Return vars',   '')

add_block('built-in/Note',[sys,'/','Reset'])
set_param([sys,'/','Reset'],...
		'position',[45,130,50,135])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','2',...
		'position',[165,218,195,247])

add_block('built-in/Fcn',[sys,'/',['Ball hits ground.',13,'(Ball is dropping and',13,'below or on ground).']])
set_param([sys,'/',['Ball hits ground.',13,'(Ball is dropping and',13,'below or on ground).']],...
		'orientation',2,...
		'Expr','(u[2] <= 0) && (u[1] < 0)',...
		'position',[90,225,130,245])

add_block('built-in/Integrator',[sys,'/','Position'])
set_param([sys,'/','Position'],...
		'Initial','10',...
		'position',[245,66,280,114])

add_block('built-in/Reset integrator',[sys,'/','Velocity'])
set_param([sys,'/','Velocity'],...
		'Initial','ic',...
		'Mask Display','1\n-\ns',...
		'Mask Type','Reset Integrator',...
		'Mask Dialogue','Reset Integrator|Initial value:',...
		'Mask Translate','ic=@1;',...
		'Mask Entries','15\/')
set_param([sys,'/','Velocity'],...
		'position',[130,67,165,113])

add_block('built-in/Scope',[sys,'/','Velocity '])
set_param([sys,'/','Velocity '],...
		'Vgain','22.000000',...
		'Hgain','20.000000',...
		'Vmax','44.000000',...
		'Hmax','40.000000',...
		'Window',[544,535,824,755],...
		'position',[250,15,270,45])

add_block('built-in/Scope',[sys,'/','Position '])
set_param([sys,'/','Position '],...
		'Vgain','23.000000',...
		'Hgain','21.000000',...
		'Vmax','46.000000',...
		'Hmax','42.000000',...
		'Window',[543,183,823,403])
open_system([sys,'/','Position '])
set_param([sys,'/','Position '],...
		'position',[380,75,400,105])

add_block('built-in/Note',[sys,'/','Reset velocity'])
set_param([sys,'/','Reset velocity'],...
		'position',[135,130,140,135])

add_block('built-in/Constant',[sys,'/','Gravity'])
set_param([sys,'/','Gravity'],...
		'Value','-9.81',...
		'position',[20,60,55,90])

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',[154,330,159,335])

add_block('built-in/Note',[sys,'/',['Bouncing Ball Model',13,'(Double click on the "?" for more info)']])
set_param([sys,'/',['Bouncing Ball Model',13,'(Double click on the "?" for more info)']],...
		'position',[130,295,135,300])


%     Subsystem  'More Info'.

new_system([sys,'/','More Info'])
set_param([sys,'/','More Info'],'Location',[670,234,1054,419])

add_block('built-in/Note',[sys,'/',['More Info/A rubber ball is thrown into the air with the velocity 15',13,'meters per second from a height of 10 m.  The velocity',13,'and position of the ball are displayed on scopes.']])
set_param([sys,'/',['More Info/A rubber ball is thrown into the air with the velocity 15',13,'meters per second from a height of 10 m.  The velocity',13,'and position of the ball are displayed on scopes.']],...
		'position',[186,23,193,35])

add_block('built-in/Note',[sys,'/',['More Info/This system uses a reset-integrator to change the',13,'direction of the ball as it comes into contact with',13,'the ground']])
set_param([sys,'/',['More Info/This system uses a reset-integrator to change the',13,'direction of the ball as it comes into contact with',13,'the ground']],...
		'position',[191,83,198,95])
set_param([sys,'/','More Info'],...
		'Mask Display','?')


%     Finished composite block 'More Info'.

set_param([sys,'/','More Info'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[265,295,292,320])


%     Subsystem  'More Info1'.

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

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


%     Finished composite block 'More Info1'.

set_param([sys,'/','More Info1'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[326,303,428,354])


%     Subsystem  'Hit Crossing'.

new_system([sys,'/','Hit Crossing'])
set_param([sys,'/','Hit Crossing'],'Location',[157,542,517,731])

add_block('built-in/Constant',[sys,'/','Hit Crossing/Constant1'])
set_param([sys,'/','Hit Crossing/Constant1'],...
		'Value','cross',...
		'position',[10,70,60,90])

add_block('built-in/Inport',[sys,'/','Hit Crossing/in_1'])
set_param([sys,'/','Hit Crossing/in_1'],...
		'position',[25,25,45,45])

add_block('built-in/Relational Operator',[sys,'/',['Hit Crossing/Relational',13,'operator']])
set_param([sys,'/',['Hit Crossing/Relational',13,'operator']],...
		'Operator','<',...
		'position',[110,27,140,58])

add_block('built-in/Reset integrator',[sys,'/',['Hit Crossing/Resetable',13,'Integrator']])
set_param([sys,'/',['Hit Crossing/Resetable',13,'Integrator']],...
		'Initial','ic',...
		'Mask Display','1\n-\ns',...
		'Mask Type','Reset Integrator',...
		'Mask Dialogue','Reset Integrator|Initial value:',...
		'Mask Translate','ic=@1;')
set_param([sys,'/',['Hit Crossing/Resetable',13,'Integrator']],...
		'Mask Entries','0\/',...
		'position',[305,27,335,143])

add_block('built-in/Constant',[sys,'/','Hit Crossing/Constant'])
set_param([sys,'/','Hit Crossing/Constant'],...
		'Value','0',...
		'position',[240,115,260,135])

add_block('built-in/Gain',[sys,'/','Hit Crossing/Gain'])
set_param([sys,'/','Hit Crossing/Gain'],...
		'Gain','1/tol',...
		'position',[240,35,260,55])

add_block('built-in/Sum',[sys,'/','Hit Crossing/Sum'])
set_param([sys,'/','Hit Crossing/Sum'],...
		'inputs','-+',...
		'position',[230,75,250,95])

add_block('built-in/Constant',[sys,'/','Hit Crossing/Constant2'])
set_param([sys,'/','Hit Crossing/Constant2'],...
		'position',[175,100,195,120])


%     Subsystem  'Hit Crossing/Hit crossing1'.

new_system([sys,'/','Hit Crossing/Hit crossing1'])
set_param([sys,'/','Hit Crossing/Hit crossing1'],'Location',[143,475,427,741])

add_block('built-in/Fcn',[sys,'/','Hit Crossing/Hit crossing1/Fcn'])
set_param([sys,'/','Hit Crossing/Hit crossing1/Fcn'],...
		'Expr','u<cross',...
		'position',[55,65,95,85])

add_block('built-in/Inport',[sys,'/','Hit Crossing/Hit crossing1/in_1'])
set_param([sys,'/','Hit Crossing/Hit crossing1/in_1'],...
		'position',[10,65,30,85])

add_block('built-in/Gain',[sys,'/','Hit Crossing/Hit crossing1/Gain'])
set_param([sys,'/','Hit Crossing/Hit crossing1/Gain'],...
		'Gain','1/tol',...
		'position',[125,65,145,85])

add_block('built-in/Constant',[sys,'/','Hit Crossing/Hit crossing1/Constant'])
set_param([sys,'/','Hit Crossing/Hit crossing1/Constant'],...
		'Value','0',...
		'position',[125,145,145,165])

add_block('built-in/Reset integrator',[sys,'/',['Hit Crossing/Hit crossing1/Resetable',13,'Integrator']])
set_param([sys,'/',['Hit Crossing/Hit crossing1/Resetable',13,'Integrator']],...
		'Initial','ic',...
		'Mask Display','1\n-\ns',...
		'Mask Type','Reset Integrator',...
		'Mask Dialogue','Reset Integrator|Initial value:')
set_param([sys,'/',['Hit Crossing/Hit crossing1/Resetable',13,'Integrator']],...
		'Mask Translate','ic=@1;',...
		'Mask Entries','0\/',...
		'position',[190,57,220,173])

add_block('built-in/Fcn',[sys,'/','Hit Crossing/Hit crossing1/Fcn1'])
set_param([sys,'/','Hit Crossing/Hit crossing1/Fcn1'],...
		'Expr','1-u',...
		'position',[115,105,155,125])
add_line([sys,'/','Hit Crossing/Hit crossing1'],[160,115;185,115])
add_line([sys,'/','Hit Crossing/Hit crossing1'],[150,155;185,155])
add_line([sys,'/','Hit Crossing/Hit crossing1'],[100,75;110,115])
add_line([sys,'/','Hit Crossing/Hit crossing1'],[35,75;50,75])
add_line([sys,'/','Hit Crossing/Hit crossing1'],[150,75;185,75])
add_line([sys,'/','Hit Crossing/Hit crossing1'],[100,75;120,75])


%     Finished composite block 'Hit Crossing/Hit crossing1'.

set_param([sys,'/','Hit Crossing/Hit crossing1'],...
		'position',[715,100,755,130])
add_line([sys,'/','Hit Crossing'],[200,110;200,90;225,90])
add_line([sys,'/','Hit Crossing'],[145,45;235,45])
add_line([sys,'/','Hit Crossing'],[180,45;180,80;225,80])
add_line([sys,'/','Hit Crossing'],[65,80;70,80;70,50;105,50])
add_line([sys,'/','Hit Crossing'],[265,45;300,45])
add_line([sys,'/','Hit Crossing'],[50,35;105,35])
add_line([sys,'/','Hit Crossing'],[265,125;300,125])
add_line([sys,'/','Hit Crossing'],[255,85;300,85])
set_param([sys,'/','Hit Crossing'],...
		'Mask Display','plot(-0.2,-0.2,3.45,1.2,dvec,abs(cos(dvec)),[.6 2.5],[0 0])',...
		'Mask Type','Crossing')
set_param([sys,'/','Hit Crossing'],...
		'Mask Dialogue','Causes simulation to take smaller steps at the crossing value (for instance, near a discontinuity). |Crossing Value:|Tol: (typically twice the simulation tol)')
set_param([sys,'/','Hit Crossing'],...
		'Mask Translate','cross=@1; tol=@2;dvec=-.3:.03:(pi+.3);')
set_param([sys,'/','Hit Crossing'],...
		'Mask Help','This block is used to cause the simulator to take smaller steps near a crossing point.  It uses a resetable integrator to accomplish this.  Systems with disontinuities can be more accurately modeled with this block.')
set_param([sys,'/','Hit Crossing'],...
		'Mask Entries','0\/1e-6\/')


%     Finished composite block 'Hit Crossing'.

set_param([sys,'/','Hit Crossing'],...
		'orientation',2,...
		'position',[260,161,295,199])

add_block('built-in/Gain',[sys,'/','Elasticity'])
set_param([sys,'/','Elasticity'],...
		'orientation',2,...
		'Gain','-0.8',...
		'position',[120,157,165,193])
add_line(sys,[60,75;125,75])
add_line(sys,[170,90;240,90])
add_line(sys,[115,175;90,175;90,105;125,105])
add_line(sys,[285,90;340,90;340,240;200,240])
add_line(sys,[210,90;210,30;245,30])
add_line(sys,[340,90;375,90])
add_line(sys,[160,235;135,235])
add_line(sys,[85,235;70,235;70,90;125,90])
add_line(sys,[340,180;300,180])
add_line(sys,[210,90;200,225])
add_line(sys,[210,175;170,175])

% 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
