# -*- coding: utf-8 -*-
"""
Annexe du DM 12

@author: pjaub
"""

import matplotlib.pyplot as plt
import math as m

plt.close('all')

a = 2

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 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_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()

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()
