Index in position 2 exceeds array bounds
2 ビュー (過去 30 日間)
古いコメントを表示
Have a function to model a quarter-car suspension system and am attempting to solve values at differnet stiffnesses adjusting values to use different functions and compare their pros/cons. I keep getting the error that the 'Index in position 2 exceeds array bounds.'
Arms -> Root mean square of acceleration
Vh=30*0.44704;B=2200;K2=10440;K1=5410;t=[0, 20];M=345;m=20;
%0.44704 is conversion from mph to m/s
opts=odeset('MaxStep',0.1);
[T,X]=ode23s(@(t,x)p5fun(t,x,Vh,B,K1,K2),[0 20],[0,0,0,0],opts);
y=X(:,1);z=X(:,2);vy=X(:,3);vz=X(:,4); %Asssigning the columns in X to corresponding values
figure(1)
subplot(2,2,1);plot(T,y)
subplot(2,2,2);plot(T,z)
subplot(2,2,3);plot(T,vy)
subplot(2,2,4);plot(T,vz)
dotvy=(K2/M)*(z-y)+(B/M)*(vz-vy); %Equation for vertical acceleration
figure(2)
plot(T,dotvy);
ymax=max(abs(y)); %Finding the maximum value for displacement
Arms=sqrt((1/20)*trapz(T,dotvy.^2)); %Equation to describe the root mean square of acceleration
fprintf('The max vertical displacement is %g metres. \n',ymax)
fprintf('The root mean square of acceleration is %g m/s^2.\n',Arms)
Vht=[10:5:80]*0.44704; %Range of velocity values
for k=1:length(Vht)
[T1,X1]=ode23s(@(t,x)p5fun(t,x,Vht(k),B,K1,K2),[0 20],[0,0,0,0],opts); %Solve the equation for the kth values of Vht
[ymax1(k),Arms1(k)]=p5_3fun(Vh,B,K1,K2);
end
figure(3);plot(Vht,ymax1)
figure(4);plot(Vht,Arms1)
B1o=[1000:200:4000];
K2o=(5000:1000:15000);
for p=1:length(K2o)
[T1,X1]=ode23s(@(t,x)p5fun(t,x,Vh,B,K1,K2o(p)),[0 20],[0,0,0,0],opts);
[ymax2(p),Arms2(p)]=p5_3fun(Vh,B,K1,K2o(p));
end
figure(5); plot(K2o,ymax2)
figure(6);plot(K2o,Arms2)
for q=1:length(B1o)
[T1,X1]=ode23s(@(t,x)p5fun(t,x,Vh,B,K1,K2),[0 20],[0,0,0,0],opts);
[ymax3(q),Arms3(q)]=p5_3fun(Vh,B1o(q),K1,K2);
end
figure(7);plot(B1o,ymax3)
figure(8);plot(B1o,Arms3)
B1o=[1000:200:4000];
K2o=(5000:1000:15000);
for p=1:lgenth(K2o)
for q=1:length(B1o)
[T1,X1]=ode23s(@(t,x)p5fun(t,x,Vh,B1o(q),K1,K2(p),[0 20],[0,0,0,0],opts));
[ymax4(q,p),Arms4(q,p)]=p5_3fun(Vh,B1o(q),K1,K2o(p));
end
end
figure(9);mesh(K2o,B1o,ymax4)
figure(10);mesh(K2o,B1o,Arms4)
function u=roadprofile(L)
if (L>2) && (L<=2.5)
u=-0.2*(L-2); %The decreasing gradient for the car in the chuck hole
elseif (L>2.5) && (L<=3.5)
u=-0.1; %Height for car in chuck hole
elseif (L>3.5) && (L<=4)
u=0.2*(L+4); %Increasing gradient of car leaving chuck hole
elseif (L>4) && (L<=100)
u=0;
elseif (L>100) && (L<=104)
u=-0.15*sin((pi/4)*L-(28*pi)); %At the bump, this function describes the cars change in height
else
u=0; %If the value of L falls out of any restrictions as stated above
end
end
function dotx=p5fun(t,x,Vh,B,K1,K2)
L=Vh*t; u=roadprofile(L); M=345;m=20;
dotx=[x(3);x(4);
(K2/M)*(x(2)-x(1))+(B/M)*(x(4)-x(3));
(K1/m)*(u-x(2))-(K2/m)*(x(2)-x(1))-(B/m)*(x(4)-x(3))];
end
function [ymax,Arms]=p5_3fun(Vht,B,K1,K2)
global X1 T1
M=345;
dotvy1=(K2/M).*(X1(:,2)-X1(:,1))+(B/M).*(X1(:,4)-X1(:,3));
ymax=max(abs(X1(:,1)));
Arms=sqrt((1/20)*trapz(T1,dotvy1.^2));
end
The error is shown below. It feels like something really simple but I can't figure it out. I understand I have a fucntion for ymax and Arms, and also a section dedicated to calculating these values. I was asked to do both.
Index in position 2 exceeds array bounds.
Error in the_last_one_pls>p5_3fun (line 82)
dotvy1=(K2/M).*(X1(:,2)-X1(:,1))+(B/M).*(X1(:,4)-X1(:,3));
Error in the_last_one_pls (line 23)
[ymax1(k),Arms1(k)]=p5_3fun(Vh,B,K1,K2);
Tried to fix it by declaring p5_3fun as below, but this presents the error that I am out of memory.
function [ymax,Arms]=p5_3fun(Vht,B,K1,K2)
%global X1 T1
M=345;
options =odeset('MaxStep',0.1);
[T1,X1]=ode23s(@(t,x)p5_3fun(Vht,B,K1,K2),[0 20],[0,0,0,0],options);
dotvy1=(K2/M).*(X1(:,2)-X1(:,1))+(B/M).*(X1(:,4)-X1(:,3));
ymax=max(abs(X1(:,1)));
Arms=sqrt((1/20)*trapz(T1,dotvy1.^2));
end
0 件のコメント
回答 (1 件)
Guillaume
2020 年 3 月 16 日
The error message is clear, you're using an index greater than the size of the 2nd dimension of an array. The only indexed array in your expression is X1 and your index goes up to 4, so clearly X1 has less than four columns.
Unfortunately, your X1 is global. We strongly advise against using global variables, they're the source of many problems such as this one. It's unclear where X1 is created, nowhere in the code you show so possibly it's never created and hence X1 is empty which would explain the error.
Your fix change the function into a never-ending recursive function, which tries to solve an ode with itself as the ode, so yes it's not going to end well. I'm a bit unclear why you tried that. Your original p5_3fun doesn't involve ODEs at all.
I've not really tried to understand what the rest of your code is doing, just looking at it, you could just replace the original p5_3fun by:
function [ymax,Arms]=p5_3fun(Vht,B,K1,K2, T1, X1) %just add T1 and X1 as inputs
M=345;
dotvy1=(K2/M).*(X1(:,2)-X1(:,1))+(B/M).*(X1(:,4)-X1(:,3));
ymax=max(abs(X1(:,1)));
Arms=sqrt((1/20)*trapz(T1,dotvy1.^2));
end
and add T1 and X1 to the input list every time you call p5_3fun, eg.:
for p=1:length(K2o)
[T1,X1]=ode23s(@(t,x)p5fun(t,x,Vh,B,K1,K2o(p)),[0 20],[0,0,0,0],opts);
[ymax2(p),Arms2(p)]=p5_3fun(Vh,B,K1,K2o(p), T1, X1); %now with T1 and X1 as inputs
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Particle & Nuclear Physics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!