# -*- coding: utf-8 -*-
"""
Created on Mon Sep 28 16:53:04 2026

@author: EYER
"""
## Import des bibliothèques
import numpy as np    
from math import *    
import matplotlib.pyplot as plt

## Fonction dichotomie
def dichotomie(f, y, xmin, xmax, epsilon):
    # Détermine la valeur de x qui vérifie f(x,y)=0 par dichotomie sur le domaine [xmin, xmax]
    # avec une précision de epsilon sur la valeur de x.
    a, b = xmin, xmax
    # Vérification que f(a,y) et f(b,y) sont de signes opposés
    if f(a, y) * f(b, y) > 0:
        print("Erreur : f(a,y) et f(b,y) doivent être de signes opposés")
        return None
    # Algorithme de dichotomie
    while abs(b - a) > epsilon:
        x = (a + b) / 2 # Calcul du milieu de l’intervalle
        if f(x, y) == 0: # Si on trouve la solution exacte
            return x
        elif f(x, y) * f(a, y) < 0: # Si f(x,y) et f(a,y) sont de signes opposés
            b = x # On réduit l’intervalle [a,x]
        else:
            a = x # On réduit l’intervalle [x,b]
    # Retourne la valeur approchée
    return (a + b) / 2
    
## Mesure des différentes grandeurs
k = 2/8 #m/cm - c'est l'échelle
L1 = 0.8*k
L2 = 1.4*k
L3 = 1.05*k
L4 = 4.7*k
L5 = 10.6*k
L6 = 0.8*k
L7 = 4.9*k
L8 = 1.0*k
t6 = np.pi/6

YA = 1.3*k
XD = -1.4*k
XF = -6.8*k
YF = 0.5*k
XH = -15*k
YH = 1.8*k
XL = -17.3*k
YL = 6.6*k

## Loi ES : DCBAD
def loi31(theta3, theta1):
    # Évalue la loi entrée sortie liant les angles theta3 et theta1 pour des valeurs particulières,
    # tel que loi31(theta3,theta1) = 0 si la loi est vérifiée.
    #
    return L2**2-(-XD+L3*sin(theta3)-L1*cos(theta1))**2-(YA-L3*cos(theta3)-L1*sin(theta1))**2

## Paramètres
n = 100
eps = 0.001

## Définition de theta1
theta1 = np.linspace(30*np.pi/180,120*np.pi/180,n)

## Obtention de theta3
theta3 = []
for t1 in theta1:
    solt3 = dichotomie(loi31, t1, -1, 2, eps)
    theta3.append(solt3)
theta3 = np.array(theta3)

# Tracé
plt.plot(theta1,theta3)
plt.grid()
plt.xlabel('$\\theta_1$ en radians')
plt.ylabel('$\\theta_3$ en radians')


## Obtention de lambda5
def LAMBDA5(t3):
    return np.sqrt(L2**2-(YF-L3*cos(t3)))-L4+XD-XF-L3*sin(t3)

LAMBDA5 = np.vectorize(LAMBDA5) ## Permet de construire une fonction vectorielle 
                                ## cela signifie que la fonction peut calculer 
                                ## directement chacun des termes d'un tableau array
                                ## en utilisant directement la fonction.
                                ## Cela évite de calculer les termes un à un avec une boucle.

lambda5 = LAMBDA5(theta3)

## Obtention de lambda6
def THETA6(l5):
    return atan(  (YF-YH) / (XF-XH-L5-l5)  )

THETA6 = np.vectorize(THETA6)

theta6 = THETA6(lambda5)

# Tracé
plt.figure()
plt.plot(theta1,theta6)
plt.grid()
plt.xlabel('$\\theta_1$ en radians')
plt.ylabel('$\\theta_6$ en radians')


## Loi ES : LKJHL Obtention de beta
ps6 = np.pi/6
def loibeta6(beta, theta6):
    # Évalue la loi entrée sortie liant les angles theta3 et theta1 pour des valeurs particulières,
    # tel que loi31(theta3,theta1) = 0 si la loi est vérifiée.
    #
    return L7**2 - ( XL-XH+L8*cos(beta)-L6*cos(theta6+ps6) )**2 - ( YL-YH+L8*sin(beta)-L6*sin(theta6+ps6) )**2

## Obtention de beta
beta = []
for t6 in theta6:
    solbeta = dichotomie(loibeta6, t6, -1, 3, eps)
    beta.append(solbeta)

beta = np.array(beta)

## Tracé
plt.figure()
plt.plot(theta1,beta)
plt.grid()
plt.xlabel('$\\theta_1$ en radians')
plt.ylabel('$\\beta$ en radians')





## Pour la régression
plt.figure()
plt.plot(theta3,beta)
plt.grid()
plt.xlabel('$\\theta_3$ en radians')
plt.ylabel('$\\beta$ en radians')

P = np.polyfit(theta3,beta,1)
print('On a une relation beta = '+str(round(P[0],3))+'*theta3 + '+str(round(P[1],3))+'.')






































