How can I evaluate the transition matrix using the fourth order Runge-Kutta ODEs

78 views (last 30 days)
I have a two-degree-of-freedom system. for solving the system I am trying to use RK4. Can anyone help me with correctly writing the transition matrix in my matlab code according to the definition in this reference?
% Time settings
t0 = 0;
tf = pi / omega_rad_s^2;
N = 100;
ts = (tf - t0) / N;
t = linspace(t0, tf, N+1);
%Initial condition
y01 = 1;
y02 = 0;
y03 = 0;
y04 = 0;
y = [y01; y02; y03; y04]; % IC vector
% Solve the system using RK4
for i = 1:N
k1 = Function_system(t(i), y(:, i));
k2 = Function_system(t(i) + ts/2, y(:, i) + ts/2 * k1);
k3 = Function_system(t(i) + ts/2, y(:, i) + ((sqrt(2)-1)/2) * ts * k1 + (1 - (1/sqrt(2))) * ts * k2);
k4 = Function_system(t(i) + ts, y(:, i) - (1/sqrt(2)) * ts * k2 + (1 + 1/sqrt(2)) * ts * k3);
y(:, i+1) = y(:, i) + ts/6 * (k1 + 2*(1 - (1/sqrt(2)))*k2 + 2*(1 + (1/sqrt(2)))*k3 + k4);
end
  2 Comments
Torsten
Torsten on 13 Nov 2024 at 20:41
Edited: Torsten on 13 Nov 2024 at 20:41
Here your function "Function_system" has three inputs:
k1 = Function_system(t(i), y(:, i), K_theta);
Here it has two:
k2 = Function_system(t(i) + ts/2, y(:, i) + ts/2 * k1);
What is correct ?
Nikoo
Nikoo on 13 Nov 2024 at 21:04
k1 = Function_system(t(i), y(:, i));
k2 = Function_system(t(i) + ts/2, y(:, i) + ts/2 * k1);
All should have 2

Sign in to comment.

Answers (1)

Torsten
Torsten on 13 Nov 2024 at 21:11
Edited: Torsten on 13 Nov 2024 at 21:17
Seems to work. What's your problem ?
Don't use the transition matrix to integrate in one pass. If you have to, generate it with symbolic inputs to "Function_system".
% Time settings
t0 = 0;
tf = 3;
N = 100;
ts = (tf - t0) / N;
t = linspace(t0, tf, N+1);
%Initial condition
y01 = 1;
y02 = 1;
y03 = 1;
y04 = 1;
y = [y01; y02; y03; y04]; % IC vector
% Solve the system using RK4
for i = 1:N
k1 = Function_system(t(i), y(:, i));
k2 = Function_system(t(i) + ts/2, y(:, i) + ts/2 * k1);
k3 = Function_system(t(i) + ts/2, y(:, i) + ((sqrt(2)-1)/2) * ts * k1 + (1 - (1/sqrt(2))) * ts * k2);
k4 = Function_system(t(i) + ts, y(:, i) - (1/sqrt(2)) * ts * k2 + (1 + 1/sqrt(2)) * ts * k3);
y(:, i+1) = y(:, i) + ts/6 * (k1 + 2*(1 - (1/sqrt(2)))*k2 + 2*(1 + (1/sqrt(2)))*k3 + k4);
end
plot(t,y(3,:))
function dydt = Function_system(t,y)
dydt = [-y(1),-2*y(2),y(3),2*y(4)].';
end
  13 Comments
Nikoo
Nikoo on 18 Nov 2024 at 21:40
Yes, my goal is to find the transition matrix and analyze its eigenvalues.
I thought that ODE45 might be a suitable function, but after doing some research, I think using RK4 might give me more accurate results.
Torsten
Torsten on 18 Nov 2024 at 22:33
Edited: Torsten on 18 Nov 2024 at 22:34
I thought that ODE45 might be a suitable function, but after doing some research, I think using RK4 might give me more accurate results.
No, but how to find the numerical transition matrix for "ODE45" ? Do you know how to compute K for the scheme implemented in "ODE45" ?

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!