Array indices must be positive integers or logical values.

3 ビュー (過去 30 日間)
Angelina
Angelina 2023 年 2 月 21 日
編集済み: Askic V 2023 年 2 月 21 日
I have a function that keeps giving me this error in line 2. I am trying to plot a third order differential equation. I was previously experiencing a different error, but now I have this. I am creating a function and then calling it when I use ode45. Thank you in advance!
function dYdt = problem03ODEFunction(t,y)
x1 = y(t);
X2 = dx1/dt == dy/dt;
X3 = dx2/dt == d2y/dt2;
f(t) = heaviside(t-1) - heaviside(t-10);
dx3dt = d3y/dt3 == f(t) - (19/12)*d2y/dt2 - (19/24)*dydt - (1/8)*y(t);
end
  3 件のコメント
Angelina
Angelina 2023 年 2 月 21 日
The purpose of the second line is to define x1. x1 is used in the following line. Basically the idea is to change the equation from a third order to a second order.
Dyuman Joshi
Dyuman Joshi 2023 年 2 月 21 日
Okay, but what about y and t? What are they supposed to be? y as a function of t? or otherwise? Please specify.

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

回答 (1 件)

Askic V
Askic V 2023 年 2 月 21 日
編集済み: Askic V 2023 年 2 月 21 日
It seems to me that you actually want this:
[t,y]=ode45(@problem03ODEFunction,[0 20],[2 0 0]);
plot(t,y(:,1))
hold on; grid on;
plot(t,y(:,2))
leg = legend('y(t)', '$\frac{dy}{dt}$','interpreter','latex');
leg.FontSize = 12;
function dy = problem03ODEFunction(t,y)
dy = zeros(3,1);
dy(1) = y(2);
dy(2) = y(3);
f = heaviside(t-1) - heaviside(t-10);
dy(3) = f - (19/12)*dy(2) - (19/24)*dy(1) - (1/8)*y(1);
end

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by