Clear Filters
Clear Filters

Runge-Kutta with a function that changes at each time step

16 views (last 30 days)
I previously asked a question about solving an equation dx/dt = u(x,t) where u(x,t) is a velocity. I want to solve for the positions x to obtain the trajectory x(t). Here, I have a starting position x0 and compute the velocity at that initial point, u(x0,t0). I want to use Runge-Kutta to determine the time evolution of the position based on the velocitie. I can calculate an arbitrary instantaneous velocity u(x,t) from the forces acting on a position x. I think I'm close, but need to modify the following to take into account the arguments I'm passing to the velocity function, and then calculate the time evolution of all points / particles (two in this case). How do I do that? I'm not sure how to pass the arguments into the velocity function for one thing.
Here's the simplified current version:
% Input
f = [0.05;0;0]; % Force
N = 2; % Number of points
% Initial positions
x1 = [-0.5,0,0];
x2 = [0.5,0,0];
deltaX = x2-x1;
c = 3; % Constant
x = [x1;x2];
% Define second force
fs = @(deltaX) c*deltaX;
% Initial values
t0 = 0;
tf = 2.5;
sol = ode45(@(t,x) u(x,t),[t0 tf],[x1(1) x2(1)]);
function v = u(x,t)
% Calculate forces on each point
fs_x1 = fs(deltaX);
fs_x2 = -fs_x1;
% Calculate net forces
f_k1 = -f + fs_x1(:);
f_k2 = f + fs_x2(:);
f_k = [f_k1(1);f_k1(2);f_k1(3);f_k2(1);f_k2(2);f_k2(3)];
% Calculate velocities
v = S*f_k; % I have a separate function for the calculation of S, which is a function of the positions
end
  2 Comments
Sam Chak
Sam Chak on 23 Aug 2023
Suggest you show math model of the dynamic system instead of the code so that we can understand the problem clearly.
Also show how the matrix S is calculated.
Sam Chak
Sam Chak on 24 Aug 2023
Edited: Sam Chak on 24 Aug 2023
@L'O.G., have you shown the full equations of motion? It looks a 6th-order system.

Sign in to comment.

Answers (1)

Bruno Luong
Bruno Luong on 23 Aug 2023
Edited: Bruno Luong on 23 Aug 2023
Again everything is written in the doc of ode45 "For information on how to provide additional parameters to the function odefun, see Parameterizing Functions."
Just a question of reading.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!