Info

This question is closed. Reopen it to edit or answer.

Index exceeds matrix dimensions

1 view (last 30 days)
Aravind Krishnamoorthi
Aravind Krishnamoorthi on 11 Dec 2017
Closed: MATLAB Answer Bot on 20 Aug 2021
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

Answers (1)

Jan
Jan on 11 Dec 2017
Edited: Jan on 11 Dec 2017
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 Comment
Aravind Krishnamoorthi
Aravind Krishnamoorthi on 11 Dec 2017
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.

This question is closed.

Community Treasure Hunt

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

Start Hunting!