Effacer les filtres
Effacer les filtres

My index starts at the second element. I dont know why

6 vues (au cours des 30 derniers jours)
Oscar Kinsella
Oscar Kinsella le 22 Nov 2023
Below I have attached the code. I am trying to write a script to plot the responses of three different damping ratios on a single degree of freedom system. To me the code looks write with maybe a few errors. However the error I am constantly running into is that even though I have set i = 1, once my for loop starts i becomes = 2. Until the line for i = 1:length(d_ratio), i = 1. i have tested this. But once the for loop starts its first iteration it wont run due the index starting at 2. If someone could explain to me what I am missing that would appreciated.
Given values
% Given Values
d_ratio = [0.05,0.1,0.2]; % Damping Ratios
w_n = 5; % Damped Angular Frequency
% Initial Values
t = 0; % Time
v = 60; % Velocity
x = 0; % Displacement
i=1; t(i)=0; v(i)=60; x(i)=0;
% Acceleration Values (constant)
w_d = sqrt(1 - (d_ratio.^2 * w_n)); % Acceleration equation. Gives a value of acceleration for each ratio value. These values stay constant
% Time Values
h = 0.01; % Time step
total_t = 7; % Total period of time
time_array = 0:h:total_t; % An array which holds all time steps of 0.01 from 0 to 7 seconds
For Loop
for i = 1:length(d_ratio) % index = 3 values of array d_ratio, starting at the first element
C = sqrt(x(i)^2) + ((d_ratio*w_n*x(i)+v(i))/(w_d)); % Amplitude equation
phase = atan2((d_ratio*w_n*x(i)+v(i)), (w_d*x(i))); % Phase equation
x = C*exp(-d_ratio*w_n*t(i)).*cos(w_d*t(i)-phase); % System response equation
% Plotting the responses for the ratios 0.05, 0.1, 0.2
plot(t,x);
hold on;
end
Plot Formatting
xlabel('time (s)');
ylabel('displacement (cm)');
title('Response of the System for the Damping Ratios: 0.05, 0.1, 0.2');
grid on;
hold off;

Réponses (1)

Torsten
Torsten le 22 Nov 2023
Before MATLAB enters the for-loop, t(i), v(i) and x(i) are only defined for i = 1. In the loop, you also don't define t(i), v(i) and x(i) for i>1. This will make MATLAB error for the second loop index (i=2).

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by