Clear Filters
Clear Filters

'Index exceeds the number of array elements ' why this error is showing?

1 view (last 30 days)
function dydt = my_ode_without_tld(i,y)
% tspan = i; %% Time span
%% Initial inputs
m1 = 10; %% mass of the structure(kg)
k1 = 15; %% stiffness of the structure(N/m)
c1 = 5; %% damping of the structure(Ns/m)
A = 0.05; %% Amplitude of ground displacement(m)
w = 6.981; %% Input natural frequencycy(rad/s)
%% define frequency
w1 = sqrt(k1/m1); %% frequency of structure
%% define damping ratio of structure
r1 = c1/(2*m1*w1);
%% define ground acceleration
ugdd = -A*w^2*sin(w*i);
%% Equation to solve
dydt = [y(4)
(-ugdd-(2*r1*w1*y(4))-((w1)^2*y(3)))];
%% To run mass spring damper system
i = 0:0.01:20;
y = [0 0];
%% Solve using ode45
[tsol,ysol] = ode45('my_ode_without_tld', i, y,[1 0;0 1]);
%% plotting
plot(tsol,ysol(:,1))
xlabel('time(sec)')
ylabel('displacement(m)')
grid on
title('Displacement response of structure')
figure
plot(tsol,ysol(:,2))
xlabel('time(sec)')
ylabel('velocity(m/s)')
grid on
title('Velocity response of structure')

Answers (2)

KSSV
KSSV on 2 Mar 2022
This line:
dydt = [y(4)
(-ugdd-(2*r1*w1*y(4))-((w1)^2*y(3)))];
expects y to be 1x4 vector, but your y is 1x2.

VBBV
VBBV on 2 Mar 2022
Edited: VBBV on 2 Mar 2022
I = 0:0.1:20;
y = [0 0];
%% Solve using ode45
[tsol,ysol] = ode45(@my_ode_without_tld,[0 10],[0;1]);
%% plotting
plot(tsol,ysol(:,1))
xlabel('time(sec)')
ylabel('displacement(m)')
grid on
title('Displacement response of structure')
figure
plot(tsol,ysol(:,2))
xlabel('time(sec)')
ylabel('velocity(m/s)')
grid on
title('Velocity response of structure')
function dydt = my_ode_without_tld(I,y)
% tspan = i; %% Time span
%% Initial inputs
m1 = 10; %% mass of the structure(kg)
k1 = 15; %% stiffness of the structure(N/m)
c1 = 5; %% damping of the structure(Ns/m)
A = 0.05; %% Amplitude of ground displacement(m)
w = 6.981; %% Input natural frequencycy(rad/s)
%% define frequency
w1 = sqrt(k1/m1); %% frequency of structure
%% define damping ratio of structure
r1 = c1/(2*m1*w1);
%% define ground acceleration
ugdd = -A*w^2*sin(w*I);
%% Equation to solve
dydt = [y(2) -ugdd-(2*r1*w1*y(2)-((w1)^2*y(1)))].';
%% To run mass spring damper system
end

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!