function [ret,x0,str]=h2demo1(t,x,u,flag);
%H2DEMO1	is the M-file description of the SIMULINK system named H2DEMO1.
%	The block-diagram can be displayed by typing: H2DEMO1.
%
%	SYS=H2DEMO1(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 H2DEMO1 to return state derivitives, FLAG=2
%	discrete states, FLAG=3 system outputs and FLAG=4 next sample
%	time. For more information and other options see SFUNC.
%
%	Calling H2DEMO1 with a FLAG of zero:
%	[SIZES]=H2DEMO1([],[],[],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.

%	Copyright (c) 1990-94 by The MathWorks, Inc.

% 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',[442,222,1015,615])
     open_system(sys)
end;
set_param(sys,'algorithm',		'Linear')
set_param(sys,'Start time',	'0.0')
set_param(sys,'Stop time',		'2000')
set_param(sys,'Min step size',	'0.01')
set_param(sys,'Max step size',	'0.1')
set_param(sys,'Relative error','1e-3')
set_param(sys,'Return vars',	'')

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'inputs','++-',...
		'position',[105,152,125,198])


%     Subsystem  'More Info'.

new_system([sys,'/','More Info'])
set_param([sys,'/','More Info'],'Location',[15,105,404,220])

add_block('built-in/Note',[sys,'/',['More Info/The original open-loop plant is an unstable system. Compare',13,'regular LQG desgin to see the difference.']])
set_param([sys,'/',['More Info/The original open-loop plant is an unstable system. Compare',13,'regular LQG desgin to see the difference.']],...
		'position',[191,75,192,76])

add_block('built-in/Note',[sys,'/',['More Info/This demonstration shows LQG//LTR controller design method.',13,'Robust control tool box is used in the design']])
set_param([sys,'/',['More Info/This demonstration shows LQG//LTR controller design method.',13,'Robust control tool box is used in the design']],...
		'position',[192,25,193,26])
set_param([sys,'/','More Info'],...
		'Mask Display','?')


%     Finished composite block 'More Info'.

set_param([sys,'/','More Info'],...
		'BackGround',5,...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[810,440,837,465])

add_block('built-in/Outport',[sys,'/','Outport1'])
set_param([sys,'/','Outport1'],...
		'position',[520,50,540,70])

add_block('built-in/Outport',[sys,'/','Outport2'])
set_param([sys,'/','Outport2'],...
		'Port','2',...
		'position',[525,90,545,110])

add_block('built-in/Outport',[sys,'/','Outport3'])
set_param([sys,'/','Outport3'],...
		'Port','3',...
		'position',[525,240,545,260])

add_block('built-in/Note',[sys,'/','H_2 Controller Design'])
set_param([sys,'/','H_2 Controller Design'],...
		'position',[289,10,290,11])

add_block('built-in/Inport',[sys,'/','Inport'])
set_param([sys,'/','Inport'],...
		'position',[35,105,55,125])

add_block('built-in/Signal Generator',[sys,'/','Signal Gen.'])
set_param([sys,'/','Signal Gen.'],...
		'Peak','1.000000',...
		'Peak Range','5.000000',...
		'Freq','1.000000',...
		'Freq Range','5.000000',...
		'Wave','Sqr',...
		'Units','Rads',...
		'position',[30,158,70,192])

add_block('built-in/State-space',[sys,'/','Controller1'])
set_param([sys,'/','Controller1'],...
		'A','ae',...
		'B','be',...
		'C','ce',...
		'D','de',...
		'Mask Display','H_2 Controller',...
		'Mask Type','H_2 CONTROLLER')
set_param([sys,'/','Controller1'],...
		'Mask Dialogue','H_2 controller.\n(Requires Control Toolbox and RCT)|Controller Matrix A:|Controller Matrix B:|Controller Matrix C:|Controller Matrix D:',...
		'Mask Translate','ae=@1; be=@2; ce=@3; de=@4;')
set_param([sys,'/','Controller1'],...
		'Mask Help','H_2 controller design. Mask and unmask this block to see how it works.',...
		'Mask Entries','ae\/be\/ce\/de\/',...
		'position',[175,150,265,200])

add_block('built-in/Outport',[sys,'/','Outport'])
set_param([sys,'/','Outport'],...
		'Port','4',...
		'position',[520,140,540,160])


%     Subsystem  'Plant'.

new_system([sys,'/','Plant'])
set_param([sys,'/','Plant'],'Location',[646,300,1072,521])

add_block('built-in/State-space',[sys,'/','Plant/State-space'])
set_param([sys,'/','Plant/State-space'],...
		'A','a',...
		'B','b',...
		'C','c',...
		'D','d',...
		'Mask Display','',...
		'position',[110,40,170,70])

add_block('built-in/Outport',[sys,'/','Plant/out'])
set_param([sys,'/','Plant/out'],...
		'position',[380,55,400,75])

add_block('built-in/Sum',[sys,'/','Plant/Sum1'])
set_param([sys,'/','Plant/Sum1'],...
		'inputs','+++',...
		'position',[320,47,340,83])

add_block('built-in/Product',[sys,'/','Plant/Product'])
set_param([sys,'/','Plant/Product'],...
		'position',[225,90,250,110])

add_block('built-in/Inport',[sys,'/','Plant/in'])
set_param([sys,'/','Plant/in'],...
		'position',[40,45,60,65])


%     Subsystem  ['Plant/Multiplicative',13,'noise'].

new_system([sys,'/',['Plant/Multiplicative',13,'noise']])
set_param([sys,'/',['Plant/Multiplicative',13,'noise']],'Location',[0,0,262,236])

add_block('built-in/Outport',[sys,'/',['Plant/Multiplicative',13,'noise/out_1']])
set_param([sys,'/',['Plant/Multiplicative',13,'noise/out_1']],...
		'position',[235,70,255,90])

add_block('built-in/White Noise',[sys,'/',['Plant/Multiplicative',13,'noise/White Noise']])
set_param([sys,'/',['Plant/Multiplicative',13,'noise/White Noise']],...
		'Seed','seed',...
		'position',[55,70,75,90])

add_block('built-in/Discrete Transfer Fcn',[sys,'/',['Plant/Multiplicative',13,'noise/Zero Order Hold']])
set_param([sys,'/',['Plant/Multiplicative',13,'noise/Zero Order Hold']],...
		'Numerator','[sqrt(Cov)]',...
		'Denominator','[sqrt(Ts)]',...
		'Sample time','Ts',...
		'position',[140,61,205,99])
add_line([sys,'/',['Plant/Multiplicative',13,'noise']],[210,80;225,80])
add_line([sys,'/',['Plant/Multiplicative',13,'noise']],[80,80;130,80])
set_param([sys,'/',['Plant/Multiplicative',13,'noise']],...
		'Mask Display','plot(1:20,rand(1,20))',...
		'Mask Type','Input Noise')
set_param([sys,'/',['Plant/Multiplicative',13,'noise']],...
		'Mask Dialogue','White noise for continuous (s-domain) systems.\nBand-limited using zero-order-hold.|Noise Variance:|Sample Time:|Seed')
set_param([sys,'/',['Plant/Multiplicative',13,'noise']],...
		'Mask Translate','Cov = @1; Ts = @2; seed = @3; r = rand(1,12)-0.5; ')
set_param([sys,'/',['Plant/Multiplicative',13,'noise']],...
		'Mask Help','Implemented using white noise into Zero Order Hold block. For faster simulation set sample time to highest value possible but in accordance with the fastest dynamics of system.')
set_param([sys,'/',['Plant/Multiplicative',13,'noise']],...
		'Mask Entries','0.1\/1\/68342\/')


%     Finished composite block ['Plant/Multiplicative',13,'noise'].

set_param([sys,'/',['Plant/Multiplicative',13,'noise']],...
		'position',[65,89,105,121])


%     Subsystem  ['Plant/Additive',13,'noise'].

new_system([sys,'/',['Plant/Additive',13,'noise']])
set_param([sys,'/',['Plant/Additive',13,'noise']],'Location',[0,0,262,236])

add_block('built-in/Outport',[sys,'/',['Plant/Additive',13,'noise/out_1']])
set_param([sys,'/',['Plant/Additive',13,'noise/out_1']],...
		'position',[235,70,255,90])

add_block('built-in/White Noise',[sys,'/',['Plant/Additive',13,'noise/White Noise']])
set_param([sys,'/',['Plant/Additive',13,'noise/White Noise']],...
		'Seed','seed',...
		'position',[55,70,75,90])

add_block('built-in/Discrete Transfer Fcn',[sys,'/',['Plant/Additive',13,'noise/Zero Order Hold']])
set_param([sys,'/',['Plant/Additive',13,'noise/Zero Order Hold']],...
		'Numerator','[sqrt(Cov)]',...
		'Denominator','[sqrt(Ts)]',...
		'Sample time','Ts',...
		'position',[140,61,205,99])
add_line([sys,'/',['Plant/Additive',13,'noise']],[210,80;225,80])
add_line([sys,'/',['Plant/Additive',13,'noise']],[80,80;130,80])
set_param([sys,'/',['Plant/Additive',13,'noise']],...
		'Mask Display','plot(1:20,rand(1,20))',...
		'Mask Type','Input Noise')
set_param([sys,'/',['Plant/Additive',13,'noise']],...
		'Mask Dialogue','White noise for continuous (s-domain) systems.\nBand-limited using zero-order-hold.|Noise Variance:|Sample Time:|Seed')
set_param([sys,'/',['Plant/Additive',13,'noise']],...
		'Mask Translate','Cov = @1; Ts = @2; seed = @3; r = rand(1,12)-0.5; ')
set_param([sys,'/',['Plant/Additive',13,'noise']],...
		'Mask Help','Implemented using white noise into Zero Order Hold block. For faster simulation set sample time to highest value possible but in accordance with the fastest dynamics of system.')
set_param([sys,'/',['Plant/Additive',13,'noise']],...
		'Mask Entries','0.001\/0.1\/12345\/')


%     Finished composite block ['Plant/Additive',13,'noise'].

set_param([sys,'/',['Plant/Additive',13,'noise']],...
		'position',[60,154,105,186])
add_line([sys,'/','Plant'],[65,55;100,55])
add_line([sys,'/','Plant'],[255,100;265,100;265,65;310,65])
add_line([sys,'/','Plant'],[175,55;190,55;190,95;215,95])
add_line([sys,'/','Plant'],[175,55;310,55])
add_line([sys,'/','Plant'],[345,65;370,65])
add_line([sys,'/','Plant'],[110,105;215,105])
add_line([sys,'/','Plant'],[110,170;285,170;285,75;310,75])


%     Finished composite block 'Plant'.

set_param([sys,'/','Plant'],...
		'position',[310,148,340,202])


%     Subsystem  ['About the musyn',13,'demo1'].

new_system([sys,'/',['About the musyn',13,'demo1']])
set_param([sys,'/',['About the musyn',13,'demo1']],'Location',[2,439,106,512])
set_param([sys,'/',['About the musyn',13,'demo1']],...
		'Mask Display','?',...
		'Mask Type','',...
		'Mask Dialogue','eval(''clc; more(24); help h2data; more off;'')',...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block ['About the musyn',13,'demo1'].

set_param([sys,'/',['About the musyn',13,'demo1']],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[520,320,551,349])

add_block('built-in/Scope',[sys,'/','Scope'])
set_param([sys,'/','Scope'],...
		'Vgain','3.000000',...
		'Hgain','10.000000',...
		'Vmax','3.000000',...
		'Hmax','20.000000',...
		'Window',[0,0,466,219],...
		'position',[520,177,540,203])


%     Subsystem  ['Double click here',13,'to start loading data',13,'and mu-syn design1'].

new_system([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design1']])
set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design1']],'Location',[2,439,106,512])
set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design1']],...
		'Mask Display','Re-Load\nData',...
		'Mask Type','')
set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design1']],...
		'Mask Dialogue','eval(''if ~exist(''''obalreal''''),fprintf(''''\nMust be licensed for RCT Toolbox\n''''), else, fprintf(''''\nCtrl_C to abort RCT Calculation\n\n''''); h2data; end'')')
set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design1']],...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block ['Double click here',13,'to start loading data',13,'and mu-syn design1'].

set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design1']],...
		'hide name',0,...
		'position',[20,314,91,353])


%     Subsystem  ['Double click here',13,'to start loading data',13,'and mu-syn design'].

new_system([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design']])
set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design']],'Location',[2,439,106,512])
set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design']],...
		'Mask Display','Re-Design',...
		'Mask Type','')
set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design']],...
		'Mask Dialogue','eval(''if ~exist(''''obalreal''''),fprintf(''''\nMust be licensed for RCT Toolbox\n''''), else, fprintf(''''\nCtrl_C to abort RCT Calculation\n\n''''); h2des1; end'')')
set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design']],...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block ['Double click here',13,'to start loading data',13,'and mu-syn design'].

set_param([sys,'/',['Double click here',13,'to start loading data',13,'and mu-syn design']],...
		'hide name',0,...
		'position',[100,313,172,352])


%     Subsystem  ['Double click here',13,'to show zero-pole',13,'plot of closed-loop4'].

new_system([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of closed-loop4']])
set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of closed-loop4']],'Location',[2,439,106,512])
set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of closed-loop4']],...
		'Mask Display','Controller\nBode Plot',...
		'Mask Type','')
set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of closed-loop4']],...
		'Mask Dialogue','eval(''if ~exist(''''ae''''),fprintf(''''\nMust Load Data & Design First\n''''),else  [x,y,w]=bode(ae,be,ce,de); clg; subplot(211); loglog(w,x(:,1)); subplot(212); semilogx(w,y(:,1)); subplot(111); end'')')
set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of closed-loop4']],...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block ['Double click here',13,'to show zero-pole',13,'plot of closed-loop4'].

set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of closed-loop4']],...
		'hide name',0,...
		'position',[268,311,343,353])


%     Subsystem  ['Double click here',13,'to show bode plot ',13,'of controller'].

new_system([sys,'/',['Double click here',13,'to show bode plot ',13,'of controller']])
set_param([sys,'/',['Double click here',13,'to show bode plot ',13,'of controller']],'Location',[2,439,106,512])
set_param([sys,'/',['Double click here',13,'to show bode plot ',13,'of controller']],...
		'Mask Display','Closed-Loop\nNyquist',...
		'Mask Type','')
set_param([sys,'/',['Double click here',13,'to show bode plot ',13,'of controller']],...
		'Mask Dialogue','eval(''if ~exist(''''ae''''), printf(''''Must Load Data & Desgin First''''), else,[x,y,z,w]=feedback(a,b,c,d,ae,be,ce,de,-1); clg, nyquist(x,y,z,w); end'')')
set_param([sys,'/',['Double click here',13,'to show bode plot ',13,'of controller']],...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block ['Double click here',13,'to show bode plot ',13,'of controller'].

set_param([sys,'/',['Double click here',13,'to show bode plot ',13,'of controller']],...
		'hide name',0,...
		'position',[350,312,429,353])


%     Subsystem  ['Double click here',13,'to show zero-pole',13,'plot of controller'].

new_system([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of controller']])
set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of controller']],'Location',[2,439,106,512])
set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of controller']],...
		'Mask Display','Controller\nNyquist',...
		'Mask Type','')
set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of controller']],...
		'Mask Dialogue','eval(''if ~exist(''''ae''''), printf(''''Must Load Data & Desgin First''''), else,clg; nyquist(ae,be,ce,de), end'')',...
		'Mask Translate','')
set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of controller']],...
		'Mask Help','')


%     Finished composite block ['Double click here',13,'to show zero-pole',13,'plot of controller'].

set_param([sys,'/',['Double click here',13,'to show zero-pole',13,'plot of controller']],...
		'hide name',0,...
		'position',[435,312,508,353])

add_block('built-in/Transfer Fcn',[sys,'/',['Robustness Weighting',13,'Inv(W3) function']])
set_param([sys,'/',['Robustness Weighting',13,'Inv(W3) function']],...
		'Numerator','num3',...
		'Denominator','den3',...
		'Mask Display','plot(log10(w3),log10(ma3))',...
		'Mask Type','polynomial: num/den')
set_param([sys,'/',['Robustness Weighting',13,'Inv(W3) function']],...
		'Mask Dialogue','Transfer Function\nnumerator and denominator:|numerator:|denominator:',...
		'Mask Translate','num3=@1;den3=@2;[ma3,ph3,w3]=bode(num3,den3);')
set_param([sys,'/',['Robustness Weighting',13,'Inv(W3) function']],...
		'Mask Help','Transfer Function',...
		'Mask Entries','num3\/den3\/',...
		'position',[380,226,485,274])

add_block('built-in/Transfer Fcn',[sys,'/',['Input Weighting',13,'W2 Function']])
set_param([sys,'/',['Input Weighting',13,'W2 Function']],...
		'Numerator','num2',...
		'Denominator','den2',...
		'Mask Display','plot(log10(w2),log10(ma2));',...
		'Mask Type','polynomial: num/den')
set_param([sys,'/',['Input Weighting',13,'W2 Function']],...
		'Mask Dialogue','Transfer Function\nnumerator and denominator:|numerator:|denominator:',...
		'Mask Translate','num2=@1;den2=@2;[ma2,pa2,w2]=bode(num2,den2);')
set_param([sys,'/',['Input Weighting',13,'W2 Function']],...
		'Mask Help','Transfer Function',...
		'Mask Entries','num2\/den2\/',...
		'position',[335,76,440,124])

add_block('built-in/Transfer Fcn',[sys,'/',[' Performance Weighting',13,'Inv(W1) function ']])
set_param([sys,'/',[' Performance Weighting',13,'Inv(W1) function ']],...
		'Numerator','num1',...
		'Denominator','den1',...
		'Mask Display','plot(log10(w1),log10(ma1));',...
		'Mask Type','polynomial: num/den')
set_param([sys,'/',[' Performance Weighting',13,'Inv(W1) function ']],...
		'Mask Dialogue','Transfer function\n numerator and denominator|numerator:|denominator:',...
		'Mask Translate','num1=@1;den1=@2;[ma1,ph1,w1]=bode(num1,den1);')
set_param([sys,'/',[' Performance Weighting',13,'Inv(W1) function ']],...
		'Mask Help','Transfer function ',...
		'Mask Entries','num1\/den1\/',...
		'position',[185,37,285,83])


%     Subsystem  ['Double click here',13,'to show bode plot',13,'of closed-loop3'].

new_system([sys,'/',['Double click here',13,'to show bode plot',13,'of closed-loop3']])
set_param([sys,'/',['Double click here',13,'to show bode plot',13,'of closed-loop3']],'Location',[2,439,106,512])
set_param([sys,'/',['Double click here',13,'to show bode plot',13,'of closed-loop3']],...
		'Mask Display','Closed-Loop\nBode Plot',...
		'Mask Type','')
set_param([sys,'/',['Double click here',13,'to show bode plot',13,'of closed-loop3']],...
		'Mask Dialogue','eval(''if ~exist(''''ae''''),fprintf(''''\nMust Load Data & Design First\n''''),else [x,y,z,w]=series(a,b,c,d,ae,be,ce,de);[x,y,z,w]=cloop(x,y,z,w,-1); [x,y,w]=bode(x,y,z,w); clg; subplot(211); loglog(w,x(:,1)); subplot(212); semilogx(w,y(:,1)); subplot(111); end'')')
set_param([sys,'/',['Double click here',13,'to show bode plot',13,'of closed-loop3']],...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block ['Double click here',13,'to show bode plot',13,'of closed-loop3'].

set_param([sys,'/',['Double click here',13,'to show bode plot',13,'of closed-loop3']],...
		'hide name',0,...
		'position',[180,312,259,353])

add_block('built-in/Note',[sys,'/','* Curves inside weighting function blocks are the weighting function bode plot'])
set_param([sys,'/','* Curves inside weighting function blocks are the weighting function bode plot'],...
		'position',[294,365,295,366])
add_line(sys,[345,175;460,175;460,190;510,190])
add_line(sys,[355,175;355,250;90,250;90,190;95,190])
add_line(sys,[355,250;370,250])
add_line(sys,[270,175;300,175])
add_line(sys,[285,175;285,100;325,100])
add_line(sys,[345,175;460,175;460,150;510,150])
add_line(sys,[490,250;515,250])
add_line(sys,[75,175;95,175])
add_line(sys,[60,115;80,115;80,160;95,160])
add_line(sys,[130,175;165,175])
add_line(sys,[130,175;150,175;150,60;175,60])
add_line(sys,[290,60;510,60])
add_line(sys,[445,100;515,100])
% 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
