Зацикливание моего алгоритма для построения другого значения параметра на том же графике (MATLAB) - PullRequest
0 голосов
/ 16 ноября 2018

Я реализовал алгоритм для моего физического проекта, который делает именно то, что я хочу. Проблема, в которой я застрял, заключается не в самом содержании физики, поэтому я думаю, что объяснение того, что делает мой код, может быть несколько тривиальным. В основном я застрял в том, как работает построение графиков в MATLAB, если бы мне пришлось циклически повторять один и тот же алгоритм для получения похожих графиков с небольшим изменением значения моего параметра. Вот мой код ниже:

clear; clc; close all;

% Parameters:
z_nn = 4;   % Number of nearest-neighbour in lattice (square = 4).
z_nnn = 4;  % Number of next-nearest-neighbours in lattice (square = 4).
Lx = 40;    % Number of sites along x-axis.
Ly = 40;    % Number of sites along y-axis.
sigma = 1;  % Size of a site (defines our units of length).
beta = 1.2; % Inverse temperature beta*epsilon.
mu = -2.53; % Chemical potential mu/epsilon.
mu_2 = -2.67; % Chemical potential mu/epsilon for 2nd line.
J = linspace(1, 11, 11);%J points for the line graph plot

potential = zeros(Ly);
attract = 1.6; %wall attraction constant
k = 1;         %wall depth


rho_0 = 0.4;   % Initial density.
tol = 1e-12;   % Convergence tolerance.
count = 30000; % Upper limit for iterations.
alpha  = 0.01; % Mixing parameter.

conv = 1; cnt = 1;       % Convergence value and counter.
rho = rho_0*ones(Ly); % Initialise rho to the starting guess(i-th rho_old) in Eq(47)
rho_rhs = zeros(Ly);  % Initialise rho_new to zeros.

% Solve equations iteratively:
while conv>=tol && cnt<count
  cnt = cnt + 1; % Increment counter.
  % Loop over all lattice sites:

    for j=1:Ly
        %Defining the Lennard-Jones potential
        if j<k
            potential(j) = 1000000000;
        else
            potential(j) = -attract*(j-k)^(-3); 
        end
        % Handle the periodic boundaries for x and y:
        %left = mod((i-1)-1,Lx) + 1; % i-1, maps 0 to Lx.
        %right = mod((i+1)-1,Lx) + 1; % i+1, maps Lx+1 to 1.
        if j<k+1 %depth of wall
          rho_rhs(j) = 0;
          rho(j) = 0;
        elseif j<(20+k)
          rho_rhs(j) = (1 - rho(j))*exp((beta*((3/2)*rho(j-1) + (3/2)*rho(j+1) + 2*rho(j) + mu) - potential(j)));
        else
          rho_rhs(j) = rho_rhs(j-1);
        end

    end

    conv = sum(sum((rho - rho_rhs).^2)); % Convergence value is the sum of the differences between new and current solution.
    rho = alpha*rho_rhs + (1 - alpha)*rho; % Mix the new and current solutions for next iteration.
end



% disp(['conv = ' num2str(conv_2) ' cnt = ' num2str(cnt)]); % Display final answer.
% figure(2);
% pcolor(rho_2);

figure(1);
plot(J, rho(1:11));
hold on;
% plot(J, rho_2(1,1:11));
hold off;

disp(['conv = ' num2str(conv) ' cnt = ' num2str(cnt)]); % Display final answer.
figure(3);
pcolor(rho);

Выполнение этого кода должно дать вам такой график enter image description here

Теперь я хочу создать аналогичный график, но с одним измененным значением переменной, нанесенным на тот же график. Мой подход, который я попробовал, ниже:

clear; clc; close all;

% Parameters:
z_nn = 4;   % Number of nearest-neighbour in lattice (square = 4).
z_nnn = 4;  % Number of next-nearest-neighbours in lattice (square = 4).
Lx = 40;    % Number of sites along x-axis.
Ly = 40;    % Number of sites along y-axis.
sigma = 1;  % Size of a site (defines our units of length).
beta = 1.2; % Inverse temperature beta*epsilon.
mu = [-2.53,-2.67];     % Chemical potential mu/epsilon.
mu_2 = -2.67;           % Chemical potential mu/epsilon for 2nd line.
J = linspace(1, 10, 10);%J points for the line graph plot

potential = zeros(Ly, length(mu));
gamma = zeros(Ly, length(mu));
attract = 1.6; %wall attraction constant
k = 1;         %wall depth


rho_0 = 0.4;   % Initial density.
tol = 1e-12;   % Convergence tolerance.
count = 30000; % Upper limit for iterations.
alpha = 0.01;  % Mixing parameter.

conv = 1; cnt = 1;    % Convergence value and counter.
rho = rho_0*[Ly,length(mu)]; % Initialise rho to the starting guess(i-th rho_old) in Eq(47)
rho_rhs = zeros(Ly,length(mu));  % Initialise rho_new to zeros. 


figure(3);
hold on;
% Solve equations iteratively:
while conv>=tol && cnt<count
    cnt = cnt + 1; % Increment counter.
    % Loop over all lattice sites:


        for j=1:Ly

            for i=1:length(mu)
                y = 1:Ly;
                MU = mu(i).*ones(Ly)

                %Defining the Lennard-Jones potential
                if j<k
                    potential(j) = 1000000000;
                else
                    potential(j) = -attract*(j-k).^(-3); 
                end
                % Handle the periodic boundaries for x and y:
                %left = mod((i-1)-1,Lx) + 1; % i-1, maps 0 to Lx.
                %right = mod((i+1)-1,Lx) + 1; % i+1, maps Lx+1 to 1.
                if j<k+1 %depth of wall
                  rho_rhs(j) = 0;
                  rho(j) = 0;
                elseif j<(20+k)
                  rho_rhs(j) = (1 - rho(j))*exp((beta*((3/2)*rho(j-1) + (3/2)*rho(j+1) + 2*rho(j) + MU - potential(j)));
                    else
                      rho_rhs(j) = rho_rhs(j-1);
                end
            end
        end

        conv = sum(sum((rho - rho_rhs).^2));   % Convergence value is the sum of the differences between new and current solution.
        rho = alpha*rho_rhs + (1 - alpha)*rho; % Mix the new and current solutions for next iteration.

        disp(['conv = ' num2str(conv) ' cnt = ' num2str(cnt)]); % Display final answer.
        figure(1);
        pcolor(rho);


        plot(J, rho(1:10));

end
hold off;

Единственная переменная, которую я здесь изменяю, это mu. Я хотел бы зациклить мой первый код, чтобы я мог ввести произвольное количество различных значений mu и построить их на одном графике. Естественно, мне пришлось изменить все размеры списков с (1 на размер Ly) на (# # mu (s) на размер Ly), чтобы при циклическом цикле первого кода значение i-го mu в этом цикл превращается в списки с размерностью до Ly. Поэтому я подумал, что я буду делать построение в цикле и использовать «удержание» для инкапсуляции всего цикла, чтобы каждый график, который был сгенерирован в каждом цикле, не был удален. Но я часами пытался выяснить семантику MATLAB, но в конечном итоге я не могу понять, что делать. Надеюсь, я смогу помочь с этим!

1 Ответ

0 голосов
/ 16 ноября 2018

hold on применяется только к активной фигуре, это не общее свойство, разделяемое между всеми фигурами.Что делает, так это изменяет значение текущего свойства NextPlot , которое определяет поведение при добавлении графиков к рисунку.

hold on эквивалентно set(gcf,'NextPlot','add');

hold off эквивалентно set(gcf,'NextPlot','replace');

В вашем коде у вас есть:

figure(3);  % Makes figure 3 the active figure
hold on;    % Sets figure 3 'NextPlot' property to 'add'
% Do some things %
while conv>=tol && cnt<count
   % Do many things %
   figure(1);  % Makes figure 1 the active figure; 'hold on' was not applied to that figure
   plot(J, rho(1:10));  % plots rho while erasing the previous plot
end

Вы должны попытаться добавить еще один оператор hold on после figure(1)

figure(1);  
hold on
plot(J, rho(1:10));
...