## Exercice 1

from matplotlib.pyplot import *

from math import *

# plot([5,0],[3,2])
#
# plot([1,7,5,1],[2,2,4,1])
#
# show()

# for p in range(2,11):
#     n=2**p
#     X=[cos(2*k*pi/n) for k in range(n+1)]
#     Y=[sin(2*k*pi/n) for k in range(n+1)]
#     plot(X,Y,label="n="+str(n))

n=34
m=15
X=[cos(2*m*k*pi/n) for k in range(n+1)]
Y=[sin(2*m*k*pi/n) for k in range(n+1)]
plot(X,Y,label="n="+str(n))

axis("equal")
title("Un joli dessin")

show()

## Exercice 2

from matplotlib.pyplot import *
from numpy import *

# Définition de la fonction :
def f(t):
    return t*sqrt(1-t**2)

# Bornes du graphique, a<=x<=b  et  c<=y<=d :
a,b=-1,1
c,d=-1,1

# Tracé de la fonction :
x=linspace(a,b,200)
y=f(x)
plot(x,y,label="y=f(x)")

theta=linspace(0,2*pi,1000)
plot(cos(theta),sin(theta))

# Grille :
grid()

# Axes :
axhline(color="black")
axvline(color="black")
xlabel("x")
ylabel("y")

# Repère orthonormé :
axis("equal")

# Fenêtre à afficher :
axis([a,b,c,d])

# Légende :
legend(loc="upper right")   # ou "lower left" etc.

# Affichage :
show()

## Exercice 3

from numpy import *
from matplotlib.pyplot import *

K=1
om=70.71
tau=0.004
xi=0.141

H=lambda p:K*(1+tau*p)/(1+(2*xi/om)*p+p**2/om**2)

puissance_w=linspace(0,4,401)

W=10**puissance_w

phase=angle(H(1j*W))

gain=20*log(absolute(H(1j*W)))

subplot(2,1,1)
semilogx(W,gain)
subplot(2,1,2)
semilogx(W,phase)
show()

## Exercice 4

from matplotlib.pyplot import *
from numpy import *
from cv2 import *
import matplotlib.animation as animation
#
# Writer = animation.writers['ffmpeg']
# writer = Writer(fps=15, metadata=dict(artist='You'), bitrate=1800)

alpha=4

def f(t):
    return sin(t)/(alpha+cos(t))

def df(t):
    return (1+alpha*cos(t))/(alpha+cos(t))**2

a,b=-15,15
c,d=-10,10

x=linspace(a,b,200)
y=f(x)

xb=array([-1,-3,0,3,1,-1])
yb=array([0,1,4,1,0,0])

h=0.5


# plot(x,f(x))
# axis([a,b,c,d])
# pause(5)

for x0 in range(-15,16):
    clf()

    # fig = figure()
    # ims = []

    # alpha=3*(1+1/(1+(x0/10)**2))
    # h=0.5*(1+1/(1+(x0/10)**2))

    alpha=3*(1+cos(x0)/4)
    h=0.5*(1+sin(x0/1.7)/2)

    xb1=h*xb
    yb1=h*yb

    th=arctan(df(x0))
    xb2=xb1*cos(th)-yb1*sin(th)
    yb2=xb1*sin(th)+yb1*cos(th)

    xb3=xb2+x0
    yb3=yb2+f(x0)

    # cap = VideoCapture(0)
    # ax=
    plot(x,f(x))
    plot(xb3,yb3)
    axis([a,b,c,d])

    # im_ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=3000,
    #                                blit=True)
    # im_ani.save('im.mp4', writer=writer)

    # im = imshow(grab_frame(cap))
    # im.set_data(grab_frame(cap))
    pause(1e-3)

show()
