Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Index exceeds matrix dimensions

1 回表示 (過去 30 日間)
Aravind Krishnamoorthi
Aravind Krishnamoorthi 2017 年 12 月 11 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello I am working on a double inverted pendulum. I come up with this error 'index exceeds Matrix Dimensions' Error in Test_1>pend (line 8) x3=x(3);
function pend1
pendsss
end
function dx= pend(t,x)
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
% parameters
m1=1;
m2=1;
a1=0.5;
L1=1;
a2=0.5;
L2=1;
g=9.8;
I1=0.0126;
I2=0.0185;
d1=I1+a1+a1+m1+L1*L1*m2;
d2=a2*L1*m2;
d3=(a1*m1+L1*m2)*g;
d4=a2*L1*m2;
d5=a1*m1+L1*m2;
d6=I2+a2*a2*m2;
d7=a2*m2*g;
d8=a2*m2;
e1=0.015;
e2=0.015;
D=[d1 d2*cos(x1-x3);d4*cos(x1-x3) d6];
C=[d4*sin(x1-x3)*x4;-d4*sin(x1-x3)*x2];
G=[d3*sin(x1);d7*sin(x3)];
F=[-d5*cos(x1);-d8*cos(x3)];
I= inv(D);
dx=I*(-C-G+F);
end
function pendsss
tspan=[0 1]; %time span
ic=[0;pi];%initial conditions
[t,x]=ode45(@pend,tspan,ic);
plot (t,x),
grid on
xlabel('Time(s)');
legend('theta1', 'theta2')
end

回答 (1 件)

Jan
Jan 2017 年 12 月 11 日
編集済み: Jan 2017 年 12 月 11 日
If the initial conditions have 2 components "ic=[0;pi]", why do you think, that the state variable has 4? The size of x in pend is the same as the size of the initial conditions.
By the way: This is a bad idea:
I = inv(D);
dx = I*(-C-G+F);
As explained in doc inv, calculating the inverse explicitly is slower and less accurate than using the slash operator:
dx = D \ (-C -G + F)
  1 件のコメント
Aravind Krishnamoorthi
Aravind Krishnamoorthi 2017 年 12 月 11 日
So if i give the initial conditions vector as ic=[0;pi;-pi;0] would that be good enough.
And thank you for the idea.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by