File size: 2,357 Bytes
69d4c13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d74132f
 
 
 
 
 
 
 
 
 
 
 
f910a89
ccccc19
d74132f
 
 
 
 
 
69d4c13
ccccc19
d74132f
ccccc19
d74132f
 
ccccc19
d74132f
 
ccccc19
d74132f
69d4c13
 
 
 
 
d74132f
180363d
ce6c838
d74132f
 
 
 
 
ce6c838
cabf9fd
 
 
69d4c13
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
import matplotlib.pyplot as plt
from smolagents.tools import tool
import time

@tool
def plot_parabola_graph(a: float, b: float, c: float) -> str:
    """
    Gera e salva o gráfico de uma função quadrática (parábola) a partir de seus coeficientes a, b, e c.
    A função tem o formato: y = ax² + bx + c.
    Args:
        a: O coeficiente 'a' (não pode ser zero).
        b: O coeficiente 'b'.
        c: O coeficiente 'c'.
    Returns:
        O caminho do arquivo da imagem PNG gerada.
    """
    if a == 0:
        return "Erro: O coeficiente 'a' não pode ser zero para uma parábola."
    
    try:
        # Calcula as raízes 
        delta = b**2 - 4*a*c
        
        if delta >= 0:
            raiz1 = (-b - np.sqrt(delta)) / (2*a)
            raiz2 = (-b + np.sqrt(delta)) / (2*a)
            # Define o intervalo X para ir um pouco além das raízes
            x_min_plot = min(raiz1, raiz2) - 1.5
            x_max_plot = max(raiz1, raiz2) + 1.5
        else: # Se não houver raízes reais, usa o vértice como centro
            x_min_plot = -5
            x_max_plot = 5
        
        
        x_vertice = -b / (2*a)
        y_vertice = a * x_vertice**2 + b * x_vertice + c

        # Aumenta o intervalo para garantir que o vértice e as raízes estejam visíveis
        x_min_plot = min(x_min_plot, x_vertice - 2)
        x_max_plot = max(x_max_plot, x_vertice + 2)

        
        x = np.linspace(x_min_plot, x_max_plot, 400)
        
        y = a * x**2 + b * x + c

        
        plt.figure(figsize=(8, 6))
        plt.plot(x, y)
        
        plt.title("Gráfico da Parábola")
        plt.xlabel("x")
        plt.ylabel("y")
        plt.grid(True)
        plt.axhline(0, color='black', linewidth=0.5)
        plt.axvline(0, color='black', linewidth=0.5)

        
        
        # Ajusta os limites do eixo Y para focar no vértice
        y_min_plot = min(y_vertice, 0) - abs(y_vertice) * 0.5 - 2
        y_max_plot = max(y_vertice, y.max()) + abs(y_vertice) * 0.5 + 2
        plt.ylim(y_min_plot, y_max_plot)

        file_path = f"parabola_{int(time.time_ns())}.png"
        plt.savefig(file_path)
        plt.close()

        return f"Gráfico da parábola gerado e salvo em: {file_path}"
    except Exception as e:
        return f"Erro ao gerar o gráfico da parábola: {str(e)}"