# -*- coding: utf-8 -*-
"""
Annexe du DM 12

@author: pjaub
"""

import matplotlib.pyplot as plt
import math as m

plt.close('all')


x0 = 1
y0 = 0.5
T = 20
h = 0.01


def lapin(x, y):
    return x - x*y

def lynx(x, y):
    return -a*y + x*y

def resol_1(x0 , y0 , T, h):
    x, y = x0, y0
    t = 0
    Lx = [x]
    Ly = [y]
    Lt = [t]
    while t+h <= T:
        t += h
        Lt.append(t)
        x, y = x + h*lapin(x,y), y + h*lynx(x, y)
        Lx.append(x)
        Ly.append(y)
    return [Lt , Lx , Ly]

def trace_pop_1():
    L = resol_1(x0 , y0 , T, h)
    plt.plot(L[0], L[1])
    plt.plot(L[0], L[2],'--')
    plt.subplots_adjust(bottom=0.2)  # augmente la marge basse
    plt.xlabel("Figure 1 - Tracé donné par trace_pop_1", fontsize = 16, fontfamily="serif", labelpad=15)
    plt.show()
    
def fonctionV(x, y):
    return x - a*m.log(x)+y-m.log(y)
    
def trace_v_1 ():
    L = resol_1(x0 , y0 , T, h)
    Lv = []
    for k in range(len(L[1])):
        v = fonctionV( L[1][k], L[2][k] )
        Lv.append(v)
    plt.plot(L[0], Lv)
    plt.show ()
    
def resol_2(x0 , y0 , T, h):
    x, y = x0, y0
    t = 0
    Lx = [x]
    Ly = [y]
    Lt = [t]
    while t+h <= T:
        t += h
        Lt.append(t)
        u = x + h*lapin(x,y)
        w = y + h*( -a*y + x*y )      # ici il y avait une erreur d'énoncé
        aux_x = x + h/2*lapin(x,y)+h/2*lapin(u, w)
        y = y + h/2*lynx(x,y)+h/2*lynx(u, w)
        x = aux_x
        Lx.append(x)
        Ly.append(y)
    return [Lt , Lx , Ly]


def trace_pop_2():
    L = resol_2(x0 , y0 , T, h)
    plt.plot(L[0], L[1])
    plt.plot(L[0], L[2], '--')
    plt.subplots_adjust(bottom=0.2)  # augmente la marge basse
    plt.xlabel("Figure 3 - Tracé donné par trace_pop_2", fontsize = 16, fontfamily="serif", labelpad=15)
    plt.show()

def trace_v_2():
    L = resol_2(x0 , y0 , T, h)
    Lv = []
    for k in range(len(L[1])):
        v = fonctionV( L[1][k], L[2][k] )
        Lv.append(v)
    plt.plot(L[0], Lv)
    plt.ylim(2.192, 2.194)


def trace_phase_1():
    L = resol_1(x0 , y0 , T, h)
    plt.plot(L[1], L[2])
    plt.title("Avec la méthode d'Euler")
    plt.show()
    
def trace_phase_2():
    L = resol_2(x0 , y0 , T, h)
    plt.plot(L[1], L[2])
    plt.title("Avec la méthode de Heun")
    plt.show()

a = 2

plt.figure('Figure 1')
trace_pop_1()


fig = plt.figure('Figure 2', figsize = (10,3))
plt.subplot(121)
trace_v_1()
plt.subplot(122)
trace_v_2()
fig.subplots_adjust(bottom=0.2)

# Ajout du texte centré sous toute la figure
fig.text(0.5, 0.02,
         "Figure 2 - Tracé donné par trace_V_1 (à gauche) et par trace_V_2 (à droite)",
         ha='center',
         fontsize=16, fontfamily="serif")

plt.figure('Figure 3')
trace_pop_2()

plt.figure('Figure 4 : Prédateurs en fonction des Proies', figsize = (10,3))
plt.subplot(121)
trace_phase_1()
plt.subplot(122)
trace_phase_2()
