how do I make a for loop

조회 수: 1 (최근 30일)
Abed Eltablawy
Abed Eltablawy 2021년 4월 8일
댓글: Abed Eltablawy 2021년 4월 8일
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
Image Analyst
Image Analyst 2021년 4월 8일
편집: Image Analyst 2021년 4월 8일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2021년 4월 8일
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
Abed Eltablawy
Abed Eltablawy 2021년 4월 8일
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 2021년 4월 8일
thank you for answers I really appriciate that

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by