Wrong cosine function graph in Manim

Viewed 143

How can I edit my code to make the graph in my Manim code similar to the actual graph of the function cos(1/x)?

Actual graph: Actual Graph

from manim import *
from numpy import cos, sqrt

class CreateGraph(GraphScene):
    def __init__(self, **kwargs):
        GraphScene.__init__(
            self,
            x_min=-5,
            x_max=5,
            y_min=-5,
            y_max=5,
            graph_origin=ORIGIN,
            axes_color=GREEN)

def construct(self):
    # Create Graph
    self.setup_axes(animate=True)
    f1 = self.get_graph(lambda x: cos(1/x) , x_min=-4, x_max=4)
    
    # Construct the Figures
    self.play(ShowCreation(f1))
    self.wait(1)

Graph from Manim: Graph from Manim

1 Answers

Since cos(1/x) isn't really defined at 0, try this:

f1 = self.get_graph(lambda x: cos(1/x), discontinuities=[inverse_interpolate(self.x_min, self.x_max, 0)])

Edit as per your liking. It basically tells manim to not render the graph for cos(1/x) at x=0.

Related