Why do I get "Array indices must be positive integers or logical values" when I am trying to plot my functions?
Mostra commenti meno recenti
figure(1)
alpha=0.89;
beta=0.89;
gamma=0.5;
c(0)=1;
i(0)=1;
y(0)=2;
for t=1:50
y(t) = c(t) + i(t);
c(t) = alpha*y(t - 1);
i(t) = beta*(c(t) - c(t - 1)) + gamma;
end
plot(y,'LineWidth',2)
hold on
plot(c,'LineWidth',2)
plot(i,'LineWidth',2)
xlabel('Time period')
ylabel('BKT (M€)')
Hey! I am trying to plot this, but for some reason I get the message "Array indices must be positive integers or logical values". What seems to be the problem in my code?
Risposta accettata
Più risposte (2)
Austin M. Weber
il 31 Gen 2024
Modificato: Austin M. Weber
il 31 Gen 2024
In MATLAB, indexing starts at 1, so you cannot have C(0), i(0), or y(0). Rather, it needs to be C(1), i(1), or y(1).
EDIT: @Mathieu NOE is correct, in addition to what I said, that also means that you need to start your for-loop at index position 2 instead of position 1. Otherwise, when your script gets to this line:
for t = 1:50
c(t) = alpha*y(t - 1);
You will still get an error because y(t-1) would be the same as y(0) in the first iteration of the loop. So, you have to start the loop at t = 2:50:
for t = 2:50
1 Commento
Jenni
il 31 Gen 2024
Image Analyst
il 31 Gen 2024
1 voto
Categorie
Scopri di più su Matrices and Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!