that_one_nigga_you_k

função terceiro grau

Jan 20th, 2025 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. from manim import *
  2. import numpy as np
  3.  
  4. config.pixel_height = 1920
  5. config.pixel_width = 1080
  6. config.frame_height = 16
  7. config.frame_width = 9
  8. config.frame_rate = 60
  9.  
  10. class Funcao(Scene):
  11.     def construct(self):
  12.         # Criando o plano cartesiano personalizado
  13.         npl = NumberPlane(
  14.             x_range=[-config.frame_width / 2, config.frame_width / 2],
  15.             y_range=[-config.frame_height / 2, config.frame_height / 2],
  16.             axis_config={"stroke_color": WHITE}
  17.         )
  18.         npl.add_coordinates()
  19.         self.play(Create(npl))
  20.  
  21.         # Função de terceiro grau: f(x) = ax³ + bx² + cx + d
  22.         a = ValueTracker(0)
  23.         b = ValueTracker(0)
  24.         c = ValueTracker(0)
  25.         d = ValueTracker(0)
  26.  
  27.         def cubic_function(x):
  28.             return a.get_value() * x**3 + b.get_value() * x**2 + c.get_value() * x + d.get_value()
  29.  
  30.         # Desenhar gráfico inicial
  31.         graph = always_redraw(lambda: npl.plot(cubic_function, color=YELLOW))
  32.         self.play(Create(graph))
  33.  
  34.         # Criando exibição do valor de 'a'
  35.         a_text = always_redraw(lambda:
  36.             MathTex(f"a = {a.get_value():.2f}").to_corner(UP + LEFT)
  37.         )
  38.         self.add(a_text)
  39.         b_text = always_redraw(lambda:
  40.             MathTex(f"b = {b.get_value():.2f}").next_to(a_text, DOWN)
  41.         )
  42.         c_text = always_redraw(lambda:
  43.             MathTex(f"c = {c.get_value():.2f}").next_to(b_text, DOWN)
  44.         )
  45.         d_text = always_redraw(lambda:
  46.             MathTex(f"d = {d.get_value():.2f}").next_to(c_text, DOWN)
  47.         )
  48.         self.add(b_text, c_text, d_text)
  49.  
  50.         # Função para encontrar as raízes reais do polinômio
  51.         def find_real_roots():
  52.             coef = [a.get_value(), b.get_value(), c.get_value(), d.get_value()]
  53.             roots = np.roots(coef)
  54.             # Retornar apenas raízes reais
  55.             real_roots = [root.real for root in roots if np.isreal(root)]
  56.             return real_roots
  57.  
  58.         # Inicializar pontos de interseção
  59.         intersection_circles = always_redraw(lambda:
  60.             VGroup(*[
  61.                 Circle(radius=0.1, color=RED).move_to(npl.c2p(root, 0))
  62.                 for root in find_real_roots()
  63.             ])
  64.         )
  65.         self.add(intersection_circles)
  66.  
  67.        # Lista com os valores para as animações
  68.         values = [
  69.                     (5, 3, -2, 1),
  70.                     (-5, -3, 2, -1),
  71.                     (2, -4, 3, 1),
  72.                     (1.5, -4, -8, 7),
  73.                     (-1.5, 10, 0.3, -5),
  74.                     (-1.5, 10, -30, -5),
  75.                     (1.5, -10, 3, 2.5),
  76.                     (2.5, -3, -5, 2)
  77.                 ]
  78.  
  79. # Loop para aplicar as animações com os valores da lista
  80.         for a_val, b_val, c_val, d_val in values:
  81.             self.play(
  82.                 a.animate.set_value(a_val),
  83.                 b.animate.set_value(b_val),
  84.                 c.animate.set_value(c_val),
  85.                 d.animate.set_value(d_val),
  86.                 run_time=5,
  87.                 rate_func=rate_functions.ease_in_out_sine
  88.             )
  89.         self.wait(2)
  90.  
  91.  
  92.  
Add Comment
Please, Sign In to add comment