Numpy et Matplotlib

Numpy et Matplotlib

import matplotlib.pyplot as plt
import numpy as np

## Exercice 2

def cossin():
    x = np.linspace(0,4*np.pi,1000)
    ycos = np.cos(x)
    ysin = np.sin(x)
    pcos, = plt.plot(x,ycos); psin, = plt.plot(x,ysin)
    plt.legend([pcos,psin],['Cos','Sin'])
    plt.show()

## Exercice 3
def traj():
    t = np.linspace(0,2*np.pi)
    x = (np.sin(t))**3
    y = np.cos(t)-(np.cos(t))**4
    plt.plot(x,y)
    plt.show()

## Exercice 4
def Euler(F,t0,tf,y0,n):
    t=t0
    y=y0
    h=(tf-t0)/n
    abcisses=[t0]
    ordonnees=[y0]
    for i in range(n):
        y += h*F(t,y)
        t += h
        abcisses.append(t)
        ordonnees.append(y)
    return abcisses,ordonnees

a,b = Euler(lambda t,y: y, 0,1,1,100)
plt.plot(a,b)
x = np.linspace(0,1,100)
y = np.exp(x)
plt.plot(x,y)
plt.show()

## Exercice 5
def Euler2(F,G,t0,tf,x0,y0,n):
    t=t0
    x=x0
    y=y0
    h=(tf-t0)/n
    temps=[t0]
    lapins=[x0]
    renards=[y0]
    for i in range(n):
        x,y = x+h*F(t,x,y), y + h*G(t,x,y)
        t += h
        temps.append(t)
        lapins.append(x)
        renards.append(y)
    return temps, lapins, renards

## x et y en fonction du temps
t,x,y = Euler2(lambda t,x,y:x*(3-2*y),lambda t,x,y: y*(-4+x),0,10,10,1,500000)
plt.plot(t,x); plt.plot(t,y)
plt.show()

## y en fonction de x
plt.plot(x,y)
plt.show()