Time-dependent parameter in ODE solver

4 ビュー (過去 30 日間)
Monirul Hasan
Monirul Hasan 2023 年 11 月 10 日
コメント済み: Star Strider 2023 年 11 月 10 日
I was trying to understand how to deal with time-dependent parameter in ODE solver. Having this error "Error using interp1>reshapeAndSortXandV - X and V must be of the same length."
Please explain what the issue.
tspan = 1:5;
y0 = 0;
a = 1:5;
[t,y] = ode45(@(t,y)fun(t,y,a),tspan, y0);
function v = fun(t,y,a)
t_dummy = t;
a = interp1(t_dummy,a,t);
v = 2*a.*t;
end

採用された回答

Star Strider
Star Strider 2023 年 11 月 10 日
The interp1 use in the documentation section for time-dependent parameters refers to vector arguments with a dependent variable as a function of time. The interpolation would then return an interpolated value of the dependent variable at a specific time.
You need to pass ‘tv’ (or some other time vector with appropriate limits) to ‘fun’ as well in order to do the interpolation —
tspan = 1:5;
y0 = 0;
a = 1:5;
tv = 1:5;
[t,y] = ode45(@(t,y)fun(t,y,a,tv),tspan, y0);
figure
plot(t, y)
grid
xlabel('Time')
ylabel('Y')
function v = fun(t,y,a,tv)
a = interp1(tv,a,t);
v = 2*a.*t;
end
That produces a consistent result.
.
  2 件のコメント
Monirul Hasan
Monirul Hasan 2023 年 11 月 10 日
Got it. Thanks you.
Star Strider
Star Strider 2023 年 11 月 10 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by