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)
이전 댓글 표시
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
Mathieu NOE
2020년 10월 6일
seems y_0 and u_0 are never initialized.
Jon
2020년 10월 6일
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.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Simulink에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!