How can i implement this code as a simulation uting simulink matlab function block? (changing t and u variables for the time and the step input)
Show older comments
hi, im new in this forum. i want to implement a discrete transfer function algorithm into simulink but i dont know how to get the variables that i use in the matlab code (u=step function, t=simulation time), i want to change this code in a way that i can use it on a simulation in simulink using the matlab function block. my code is:
clc
clear
%simulation time vector
dt=0.04;
t=0:dt:1;
%step
u=ones(size(t));
u(1)=0;
%inicialization
yk_1=0;
yk_2=0;
uk_1=0;
uk_2=0;
%diferential equation solution
for i=1:length(t)
if i==1
terminos_u=0.1322*uk_1 + 0.1096*uk_2;
terminos_y=1.45*yk_1 - 0.5712*yk_2;
y(i)=terminos_u + terminos_y;
elseif i==2
terminos_u=0.1322*u(i-1) + 0.1096*uk_1;
terminos_y=1.45*y(i-1) - 0.5712*yk_1;
y(i)=terminos_u + terminos_y;
else
terminos_u=0.1322*u(i-1) + 0.1096*u(i-2);
terminos_y=1.45*y(i-1) - 0.5712*y(i-2);
y(i)=terminos_u + terminos_y;
end
end
plot(t,y)
the simulink i want to use is:

i have tried doing this but it wont work in simulink
function y = fcn(u)
%funcion step
persistent yk_1 yk_2 uk_1 uk_2 y_0 u_0
if isempty(u_0)
yk_1=0;
yk_2=0;
uk_1=0;
uk_2=0;
end
uk_2=uk_1; uk_1=u_0;yk_2=yk_1; yk_1=y_0;
y = 0.1322*uk_1 + 0.1096*uk_2 + 1.45*yk_1 - 0.5712*yk_2;
u_0=u;y_0=y;
end
is says the error:

for all my variables
2 Comments
Mathieu NOE
on 6 Oct 2020
seems y_0 and u_0 are never initialized.
Jon
on 6 Oct 2020
I think you may be misunderstanding the purpose of running a Simulink simulation. You would normally run a Simulink simulation to find the solution to a system differential equations. In your case you already have the solution and it looks like you just want to plot the answer. Please explain further what problem you would like to solve by using Simulink.
Answers (0)
Categories
Find more on Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!