ODE error Array indices must be positive integers or logical values.

4 次查看(过去 30 天)
I've been trying to get this working, but every time I run the script I get an error saying "Array indices must be positive integers or logical values and points to the dxdt(2) for the error. This is the code:
t = (0:0.1:5);
condIni = [0;0;0;0;0];
[T,Y] = ode45(@Grua,t,condIni);
x1 = Y(:,1);
x2 = Y(:,2);
x3 = Y(:,3);
x4 = Y(:,4);
plot(t,x1,'-b','linewidth',2)
xlabel('tiempo (s)');
ylabel('desplazamiento (m)');
figure()
plot(t,x2,'-b','linewidth',2)
xlabel('tiempo (s)');
ylabel('Velocidad (m/s)');
figure()
plot(t,x3,'-b','linewidth',2)
xlabel('tiempo (s)');
ylabel('desplazamiento angular (rad)');
figure()
plot(t,x4,'-b','linewidth',2)
xlabel('tiempo (s)');
ylabel('Velocidad angular (rad/s)');
function dxdt = Grua(t,x)
L=3;
m=100;
M=30;
r1=0.05;
r2=0.1;
R=0.15;
ct=0.01;
cp=0.1;
c=1;
K=10;
Kb=9;
Lb=1e-2;
Rb=6.2;
J=1e-3;
E=100;
N=r1/r2;
g=9.81;
dxdt(1) = x(2);
dxdt(2) = (K*L*x(5)-L*ct*x(2)+N*R*cp*x(4)-L*N^2*R^2*c*x(2)+L*R*N*m*g*x(3))/(L(M*N^2*R^2+J));
dxdt(3) = x(4);
dxdt(4) = ((-cp*x(4)-m*g*L*x(3))/L^2) -((m*R*L*N)/L^2)*((K*L*x(5)-L*ct*x(2)+N*R*cp*x(4)-L*N^2*R^2*c*x(2)+L*N*R*g*m*x(3))/(L(M*N^2*R^2+J)));
dxdt(5) = (E-Kb*x(2)-x(5)*Rb)/Lb;
dxdt = dxdt';
end

采纳的回答

Ameer Hamza
Ameer Hamza 2020-11-1
You missed the multiplication operator in the denominator
dxdt(2) = ../(L*(M*N^2*R^2+J));
%^ this is missing
dxdt(4) = ../(L*(M*N^2*R^2+J)));
%^ this is missing

更多回答(1 个)

KSSV
KSSV 2020-11-1
The indices for an array in MATLAB should be posititve integers or logicals. If you give fractions, 0, negative as index it will throw error.
Example:
A = rand(1,10) ;
A(1) % no error
A(10) % no error
A(0) % error as index cannot be zero
A(-1) % error, as index cannot be negative
A(0.5) % error, as index cannot be fraction
idx = A<0.5 ;
A(idx) % idx has 0, 1 they are logicals, so allowed
In your case..check where the index is taking not allowed value. Learn to debug your code...Read about debugging.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by