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

1 回表示 (過去 30 日間)
RAJKUMAR SAHA
RAJKUMAR SAHA 2022 年 3 月 2 日
コメント済み: VBBV 2022 年 3 月 3 日
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
  2 件のコメント
RAJKUMAR SAHA
RAJKUMAR SAHA 2022 年 3 月 2 日
Thank you
VBBV
VBBV 2022 年 3 月 3 日
Thanks is accepting answer. :)

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by