I am trying to solve an equation dx/dt = f(x,t) where f(x,t) is a velocity. I want to solve for the positions x to obtain the trajectory x(t). I've seen that the time evolution of the positions x can be calculated using Runge-Kutta, but Matlab's implementation via ode45 (or similar) looks like it requires a function in the first argument. Here, I instead have a starting position x0 and compute the velocity at that initial point, f(x0,t0). How do I use Runge-Kutta to determine the time evolution of the position based on the velocities? If it matters, I can calculate an arbitrary instantaneous velocity f(x,t) from the forces acting on a position x. To give more context, I am calculating the velocity fxt = S*Fx where the matrix S and the force Fx both depend on the particle position, so the velocity that I calculate is a scalar.

 採用された回答

Bruno Luong
Bruno Luong 2023 年 8 月 23 日
編集済み: Bruno Luong 2023 年 8 月 23 日

0 投票

" I can calculate an arbitrary velocity f(x,t) from the forces acting on a position x."
Good this is a very good starting point. If you have f(x,t) in MATLAB function form,
then simply call
sol = ode45(@(t,x) f(x,t), [t0 tend], x0)
tend is the last time where you want the solution to be computed.
Read the doc of ode45

8 件のコメント

L'O.G.
L'O.G. 2023 年 8 月 23 日
編集済み: L'O.G. 2023 年 8 月 23 日
@Bruno Luong The problem is the velocity f(x,t) is just something I calculate at a specific point. It's not in "function form", if I'm understanding you correctly: it's a scalar. It depends on the forces which themselves depend on the position. Doesn't that complicate things? To give context, I am calculating the velocity fxt = S*Fx where the matrix S and the force Fx both depend on the particle position.
Bruno Luong
Bruno Luong 2023 年 8 月 23 日
編集済み: Bruno Luong 2023 年 8 月 23 日
then simply write down
function v = f(x,t)
v = S(x).*Fx(x); % you said you know how to compute S(x) and Fx(x)
end
L'O.G.
L'O.G. 2023 年 8 月 23 日
編集済み: L'O.G. 2023 年 8 月 23 日
@Bruno Luong Thank you, and then pass that into ode45? How do I choose the time step for the method?
Torsten
Torsten 2023 年 8 月 23 日
dx/dt = v, and v is a function of the position that is given to you in the input variables (t,x). What is the problem ?
L'O.G.
L'O.G. 2023 年 8 月 23 日
@Torsten I only know the initial position and velocity. As mentioned, I want to determine the particle trajectory. I hope that makes sense.
Bruno Luong
Bruno Luong 2023 年 8 月 23 日
ode45 adjusts the time step for you automatically. If you need solution at specific time, set tspan argument. Please read the doc in the link above.
Torsten
Torsten 2023 年 8 月 23 日
編集済み: Torsten 2023 年 8 月 23 日
I only know the initial position and velocity. As mentioned, I want to determine the particle trajectory. I hope that makes sense.
ode45 has x - the actual position at time t - as input to your function where you define dx/dt.
L'O.G.
L'O.G. 2023 年 8 月 23 日
Thank you, I have a follow up question that I will ask

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2021b

質問済み:

2023 年 8 月 23 日

コメント済み:

2023 年 8 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by