# molecular code in matlab

7 views (last 30 days)
Carc on 25 Jan 2023
Answered: Walter Roberson on 26 Jan 2023
This is code for molucular dynamics, but I am not sure why this code couldn't run. Error message is as belows:
error in untitled (line 4)
sum_Vsq(it) = sum_Vsq(it)+ vhx(k,it)*vhx(k,it)+vhy(k,it)*vhy(k,it);
-----------------------
nonIdealTerm = 0;
sum_PE=0;
for k=1:nP
sum_Vsq(it) = sum_Vsq(it)+ vhx(k,it)*vhx(k,it)+vhy(k,it)*vhy(k,it);
for k1=1:nP
if (k1 ~= k)
dx1=hx(k,it)-hx(k1,it);
dy1=hy(k,it)-hy(k1,it);
% check for boundary conditions
if(bC == 0)
if(dx1>sL/2)
dx1=dx1-sL;
end
if(dx1<-sL/2)
dx1=dx1+sL;
end
if(dy1>sL/2)
dy1=dy1-sL;
end
if(dy1<-sL/2)
dy1=dy1+sL;
end
end
r1=sqrt(dx1^2+dy1^2);
sum_PE=sum_PE + 4*(1/r1^12-1/r1^6);
% Lennard-Jones model
f1=24*(2/r1^13-1/r1^7);
fx1=f1*dx1/r1;
fy1=f1*dy1/r1;
nonIdealTerm = nonIdealTerm + fx1*dx1 + fy1*dy1;
end
end
end
KE(it)=0.5* sum_Vsq(it);
PE(it)= 0.5*sum_PE;
TE(it) = KE(it) + PE(it);
T(it) = sum_Vsq(it)/( 2*(nP-1));
p(it) = nP*T(it)/(sL*sL);
pNonIdeal(it) = p(it) + (1/(2*sL*sL))*nonIdealTerm;
nonIdealTerm = 0
sum_PE=0;
for k=1:nP
sum_Vsq(it) = sum_Vsq(it)+ vhx(k,it)*vhx(k,it)+vhy(k,it)*vhy(k,it);
for k1=1:nP
if (k1 ~= k)
dx1=hx(k,it)-hx(k1,it);
dy1=hy(k,it)-hy(k1,it);
% check for boundary conditions
if(bC == 0)
if(dx1>sL/2)
dx1=dx1-sL;
end
if(dx1<-sL/2)
dx1=dx1+sL;
end
if(dy1>sL/2)
dy1=dy1-sL;
end
if(dy1<-sL/2)
dy1=dy1+sL;
end
end
r1=sqrt(dx1^2+dy1^2);
sum_PE=sum_PE + 4*(1/r1^12-1/r1^6);
% Lennard-Jones model
f1=24*(2/r1^13-1/r1^7);
fx1=f1*dx1/r1;
fy1=f1*dy1/r1;
nonIdealTerm = nonIdealTerm + fx1*dx1 + fy1*dy1;
end
end
end
KE(it)=0.5* sum_Vsq(it);
PE(it)= 0.5*sum_PE;
TE(it) = KE(it) + PE(it);
T(it) = sum_Vsq(it)/( 2*(nP-1));
p(it) = nP*T(it)/(sL*sL);
pNonIdeal(it) = p(it) + (1/(2*sL*sL))*nonIdealTerm;

Walter Roberson on 26 Jan 2023
There are a lot of undefined variables there.
There are two possibilities:
1. There might be another script that you have to execute that defines the variables; OR
2. This code might originally have been part of a function, but the function line might have been removed. The code might be intended to be invoked as a function passing in values of the missing variables.

### Categories

Find more on Biological Physics 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!

Translated by