\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{float}
\title{Solving Second-Order Ordinary Differential Equations Using Neural Networks with DeepXDE}
\author{Your Name}
\date{\today}
\lstset{ %
language=Python,
basicstyle=\ttfamily\small,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{teal},
stringstyle=\color{orange},
showstringspaces=false,
frame=single, % box around the code
rulecolor=\color{black},
breaklines=true, % auto-wrap lines
postbreak=\mbox{\textcolor{red}{↪}\space},
}
\begin{document}
\maketitle
\section{Introduction}
This project explores solving a second-order ODE using neural networks through the DeepXDE library. The project demonstrates the use of machine learning in approximating solutions to differential equations, highlighting modern computational approaches.
\section{Problem Setup}
We will solve the following second-order ODE:
\begin{equation}
y''(t) - 10y'(t) + 9y(t) = 5t
\end{equation}
with initial conditions y(0)=−1 and y′(0)=2. The exact solution to this ODE is:
\begin{equation}
y(t) = \frac{50}{81} + \frac{5}{9}t - 2e^{t} + \frac{31}{81}e^{9t}
\end{equation}
\section{Implementation}
\subsection{Import Libraries}
We begin by importing the necessary libraries for solving the ODE.
\begin{lstlisting}
import deepxde as dde
import numpy as np
\end{lstlisting}
\subsection{Geometry Definition}
Define the computational time domain.
\begin{lstlisting}
geom = dde.geometry.TimeDomain(0, 0.25)
\end{lstlisting}
\subsection{ODE Residual Function}
Define the residual of the ODE using derivatives.
\begin{lstlisting}
def ode(t, y):
dy_dt = dde.grad.jacobian(y, t)
d2y_dt2 = dde.grad.hessian(y, t)
return d2y_dt2 - 10 * dy_dt + 9 * y - 5 * t
\end{lstlisting}
\subsection{Exact Solution}
The reference function for the exact solution.
\begin{lstlisting}
def func(t):
return 50 / 81 + t * 5 / 9 - 2 * np.exp(t) + (31 / 81) * np.exp(9 * t)
\end{lstlisting}
\subsection{Initial Conditions}
Define the initial conditions using DeepXDE's tools.
\begin{lstlisting}
ic1 = dde.icbc.IC(geom, lambda x: -1, lambda _, on_initial: on_initial)
def boundary_l(t, on_boundary):
return on_boundary and dde.utils.isclose(t[0], 0)
def error_2(inputs, outputs, X):
return dde.grad.jacobian(outputs, inputs, i=0, j=None) - 2
ic2 = dde.icbc.OperatorBC(geom, error_2, boundary_l)
\end{lstlisting}
\subsection{Constructing the PDE Problem}
Setup the PDE problem with the specified initial conditions.
\begin{lstlisting}
data = dde.data.TimePDE(geom, ode, [ic1, ic2], 16, 2, solution=func, num_test=500)
\end{lstlisting}
\subsection{Neural Network Construction}
Setup a neural network model.
\begin{lstlisting}
layer_size = [1] + [50] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.nn.FNN(layer_size, activation, initializer)
\end{lstlisting}
\subsection{Model Compilation and Training}
Compile and train the network to learn the ODE solution.
\begin{lstlisting}
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"], loss_weights=[0.01, 1, 1])
losshistory, train_state = model.train(iterations=10000)
\end{lstlisting}
\subsection{Saving and Plotting Results}
Visualize the solution and save the results.
\begin{lstlisting}
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
\end{lstlisting}
\section{Results and Discussion}
Training results show that the predictive power of the neural network model aligns closely with the exact solution. The L2 relative error demonstrates satisfactory approximation accuracy. Below is a plot comparing the actual and predicted functions:
\begin{figure}[H]
\centering
\includegraphics[width=0.7\textwidth]{ode_results.png}
\caption{Comparison of the neural network solution with the exact solution.}
\end{figure}
\section{Complete Code}
For reference, here is the complete code in one piece:
\begin{lstlisting}
import deepxde as dde
import numpy as np
def ode(t, y):
dy_dt = dde.grad.jacobian(y, t)
d2y_dt2 = dde.grad.hessian(y, t)
return d2y_dt2 - 10 * dy_dt + 9 * y - 5 * t
def func(t):
return 50 / 81 + t * 5 / 9 - 2 * np.exp(t) + (31 / 81) * np.exp(9 * t)
geom = dde.geometry.TimeDomain(0, 0.25)
def boundary_l(t, on_initial):
return on_initial and dde.utils.isclose(t[0], 0)
def error_2(inputs, outputs, X):
return dde.grad.jacobian(outputs, inputs, i=0, j=None) - 2
ic1 = dde.icbc.IC(geom, lambda x: -1, lambda _, on_initial: on_initial)
ic2 = dde.icbc.OperatorBC(geom, error_2, boundary_l)
data = dde.data.TimePDE(geom, ode, [ic1, ic2], 16, 2, solution=func, num_test=500)
layer_size = [1] + [50] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.nn.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"], loss_weights=[0.01, 1, 1])
losshistory, train_state = model.train(iterations=10000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
\end{lstlisting}
\section{Conclusion}
The use of neural networks with the DeepXDE library has shown great promise in solving ODEs, providing an effective methodology for approximating solutions to differential equations. Future work can explore more complex equations and extended domains.
\section{References}
\begin{itemize}
\item Lu, L., Meng, X., Mao, Z., & Karniadakis, G. E. (2021). DeepXDE: A deep learning library for solving differential equations. \emph{SIAM Review}, 63(1), 208-228.
\item Goodfellow, I., Bengio, Y., & Courville, A. (2016). \emph{Deep Learning}. MIT Press.
\item Kingma, D. P., & Ba, J. (2014). Adam: A method for stochastic optimization. \emph{arXiv preprint arXiv:1412.6980}.
\end{itemize}
\end{document}
At each step in 3 explain in detail each function with functions and the maths behind it