Index in position 1 exceeds array bounds (must not exceed 1)?

1 Ansicht (letzte 30 Tage)
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 Kommentare
per isakson
per isakson am 10 Okt. 2020
It's easier to debug code like yours if one
  • 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.)
Abdirahman Mohamed
Abdirahman Mohamed am 10 Okt. 2020
Sorry here is the complete version of my code:
I have done my best to condense the expression in the second for loop
clear
clc
close all
theta=0.5;
alpha=[];
alpha=[1, 10, 100];
v=[];
v(1:4,1)=0; %initial villkor
steg=0;
T=0.5;
t=[];
m1=460; m2=60; k1=5500;k2=130000; c1=300; c2=1300; v=60/3.6;
H=0.2; L=1;
nyak2=100*k2;
A = [0 0 1 0;0 0 0 1;-k1/m1 k1/m1 -c1/m1 c1/m1;k1/m2 -(k1+nyak2)/m2 c1/m2 -(c1+c2)/m2];
eigen=eig(A); %egenvärde för matrisen B
for i=1:4
emax(i)=-2*real(eigen(i))/(norm(eigen(i))^2);
end
hmax=min(emax); %nya maximala tidssteget
options=odeset('RelTol',1e-9,'AbsTol',1e-9,'Refine',1);
idenmat=[1 0 0 0;0 1 0 0; 0 0 1 0; 0 0 0 1];
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

per isakson
per isakson am 10 Okt. 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;

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by