```python
import sympy as sp
from sympy import sin, cos, exp
```


```python
h, s, v, gam = sp.symbols('h s v gam')
lamH, lamS, lamV, lamGAM, w = sp.symbols('lamH lamS lamV lamGAM w')

rho0, H, beta, LD, R_m, g, rho0, g = sp.symbols('rho0, H, beta, LD, R_m, g, rho0, g')
u = sp.symbols('u')

# Define sub-expressions to make things a bit easier to read
v2 = v*v
rho = rho0 * sp.exp(-h/H) 
D_m = rho * v2 / (2 * beta)
r = R_m + h

# Define equations of motion f(t, x, u)
f = sp.Matrix([v * sin(gam),       # dh/dt
     v * cos(gam),       # ds/dt
     -D_m - g*sin(gam),  # dV/dt
     (v2 * cos(gam)/r + D_m*LD*cos(u) - g*cos(gam))/v # dgam/dt. Hint: L/m = (D/m) * (L/D)
    ])

lamT = sp.Matrix([lamH, lamS, lamV, lamGAM])
X = sp.Matrix([h, s, v, gam])

# lamda_dot = - lambda_T . df/dx
dLamDt = [-lamT.dot(f.diff(x)) for x in X]

# Add lamU
dLamUDt = -(lamT.dot(f)).diff(u)
dLamDt.append(dLamUDt)

# Substitute some sub-expressions
dLamDt = [expr.subs({D_m: 'D_m', r: 'r', rho: 'rho'})
          for expr in dLamDt]

lam_vars = [str(lam) for lam in lamT]
lam_vars.append('lamU')
for var, expr in zip(lam_vars, dLamDt):
    print(str(var)+'dot','=',expr.simplify())
```

    lamHdot = D_m*LD*lamGAM*cos(u)/(H*v) - D_m*lamV/H + lamGAM*v*cos(gam)/r**2
    lamSdot = 0
    lamVdot = D_m*LD*lamGAM*cos(u)/v**2 - LD*lamGAM*rho*cos(u)/beta - g*lamGAM*cos(gam)/v**2 - lamGAM*cos(gam)/r - lamH*sin(gam) - lamS*cos(gam) + lamV*rho*v/beta
    lamGAMdot = -g*lamGAM*sin(gam)/v + g*lamV*cos(gam) + lamGAM*v*sin(gam)/r - lamH*v*cos(gam) + lamS*v*sin(gam)
    lamUdot = LD*lamGAM*rho*v*sin(u)/(2*beta)



```python

```

72c977a45205f4f19e109a7367d974c326fd9f5f
ipynb
Jupyter Notebook
notebooks/01-Derive-Costate-Equations.ipynb
thomasantony/msl-apollo-entry-guidance
2da4e3ee4338348429895ace636c47c76ac16d7b
MIT
2021-08-23T02:24:52.000Z
2022-02-07T13:41:23.000Z
notebooks/01-Derive-Costate-Equations.ipynb
thomasantony/msl-apollo-entry-guidance
2da4e3ee4338348429895ace636c47c76ac16d7b
MIT
2021-08-27T23:01:49.000Z
2021-08-27T23:01:49.000Z
notebooks/01-Derive-Costate-Equations.ipynb
thomasantony/msl-apollo-entry-guidance
2da4e3ee4338348429895ace636c47c76ac16d7b
MIT
2021-08-24T18:30:14.000Z
2021-09-01T17:47:57.000Z
Qwen/Qwen-72B
1. YES
2. YES