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

1 次查看(过去 30 天)
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')

回答(2 个)

KSSV
KSSV 2022-3-2
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 2022-3-2
编辑:VBBV 2022-3-2
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

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by