Borrar filtros
Borrar filtros

How to solve for the derivative using ode solver

6 visualizaciones (últimos 30 días)
Noya Linder
Noya Linder el 12 de Jul. de 2023
Comentada: Noya Linder el 12 de Jul. de 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!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by