Using neural network

The code below solves the ODE using a neural network. The number of values for the input \( \vec x \) is 10, number of hidden neurons in the hidden layer being 10 and th step size used in gradien descent \( \lambda = 0.001 \). The program updates the weights and biases in the network num_iter times. Finally, it plots the results from using the neural network along with the analytical solution.

npr.seed(15)

## Decide the vales of arguments to the function to solve
N = 10
x = np.linspace(0, 1, N)

## Set up the initial parameters
num_hidden_neurons = 10
num_iter = 10000
lmb = 0.001

P = solve_ode_neural_network(x, num_hidden_neurons, num_iter, lmb)

res = g_trial(x,P) 
res_analytical = g_analytic(x)

print('Max absolute difference: %g'%np.max(np.abs(res - res_analytical)))

plt.figure(figsize=(10,10))

plt.title('Performance of neural network solving an ODE compared to the analytical solution')
plt.plot(x, res_analytical)
plt.plot(x, res[0,:])
plt.legend(['analytical','nn'])
plt.xlabel('x')
plt.ylabel('g(x)')
plt.show()