将numpy导入为np 从scipy.integrate导入resolve_bvp,simps,trapz
“”“ Opt1:最佳控制教程的示例1。
min.: J = int_0^1 (u-x)**2 dt
sub to: x' = u; x(0) = 1
The minimum principle gives the bvp problem:
x' = u; p' = 2*(u-x); with x(0) = 1, p(1) = 0
where u = x - p/2.
Note: lambda is renamed as p!
“”“
def ode(t,y):
u = y[0] - y[1]/2.
return [u, 2*(u-y[0])]
def bc(ya,yb):
return [ya[0]-1, yb[1]]
N = 11
xin = np.linspace(0,1,N)
yin = np.zeros((2,N))#琐碎的猜测
sol = solve_bvp(ode,bc,xin,yin)
x = sol.x
y = sol.y [0]
p = sol.y [1]
u = y-p / 2。
w =(u-y)** 2
J = trapz(w,x = x)
将matplotlib.pyplot导入为plt
plt.figure(1)
plt.plot(x,y,'-',label ='$ y(t)$')
plt.plot(x,p,'-',label ='$ p(t)$')
plt.plot(x,u,'r:',label ='$ u(t)$')
plt.text(.23,1.1,'x(t)')
plt.text(.33,1.2,'u(t)')
plt.text(.23,0.1,'p(t)')
s ='最终成本为:J ='+ str(J)
plt.text(.41,.8,s)
plt.xlabel('time')
plt.ylabel('states')
plt.grid()
plt.legend(framealpha = 1,shadow = True)
plt.title('最佳控件1')
plt.savefig('opt1.eps')
plt.show()
OptEx1:最佳控制问题的示例1。 最小化:J = int_0 ^ 1 x dt 服从:x'= u; x(0)= 1 最小原则给出了bvp问题: x'= u; p'= -1; x(0)= 1,p(1)= 0 其中u = -1 注意:lambda重命名为p