Esempio n. 1
0
    def shade_under_curve(self, fig, f):
        """
        shade_under_curve draws a plot of f from a to b and adds a
        lagrange fit that shows a pictorial representation of
        Gaussian-Legendre Quadrature.

        Parameters
        ----------
        fig : The figure in which to add the plots
        f : The function to be plotted.
        """
        # Add a subplot to the supplied figure.
        ax = fig.add_subplot(111)

        # Create the Lagrange Interpolation fit.
        x_points = self.evaluation_pts
        y_points = f(x_points)
        poly_fit = scibary(x_points, y_points)

        # Create the points to be graphed.
        xL = np.linspace(self.a, self.b, 150)
        yL = poly_fit(xL)

        # Draw yellow lines going from the x-axis to the evaluation
        # points.
        for x, y in zip(x_points, y_points):
            ax.plot([x, x], [0, y], 'y-')

        # Plot the Lagrange fit and shade underneath.
        ax.plot(xL, yL, 'y-')
        ax.fill_between(xL, yL, color='y', alpha='0.25')

        # Plot the actual function and set appropriate x-axis limits.
        ax.plot(xL, f(xL))
        ax.set_xlim([self.a, self.b])
Esempio n. 2
0
    def shade_under_curve(self, fig, f):
        """
        Adds floor(n/2) quadratic lagrange interpolants using 3
        adjacent points to fig and shades them underneath. Also draws
        a plot of f.

        Parameters
        ----------
        fig : The figure in which to add the trapezoids or rectangles.
        f : The function to be plotted.
        """
        # Add a sub plot to the supplied figure.
        ax = fig.add_subplot(111)

        # Create the points to make the Quadratic fits.
        x_points = self.evaluation_pts
        y_points = f(x_points)
        points = zip(x_points, y_points)
        pts1 = points[0:-1:2]
        pts2 = points[1::2]
        pts3 = points[2::2]
        plotPts = zip(pts1, pts2, pts3)

        # Plot the Quadratic fits and shade them in.
        for pts in plotPts:
            x_pts = [p[0] for p in pts]
            y_pts = [p[1] for p in pts]
            poly_fit = scibary(x_pts, y_pts)
            x1 = x_pts[0]
            x2 = x_pts[-1]
            if self.n > 150:
                xL = np.linspace(x1, x2, 1)
            else:
                xL = np.linspace(x1, x2, 150 / self.n)
            yL = poly_fit(xL)
            for x, y in pts:
                ax.plot([x, x], [0, y], 'y-')
            ax.plot(xL, yL, 'y-')
            ax.fill_between(xL, yL, color='y', alpha='0.25')

        # Plot the actual function.
        xL2 = np.linspace(self.a, self.b, 150)
        ax.plot(xL2, f(xL2))
        ax.set_xlim([self.a, self.b])