Skip to content

Commit

Permalink
modernize code with asb.Opti
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdsharpe committed Apr 5, 2024
1 parent 82888b0 commit 1229420
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions studies/AtmosphereFitting_v1/optimal_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@
You want to pick n points to linearly interpolate this such that the L2-error is minimized. How do you do it?
"""
import numpy as np
import casadi as cas
import aerosandbox as asb
import aerosandbox.numpy as np
import matplotlib.pyplot as plt

opti = cas.Opti()
opti = asb.Opti()

n = 101

x = opti.variable(n) # cas.linspace(0,1,n)

opti.set_initial(x, cas.linspace(0, 10, n))
x = opti.variable(np.linspace(0, 10, n))

opti.subject_to([
x[0] == 0,
x[-1] == 10,
])

y = cas.exp(-x)
y = np.exp(-x)

x1 = x[:-1]
x2 = x[1:]
errors = -x1 * cas.exp(-x2) / 2 - x1 * cas.exp(-x1) / 2 + x2 * cas.exp(-x2) / 2 + x2 * cas.exp(-x1) / 2 + cas.exp(
-x2) - cas.exp(-x1)
error = cas.sum1(errors)
errors = -x1 * np.exp(-x2) / 2 - x1 * np.exp(-x1) / 2 + x2 * np.exp(-x2) / 2 + x2 * np.exp(-x1) / 2 + np.exp(
-x2) - np.exp(-x1)
error = np.sum(errors)

opti.minimize(error * 1e3)
opti.solver('ipopt')
sol = opti.solve()

xopt = sol.value(x)
minerror = sol.value(error)
xopt = sol(x)
minerror = sol(error)

0 comments on commit 1229420

Please sign in to comment.