Piecewise MATLAB ode45 problem for Lennard-Jones

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB implementation of the Lennard-Jones potential using the ode45 function to simulate the motion of two particles and their collisions with walls. Participants are addressing issues related to the code, specifically the calculation of displacement after collisions and the handling of undefined variables.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to calculate the displacement of two particles after a collision with walls, providing MATLAB code for the Lennard-Jones equation of motion.
  • Another participant questions the clarity of the original post and requests further details on how the program handles collisions.
  • A later reply suggests that the condition for detecting a collision in the code may never be met, which would result in the variable T1 remaining undefined.
  • There is a repeated assertion regarding the meaning of t = 4.1 s, indicating that it represents the time of collision with the hard boundary.

Areas of Agreement / Disagreement

Participants express uncertainty about the collision detection mechanism in the code. There is no consensus on whether the collision condition is correctly implemented or if it can be met during the simulation.

Contextual Notes

The discussion highlights potential limitations in the collision detection logic and the handling of time increments in the simulation, but these issues remain unresolved.

Adrian Soo
Messages
7
Reaction score
0
I could run the MATLAB codes but the main issue is that data for T1 and U1 could not be produced. I am currently trying to calculate the displacement of two particles after the collision with walls with length of 10 units.

Here are the codes. If you don't understand its context, you can refer to the Microsoft word files I posted. Note that each particle HAS A RADIUS OF 1 UNIT.​
================================================================================
Lennard-Jones's equation of motion function

function du = lennardJones(~,u)

du = zeros(8,1);

du(1) = u(2);
du(2) = -24*( u(1) - u(3) )/( ( u(1) - u(3) )^2 + ( u(5) - u(7) )^2 )^7 + 24*( u(1) - u(3) )/( ( u(1) - u(3) )^2 + ( u(5) - u(7) )^2 )^4;
du(3) = u(4);
du(4) = -24*( -u(1) + u(3) )/( ( u(1) - u(3) )^2 + ( u(5) - u(7) )^2 )^7 + 24*( -u(1) + u(3) )/( ( u(1) - u(3) )^2 + ( u(5) - u(7) )^2 )^4;
du(5) = u(6);
du(6) = -24*( u(5) - u(7) )/( ( u(1) - u(3) )^2 + ( u(5) - u(7) )^2 )^7 + 24*( u(5) - u(7) )/( ( u(1) - u(3) )^2 + ( u(5) - u(7) )^2 )^4;
du(7) = u(8);
du(8) = -24*( -u(5) + u(7) )/( ( u(1) - u(3) )^2 + ( -u(5) + u(7) )^2 )^7 + 24*( -u(5) + u(7) )/( ( u(1) - u(3) )^2 + ( u(5) - u(7) )^2 )^4;================================================================================
clear all

t0 = 0; % Initial time
deltat = 0.1; % Step size of time
tend = 10; % Final time
u = [4,1,6,-1,1,0,1,0]; % Initial conditions for Lennard-Jones Potential
n = (tend - t0)/deltat; % for 'for' loops
t1 = t0; % Initialise t1 to t0
r = 1;

for i = 1 : n - 1

t2 = t1 + deltat; % Increment in time
options = odeset('RelTol',1e-8,'AbsTol',[1e-8 1e-8 1e-8 1e-8 1e-8 1e-8 1e-8 1e-8]);
[T,U] = ode45(@lennardJones,[t1 t2],u,options); % ode45 applied on LennardJones


if U(1) <= 1 && U(3) >= 9 %When particle collides with the wall at the distance of 9 units on the right
% and 1 unit on the left
clear t1 t2 deltat
t1 = 4.1; % t1 indicates the time it collides with the solid wall
deltat = 0.1; % increment in time
t2 = 10;
k = (t2 - t1)/deltat; %for 'for' loops
u1 = [1,1,9,-1,1,0,1,0]; %initial condition changes after colliding with walls
newtime = 0;

for j = 1:k
newtime = newtime + deltat;
[T1,U1] = ode45(@lennardJones,[t1 newtime],u1,options);
end

end

end

plot(T,U,T1,U1,'o')
============================================================================

output :
Undefined function or variable 'T1'.

Error in hardBoundary (line 37)
plot(T,U,T1,U1,'o')
 

Attachments

Do you mean that the post can't be comprehended?
 
Greg Bernhardt said:
This is an automated courtesy bump.
This message is sent automatically to new posts that do not have any replies after one week.

I don't understand how your program is supposed to deal with collisions. It seems to be integrating over a certain time interval, and then come back to an arbitrary point in time (t=4.1) if there is a collision. Could you describe in more details?

Also, please use code tags when posting code: [C O D E] Some code here [/C O D E] (without spaces in CODE).
 
Basically, t = 4.1 s means that the particle collides with the hard boundary by using ode45.
 
Adrian Soo said:
Basically, t = 4.1 s means that the particle collides with the hard boundary by using ode45.
How do you know?

As for your original problem: the condition
if U(1) <= 1 && U(3) >= 9
is never met, hence T1 is never defined.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K