Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Write a MatLab code for the following problem. Consider a 2D problem that models a pipe.…

  1. User

    Write a MatLab code for the following problem.

    Consider a 2D problem that models a pipe. In the y-direction, there is a finite width that can be specified by the user. In the x-direction, the system is not bounded. Start a random walker at the origin in the x-direction, and in the middle of the pipe along the width of the pipe in the y-direction. The random walker can only step between vertices on a regular square grid. The spacing between vertices is called the “lattice constant”. At each time step, the random walker can wait in its current location with probability "pw" and with "1 − pw" probability make a step. The step can be to the right, up, left, down, with probabilities of 1/4 for each direction. Assigning equal probabilities for each possible direction indicates that the material has isotropic transport properties. Use reflective boundary conditions in the y-direction at the edge of the pipe. The lattice constant, a, that gives the length of a step when the walker moves, and the time, t, for each time step must be set. In this first problem use pw = 0. Simulate the random walk as just described. In this example, set the width of the pipe to be 5 mm and set a = 0.01 mm.

    Note the two formulas: R"(n) = na^2 and R"(t) = DT. The first formula relates the mean squared displacement over many unrestricted random walks directly in terms of the number of random walk steps, given by n. The second formula expresses the mean displacement of a particle that is unrestricted in terms of continuous time, where it can be assumed that T = tn. Express the diffusion constant in terms of the model parameters {a,t}.

    1. Visualize the random walk trajectories
    2. In the x-direction, calculate the mean squared displacement as a function of number of steps.
    3. In the y-direction, calculate the mean squared displacement as a function of number of steps.
    4. For 1,000,000 walks, plot the histogram of locations of the random walker in the x-direction after 100, 1000, 10000, 100000, 1000000 steps.
    5. For 1,000,000 walks, plot the histogram of locations of the random walker in the y-direction after 100, 1000, 10000, 100000, 1000000 steps.
    6. In order to obtain a diffusion constant of 10^-7 cm^2/sec, what should the average time per step be (given by t) in units of seconds?
    7. Working in the x-direction only, find the diffusion constant, D by taking the slope of the mean squared displacement as a function of time.
  2. Assistant

    Here is an example MATLAB code that solves the problem:

    % Input parameters
    width = 5; % width of the pipe in mm
    a = 0.01; % lattice constant in mm
    pw = 0; % probability of waiting
    num_steps = 1000000; % number of steps for histograms
    num_walks = 1000000; % number of random walks for histograms
    
    % Calculate lattice size based on width
    lattice_size = round(width / a);
    
    % Initialize random walker position
    x = 0;
    y = lattice_size/2;
    
    % Initialize mean squared displacement variables
    msd_x = [];
    msd_y = [];
    
    % Loop over the number of steps
    for step = 1:num_steps
        % Determine if walker waits or makes a step
        if rand <= pw
            % Walker waits
            continue;
        else
            % Walker makes a step
            % Calculate the step direction
            dx = randi([-1, 1]);
            dy = randi([-1, 1]);
            
            % Update walker position
            x = x + dx;
            y = y + dy;
            
            % Reflect at the y-direction boundary
            if y < 1
                y = 2-y;
            elseif y > lattice_size
                y = 2*lattice_size-y-1;
            end
            
            % Calculate mean squared displacement in each direction
            msd_x(step) = x^2;
            msd_y(step) = (y-(lattice_size/2))^2;
        end
    end
    
    % 1) Visualize the random walk trajectories
    figure;
    plot(x, y, 'b.')
    title('Random Walk Trajectories');
    xlabel('x');
    ylabel('y');
    
    % 2) Mean squared displacement in the x-direction
    figure;
    n_steps = 1:num_steps;
    msd_x = [0, msd_x];
    plot(n_steps, msd_x, 'b')
    title('Mean Squared Displacement in the x-Direction');
    xlabel('Number of Steps');
    ylabel('Mean Squared Displacement');
    
    % 3) Mean squared displacement in the y-direction
    figure;
    msd_y = [0, msd_y];
    plot(n_steps, msd_y, 'b')
    title('Mean Squared Displacement in the y-Direction');
    xlabel('Number of Steps');
    ylabel('Mean Squared Displacement');
    
    % 4) Histogram of locations in x-direction
    figure;
    for num_steps = [100, 1000, 10000, 100000, 1000000]
        x_hist = zeros(1, lattice_size+1);
        for walk = 1:num_walks
            x = 0;
            for step = 1:num_steps
                dx = randi([-1, 1]);
                x = x + dx;
                if x < 0
                    x = 0;
                elseif x > lattice_size
                    x = lattice_size;
                end
            end
            x_hist(x+1) = x_hist(x+1) + 1;
        end
        subplot(2,3,find([100, 1000, 10000, 100000, 1000000] == num_steps));
        bar(x_hist / num_walks, 'b');
        title(['Histogram of x-locations after ', num2str(num_steps), ' steps']);
        xlabel('x');
        ylabel('Probability');
    end
    
    % 5) Histogram of locations in y-direction
    figure;
    for num_steps = [100, 1000, 10000, 100000, 1000000]
        y_hist = zeros(1, lattice_size+1);
        for walk = 1:num_walks
            x = 0;
            y = lattice_size/2;
            for step = 1:num_steps
                dx = randi([-1, 1]);
                dy = randi([-1, 1]);
                x = x + dx;
                y = y + dy;
                if y < 1
                    y = 2-y;
                elseif y > lattice_size
                    y = 2*lattice_size-y-1;
                end
            end
            y_hist(y+1) = y_hist(y+1) + 1;
        end
        subplot(2,3,find([100, 1000, 10000, 100000, 1000000] == num_steps));
        bar(y_hist / num_walks, 'b');
        title(['Histogram of y-locations after ', num2str(num_steps), ' steps']);
        xlabel('y');
        ylabel('Probability');
    end
    
    % 6) Calculate average time per step for given diffusion constant
    D_desired = 1e-7; % desired diffusion constant in cm^2/sec
    t = D_desired / (a^2); % average time per step in seconds
    
    % 7) Calculate diffusion constant in the x-direction
    figure;
    diffusion_x = gradient(msd_x) / (2 * t * num_walks);
    plot(n_steps, diffusion_x);
    title('Diffusion Constant in the x-Direction');
    xlabel('Time');
    ylabel('Diffusion Constant');
    

    Note: This code is provided as a starting point and may require modifications to fit the specifications of your exact problem.

  3. User

    Modify. Both of the figures don't show anything.

  4. Assistant

    Apologies for the mistake. The figures were not correctly implemented in the previous code. Here is the modified code where the figures are properly displayed:

    % Input parameters
    width = 5; % width of the pipe in mm
    a = 0.01; % lattice constant in mm
    pw = 0; % probability of waiting
    num_steps = 1000000; % number of steps for histograms
    num_walks = 1000000; % number of random walks for histograms
    
    % Calculate lattice size based on width
    lattice_size = round(width / a);
    
    % Initialize random walker position
    x = 0;
    y = lattice_size/2;
    
    % Initialize mean squared displacement variables
    msd_x = zeros(1, num_steps);
    msd_y = zeros(1, num_steps);
    
    % Loop over the number of steps
    for step = 2:num_steps+1
        % Determine if walker waits or makes a step
        if rand <= pw
            % Walker waits
            continue;
        else
            % Walker makes a step
            % Calculate the step direction
            dx = randi([-1, 1]);
            dy = randi([-1, 1]);
            
            % Update walker position
            x = x + dx;
            y = y + dy;
            
            % Reflect at the y-direction boundary
            if y < 1
                y = 2-y;
            elseif y > lattice_size
                y = 2*lattice_size-y-1;
            end
            
            % Calculate mean squared displacement in each direction
            msd_x(step) = (x*a)^2;
            msd_y(step) = ((y-(lattice_size/2))*a)^2;
        end
    end
    
    % 1) Visualize the random walk trajectories
    figure;
    plot((0:num_steps)*a, (msd_y*a), 'b.')
    title('Random Walk Trajectories');
    xlabel('x (mm)');
    ylabel('y (mm)');
    xlim([0, num_steps*a]);
    
    % 2) Mean squared displacement in the x-direction
    figure;
    n_steps = 1:num_steps;
    msd_x = cumsum(msd_x) ./ (1:num_steps+1);
    plot(n_steps*a, msd_x*a^2, 'b')
    title('Mean Squared Displacement in the x-Direction');
    xlabel('Number of Steps');
    ylabel('Mean Squared Displacement');
    xlim([0, num_steps*a]);
    
    % 3) Mean squared displacement in the y-direction
    figure;
    msd_y = cumsum(msd_y) ./ (1:num_steps+1);
    plot(n_steps*a, msd_y*a^2, 'b')
    title('Mean Squared Displacement in the y-Direction');
    xlabel('Number of Steps');
    ylabel('Mean Squared Displacement');
    xlim([0, num_steps*a]);
    
    % 4) Histogram of locations in x-direction
    figure;
    for num_steps = [100, 1000, 10000, 100000, 1000000]
        x_hist = zeros(1, lattice_size+1);
        for walk = 1:num_walks
            x = 0;
            for step = 1:num_steps
                dx = randi([-1, 1]);
                x = x + dx;
                if x < 0
                    x = 0;
                elseif x > lattice_size
                    x = lattice_size;
                end
            end
            x_hist(x+1) = x_hist(x+1) + 1;
        end
        subplot(2,3,find([100, 1000, 10000, 100000, 1000000] == num_steps));
        bar(0:lattice_size, x_hist / num_walks, 'b');
        title(['Histogram of x-locations after ', num2str(num_steps), ' steps']);
        xlabel('x');
        ylabel('Probability');
    end
    
    % 5) Histogram of locations in y-direction
    figure;
    for num_steps = [100, 1000, 10000, 100000, 1000000]
        y_hist = zeros(1, lattice_size+1);
        for walk = 1:num_walks
            x = 0;
            y = lattice_size/2;
            for step = 1:num_steps
                dx = randi([-1, 1]);
                dy = randi([-1, 1]);
                x = x + dx;
                y = y + dy;
                if y < 1
                    y = 2-y;
                elseif y > lattice_size
                    y = 2*lattice_size-y-1;
                end
            end
            y_hist(y+1) = y_hist(y+1) + 1;
        end
        subplot(2,3,find([100, 1000, 10000, 100000, 1000000] == num_steps));
        bar(0:lattice_size, y_hist / num_walks, 'b');
        title(['Histogram of y-locations after ', num2str(num_steps), ' steps']);
        xlabel('y');
        ylabel('Probability');
    end
    
    % 6) Calculate average time per step for given diffusion constant
    D_desired = 1e-7; % desired diffusion constant in cm^2/sec
    t = D_desired / (a^2); % average time per step in seconds
    
    % 7) Calculate diffusion constant in the x-direction
    figure;
    diffusion_x = gradient(msd_x*a^2) / (2 * t);
    plot(n_steps*a, diffusion_x);
    title('Diffusion Constant in the x-Direction');
    xlabel('Time (mm)');
    ylabel('Diffusion Constant');
    xlim([0, num_steps*a]);
    

    Now the figures should properly show the desired plots.

  5. User

    Figure 1 doesn't show anything

  6. Assistant

    Apologies for the inconvenience. It seems there was an error in the code. The plots were not correctly generated in the previous code. Here is the modified code that should correctly display Figure 1, showing the random walk trajectories:

    % Input parameters
    width = 5; % width of the pipe in mm
    a = 0.01; % lattice constant in mm
    pw = 0; % probability of waiting
    num_steps = 1000000; % number of steps for histograms
    num_walks = 1000000; % number of random walks for histograms
    
    % Calculate lattice size based on width
    lattice_size = round(width / a);
    
    % Initialize random walker position
    x = zeros(1, num_steps+1);
    y = zeros(1, num_steps+1);
    x(1) = 0;
    y(1) = lattice_size/2;
    
    % Loop over the number of steps
    for step = 2:num_steps+1
        % Determine if walker waits or makes a step
        if rand <= pw
            % Walker waits
            x(step) = x(step-1);
            y(step) = y(step-1);
        else
            % Walker makes a step
            % Calculate the step direction
            dx = randi([-1, 1]);
            dy = randi([-1, 1]);
            
            % Update walker position
            x(step) = x(step-1) + dx;
            y(step) = y(step-1) + dy;
            
            % Reflect at the y-direction boundary
            if y(step) < 1
                y(step) = 2 - y(step);
            elseif y(step) > lattice_size
                y(step) = 2 * lattice_size - y(step) - 1;
            end
        end
    end
    
    % 1) Visualize the random walk trajectories
    figure;
    plot(x*a, y*a, 'b.')
    title('Random Walk Trajectories');
    xlabel('x (mm)');
    ylabel('y (mm)');
    
    % 2) Mean squared displacement in the x-direction
    figure;
    n_steps = 1:num_steps;
    msd_x = cumsum((x * a).^2) ./ n_steps;
    plot(n_steps*a, msd_x, 'b')
    title('Mean Squared Displacement in the x-Direction');
    xlabel('Number of Steps');
    ylabel('Mean Squared Displacement');
    
    % 3) Mean squared displacement in the y-direction
    figure;
    msd_y = cumsum(((y-(lattice_size/2)) * a).^2) ./ n_steps;
    plot(n_steps*a, msd_y, 'b')
    title('Mean Squared Displacement in the y-Direction');
    xlabel('Number of Steps');
    ylabel('Mean Squared Displacement');
    
    % 4) Histogram of locations in x-direction
    figure;
    for num_steps = [100, 1000, 10000, 100000, 1000000]
        x_hist = zeros(1, lattice_size+1);
        for walk = 1:num_walks
            x = 0;
            for step = 1:num_steps
                dx = randi([-1, 1]);
                x = x + dx;
                if x < 0
                    x = 0;
                elseif x > lattice_size
                    x = lattice_size;
                end
            end
            x_hist(x+1) = x_hist(x+1) + 1;
        end
        subplot(2,3,find([100, 1000, 10000, 100000, 1000000] == num_steps));
        bar(0:lattice_size, x_hist / num_walks, 'b');
        title(['Histogram of x-locations after ', num2str(num_steps), ' steps']);
        xlabel('x');
        ylabel('Probability');
    end
    
    % 5) Histogram of locations in y-direction
    figure;
    for num_steps = [100, 1000, 10000, 100000, 1000000]
        y_hist = zeros(1, lattice_size+1);
        for walk = 1:num_walks
            x = 0;
            y = lattice_size/2;
            for step = 1:num_steps
                dx = randi([-1, 1]);
                dy = randi([-1, 1]);
                x = x + dx;
                y = y + dy;
                if y < 1
                    y = 2-y;
                elseif y > lattice_size
                    y = 2*lattice_size-y-1;
                end
            end
            y_hist(y+1) = y_hist(y+1) + 1;
        end
        subplot(2,3,find([100, 1000, 10000, 100000, 1000000] == num_steps));
        bar(0:lattice_size, y_hist / num_walks, 'b');
        title(['Histogram of y-locations after ', num2str(num_steps), ' steps']);
        xlabel('y');
        ylabel('Probability');
    end
    
    % 6) Calculate average time per step for given diffusion constant
    D_desired = 1e-7; % desired diffusion constant in cm^2/sec
    t = D_desired / (a^2); % average time per step in seconds
    
    % 7) Calculate diffusion constant in the x-direction
    figure;
    diffusion_x = gradient(msd_x*a^2) / (2 * t
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.