# How to solve this ode equation where one of the inputs has some delay and equation has written in matrix form in MATLAB ode45?

16 views (last 30 days)
Sol Elec on 15 Mar 2022
Commented: Walter Roberson on 16 Mar 2022
I solved this ode for constant input u. Function script of this ode is written here-
function dx = mySystem(t,x,u,A,B)
dx = A*x +B*u
end
A is (6*6) matrix. B is (4*6) matrix. C is (6*1) matrix. D is (4*1) matrix.
Main Script
A=[ -0.1078 0 0 0 -10 0; 0 -0.0898 0 0 0 -8.3333; 0 0 -0.2352 0 10 0; 0 0 0 -0.196 0 8.3333; 1 0 -1 0 0 0; 0 1 0 -1 0 0];
B=[0.3313 0 -8.9335 0; 0 0.2761 0 -7.4446; 0 0 3.7249 0;0 0 0 3.104; 0 0 0 0; 0 0 0 0];
C=[ 0 0 1.4206 1.4206 0 0]; D= [0 0 -15 -15];
u=zeros(2*2,1);
for i=1:2
u(i)=0.5;
end
fun = @(t, x) mySystem(t, x, u, A, B);
tspan = [0, 20]; % Time span
x0 = zeros(3*3,1); %Initial Condition
[t, YSol] = ode45(fun, tspan, x0); % ode
Pe=C*YSol(:,1:3*2)'+D*u; % Output Equation
% Plot
figure(5)
plot(t,Pe);
xlabel('time'); ylabel('{P');
title(' Change in P')
Above ode has been solved easily. But actually my system has delayed input. u(1)=0.5 is constant but u(2)=0.5*(tau-5), i.e. u(2) is delayed by 10 second. In this situation how can I solve this? What will be the modified code? Other thing is if the system have more than 100 order then how it can be solved? A is (3*n by3*n); B is(3*n by 2*n); C is (1 by 3*n);D is (1 by 2*n); where n=100, and out of 2*n input u(2):u(n) have delay of different tau.
%% Delay in Input
u(1)=0.5;
if t <= 10
u(2)=0;
else
u(2)=0.5;
end
Walter Roberson on 15 Mar 2022

Torsten on 15 Mar 2022
Order the complete list of time instances when the control variables change their values.
If these times are 0 < t1 < t2 < t3 < ... < tn, then call ode45 in a loop and integrate first with tspan1 = [0,t1], then with tspan2 = [t1,t2] by using the end values at t1 as start values at t2 and so on.
Walter Roberson on 16 Mar 2022
Note: you might want to do something like
N = 5; %points per interval
tspan = linspace(Delay(i), Delay(i+1), N);
The difference is that when you use exactly two elements in a time range, then ode45() will return back as many time points as it feels like, whereas if you pass in a vector of more than two elements then ode45() will return at those particular times.
Walter Roberson on 16 Mar 2022
T = [T ; t(1:end-1)];
X = [X ; x(1:end-1,:)];
However at the very end, you would want to append t(end) to T and x(end,:) to X .
Also, in some cases you need to adjust the boundary conditions. For example if you were adding a jolt of energy or were bouncing off a floor, then one or more of the boundary conditions for the new interval should reflect the change. "Your y velocity was -5 when you hit the wall, you need to reverse that to +4.97 at the bounce". Because of that, your conditions upon "entering" time Delay(i+1) "from below" might not be the same as your conditions "leaving" that at what is now Delay(i) [because i value changed]. You need to figure out which of the two sets of values you want to put into your accumulated data. @Torsten chose to leave out the "entering" state data and include the "leaving" state data. Which is a perfectly reasonable choice in many circumstances... but sometimes you want the other choice, and sometimes you want the data to include both so that you can visually see the step when you plot.