Filter löschen
Filter löschen

How to solve for the derivative using ode solver

5 Ansichten (letzte 30 Tage)
Noya Linder
Noya Linder am 12 Jul. 2023
Kommentiert: Noya Linder am 12 Jul. 2023
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!

Akzeptierte Antwort

Jayant
Jayant am 12 Jul. 2023
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 Kommentar
Noya Linder
Noya Linder am 12 Jul. 2023
Thank you so much! I can't believe the solution was this simple and I made it so complicated in my head lol

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by