how do I make a for loop

3 views (last 30 days)
Abed Eltablawy
Abed Eltablawy on 8 Apr 2021
Commented: Abed Eltablawy on 8 Apr 2021
Hi ,
i want to code some equation to calculate displacement , velocity and acceleration but the thing is that I need to use a for loop to calculate many times and the equation used in calculations depend on the output and my first out put must be zero I can't solve this problem
% response spectrum
% response spectrum
[kobe]=xlsread("Kobe.xlsx.xlsx")
[dt]=kobe(:,1);
[E ]=kobe (:,2);
[T ]= (0.1:0.02:8);
M = 4./(dt).^2
N = 4*pi^2 ./(T).^2;
H= 8*pi*0.05./(T);
for j = 1:length(T)
[T ]= (0.1:0.02:8);
dis = dis(j);
vel = vel(j);
acc = acc(j);
if (j)==1
dis = 0; % the first input
vel = 0; % the first input
acc = 0; % the first input
end
delta_dis = - (E) + M * vel +2* acc +H * vel / N + H ./(dt) + M;
delta_vel = 2./(dt) * delta_dis - 2* vel;
delta_acc = (delta_dis - vel.*(dt))*4./(dt).^2 - 2 *acc;
dis = dis +delta_dis; % after the first loop the new value for dis,vel and acc
vel = vel + delta_vel; % and I need to put this new value in the loop
acc = acc + delta_acc;
end
figure (1) ; % i want to plot the maximum values
subplot (3,1,1);
plot (T , max(dis));
subplot (3,1.2);
plot (T , max(vel));
subplot (3,1,3);
plot (T , max(acc));
  1 Comment
Image Analyst
Image Analyst on 8 Apr 2021
Edited: Image Analyst on 8 Apr 2021
People are probably waiting for you to attach the "Kobe.xlsx.xlsx" that you forgot to attach.
Also, type control-a, control-i in MATLAB before you paste it here so that the indenting will be correct.

Sign in to comment.

Answers (1)

Jan
Jan on 8 Apr 2021
You are mixing the values of the former step, the current step and the vector of outputs. Without clear comments or an exhaustive explanation of what you want to do, it is not possible to guess the intention of the code reliably. Some guessing:
kobe=xlsread("Kobe.xlsx.xlsx")
dt= kobe(:,1);
E = kobe(:,2);
T = 0.1:0.02:8;
M = 4 ./ dt.^2
N = 4 * pi^2 ./ T.^2;
H = 8 * pi * 0.05 ./ T;
dis = 0; % Initial value?
vel = 0; % Initial value?
acc = 0; % Initial value?
% Maybe:
disOut = zeros(1, length(T));
velOut = zeros(1, length(T));
accOut = zeros(1, length(T));
for j = 1:length(T)
% dis = dis(j); % This has not been defined before?
% vel = vel(j);
% acc = acc(j);
delta_dis = -E + M * vel + 2 * acc + H * vel / N + H ./ dt(j) + M;
% Sure that you mean delta_dis in these two lines?
delta_vel = 2 ./ dt * delta_dis - 2 * vel;
delta_acc = (delta_dis - vel * dt(j)) * 4 ./ dt(j) .^ 2 - 2 * acc;
dis = dis + delta_dis; % after the first loop the new value for dis,vel and acc
vel = vel + delta_vel; % and I need to put this new value in the loop
acc = acc + delta_acc;
disOut(j) = dis;
velOut(j) = vel;
accOut(j) = acc;
end
  2 Comments
Abed Eltablawy
Abed Eltablawy on 8 Apr 2021
ok I will explain in more details .
I have variable (T= 0.1:0.02:8) and i have data for dt and E for each number of T I need to compute 3 variables dis,vel and acc. i will have too meny values for each of them so , I will pick the max for each value of T and the three variables
doing this for each no. of T and plot graphs between T and maxdis , T and maxvel , T and maxacc
Abed Eltablawy
Abed Eltablawy on 8 Apr 2021
thank you for answers I really appriciate that

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!