# -*- coding: utf-8 -*-

import numpy as np
import numpy.linalg as npl
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy.optimize import fsolve
from scipy.optimize import newton

##################################################################################################
# Résolution approchée de -u'' + g(u) = f dans ]0, 1[ avec conditions de Dirichlet homogènes     #
# par une méthode de tir.                                                                        #
##################################################################################################

def g(u):
	#return alpha*u
	return np.exp(u)

def f(x):
	#return(1 + 0.*x)
	return 4.*np.pi*np.pi*np.sin(2.*np.pi*x) + np.exp(np.sin(2.*np.pi*x)) # Pour ce second membre et g(u) = exp(u), la solution est sin(2 pi x). 

def champ(w,x):
	y = np.zeros(np.size(w))
	y[0] = w[1]
	y[1] = g(w[0]) - f(x)
	return y

#def sol(a):
#	W = odeint(champ,np.array([0.,a[0]]),X)
#	u1 = W[J-1,0]
#	return u1

def sol(a):
	W = odeint(champ,np.array([0.,a]),X)
	u1 = W[J-1,0]
	return u1

alpha = 1.
J = 1000 # Nombre de points
X = np.linspace(0.,1.,J)

#asol = fsolve(sol,0.)[0]
asol = newton(sol,0.)
print(asol)

W = odeint(champ,np.array([0.,asol]),X)
U = W[:,0]

plt.plot(X,U,label = 'Solution approchée')
plt.legend()
plt.grid('on')
plt.show()

#############################
