I get this error: Attempted to access l(0); index must be a positive integer or logical.

1 view (last 30 days)
if true
% code
end
function [] = pend()
clc
p = setpar();
tend= 5;
[t1 y1]= ode23(@plenght,[0 tend],[p.n0 p.l0 p.Q0 p.q0],[],p);
plot(t1,y1(:,1),'m-',t1,y1(:,2),'k',t1,y1(:,3),'r-',t1,y1(:,4),'b');
xlabel('Time [sec]');
legend('dL','L','dQ','Q');
function[p]=setpar()
p.g= 10.0;
p.m=0.1;
p.C=3.0;
p.L0=0.5;
p.n0=0;
p.l0=1;
p.Q0=0;
p.q0=30;
function[dw]= plenght(t,len,p)
n=len(1);
l=len(2);
Q=len(3);
q=len(4);
dn =1/p.m*((p.m*p.g*cos(q))-(p.C*l)+(p.C*p.L0)+(p.m*l(Q)^2));
dl=n;
dQ= -1/l*(p.g*sin(q)+2*n*Q);
dq=Q;
dw=[dn;dl;dQ;dq];

Answers (1)

Geoff Hayes
Geoff Hayes on 6 Nov 2014
Omar - the line of code that is causing the problem is
dn =1/p.m*((p.m*p.g*cos(q))-(p.C*l)+(p.C*p.L0)+(p.m*l(Q)^2))
because Q happens to be zero and the code is trying to access the zeroth element of l at
p.m*l(Q)^2
Given your use of l and Q elsewhere in the code, I think that you know that l is not an array, and so are just missing an operation between the l and the (Q)^2. Please review the
l(Q)^2
and decide how best it should be corrected.

Community Treasure Hunt

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

Start Hunting!