Giter Club home page Giter Club logo

calculus's Introduction

Calculus

Calculate area under a graph.

Maintainability

The program uses the trapezoidal rule to calculate the area under a graph with a custom number of splits. The individual splits are shown on the graph using matplotlib.

โšก Set-up

pip install matplotlib

๐Ÿ“‹ How it Works

  1. The line equation is calculated for each set of points by calculating the gradient and solving for the y-intercept.
# Gets gradient and y-intercept for each line
    def getEquations():
        # Using equation y=mx+c
        counter = 1
        m = []
        c = []
        while counter <= len(x)-1:
            yDif = y[counter] - y[counter - 1]
            xDif = x[counter] - x[counter - 1]
            grad = yDif / xDif
            yIntercept = y[counter] - x[counter] * grad

            m.append(grad)
            c.append(yIntercept)

            counter += 1

        return m, c
  1. The x-coordinate for every split is calculated
    # Gets x-coords for each split
    def getSplits():
        interval = (x[-1] - x[0]) / splits
        x1 = [x[0]]
        for i in range(splits):
            xCoord = x[0] + interval * (i+1)
            x1.append(xCoord)

        return x1
  1. Calculate where each vertical split intersects the graph
    # Gets y-coord of the intersection for each vertical line split
    def getIntersections():
        # Using equation y=mx+c
        counter = 1
        equation = 1
        y1 = [y[0]]
        while counter <= splits-1:
            while x1[counter] >= x[equation]:
                equation += 1
            gradient = m[equation-1]
            yIntercept = c[equation-1]
            xCoord = x1[counter]
            # print(gradient, yIntercept, xCoord)

            yCoord = gradient * xCoord + yIntercept
            y1.append(yCoord)

            counter += 1
        y1.append(y[-1])

        return y1

๐Ÿ“ˆ Analysis of methods

Method 1

Since the graph is made of straight lines, by splitting the graph into triangles and trapeziums at each point, the real area under the graph can be calculated.

    def method1():
        # Using equation (y1+y2)*(x2-x1)/2
        area1 = 0
        for i in range(len(m)):
            area1 += (y[i]+y[i+1]) * (x[i+1]-x[i]) / 2

        return area1

Method 2

By using a trapezoidal equation devised by my friend MartinMimi, the area under the graph is approximated; the higher the number of splits, the more accurate the approximation.

    def method2():
        # Using equation x/2n(2ฮฃy-(y[0]+y[-1]))
        area2 = ((x[-1]-x[0])/(2*splits)) * (2*sum(y1)-(y1[0]+y[-1]))

        return area2

๐ŸŽฌ Screenshots

velocity time graph high number of splits

velocity time graph low number of splits

๐Ÿ“œ Credits

Everything is coded by Alex lo Storto

Licensed under the MIT License.

calculus's People

Contributors

alexlostorto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.