- #1
amr07
- 6
- 0
Hi all,
I have an optimal control problem and to solve it, after starting with the initial control 'u',I have to integrate the state equation x'=f(x(t),u(t),t) forward in time then integrate the adjoint equation PSI'=G(x(t),u(t),PSI(t),t) backward in time. I want to implement all of that by 4th runga kutta method, Can anybody look at my implementation down and say what is no? t correct?
#################
for the state equation, forward in time
def integrate1(F,t,u,x):
def run_kut4(F,tk,uk,xk,h):
K0 = h * F(tk,uk,xk)
K1 = h * F(tk+ h/2.0,uk , xk + K0/2.0)
K2 = h * F(tk+ h/2.0,uk , xk + K1/2.0)
K3 = h * F(tk+ h,uk, xk + K2)
return (K0 + 2.0 * K1 + 2.0 * K2 + K3)/6.0
for k in range(len(t)-1):
hh=t[k+1]-t[k]
x[k+1] = x[k] + run_kut4(F,t[k],.5*(u[k]+u[k+1]),x[k],hh)
return x ,t
############################
and for the adjoint,backward in time
def integrate(G,t,u,x,psi):
def run_kut4(G,tk,uk,xk,psik,h):
K0 = h * G(tk,uk,xk,psik)
K1 = h * G(tk+ h/2.0,uk ,xk + h/2.0, psik + K0/2.0)
K2 = h * G(tk+ h/2.0,uk,xk + h/2.0, psik + K1/2.0)
K3 = h * G(tk+ h,uk ,xk + h/2.0, psik + K2)
return (K0 + 2.0 * K1 + 2.0 * K2 + K3)/6.0
for k in range(len(t)-2, -1, -1):
hh=t[k]-t[k+1]
psi[k]=psi[k+1] + run_kut4(G,t[k+1],u[k+1],.5*(x[k+1]+x[k]),psi[k+1],hh)
return psi,t
###################
please, Can anybody help?
many thanks,
P.S
u is the control function
x is the state function
psi is the adjoint function
I have an optimal control problem and to solve it, after starting with the initial control 'u',I have to integrate the state equation x'=f(x(t),u(t),t) forward in time then integrate the adjoint equation PSI'=G(x(t),u(t),PSI(t),t) backward in time. I want to implement all of that by 4th runga kutta method, Can anybody look at my implementation down and say what is no? t correct?
#################
for the state equation, forward in time
def integrate1(F,t,u,x):
def run_kut4(F,tk,uk,xk,h):
K0 = h * F(tk,uk,xk)
K1 = h * F(tk+ h/2.0,uk , xk + K0/2.0)
K2 = h * F(tk+ h/2.0,uk , xk + K1/2.0)
K3 = h * F(tk+ h,uk, xk + K2)
return (K0 + 2.0 * K1 + 2.0 * K2 + K3)/6.0
for k in range(len(t)-1):
hh=t[k+1]-t[k]
x[k+1] = x[k] + run_kut4(F,t[k],.5*(u[k]+u[k+1]),x[k],hh)
return x ,t
############################
and for the adjoint,backward in time
def integrate(G,t,u,x,psi):
def run_kut4(G,tk,uk,xk,psik,h):
K0 = h * G(tk,uk,xk,psik)
K1 = h * G(tk+ h/2.0,uk ,xk + h/2.0, psik + K0/2.0)
K2 = h * G(tk+ h/2.0,uk,xk + h/2.0, psik + K1/2.0)
K3 = h * G(tk+ h,uk ,xk + h/2.0, psik + K2)
return (K0 + 2.0 * K1 + 2.0 * K2 + K3)/6.0
for k in range(len(t)-2, -1, -1):
hh=t[k]-t[k+1]
psi[k]=psi[k+1] + run_kut4(G,t[k+1],u[k+1],.5*(x[k+1]+x[k]),psi[k+1],hh)
return psi,t
###################
please, Can anybody help?
many thanks,
P.S
u is the control function
x is the state function
psi is the adjoint function