2

I want to calculate the minimum distance between two atoms by means of the Lennard Jones potential. The formula I want to use is:

V(r) = C12/r^12 - C6/r^6 where C12 = 1 and C6 = 10

The following construct of the function including variables was already given and the solutions for this example should be 0.764724.

My idea is to use the "minimum"-function but I have absolutely no clue how to use it. Not even after reading the SciPy manual. How should I start?

import numpy as np
from scipy.optimize import minimize, leastsq, least_squares, curve_fit

def get_minimum_energy_distance(C12, C6):
   my code
Shaido
  • 27,497
  • 23
  • 70
  • 73
riejuhatza
  • 31
  • 2

1 Answers1

1

The first thing you'll need to do is define your function to optimise:

def f(r, C12=1, C6=10):
    return (C12/r**12) - (C6/r**6)

Finally, we call scipy's minimze, passing in our function we defined. We also need to pass in a guess of the r-location corresponding to the minimum y, and I've chosen r=1.5:

from scipy.optimize import minimize

res = minimize(f,1.5)

#      fun: -24.999999999999936
#  hess_inv: array([[0.00032439]])
#       jac: array([2.38418579e-06])
#   message: 'Optimization terminated successfully.'
#      nfev: 36
#       nit: 4
#      njev: 12
#    status: 0
#   success: True
#         x: array([0.76472448])

res['x']

# array([0.76472448])

And there's your result. I would recommend reading the scipy minimize documentation to become familiar with some of the optimisation parameters and then have a play with them to see how the method responds to different initial guesses of x0 (r-value corresponding to min y).

user6386471
  • 1,203
  • 1
  • 8
  • 17
  • 1
    @riejuhatza: Consider accepting the answer since it helped you solve the question, see: https://stackoverflow.com/help/someone-answers – Shaido Dec 09 '20 at 03:11