I have a higher order system of ode's that I am trying to solve using ode45. I am receiving an error:
Subscript indices must either be real positive integers or logicals.
My two files are below, followed by a complete list of errors.
function xdot = fdynamics(t,y)
F = 0;
T = 0;
m1 = 2;
m2 = 3;
g = 9.81;
l1 = 1;
l2 = 1;
xdot(1) = y(2);
xdot(2) = (F-m2*g*sin(y(3)))/m2 + (l1+y(1))*y(4)^2;
xdot(3) = y(4);
xdot(4) = (T-(2*m2*(l1+y(1)))*y(4)*y(2) - g*cos(y(3))*(m1*(l1/2)+m2*(l1+y(1))) /((1/3)*m1*(l1)^2 + (1/12)*m2*l2^2 + m2(l1+y(1))^2));
xdot = xdot';
end
//AND//
clc
clear
options = odeset('Refine',1','MaxStep',1/60);
[t,y]=ode45(@fdynamics,[0 5],[0; 0; 0; 0],options);
plot(t,y(:,1),'-x')
title('D vs t');
xlabel('Time t');
ylabel('D');
legend('D')
figure
plot(t,y(:,2),'-x')
title('D-Dot vs t');
xlabel('Time t');
ylabel('D-Dot');
legend('D-Dot')
figure
plot(t,y(:,3),'-x')
title('Theta vs t');
xlabel('Time t');
ylabel('Theta');
legend('Theta')
figure
plot(t,y(:,4),'-o')
title('Theta-Dot vs t');
xlabel('Time t');
ylabel('Theta-Dot');
legend('Theta-Dot')
//ERRORS//
Subscript indices must either be real positive integers or
logicals.
Error in fdynamics (line 16)
xdot(4) = (T-(2*m2*(l1+y(1)))*y(4)*y(2) -
g*cos(y(3))*(m1*(l1/2)+m2*(l1+y(1))) /((1/3)*m1*(l1)^2 +
(1/12)*m2*l2^2 + m2(l1+y(1))^2));
Error in ode45 (line 262)
f(:,3) = feval(odeFcn,t+hA(2),y+f*hB(:,2),odeArgs{:});
Error in attempt2 (line 9)
[t,y]=ode45(@fdynamics,[0 5],[0; 0; 0; 0],options);

 Accepted Answer

You’re missing a multiplication operator:
xdot(4) = (T-(2*m2*(l1+y(1)))*y(4)*y(2) - g*cos(y(3))*(m1*(l1/2)+m2*(l1+y(1))) /((1/3)*m1*(l1)^2 + (1/12)*m2*l2^2 + m2*(l1+y(1))^2));
↑ — INSERT MULTIPLICATION OPERATOR HERE
Without the operator (I assume you intend multiplication), MATLAB assumes ‘m2’ is an array, and throws that error.

2 Comments

Thank you, I've been stuck on this system for weeks.
My pleasure.
You have my sympathies. If it makes you feel any better, I’ve had the same sort of problem, and I’m confident many if not most here have.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!