- use intermediary variables to breaks the long expression into smaller expressions.
- can run the code, which requires a realistic set of variable values ( alpha, hmax, etc.)
Index in position 1 exceeds array bounds (must not exceed 1)?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Abdirahman Mohamed
le 10 Oct 2020
Réponse apportée : per isakson
le 10 Oct 2020
When i run my program i get the error "Index in position 1 exceeds array bounds (must not exceed 1)." the error points to the line where i have put the arrow.
I assume the error is related to my v but i dont seem to get the error fixed. Any help would be appreciated
Thanks!
v=[];
v(1:4,1)=0;
for i=1:length(alpha)
steg=alpha(i)*hmax;
t=0:steg:T;
for n=1:(T/steg)
-> v(1:4,n+1)=(idenmat-(steg*theta*A))\(v(1:4,n)+(steg*theta*(quartercar(t(n),v(1:4,n),A,nyak2,c2,m2,H,L,v)+roadprofile(H,L,v,t(n+1),nyak2))));
end
end
2 commentaires
per isakson
le 10 Oct 2020
It's easier to debug code like yours if one
Réponse acceptée
per isakson
le 10 Oct 2020
I replaced the inner for-loop by
for n=1:(T/steg)
f01 = idenmat - steg*theta*A;
f22 = quartercar( t(n), v(1:4,n), A, nyak2, c2, m2, H, L, v );
f23 = roadprofile( H, L, v, t(n+1), nyak2 );
tmp = f01 \ (v(1:4,n)+(steg*theta*( f22 + f23 ) ) );
%
v(1:4,n+1)=(idenmat-(steg*theta*A))\(v(1:4,n)+(steg*theta*(quartercar(t(n),v(1:4,n),A,nyak2,c2,m2,H,L,v)+roadprofile(H,L,v,t(n+1),nyak2))));
end
to make it easier to spot the error and inspect variable values. (My variable names indicate lack of fantacy. )
Next, I turned your script into a function
function cssm
% your script
end
to avoid litter my base workspace (and because I prefer functions)
Next, I enabled Pause on Errors (in the Run menu)
And clicked Run. The execution halted on the line
f22 = quartercar( t(n), v(1:4,n), A, nyak2, c2, m2, H, L, v );
Hovering with the cursor over v(1:4,n) showed that the value of v is a scalar. v is assigned the scalar value at the line
m1=460; m2=60; k1=5500;k2=130000; c1=300; c2=1300; v=60/3.6;
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!