Hi Muhammad Qureshi,
It seems to me that you are having trouble switching between systems based on the time. Making a custom function that modifies the "xdot" in response to time can serve as one of the probable methods to achieve this. Here is a sample code for the same:
% Define the system matrices A and B
A = [1 2; 3 4]; % Replace with your own matrices
B = [5 6; 7 8]; % Replace with your own matrices
% Set initial conditions and time span
x0 = [1 1]; % Replace with your own initial conditions
totaltime = 5; % total seconds
t = [];
x = [];
% Solve the differential equations for each time interval
for i= 0:totaltime
[t1, x1] = ode45(@(t, x) systemODE(t, x, A, B), [i i+0.4], x0(end, :)');
[t2, x2] = ode45(@(t, x) systemODE(t, x, A, B), [i+0.4 i+1], x1(end, :)');
x0 = x2;
x = [x; x1; x2];
t= [t; t1; t2];
end
% Plot the results
plot(t, x);
xlabel('Time');
ylabel('State');
function xdot = systemODE(t, x, A, B)
timeDecimal = t - floor(t);
if timeDecimal >= 0 && timeDecimal < 0.4
xdot = A * x;
elseif timeDecimal >= 0.4 && timeDecimal < 1
xdot = B * x;
end
end
You can refer to the following documentation to know more about the how to create custom functions: https://www.mathworks.com/help/matlab/ref/function.html
Best Regards,
Abhishek Chakram