import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt


########################################################################
### Equation d'ordre 1 :
########################################################################


### exemple avec (1+t^2)y'-(t+1)y=2 avec y(0)=1

T = np.linspace(0, 10, 41)

def f(x) :
    # solution "exacte"
    return x-1+2*np.sqrt(x**2+1)*np.exp(np.arctan(x))

def phi(y,t) :
    return (2+(t+1)*y)/(t**2+1)

S = spi.odeint(phi,1,T)

plt.plot(T,S,"o",label='solution approchée')
plt.plot(T,f(T),label='solution exacte')
plt.legend(loc='best')
plt.xlabel('t')
plt.ylabel('y(t)')
plt.show()


########################################################################
### Equation d'ordre 2 :
########################################################################

# exemple avec (1+t^2)y''+4ty'+2y=0 avec y(0)=2 et y'(0)=1

##T = np.linspace(0, 10, 41)
##
##def f(t) :
##    # solution "exacte"
##    return (2+t)/(1+t**2)
##
##def phi(Y,t) :
##    return [Y[1],-2/(1+t**2)*(Y[0]+2*t*Y[1])]
##
##S = spi.odeint(phi,[2,1],T)
##
##plt.plot(T,S[:,0],"o",label='solution approchée')
##plt.plot(T,S[:,1],"o",label='dérivée ')
##plt.plot(T,f(T),label='solution exacte')
##plt.legend(loc='best')
##plt.xlabel('t')
##plt.ylabel('y(t)')
##plt.show()


