MATLAB: Index exceeds Matrix Dimensions

MATLABode45

I'm trying to solve a differential equation using ode45, its working fine with the 2 DOF system but keeps giving "index exceeds matrix dimensions" for a 4 DOF system
function y_dot = ode_sin_roll (t,y)
global A U itv w
y_dot = zeros(4,1);
if t<itv
y_dot = A*[y(1) ; y(2) ; y(3) ; y(4)] + U*(0.1*(sin(w*t))^2);
else
y_dot = A*[y(1) ; y(2) ; y(3) ; y(4)];
end
clear all; close all; clc
filename = 'VD_A4_D2.xlsx';
sheet = 1;
range = 'AN4:BC14';
DATA = xlsread(filename, sheet, range);
vx = [1; 5; 10; 15; 20; 25; 30; 35; 40; 45; 50];
for i = (1:length(DATA))
A11 = DATA(i,1);
A12 = DATA(i,2);
A13 = DATA(i,3);
A14 = DATA(i,4);
A21 = DATA(i,5);
A22 = DATA(i,6);
A23 = DATA(i,7);
A24 = DATA(i,8);
A31 = DATA(i,9);
A32 = DATA(i,10);
A33 = DATA(i,11);
A34 = DATA(i,12);
A41 = DATA(i,13);
A42 = DATA(i,14);
A43 = DATA(i,15);
A44 = DATA(i,16);
U11 = 353.5;
U21 = -276.9;
U31 = 0;
U41 = 1300.5;
global A U itv w
A = [ A11 A12 A13 A14; A21 A22 A23 A24; A31 A32 A33 A34; A41 A42 A43 A44]; %A Matrix of Constants
U = [ U11 ; U21 ; U31 ; U41 ]; % U Vector of constants
itv = 100/vx(i,1);
w = pi*(1/itv);
[Ts, Ys] = ode45('ode_sin_roll', [0 10], [0 0]);
plot(Ts, Ys(:,1))
xlabel('Time (s)')
ylabel('v_y(m/s)')
hold on
end
legend1 = legend('1','5','10','15','20','25','30','35','40','45','50');

Best Answer

In your
[Ts, Ys] = ode45('ode_sin_roll', [0 10], [0 0]);
line, the [0 0] is the initial values for the y that will be passed in to your ode function. If you want your y to have 4 elements then you need to pass in something that is 4 elements long instead of [0 0]