필터 지우기
필터 지우기

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 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