Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Saturation and floor functions in Simulink ?

145 views
Skip to first unread message

Khaled Ali M. El-Metwally

unread,
Aug 17, 1995, 3:00:00 AM8/17/95
to
Hi all,

I'm trying to simulate a non-linear model which includes saturation. I've
build a samll test example using simulink to demonstrate the saturation
function. In this example saturation fuction appears to map (scale) the input
signal (u) into the saturation limits. It should had clipped the signal off at
the saturation limit.

I've also tried to get a floor value of a signal (u) using the function
block, but also this didn't work as I expected. It floors the signal boundaries
only and ignores the values in between. Or it shifts the signal to the floor
limits of its boundaries.

I'm including this example , I would appreciate any help or suggestions.
Thanks,

Khaled

------------------------- Start of the example -------------------------------
function [ret,x0,str,ts,xts]=sat(t,x,u,flag);
%SAT is the M-file description of the SIMULINK system named SAT.
% The block-diagram can be displayed by typing: SAT.
%
% SYS=SAT(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 SAT 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 SAT with a FLAG of zero:
% [SIZES]=SAT([],[],[],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',[364,84,881,429])
open_system(sys)
end;
set_param(sys,'algorithm', 'RK-45')
set_param(sys,'Start time', '0.0')
set_param(sys,'Stop time', '100')
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/Switch',[sys,'/','Switch1'])
set_param([sys,'/','Switch1'],...
'orientation',2,...
'position',[265,77,285,123])

add_block('built-in/Constant',[sys,'/','Upper Limit1'])
set_param([sys,'/','Upper Limit1'],...
'orientation',2,...
'Value','2',...
'position',[280,39,300,61])

add_block('built-in/Switch',[sys,'/','Switch2'])
set_param([sys,'/','Switch2'],...
'orientation',2,...
'Threshold','2',...
'position',[205,76,225,124])


% Subsystem 'Saturation #1'.

new_system([sys,'/','Saturation #1'])
set_param([sys,'/','Saturation #1'],'Location',[0,59,274,252])

add_block('built-in/Inport',[sys,'/','Saturation #1/x'])
set_param([sys,'/','Saturation #1/x'],...
'position',[65,55,85,75])

add_block('built-in/S-Function',[sys,'/',['Saturation #1/S-function',13,'M-file
which plots',13,'lines',13,'']])
set_param([sys,'/',['Saturation #1/S-function',13,'M-file which
plots',13,'lines',13,'']],...
'function name','sfunyst',...
'parameters','ax, color, npts, dt',...
'position',[130,55,180,75])
add_line([sys,'/','Saturation #1'],[90,65;125,65])
set_param([sys,'/','Saturation #1'],...
'Mask Display','plot(0,0,100,100,[83,76,63,52,42,38,28,16,11,84,11,11,11,90,90,11],[75,58,47,54,72,80,84,74,65,65,65,90,40,40,90,90])',...
'Mask Type','Storage scope.')
set_param([sys,'/','Saturation #1'],...
'Mask Dialogue','Storage scope using MATLAB graph window.\nEnter plotting
ranges and line type.|Initial Time Range:|Initial y-min:|Initial y-max:|Storage
pts.:|Line type (rgbw-.:xo):')
set_param([sys,'/','Saturation #1'],...
'Mask Translate','npts = @4; color = @5; ax = [0, @1, @2, @3]; dt=-1;')
set_param([sys,'/','Saturation #1'],...
'Mask Help','This block uses a MATLAB figure window to plot the input signal.
The graph limits are automatically scaled to the min and max values of the
signal stored in the scope''s signal buffer. Line type must be in quotes. See
the M-file sfunyst.m.')
set_param([sys,'/','Saturation #1'],...
'Mask Entries','100\/-10\/10\/500\/''y-/g--/c-./w:/m*/ro/b+''\/')


% Finished composite block 'Saturation #1'.

set_param([sys,'/','Saturation #1'],...
'orientation',2,...
'position',[115,80,145,120])

add_block('built-in/Sum',[sys,'/','sum'])
set_param([sys,'/','sum'],...
'orientation',2,...
'inputs','+-',...
'position',[425,82,445,118])

add_block('built-in/Constant',[sys,'/','Shift e'])
set_param([sys,'/','Shift e'],...
'orientation',2,...
'Value','0.5',...
'position',[475,129,495,151])


% Subsystem ['Pulse',13,'Generator'].

new_system([sys,'/',['Pulse',13,'Generator']])
set_param([sys,'/',['Pulse',13,'Generator']],'Location',[163,466,764,707])

add_block('built-in/Outport',[sys,'/',['Pulse',13,'Generator/out_1']])
set_param([sys,'/',['Pulse',13,'Generator/out_1']],...
'hide name',0,...
'position',[560,105,580,125])

add_block('built-in/Gain',[sys,'/',['Pulse',13,'Generator/Gain']])
set_param([sys,'/',['Pulse',13,'Generator/Gain']],...
'hide name',0,...
'Gain','ht',...
'position',[510,102,535,128])

add_block('built-in/Product',[sys,'/',['Pulse',13,'Generator/Product']])
set_param([sys,'/',['Pulse',13,'Generator/Product']],...
'hide name',0,...
'position',[455,105,480,125])

add_block('built-in/Logical
Operator',[sys,'/',['Pulse',13,'Generator/Logical',13,'Operator1']])
set_param([sys,'/',['Pulse',13,'Generator/Logical',13,'Operator1']],...
'Operator','NOT',...
'Number of Input Ports','1',...
'position',[355,38,385,62])

add_block('built-in/Logical
Operator',[sys,'/',['Pulse',13,'Generator/Logical',13,'Operator']])
set_param([sys,'/',['Pulse',13,'Generator/Logical',13,'Operator']],...
'Operator','XOR',...
'position',[285,38,315,62])

add_block('built-in/Constant',[sys,'/',['Pulse',13,'Generator/Constant1']])
set_param([sys,'/',['Pulse',13,'Generator/Constant1']],...
'Value','stt',...
'position',[295,149,320,171])

add_block('built-in/Clock',[sys,'/',['Pulse',13,'Generator/Clock1']])
set_param([sys,'/',['Pulse',13,'Generator/Clock1']],...
'hide name',0,...
'position',[295,100,315,120])

add_block('built-in/Relational
Operator',[sys,'/',['Pulse',13,'Generator/Relational',13,'operator']])
set_param([sys,'/',['Pulse',13,'Generator/Relational',13,'operator']],...
'hide name',0,...
'position',[370,102,400,133])

add_block('built-in/Sum',[sys,'/',['Pulse',13,'Generator/Sum1']])
set_param([sys,'/',['Pulse',13,'Generator/Sum1']],...
'hide name',0,...
'inputs','+-',...
'position',[130,120,150,140])

add_block('built-in/Sum',[sys,'/',['Pulse',13,'Generator/Sum']])
set_param([sys,'/',['Pulse',13,'Generator/Sum']],...
'hide name',0,...
'inputs','+-',...
'position',[135,35,155,55])

add_block('built-in/Constant',[sys,'/',['Pulse',13,'Generator/Constant']])
set_param([sys,'/',['Pulse',13,'Generator/Constant']],...
'position',[25,30,45,50])

add_block('built-in/Unit Delay',[sys,'/',['Pulse',13,'Generator/Unit Delay']])
set_param([sys,'/',['Pulse',13,'Generator/Unit Delay']],...
'orientation',2,...
'Sample time','[Ts,st1]',...
'position',[145,75,195,95])

add_block('built-in/Unit Delay',[sys,'/',['Pulse',13,'Generator/Unit Delay1']])
set_param([sys,'/',['Pulse',13,'Generator/Unit Delay1']],...
'orientation',2,...
'Sample time','[Ts,st2]',...
'x0','ini',...
'position',[140,165,190,185])
add_line([sys,'/',['Pulse',13,'Generator']],[390,50;430,50;430,110;450,110])
add_line([sys,'/',['Pulse',13,'Generator']],[320,50;350,50])
add_line([sys,'/',['Pulse',13,'Generator']],[325,160;345,160;345,125;365,125])
add_line([sys,'/',['Pulse',13,'Generator']],[540,115;555,115])
add_line([sys,'/',['Pulse',13,'Generator']],[485,115;505,115])
add_line([sys,'/',['Pulse',13,'Generator']],[405,120;450,120])
add_line([sys,'/',['Pulse',13,'Generator']],[320,110;365,110])
add_line([sys,'/',['Pulse',13,'Generator']],[140,85;105,85;105,50;130,50])
add_line([sys,'/',['Pulse',13,'Generator']],[160,45;230,45;230,85;200,85])
add_line([sys,'/',['Pulse',13,'Generator']],[50,40;130,40])
add_line([sys,'/',['Pulse',13,'Generator']],[155,130;210,130;210,175;195,175])
add_line([sys,'/',['Pulse',13,'Generator']],[135,175;100,175;100,135;125,135])
add_line([sys,'/',['Pulse',13,'Generator']],[50,40;70,40;70,125;125,125])
add_line([sys,'/',['Pulse',13,'Generator']],[160,45;280,45])
add_line([sys,'/',['Pulse',13,'Generator']],[155,130;245,130;245,55;280,55])
set_param([sys,'/',['Pulse',13,'Generator']],...
'Mask Display','plot(0,0,100,100,[90,75,75,60,60,35,35,20,20,10],[20,20,80,80,20,20,80,80,20,20])',...
'Mask Type','Pulse Generator')
set_param([sys,'/',['Pulse',13,'Generator']],...
'Mask Dialogue','Pulse Generator.|Pulse period (secs):|Pulse width:|Pulse
height:|Pulse start time:')
set_param([sys,'/',['Pulse',13,'Generator']],...
'Mask Translate','Ts=@1; du=@2; ht=@3; stt=@4; ini=ones(length(ht),1);
st1=rem(stt,Ts);
st2=rem(stt+du,Ts);if(Ts<=1.2*du),ini=zeros(length(ht),1);end;')
set_param([sys,'/',['Pulse',13,'Generator']],...
'Mask Help','Pulse generator which ensures pulse\ntransitions are hit.
Provides a vector of pulses when the height is entered as a vector.\nUnmask to
see how it works.')
set_param([sys,'/',['Pulse',13,'Generator']],...
'Mask Entries','20\/10\/1\/0.5\/')


% Finished composite block ['Pulse',13,'Generator'].

set_param([sys,'/',['Pulse',13,'Generator']],...
'orientation',2,...
'position',[470,52,505,88])

add_block('built-in/Fcn',[sys,'/','Fcn'])
set_param([sys,'/','Fcn'],...
'orientation',2,...
'Expr','5*u',...
'position',[360,90,400,110])

add_block('built-in/Constant',[sys,'/','Lower Limit'])
set_param([sys,'/','Lower Limit'],...
'orientation',2,...
'Value','0',...
'position',[310,134,330,156])


% Subsystem ['floor ( u )',13,''].

new_system([sys,'/',['floor ( u )',13,'']])
set_param([sys,'/',['floor ( u )',13,'']],'Location',[0,59,274,252])

add_block('built-in/Inport',[sys,'/',['floor ( u )',13,'/x']])
set_param([sys,'/',['floor ( u )',13,'/x']],...
'position',[65,55,85,75])

add_block('built-in/S-Function',[sys,'/',['floor ( u
)',13,'/S-function',13,'M-file which plots',13,'lines',13,'']])
set_param([sys,'/',['floor ( u )',13,'/S-function',13,'M-file which
plots',13,'lines',13,'']],...
'function name','sfunyst',...
'parameters','ax, color, npts, dt',...
'position',[130,55,180,75])
add_line([sys,'/',['floor ( u )',13,'']],[90,65;125,65])
set_param([sys,'/',['floor ( u )',13,'']],...
'Mask Display','plot(0,0,100,100,[83,76,63,52,42,38,28,16,11,84,11,11,11,90,90,11],[75,58,47,54,72,80,84,74,65,65,65,90,40,40,90,90])',...
'Mask Type','Storage scope.')
set_param([sys,'/',['floor ( u )',13,'']],...
'Mask Dialogue','Storage scope using MATLAB graph window.\nEnter plotting
ranges and line type.|Initial Time Range:|Initial y-min:|Initial y-max:|Storage
pts.:|Line type (rgbw-.:xo):')
set_param([sys,'/',['floor ( u )',13,'']],...
'Mask Translate','npts = @4; color = @5; ax = [0, @1, @2, @3]; dt=-1;')
set_param([sys,'/',['floor ( u )',13,'']],...
'Mask Help','This block uses a MATLAB figure window to plot the input signal.
The graph limits are automatically scaled to the min and max values of the
signal stored in the scope''s signal buffer. Line type must be in quotes. See
the M-file sfunyst.m.')
set_param([sys,'/',['floor ( u )',13,'']],...
'Mask Entries','100\/-10\/10\/500\/''y-/g--/c-./w:/m*/ro/b+''\/')


% Finished composite block ['floor ( u )',13,''].

set_param([sys,'/',['floor ( u )',13,'']],...
'orientation',2,...
'position',[115,245,145,285])

add_block('built-in/Fcn',[sys,'/','Fcn2'])
set_param([sys,'/','Fcn2'],...
'orientation',2,...
'Expr','floor( u )',...
'position',[210,255,250,275])


% Subsystem 'Original Signal (u)'.

new_system([sys,'/','Original Signal (u)'])
set_param([sys,'/','Original Signal (u)'],'Location',[0,59,274,252])

add_block('built-in/S-Function',[sys,'/',['Original Signal
(u)/S-function',13,'M-file which plots',13,'lines',13,'']])
set_param([sys,'/',['Original Signal (u)/S-function',13,'M-file which
plots',13,'lines',13,'']],...
'function name','sfunyst',...
'parameters','ax, color, npts, dt',...
'position',[130,55,180,75])

add_block('built-in/Inport',[sys,'/','Original Signal (u)/x'])
set_param([sys,'/','Original Signal (u)/x'],...
'position',[65,55,85,75])
add_line([sys,'/','Original Signal (u)'],[90,65;125,65])
set_param([sys,'/','Original Signal (u)'],...
'Mask Display','plot(0,0,100,100,[83,76,63,52,42,38,28,16,11,84,11,11,11,90,90,11],[75,58,47,54,72,80,84,74,65,65,65,90,40,40,90,90])',...
'Mask Type','Storage scope.')
set_param([sys,'/','Original Signal (u)'],...
'Mask Dialogue','Storage scope using MATLAB graph window.\nEnter plotting
ranges and line type.|Initial Time Range:|Initial y-min:|Initial y-max:|Storage
pts.:|Line type (rgbw-.:xo):')
set_param([sys,'/','Original Signal (u)'],...
'Mask Translate','npts = @4; color = @5; ax = [0, @1, @2, @3]; dt=-1;')
set_param([sys,'/','Original Signal (u)'],...
'Mask Help','This block uses a MATLAB figure window to plot the input signal.
The graph limits are automatically scaled to the min and max values of the
signal stored in the scope''s signal buffer. Line type must be in quotes. See
the M-file sfunyst.m.')
set_param([sys,'/','Original Signal (u)'],...
'Mask Entries','100\/-10\/10\/500\/''y-/g--/c-./w:/m*/ro/b+''\/')


% Finished composite block 'Original Signal (u)'.

set_param([sys,'/','Original Signal (u)'],...
'orientation',2,...
'position',[300,275,330,315])

add_block('built-in/Saturation',[sys,'/','Saturation'])
set_param([sys,'/','Saturation'],...
'orientation',2,...
'Lower Limit','0',...
'Upper Limit','2',...
'Mask Display','',...
'position',[205,178,235,202])


% Subsystem 'Saturation #2'.

new_system([sys,'/','Saturation #2'])
set_param([sys,'/','Saturation #2'],'Location',[0,59,274,252])

add_block('built-in/S-Function',[sys,'/',['Saturation #2/S-function',13,'M-file
which plots',13,'lines',13,'']])
set_param([sys,'/',['Saturation #2/S-function',13,'M-file which
plots',13,'lines',13,'']],...
'function name','sfunyst',...
'parameters','ax, color, npts, dt',...
'position',[130,55,180,75])

add_block('built-in/Inport',[sys,'/','Saturation #2/x'])
set_param([sys,'/','Saturation #2/x'],...
'position',[65,55,85,75])
add_line([sys,'/','Saturation #2'],[90,65;125,65])
set_param([sys,'/','Saturation #2'],...
'Mask Display','plot(0,0,100,100,[83,76,63,52,42,38,28,16,11,84,11,11,11,90,90,11],[75,58,47,54,72,80,84,74,65,65,65,90,40,40,90,90])',...
'Mask Type','Storage scope.')
set_param([sys,'/','Saturation #2'],...
'Mask Dialogue','Storage scope using MATLAB graph window.\nEnter plotting
ranges and line type.|Initial Time Range:|Initial y-min:|Initial y-max:|Storage
pts.:|Line type (rgbw-.:xo):')
set_param([sys,'/','Saturation #2'],...
'Mask Translate','npts = @4; color = @5; ax = [0, @1, @2, @3]; dt=-1;')
set_param([sys,'/','Saturation #2'],...
'Mask Help','This block uses a MATLAB figure window to plot the input signal.
The graph limits are automatically scaled to the min and max values of the
signal stored in the scope''s signal buffer. Line type must be in quotes. See
the M-file sfunyst.m.')
set_param([sys,'/','Saturation #2'],...
'Mask Entries','100\/-10\/10\/500\/''y-/g--/c-./w:/m*/ro/b+''\/')


% Finished composite block 'Saturation #2'.

set_param([sys,'/','Saturation #2'],...
'orientation',2,...
'position',[115,170,145,210])
add_line(sys,[305,145;305,115;290,115])
add_line(sys,[260,100;230,100])
add_line(sys,[275,50;245,50;245,85;230,85])
add_line(sys,[200,100;150,100])
add_line(sys,[260,100;245,100;245,115;230,115])
add_line(sys,[465,70;460,70;450,90])
add_line(sys,[470,140;460,140;450,110])
add_line(sys,[420,100;405,100])
add_line(sys,[355,100;310,100;310,85;290,85])
add_line(sys,[310,100;290,100])
add_line(sys,[200,190;150,190])
add_line(sys,[355,100;355,190;240,190])
add_line(sys,[355,190;355,265;255,265])
add_line(sys,[205,265;150,265])
add_line(sys,[355,265;355,295;335,295])

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
------------------------- End of the example -------------------------------

_______________________________________________________________________________

Khaled A. El-Metwally Email: metw...@enel.ucalgary.ca
Dept. of Electrical & Computer Engg. Work: (403) 220-4867
University of Calgary, (403) 220-6077
Calgary, Alberta, Fax: (403) 282-6855
Canada T2N 1N4
_______________________________________________________________________________


0 new messages