How to solve for the derivative using ode solver

6 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by