Base ODE 2
The full solution can be found at basic_odes.ipynb.
Problem Setup
\[\frac{d x}{d t} = x, x(0)=1\]
The solution is \(x(t)=e^t\)
Implementation
-
Import necessary packages
-
Define problem
Here, we use the default training configuration, and default setup for learnable endogenous variable.ode2 = PDEModel("ode2") # define PDE model to solve ode2.set_state(["t"], {"t": [-2., 2.]}) # set the state variable, which defines the dimensionality of the problem ode2.add_endog("x") # we use endogenous variable to represent the function we want to approximate ode2.add_endog_equation("x_t=x", label="base_ode") ode2.add_endog_condition("x", "x(SV)", {"SV": torch.zeros((1, 1))}, Comparator.EQ, "1", {}, label="initial_condition")
-
Train and evaluate
-
To load a trained model
-
Plot the solutions
fig, ax = plt.subplots(1, 3, figsize=(18, 6)) t = np.linspace(-2, 2) ax[0].plot(t, np.exp(t), label="e^t") ax[1].plot(t, np.exp(t), label="e^t") ax[2].plot(t, np.exp(t), label="e^t") ode2.endog_vars["x"].plot("x", {"t": [-2, 2]}, ax=ax[0]) ode2.endog_vars["x"].plot("x_t", {"t": [-2, 2]}, ax=ax[1]) ode2.endog_vars["x"].plot("x_tt", {"t": [-2, 2]}, ax=ax[2]) plt.subplots_adjust() plt.show()