Skip to content

use DynamicsPointMass2DSpeedGamma to compute brachistochrone #154

@Zcaic

Description

@Zcaic

Hello, when I use DynamicsPointMass2DSpeedGamma to compute brachistochrone, I found the result is error. The brachistochrone question is

Image
The initial conditions are:
$$x_0$$=0, $$y_0$$=0, $$v_0$$=0
The final conditions are:
$$x_f$$=10, $$y_f$$=5

The control variable is "gamma". I want to minimize time.
I use the code below, but it doen't work:

N = 100
g = 9.81

opti = asb.Opti()

time_final = opti.variable(init_guess=5.0, lower_bound=0.01)
time = anp.linspace(0, time_final, N)

dyn = asb.DynamicsPointMass2DSpeedGamma(
    mass_props=asb.MassProperties(mass=1.0),
    x_e=opti.variable(init_guess=anp.linspace(0, 10, N), lower_bound=0.0),
    z_e=opti.variable(init_guess=anp.linspace(0, 5, N), lower_bound=0.0),
    speed=opti.variable(init_guess=1, n_vars=N),
    gamma=opti.variable(init_guess=0, n_vars=N, lower_bound=-anp.pi / 2, upper_bound=anp.pi / 2),
    alpha=0.0,
)

opti.subject_to(
    [dyn.x_e[0] == 0.0, dyn.x_e[-1] == 10.0, dyn.z_e[0] == 0.0, dyn.z_e[-1] == 5, dyn.speed[0] == 0.0]
)

dyn.add_force(Fx=-anp.sin(dyn.gamma) * g, axes="body")
dyn.constrain_derivatives(opti, time)

opti.minimize(time_final)
sol = opti.solve()

dyn = sol(dyn)

fig = plt.figure(0)
ax = fig.add_subplot(111)
ax.plot(dyn.x_e, -dyn.z_e)
plt.show()

the result is:
Image

it's wrong.

And I write my code as below, it works well:

N = 200
g = 9.81

opti = asb.Opti()

time_final = opti.variable(init_guess=5, lower_bound=0)
theta = opti.variable(init_guess=0, n_vars=N, lower_bound=0, upper_bound=anp.pi)

dt = time_final / (N - 1)

v = opti.variable(init_guess=0, n_vars=N, freeze=True)
x = opti.variable(init_guess=0, n_vars=N, freeze=True)
y = opti.variable(init_guess=0, n_vars=N, freeze=True)

for i in range(1, N):
    theta_mid = 0.5 * (theta[i - 1] + theta[i])

    v[i] = v[i - 1] + g * anp.cos(theta_mid) * dt
    x[i] = x[i - 1] + 0.5 * (v[i] + v[i - 1]) * anp.sin(theta_mid) * dt
    y[i] = y[i - 1] + 0.5 * (v[i] + v[i - 1]) * anp.cos(theta_mid) * dt

opti.subject_to([x[-1] == 10.0, y[-1] == 5.0])

opti.minimize(time_final)
sol = opti.solve()
fig = plt.figure(0)
ax = fig.add_subplot(111)
ax.plot(sol(x), -sol(y))
plt.show()

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions