# Scipy Two-point Boundary value Problem

Nonlinear ODE Statement I would like to use scipy to solve the following:

u'' + (u')^2 = sin(x)
u(0)=0,
u(1)=1


where u = u(x).

Approach I am looking at the following link

https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.integrate.solve_bvp.html

Problem - Not sure about variable wrt x or How to input BC's

The example given in the link does not include a term wrt x, in particular not sure how to include

sin(x)


from the right hand side of my problem, nor the

u(1)=1


Work Done

In the following I try to modify the example from the above link to conform to my ODE above

   from scipy.integrate import *
import numpy as np

from numpy import *
def fun(x, y):
return np.vstack((y,np.sin(x) -(y)**2))
def bc(ya, yb):
return np.array([ya, yb-1])
x = np.linspace(0, 1, 5)
y_a = np.zeros((2, x.size))
y_b = np.zeros((2, x.size))
y_b = 3

from scipy.integrate import solve_bvp
res_a = solve_bvp(fun, bc, x, y_a)
res_b = solve_bvp(fun, bc, x, y_b)
x_plot = np.linspace(0, 1, 100)
y_plot_a = res_a.sol(x_plot)
y_plot_b = res_b.sol(x_plot)
import matplotlib.pyplot as plt
plt.plot(x_plot, y_plot_a, label='y_a')
plt.plot(x_plot, y_plot_b, label='y_b')
plt.legend()
plt.xlabel("x")
plt.ylabel("y")
plt.show()

• Could you add what you actually find questionable about your result? Apart from not using the correct DE as per answer. You also should check res.success or print res.message, as it is not unlikely that something goes wrong with the convergence of the BVP solver. Also explore the tol parameter, the default is rather high. – LutzL Jan 6 at 23:14

## 1 Answer

I think in NumPy, if y is a matrix, y gets you the 0-th row, but it usually makes the code clearer if you use y[0,:].

The primary problem I see is that in fun you're defining the ODE $$u''+u^2=\sin x,$$ using y[0,:] instead of y[1,:].