フィルターのクリア

How to solve for the derivative using ode solver

5 ビュー (過去 30 日間)
Noya Linder
Noya Linder 2023 年 7 月 12 日
コメント済み: Noya Linder 2023 年 7 月 12 日
I have the following differential equation
function dRdt = odefun(t,R)
c = 29979245800;
Estart = 10^52;
ni = 10^(-2);
mp = 1.67262192*10^(-24);
T3 = 17*Estart/(8*pi*ni*mp*c^2);
dRdt = sqrt((T3-R^3)*c^2/T3);
end
and I solve it using ode78
[t, R, te, Re, ie] = ode78(@odefun, tspan, r0, options)
thus obtaining R(t). I want to solve the same equation but instead for the derivative (I want to get an array of dRdt and it's corresponding t). How should I go about that?
Thank you in advance!

採用された回答

Jayant
Jayant 2023 年 7 月 12 日
To obtain an array of dRdt and its corresponding t values correctly, you can modify the code as follows:
function dRdt = odefun(t, R)
c = 29979245800;
Estart = 10^52;
ni = 10^(-2);
mp = 1.67262192*10^(-24);
T3 = 17*Estart/(8*pi*ni*mp*c^2);
dRdt = sqrt((T3-R^3)*c^2/T3);
end
tspan = [t_start, t_end];
r0 = initial_condition;
options = [];
% Solve the differential equation
[t, R] = ode78(@odefun, tspan, r0, options);
% Calculate dRdt for each time point
dRdt = arrayfun(@odefun, t, R);
This modified code will correctly calculate dRdt for each time point by using the arrayfun function to apply the odefun to each t and R value obtained from the ode78 solver.
You may refer this documentation of arrayfun for reference.
Hope that helps.
  1 件のコメント
Noya Linder
Noya Linder 2023 年 7 月 12 日
Thank you so much! I can't believe the solution was this simple and I made it so complicated in my head lol

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by