# -*- coding: utf-8 -*-
"""
Created on Thu Dec 15 18:16:32 2022

@author: EYER
"""

####################################################################
########################### Exercice n°1 ###########################
####################################################################
import numpy as np 
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def F(Y,t):    
    return (1/Y) - 5*Y*np.sin(Y)

t0 = 0
tf = 5.
n = 1000 ## nb de subdivisions

Y0 = 2.



tab_t = np.linspace(t0,tf,n+1) ## n+1 points de discrétisation

tab_sol=odeint(F,Y0,tab_t)

plt.plot(tab_t,tab_sol)







####################################################################
########################### Exercice n°2 ###########################
####################################################################
import numpy as np 
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def F(Y,t):
    u = Y[0]
    v = Y[1]
    uprim = -np.cos(v) - np.cos(t)
    vprim = -np.sin(u) - np.sin(t)
    return [uprim,vprim]
    # return - 2*t*Y + np.cos(t)

t0 = 0
tf = 1.
n = 500 ## nb de subdivisions

Y0 = (2.,4.)



tab_t = np.linspace(t0,tf,n+1) ## n+1 points de discrétisation

tab_sol=odeint(F,Y0,tab_t)

plt.figure()
plt.plot(tab_sol[:,0],tab_sol[:,1])








####################################################################
########################### Exercice n°3 ###########################
####################################################################
import numpy as np 
import matplotlib.pyplot as plt
from scipy.integrate import odeint

m = 10
g = 9.81
L = 3
C = 50
w = np.sqrt(9.81)
J = 90

def F(Y,t):
    theta = Y[0]
    dtheta = Y[1]
    thetaprim = dtheta
    if dtheta >= 0. and theta <= 0.:    
        dthetaprim = -m*g*L*np.sin(theta)/J + C/(2*J)
    else:
        dthetaprim = -m*g*L*np.sin(theta)/J
        
    return [thetaprim,dthetaprim]
    # return - 2*t*Y + np.cos(t)

t0 = 0
tf = 120.
n = 1000 ## nb de subdivisions

Y0 = (0.1,0)



tab_t = np.linspace(t0,tf,n+1) ## n+1 points de discrétisation

tab_sol=odeint(F,Y0,tab_t)

plt.figure()
plt.plot(tab_t,tab_sol[:,0])