% Safety Critical Control Design.
% Control element Simulation and testing Examples.
% Based on requirements available at
% http://www.mathworks.in/matlabcentral/fileexchange/39047-testing-of-safet
% y-critical-control-systems
%
% Copyright Chethan CU - chethan.cu@gmail.com
% Jan 2013
clear all
DT = 0.02;
Tmax = 5;
t=0:DT:Tmax;
% Define Inputs here -----------------------------------------------
ns=[0 10 ];
ds=[1 10];
[Nz,Dz]=c2dm(ns,ds,DT,'tustin');
input = ones(length(t),1)*5+5;
input(1:10)=5;
init = zeros(length(t),1);
init(1:2)=1;
out = input(1);
pro=input(1);
pri=input(1);
output=[];
% Start of Algorithm -----------------------------------------------
for i = 1:length(t)
if init(i) > 0
out = input(i);
pro=out;
pri=input(i);
else
out=Nz(1)*input(i)+Nz(2)*pri-Dz(2)*pro;
pro=out;
pri=input(i);
end
output=[output;out];
end
% End of Algorithm -----------------------------------------------
% simulate the simulink model --------------------------------------
sim('FirstOrderFilter');
% plot results -----------------------------------------------------
subplot(211)
plot(t,input,t,output,t,o);
legend('Input','Code','Simulink')
title('Input Vs. Outputs')
subplot(212)
plot(t,output-o);
title('error=CodeOutput - SimulinkOutput ')
shg;