我想找到轨迹最小化动作数字上。
我试图通过离散化动作来做到这一点,因此它更像是一个接受向量而不是函数的函数。更准确地说,给定轨迹的初始猜测我试图找到轨迹最小化总和(这是 SHO 操作的离散化版本)然后我使用 scipy 的向下单纯形法/nelder-mead 最小化函数来执行此操作,但是它无法正常工作。
我正在关注一个参考,它说轨迹的初始猜测可以是初始位置其余位置可以是初始位置加上围绕问题长度尺度的变化. (此处为第 39 页)。但是它仍然不起作用。我试图使变化更小,但仍然不起作用。似乎要让它发挥作用,我需要为它提供一个与实际轨迹相当接近的近似值,但这违背了练习的目的。这是代码:
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt
%matplotlib inline
t_i = 0
t_f = 7
N = 100
delta_t = (t_f - t_i)/N
m = 1
k = 1
def action(x, N, m, delta_t, k):
"""
This function will take in an array of positions x_i w/ x_i = x(t_i). Then it computes the action for this trajectory.
We want to, given an initial position and a guess for the rest of the components, find the set of x_i which minimizes
the action, and return it. Scipy 'optimize' does it. See the first example in the documentation for a similar problem.
"""
tot = np.array([])
for i in range (0,N-1):
np.append(tot,((m/(2*delta_t))*((x[i+1]-x[i])**2)-((k*delta_t)/2)*((x[i])**2)))
return np.sum(tot)
x_0 = np.ones(100)
var = np.random.rand(100)
x0 = x_0 + var
res = minimize(action, x0, args=(N, m, delta_t, k), method='Nelder-Mead',options={'disp': True})
time = np.linspace(0,7,100)
fig = plt.figure()
axes = fig.add_axes([0,0,1.25,1])
axes.plot(time,res.x)
