Sie sind auf Seite 1von 4

% Generate a semi-random polymer

clc
N = 10; % number of bonds between monomers
xy = zeros(round(5*N/4),round(5*N/4)); % zeroing the array, each 0 means no monomer
x = round((5*N/4)/2); % initial row-position
y = round((5*N/4)/2); % initial column-position
xy(x,y) = 1; % put a monomer in the initial position
% Do a loop to place the rest of the monomers
for n = 1:N
% Choose a random number, test1, to decide if we place in the x- or y- direction
test1 = rand;
if test1 < 0.5
% move in the x-direction
% Choose a random number, test2, to decide if we move in the + or direction
test2 = rand;
if test2 < 0.5 % move in the direction
if (x-1) > 0 && xy(x-1,y) == 0
% ensure still in array or havent already placed
xy(x-1,y) = n+1;
% place monomer
x = x-1 ;
% required for the next iteration of the do loop
else
% if space occupied or we are off the grid
if (x+1) < (5*N/4) && xy(x+1,y) == 0 % different direction, still in array
xy(x+1,y) = n+1;
% place the monomer
x = x+1; % iteration requirement
else
% if space occupied
if (y-1) > 0 && xy(x,y-1) == 0
% different direction, still in array
xy(x,y-1) = n+1;
% place monomer
y = y-1;
% iteration requirement
else
% if occupied
if (y+1) < (5*N/4) && xy(x,y+1) == 0
% other direction, still on grid
xy(x,y+1) = n+1; % place monomer
y = y+1; % iteration requirement
end
end
end
end
else
% move in the + direction
if (x+1) < (5*N/4) && xy(x+1,y) == 0
% ensure still in array or havent already
placed
xy(x+1,y) = n+1;
% place monomer
x = x+1 ;
% required for the next iteration of the do loop
else
% if space occupied or we are off the grid
if (x-1) > 0 && xy(x-1,y) == 0
% different direction, still in array
xy(x-1,y) = n+1;
% place the monomer
x = x-1; % iteration requirement
else
% if space occupied
if (y-1) > 0 && xy(x,y-1) == 0
% different direction, still in array
xy(x,y-1) = n+1;
% place monomer
y = y-1;
% iteration requirement
else
% if occupied
if (y+1) < (5*N/4) && xy(x,y+1) == 0
% other direction, still on grid
xy(x,y+1) = n+1; % place monomer
y = y+1; % iteration requirement
end
end
end
end
end
else
% if we have to move in the y-direction
% Choose a random number, test2, to decide if we move in the + or direction
test2 = rand;
if test2 < 0.5 % move in the direction
if (y-1) > 0 && xy(x,y-1) == 0
% ensure still in array or havent already placed
xy(x,y-1) = n+1;
% place monomer
y = y-1 ;
% required for the next iteration of the do loop

else
% if space occupied or we are off the grid
if (y+1) < (5*N/4) && xy(x,y+1) == 0
% different direction, still in array
xy(x,y+1) = n+1;
% place the monomer
y = y+1; % iteration requirement
else
% if space occupied
if (x-1) > 0 && xy(x-1,y) == 0
% different direction, still in array
xy(x-1,y) = n+1;
% place monomer
x = x-1;
% iteration requirement
else
% if occupied
if (x+1) < (5*N/4) && xy(x+1,y) == 0
% other direction, but still on grid
xy(x+1,y) = n+1; % place monomer
x = x+1; % iteration requirement
end
end
end
end
else
% move in the + direction
if (y+1) < (5*N/4) && xy(x,y+1) == 0
% ensuring we are still in the array or that we
havent already placed
xy(x,y+1) = n+1;
% place monomer
y = y+1 ;
% required for the next iteration of the do loop
else
% if space occupied or we are off the grid
if (y-1) > 0 && xy(x,y-1) == 0
% move other direction, but ensure we are still in
the array
xy(x,y-1) = n+1;
% place the monomer
y = y-1; % iteration requirement
else
% if space occupied
if (x-1) > 0 && xy(x-1,y) == 0
% move in a different direction, ensuring we are
still in the array
xy(x-1,y) = n+1;
% place monomer
x = x-1;
% iteration requirement
else
% if occupied
if (x+1) < (5*N/4) && xy(x+1,y) == 0
% other direction, but still on grid
xy(x+1,y) = n+1; % place monomer
x = x+1; % iteration requirement
end
end
end
end
end
end
end
xy
% display out matrix of polymer
% -------------------------------------------------------------------% This part of the program uses the MC to allow the polymer to change
% shape based on the solvent-monomer interactions.
monomer1 = 0;
% dummy variable
for k = 1:500
% try 500 steps of MC
% Randomly pick a monomer in the chain and find its position in the matrix xy
monomer = round(1+rand*N)
% random number between 1 and N+1
if monomer~=monomer1
% this is to avoid trying the same thing over and aover again
[ik,jk]=find(xy==monomer);
% get the indices of the monomer
if (ik+1) < (5*N/4) && (jk+1) < (5*N/4) && xy(ik+1,jk+1)==0
% check if position below
and right is available
if monomer ~= N+1 && monomer ~= 1
% Make sure not first or last monomer
[inext,jnext] = find(xy==monomer+1);
% get the position of the next monomer
[iprev,jprev] = find(xy==monomer-1);
% get the position of the prev monomer
distance1 = sqrt((iprev-(ik+1))^2 + (jprev-(jk+1))^2); % distance to prev monomer
distance2 = sqrt((inext-(ik+1))^2 + (jnext-(jk+1))^2); % distance to next monomer
if distance1 == 1 & distance2 == 1
% make sure move preserves connectivity
xy(ik+1,jk+1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0;
% replace old monomer position by a solvent
end

else
if monomer == 1
% check if it is first
[inext,jnext] = find(xy==monomer+1); % get the position of the next monomer
distance = sqrt((inext-(ik+1))^2 + (jnext-(jk+1))^2); % distance
if distance == 1
% ensure the move preserves the connectivity
% Need to include the test of the 8 surrounding spots to count the number of
% solvent molecules that surround the point before and after the move. Once
% the energy is calculated based on the interaction(w), quality (q) and delta
% n (dn) we can decide whether the move will or won't occur.
xy(ik+1,jk+1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0; % replace old monomer position by a solvent
end
elseif monomer == N+1
% check if it is last monomer
[iprev,jprev] = find(xy==monomer-1);
% get the position of the previous monomer
distance = sqrt((iprev-(ik+1))^2 + (jprev-(jk+1))^2); % distance
if distance == 1
% ensure the move preserves the connectivity
xy(ik+1,jk+1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0;
% replace old monomer position by a solvent
end
end
end
elseif (ik-1) > 0 && (jk+1) < (5*N/4) && xy(ik-1,jk+1)==0
% check if position above and
right is available
if monomer ~= N+1 && monomer ~= 1
[inext,jnext] = find(xy==monomer+1);
% get the position of the next monomer
[iprev,jprev] = find(xy==monomer-1);
% get the position of the prev monomer
distance1 = sqrt((iprev-(ik-1))^2 + (jprev-(jk+1))^2); % distance to prev monomer
distance2 = sqrt((inext-(ik-1))^2 + (jnext-(jk+1))^2); % distance to next monomer
if distance1 == 1 & distance2 == 1
% make sure move preserves connectivity
xy(ik-1,jk+1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0;
% replace old monomer position by a solvent
end
else
if monomer == 1
% check if it is first
[inext,jnext] = find(xy==monomer+1); % get the position of the next monomer
distance = sqrt((inext-(ik-1))^2 + (jnext-(jk+1))^2); % distance
if distance == 1
% ensure the move preserves the connectivity
xy(ik-1,jk+1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0; % replace old monomer position by a solvent
end
elseif monomer == N+1
% check if it is last
[iprev,jprev] = find(xy==monomer-1);
% get the position of the previous monomer
distance = sqrt((iprev-(ik-1))^2 + (jprev-(jk+1))^2); % distance
if distance == 1
% ensure the move preserves the connectivity
xy(ik-1,jk) = monomer;
% move the monomer to new position
xy(ik,jk) = 0;
% replace old monomer position by a solvent
end
end
%
end
elseif (ik-1) > 0 && (jk - 1) > 0 && xy(ik-1,jk-1)==0
% check if position above and
left is available
if xy(ik,jk) ~= N+1 && xy(ik,jk) ~= 1
[inext,jnext] = find(xy==monomer+1);
% get the position of the next monomer
[iprev,jprev] = find(xy==monomer-1);
% get the position of the previous monomer
distance1 = sqrt((iprev-(ik-1))^2 + (jprev-(jk-1))^2); % distance to prev monomer
distance2 = sqrt((inext-(ik-1))^2 + (jnext-(jk-1))^2); % distance to next monomer
if distance1 == 1 & distance2 == 1
% make sure move preserves connectivity
xy(ik-1,jk-1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0;
% replace old monomer position by a solvent
end
else
if xy(ik,jk) == 1
% check if it is first
[inext,jnext] = find(xy==monomer+1); % get the position of the next monomer

distance = sqrt((inext-(ik-1))^2 + (jnext-(jk-1))^2); % distance


if distance == 1
% ensure the move preserves the connectivity
xy(ik-1,jk-1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0; % replace old monomer position by a solvent
end
elseif xy(ik,jk) == N+1
% check if it is last monomer
[iprev,jprev] = find(xy==monomer-1);
% get the position of the previous monomer
distance = sqrt((iprev-(ik-1))^2 + (jprev-(jk-1))^2); % distance
if distance == 1
% ensure the move preserves the connectivity
xy(ik-1,jk-1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0;
% replace old monomer position by a solvent
end
end
end
elseif (ik+1) < (5*N/4) && (jk-1) > 0 && xy(ik+1,jk-1)==0
% check if position below and
left is available
if xy(ik,jk) ~= N+1 && xy(ik,jk) ~= 1
[inext,jnext] = find(xy==monomer+1);
% get the position of the next monomer
[iprev,jprev] = find(xy==monomer-1);
% get the position of the previous monomer
distance1 = sqrt((iprev-(ik+1))^2 + (jprev-(jk-1))^2); % distance to prev monomer
distance2 = sqrt((inext-(ik+1))^2 + (jnext-(jk-1))^2); % distance to next monomer
if distance1 == 1 & distance2 == 1
% make sure move preserves connectivity
xy(ik+1,jk-1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0;
% replace old monomer position by a solvent
end
else
if xy(ik,jk) == 1
% check if it is first
[inext,jnext] = find(xy==monomer+1); % get the position of the next monomer
distance = sqrt((inext-(ik+1))^2 + (jnext-(jk-1))^2); % distance
if distance == 1
% ensure the move preserves the connectivity
xy(ik+1,jk-1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0; % replace old monomer position by a solvent
end
elseif xy(ik,jk) == N+1
% check if it is last monomer
[iprev,jprev] = find(xy==monomer-1);
% get the position of the previous monomer
distance = sqrt((iprev-(ik+1))^2 + (jprev-(jk-1))^2); % distance
if distance == 1
% ensure the move preserves the connectivity
xy(ik+1,jk-1) = monomer;
% move the monomer to new position
xy(ik,jk) = 0;
% replace old monomer position by a solvent
end
end
end
end
monomer1 = monomer;
end
if k ==100 || k ==200 || k ==300 || k ==400
xy
end
end
xy

Das könnte Ihnen auch gefallen