Getting the error. "index exceeds the number of array elements".
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
AVINASH SAHU
il 30 Lug 2022
Commentato: AVINASH SAHU
il 30 Lug 2022
a =2;
ndiv = 100;
x = linspace(0,1,ndiv);
dx = x(2)-x(1);
for i = 2: (ndiv-1)
h(i) = a - (a-1)*x(i);
dh = (h(i+1)-h(i-1))/(2*dx);
end
0 Commenti
Risposta accettata
Steven Lord
il 30 Lug 2022
When i is 2, you assign to h(2) but then on the next line you attempt to access h(3). That element doesn't exist yet.
I suggest moving the second line of your loop to after the loop and removing its dependence on the loop variable.
x = (1:10).^2
y = x(2:end)+x(1:end-1)
Note that y has one fewer element than x does, so you'll need to handle the edges.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!