Issue with my nested loop- taking forever to run
Show older comments
Hi All
Unfortunately I am having a few issues with my nested loop. Fundamentally the outputs in the workspace show that I am getting the ouputs of the right dimension. However my loop is taking forever to run and often I have had to cancel it- with it taking over 40mins !! It shouldn't be taking this long and I am wondering if my loop logic is not quite correct.
I have tried multiple things- such as changing index and order of some calculations.
Overall I am calculating my A matrix which is a 2*2 matrix and contains variables. I have calculated 40 sets of these 2*2 matrices and wish to save them into an array.
Then I am using the RK4 loop to calculate displacement for the acceleration and finding the peak displacement. So I want to get 40 different peak displacements with my associated state-space 2*2 A matrix.
Is there something wrong with my logic? as it is not outputting this and is taking forever to run.
Thanks in advance!!
dt_interp=0.0001;
t_interp=dt_interp:dt_interp:tf;
ft_interp=F_interp(t,ft,t_interp);
NPTS_interp=size(t_interp,2);
yt=zeros(2,NPTS_interp);
for T0=0.1:0.1:4;
for i=1:(NPTS_interp-1);
w=(2*pi)/T0;
w2=w*w;
zita=0.05;
m=773;
A1=[0 1 ; -w2 -(2*zita*w)];
A=A1(:,:);
b=[0
1/m];
g1=A(:,:)*yt(:,i)+b*ft_interp(i);
g2=A(:,:)*(yt(:,i)+0.5*g1*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g3=A(:,:)*(yt(:,i)+0.5*g2*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g4=A(:,:)*(yt(:,i)+g3*dt_interp)+b*ft_interp(i+1);
ynew=yt(:,i)+(g1+g2+g3+g4)*dt_interp/6;
yt(:,i+1)=ynew;
ut=yt(1,:);
vt=yt(2,:);
Sd_max(T0)=max(ut);
end
end
Maximum_displacement=Sd_max(1,:);
figure(1)
plot(T0,Maximum_displacement);
grid on
grid minor
xlabel('Time Period (s)');
ylabel('Peak Displacement (m)');
title('Response Spectrum for Earthquake 1');
function a=F_interp(time, F, t)
a = interp1(time, F, t);
end
3 Comments
Badly aligned code is one way that beginners hide mistakes in their code. You should use the default alignment of the MATLAB editor, then it is a lot clearer how the code is structured:
dt_interp = 0.0001;
t_interp = dt_interp:dt_interp:tf;
ft_interp = F_interp(t, ft, t_interp);
NPTS_interp = size(t_interp, 2);
yt = zeros(2, NPTS_interp);
for T0 = 0.1:0.1:4;
for i = 1:(NPTS_interp - 1);
w = (2 * pi) / T0;
w2 = w * w;
zita = 0.05;
m = 773;
A1 = [0 1; - w2 - (2 * zita * w)];
A = A1(:, :);
b = [0
1 / m];
g1 = A(:, :) * yt(:, i) + b * ft_interp(i);
g2 = A(:, :) * (yt(:, i) + 0.5 * g1 * dt_interp) + 0.5 * b * (ft_interp(i) + ft_interp(i + 1));
g3 = A(:, :) * (yt(:, i) + 0.5 * g2 * dt_interp) + 0.5 * b * (ft_interp(i) + ft_interp(i + 1));
g4 = A(:, :) * (yt(:, i) + g3 * dt_interp) + b * ft_interp(i + 1);
ynew = yt(:, i) + (g1 + g2 + g3 + g4) * dt_interp / 6;
yt(:, i + 1) = ynew;
ut = yt(1, :);
vt = yt(2, :);
Sd_max(T0) = max(ut);
end
end
Maximum_displacement = Sd_max(1, :);
figure(1)
plot(T0, Maximum_displacement);
grid on
grid minor
xlabel('Time Period (s)');
ylabel('Peak Displacement (m)');
title('Response Spectrum for Earthquake 1');
function a = F_interp(time, F, t)
a = interp1(time, F, t);
end
Serena Solanki
on 23 Feb 2018
Stephen23
on 24 Feb 2018
@Serena Solanki: is that really the code that you are running? Apparently you are using fractional values as indices, and I have never seen that work before:
for T0 = 0.1:0.1:4;
for i = ...
...
Sd_max(T0) = max(ut);
end
end
"Is there any way i can save my A- matrix data into an array?"
Of course: preallocate an array of the correct size, and then use indexing. As you do not really explain the algorithm and the code above has mistakes in it I am not sure what changes you need to make.
Answers (0)
Categories
Find more on Code Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!