# -*- coding: utf-8 -*-
"""
Created on Mon Mar 12 15:40:07 2018

@author: jaramillo
"""


#%% Une figure simple. Version 1
import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(-np.pi,np.pi,101)
y=np.sin(x)+np.sin(3*x)/3.0

plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Simple plot')

plt.show()
plt.savefig("simple_plot.pdf")

#%% Une figure simple. Version 2
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x,y)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Simple plot")


plt.show()
fig.savefig("simple_plot.pdf")

#%% Figure multiple
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi,np.pi,101)  
f = np.ones_like(x)
f[x<0] = -1
y1=(4/np.pi)*(np.sin(x)+np.sin(3*x)/3.0)
y2=y1+(4/np.pi)*(np.sin(5*x)/5.0+np.sin(7*x)/7.0)
y3=y2+(4/np.pi)*(np.sin(9*x)/9.0+np.sin(11*x)/11.0)

fig = plt.figure()
ax= fig.add_subplot(111)  #111 est equivalent a 1,1,1 

ax.plot(x,f,'b-',lw=3,label='f(x)')
ax.plot(x,y1,'c--',lw=2,label='two terms')
ax.plot(x,y2,'r-.',lw=2,label='four terms')
ax.plot(x,y3,'b:',lw=2,label='six terms')
ax.legend(loc='best')
ax.set_xlabel('x',style='italic')
ax.set_ylabel('partial sums',style='italic')
fig.suptitle('Partial sums for Fourier series of f(x)', size=16,weight='bold')

fig.show()
fig.savefig("not_so_simple_plot.pdf")


#%% Repondre a l'exercice 1.


#%% EDO simple: Repondre a l'exercice 2.


#%% Resolution num´erique d'une EDO simple 
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def rhs(Y,t,a):
    y = Y
    return -a*y

t_arr=np.linspace(0,10,101)
y_init=[1]
a=1.0
y_arr=odeint(rhs, y_init, t_arr, args=(a,))
y=y_arr[:,0]


#%% Repondre a l'exercice 3.


#%% Resolution numerique d'un systeme d'EDOs simple
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def rhs(u,t,a,b):
    x,y = u
    return -a*y, -b*x

t_arr=np.linspace(0,10.,101)
u_init=[1,0]
a, b = 1., -1.
u_arr=odeint(rhs, u_init, t_arr, args=(a,b,))
x, y = u_arr[:,0], u_arr[:,1]

fig=plt.figure()
ax1=fig.add_subplot(121)
ax1.plot(t_arr, x, t_arr, y)
ax1.set_xlabel('t')
ax1.set_ylabel('x et y')
ax2=fig.add_subplot(122)
ax2.plot(x,y)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
fig.tight_layout()
#fig.subplots_adjust(top=0.90)
plt.savefig("system_EDO.pdf")


#%% EDO de deuxi`eme ordre : Repondre a l'exercice 4.


#%% Oscillateur harmonique : Repondre a l'exercice 5.


#%% Implementer dans un fichier different ”trajectoires.py” le
# code dans la section 3.1.1


#%% Oscillateur de Van dr Pol : Repondre a l'exercice 6.
#def jac(y,t,mu):


#%% Van der Pol
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def rhs(y,t,mu):
    return [y[1], mu*(1-y[0]**2)*y[1] - y[0]]

def jac(y,t,mu): # remplir ici avec la fonction construite dans l’exercice 6

mu=1.0
t_final = 15.0 if mu<10 else 4.0*mu
n_points = 1001 if mu<10 else 1001*mu
t=np.linspace(0, t_final,n_points)
y0=np.array([2.0,0.0])
y,info=odeint(rhs,y0,t,args=(mu,),Dfun=jac,full_output=True)

print " mu = %g, le nombre d’appels au Jacobien est %d" % \
(mu, info[’nje’][-1])

plt.plot(y[:,0],y[:,1])
plt.savefig("Van_der_Pol.pdf")

#%% Equations de Kepler (Cartesiennes) : Repondre a l'exercice 7.


#%% Equations de Lorentz : Repondre a l'exercice 8.

