I am trying to solve $\frac{u''}{1+u'^2} - \frac{1}{2(1-u)} = 0$ subject to $u(0)=1, u(1)=0$.
If I understand how to do this properly, I first do the variable substitutions:
$u = y$, $y_1 = y; y_2 = y'$
yielding:
$$\frac{y_2'}{1+y_2^2} - \frac{1}{2(1-y_1)} = 0 \iff y'_2 = \frac{1 + y_2^2}{2(1-y_1)}$$
And the boundary conditions give $y(0) + y(1) - 1 = 0$
To solve the transformed problem I tried using scipy this way:
import numpy as np
from scipy.integrate import solve_bvp
import matplotlib.pyplot as plt
def fun(x, y):
denom = 2.0 * (1.0-y[0])
num = 1.0 + y[1]**2
result = num / denom
return np.vstack((y[1], result))
def bc(ya, yb):
return ya + yb - 1.0
x = np.linspace(0, 1, 10, endpoint=False)
y = np.full((2, x.shape[0]), 0.01)
res = solve_bvp(fun, bc, x, y, verbose=2)
x_plot = np.linspace(0, 1, 100)
y_plot = res.sol(x_plot)
e_plot = 1.0 - np.sqrt(-(x_plot - 2.0)*x_plot)
plt.plot(x_plot, y_plot[0], label='$y$ approx')
plt.plot(x_plot, e_plot, label='$y$ exact')
plt.legend(loc='lower right')
plt.show()
But that is giving me:
Which is not even close to correct.
endpoint=False
is wrong, you want the first and last point be the boundaries of the interval. As you do it you haveb=0.9
, that is, the second condition isu(0.9)=0
. // It is not necessary to sanitize the return values,return y[1], result
andreturn ya[0]-1, yb[0]
works equally well. $\endgroup$