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

조회 수: 13 (최근 30일)
L'O.G. 2023년 8월 23일
편집: Sam Chak 2023년 8월 24일
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없음 표시없음 숨기기
Sam Chak 2023년 8월 23일
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 2023년 8월 24일
편집: Sam Chak 2023년 8월 24일
@L'O.G., have you shown the full equations of motion? It looks a 6th-order system.

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Bruno Luong 2023년 8월 23일
편집: Bruno Luong 2023년 8월 23일
Again everything is written in the doc of ode45 "For information on how to provide additional parameters to the function odefun, see Parameterizing Functions."

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by