James
James

Reputation: 127

How to setup multiple for loops together?

I am trying to understand the placement of the for loops to have them in the proper order. I am unsure of where to put the j loop in this code. The goal of that loop is to have the velocity u change over time using u=2*sin(2*pi*T). This u is placed in the upwind 1D advection equation, which is then plotted to show what happens over time. As of right now, with the current setup, the plot is stationary at the initial position. Could anyone provide some helpful tips on how the for loops need to be sequenced? Thank you.

clear;
clc;
%Set initial values
xmin=0; 
xmax=1;
N=101; %Amount of segments
dt= 0.0001; % Time step
t=0; % t initial
tmax=2; % Run this test until t=2
T=t:dt:tmax
u=2*sin(2*pi*T); %Velocity

dx = (xmax - xmin)/100; %finding delta x from the given information
x =xmin-dx : dx : xmax+dx; %setting the x values that will be plugged in

h0= exp(-(x- 0.5).^2/0.01); %Initial gaussian profile for t=0
h = h0;
hp1=h0;
nsteps =tmax/dt; % total number of steps taken
for n=1 : nsteps
    h(1)=h(end-2); %Periodic B.C
    h(end)=h(2);

        for i =2 : N+1 
            for j=1:nsteps
                if u>0            
                hp1(i) = h(i) - 2*sin(2*pi*T(j))*dt/dx *( h(i)-h(i-1)); %Loop to solve the FOU 
            elseif u<0            
                hp1(i) = h(i) - 2*sin(2*pi*T(j))*dt/dx*(h(i+1)-h(i)); %downwind
                end
            end
        end

    t=t+dt; %Increase t after each iteration
    h= hp1; %have the new hp1 equal to h for the next step
    initial= exp(-(x- 0.5).^2/0.01); % The initial plot when t =0
    %hold on

    %plot(x,initial,'*') %plot initial vs moving
    plot(x,h,'o-')
    pause(0.001);
    %hold off


    %plot(x,initial) %plot end value
end

Upvotes: 0

Views: 127

Answers (1)

il_raffa
il_raffa

Reputation: 5190

I'm not in the postion to assess if you have implemented the algorithm in the right way, so I can not say if the sequence of for loops and the computations you make inside them is right, nevetheless there is a mistake in the way you've defined the if section.

Consider that the variable u is actually an array.

In your if section:

 if u>0
    hp1(i) = h(i) - 2*sin(2*pi*T(j))*dt/dx *( h(i)-h(i-1)); %Loop to solve the FOU
 elseif u<0
    hp1(i) = h(i) - 2*sin(2*pi*T(j))*dt/dx*(h(i+1)-h(i)); %downwind
 end

you test the whole array so the result, being u an array, is an array of logical (0 1) values (one for each element of the array u); this makes the if not able to catch the condition you look for.

You should modify the if section in order that just one element of u is tested at each iteration notice the if u(j)>0 isntead of if u>0 (same for the else):

 if u(j)>0
    hp1(i) = h(i) - 2*sin(2*pi*T(j))*dt/dx *( h(i)-h(i-1)); %Loop to solve the FOU
 elseif u(j)<0
    hp1(i) = h(i) - 2*sin(2*pi*T(j))*dt/dx*(h(i+1)-h(i)); %downwind
 end

now, at each ieteration the j-th value of the array u is tested.

Again, I'm not in the position to judge if the algorithm is correct, so I can not say if this simple modification will solve the problem.

The following is the figure I've got after the first iteration of the outer loop having modified the if condition as described above.

enter image description here

Upvotes: 1

Related Questions