Homogenuous differential equation with conditions

1 view (last 30 days)
Find and draw the first 22 terms of the solution of the homogeneous differential equation with the initial conditions: y[−1] = 0.6, y[−2] =
-8.2.
I dont understand what went wrong here?Dont understand how this work.
j=22;
y=[-8.2,0.6];
for i= -8.2:j+0.6
y(i)= 0.19*y(i-1)-0.36*y(i-2)+(0.31)^(i-3);
end
k=[-2:21];
stem(k,y);
"Array indices must be positive integers or logical values" problem

Answers (1)

Dyuman Joshi
Dyuman Joshi on 31 Jan 2023
Edited: Dyuman Joshi on 31 Jan 2023
j=22;
y=[-8.2,0.6];
As you can see below, you are using non integer values as an index, which is not possible.
i = -8.2:j+0.6
i = 1×31
-8.2000 -7.2000 -6.2000 -5.2000 -4.2000 -3.2000 -2.2000 -1.2000 -0.2000 0.8000 1.8000 2.8000 3.8000 4.8000 5.8000 6.8000 7.8000 8.8000 9.8000 10.8000 11.8000 12.8000 13.8000 14.8000 15.8000 16.8000 17.8000 18.8000 19.8000 20.8000
MATLAB accepts only Natural numbers as index values. [1 2 3 4 ....] (or logical values, as the error clearly states)
Change the loop variable values
for i=3:22
y(i)= 0.19*y(i-1)-0.36*y(i-2)+(0.31)^(i-3);
end
k=[-2:19];
stem(k,y)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!