# Introduce step signal after system response

1 view (last 30 days)
mikel lasa on 19 Jul 2022
Commented: mikel lasa on 21 Jul 2022
Hello,
sorry for the summary, I dont know how to resume my question in few words.
I have a second order system which I know how will respond without an input signal, like this:
x1'=x2;
x2'=ax1+bx2
using ODE 45 and I get this response where the position (x1) acts like this now I need to insert a step signal "u" at second 20. The system looks like this but I dont know how to do it. I have created this signal as a vector but I'm getting an error in the ODE 45 function. How can I introduce u into the equation in order to alterate this system in the second 20?
x1'=x2;
x2'=ax1+bx2 + u
this is my code so far:
%% INIT
clear variables;
close all;
clc;
%% Reference input
%STEP
t = (1:1:60)'; % entering U as a vector is not working
u=t>=20;
% figure()
% plot(t,u)
%% variable initialization
%coefs
a=-0.3;
b=-1.1;
%gains
% Kp=3;
% Ki=0.9;
% Kd=0.8;
%initial conditions
x1init= 2.5;
x2init= 2.5;
% time
time=100;
%% Space state equation
%define second order equation system
f = @(t,X) [X(2); a*X(1)+b*X(2)+u];
% define the area where the function will be analysed
[x1,x2] = meshgrid(-5:5);
x1mat = zeros(size(x1));
x2mat = zeros(size(x2));
%
t=0;
for i = 1:numel(x1)
Xprima = f(t,[x1(i); x2(i)]);
x1mat(i) = Xprima(1);
x2mat(i) = Xprima(2);
end
%% PLOT THE AREA
figure
quiver(x1,x2,x1mat,x2mat,'r'); figure(gcf)
xlabel('x1')
ylabel('x2')
axis tight equal;
title('Phase diagram ')
hold on
%% PLOT FUNCTION SOLUTION
%
%options=odeset('InitialStep',1e-3,'MaxStep',100);
for sol1 = [x1init ; x2init]
[ts,ys] = ode45(f,[0,time],sol1);
plot(ys(:,1),ys(:,2),'r')
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'ks') % ending point
end
hold off
figure
subplot(2,1,1)
plot(ts,ys(:,1),'k', 'LineWidth',2)
xlabel('time(s)','fontweight','bold','FontSize',12)
ylabel('position','fontweight','bold','FontSize',12)
subplot(2,1,2)
plot(ts,ys(:,2),'k', 'LineWidth',2)
xlabel('time(s)','fontweight','bold','FontSize',12)
ylabel('speed','fontweight','bold','FontSize',12)
title('function evolution')

Paul on 19 Jul 2022
Hi mikel,
No need to use ode45 for this particular problem. The delayed step response with initial conditions can be obtained as
a=-0.3;
b=-1.1;
sys = ss([0 1;a b],[0;1],eye(2),0);
%initial conditions
x1init= 2.5;
x2init= 2.5;
% time
time=100;
ts = 0:.1:time; % small time step, make sure a point in ts hits 20
ys = initial(sys,[x1init x2init],ts) + step(sys*tf(1,1,'InputDelay',20),ts);
% or use lsim
%ys = lsim(sys,ts>=20,ts,[x1init x2init]);
figure
subplot(2,1,1)
plot(ts,ys(:,1),'k', 'LineWidth',2)
xlabel('time(s)','fontweight','bold','FontSize',12)
ylabel('position','fontweight','bold','FontSize',12)
subplot(2,1,2)
plot(ts,ys(:,2),'k', 'LineWidth',2)
xlabel('time(s)','fontweight','bold','FontSize',12)
ylabel('speed','fontweight','bold','FontSize',12)
title('function evolution') mikel lasa on 21 Jul 2022
thank you! understood!

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!