# -*- coding: utf-8 -*-
"""trapezoidal.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/14FqIk6y_oVN6rLuk4jRLcSCQfweSmHcX
"""

############******* Integration of a function using Trapezoidal method *********#############

#This module contains other modules such as numpy, math, matplotlib.pyplot as np,m,plt respectiely.
#The module contains two function definitions : 1. trapezoidal and 2. Test
# The arguments of the function definition trapezoidal are:
  # f    (The function which is to be integrated)
  # a,b  (Initial and final values of integration)
  # h    (Stepsize of integration) 
  # hvar (for different values of h, hvar=1)
  # pltn (for plotting pltn=0)

###########**************Steps involved in the integration ************#################

  #  Import the module trapezoidal.py
  #  Define the function for integration
  #  Give the value of  a,b,h,hvar,pltn as required
  #  Call the function trapezoidal(f,a,b,h,hvar,pltn)
  #  Call the function Test(f,a,b) 

##########************ A sample example for using the module *************#################
# from trapezoidal import *
# def f(x):
#   return (3*x+5)
# a = 1
# b =  10 
# h =  1
# hvar = 0
# pltn = 1
# trapezoidal(f,a,b,h,hvar,pltn)
# Test(f,a,b)
##################################----------------------------###########################

import matplotlib.pyplot as plt 
import math as m 
import numpy as np
def trapezoidal(f,a,b,h,hvar,pltn):              # Function for estimating the integral
  try:
    z =[0]                                   # Stores the xi appended from for loop as list
    sum = 0                                  
    N = int((b-a)/h)                         # The interval is divided into N subintervals 
    out = open("trapezoidal.txt", "w")           # Generates output file
    for i in range (1,N):                    
      x = a + h*i
      z.append(x)
      sum = sum+2*f(x)
    sum = sum + f(a) + f(b)                  
    Answer = (h/2)*sum                       
    z.remove(0)
    w = np.array(z)                          # Converting list z to array w
    if pltn == 1:                            # Plot condition for plotting the function
      plt.plot(w,f(w))
      plt.xlabel('x') 
      plt.ylabel('f(x)')
      plt.title('Plot of given funtion for integration')
      plt.show()                              # Shows the plot of the function

    if hvar == 1:                            # Condition for different values of h
      print(' '*(15-len('Stepsize'))+'Stepsize' + ' ' * (15-len('Integration')) + ' Integration',file = out)
      for i in range(5):
        sum = 0
        N = int((b-a)/h)
        for i in range (1,N):
          x = a + h*i
          sum = sum+2*f(x)
        sum = sum + f(a) + f(b)
        Ans = (h/2)*sum
        print('{:15.5f}{:15.7f}'.format((100*h),Ans,),file = out)
        h = h/10 
    else:
      print('The value of Integration is {:f}'.format(Answer),file=out) 
    out.close()
  except ZeroDivisionError:                                   # When function is undefined at some value
    print('Zero Division Error Encountered.')


from scipy.integrate import quad                              # Estimating the integral by quad inbuilt function as Test
def Test(f,a,b):
  out = open("trapezoidal.txt", "w")
  print(quad(f,a,b),file = out)
  out.close()
178f859a0973e968c301570e976c6b678d3b4ab4
<mask token>

<mask token>


def Test(f, a, b):
    out = open('trapezoidal.txt', 'w')
    print(quad(f, a, b), file=out)
    out.close()

<mask token>


def trapezoidal(f, a, b, h, hvar, pltn):
    try:
        z = [0]
        sum = 0
        N = int((b - a) / h)
        out = open('trapezoidal.txt', 'w')
        for i in range(1, N):
            x = a + h * i
            z.append(x)
            sum = sum + 2 * f(x)
        sum = sum + f(a) + f(b)
        Answer = h / 2 * sum
        z.remove(0)
        w = np.array(z)
        if pltn == 1:
            plt.plot(w, f(w))
            plt.xlabel('x')
            plt.ylabel('f(x)')
            plt.title('Plot of given funtion for integration')
            plt.show()
        if hvar == 1:
            print(' ' * (15 - len('Stepsize')) + 'Stepsize' + ' ' * (15 -
                len('Integration')) + ' Integration', file=out)
            for i in range(5):
                sum = 0
                N = int((b - a) / h)
                for i in range(1, N):
                    x = a + h * i
                    sum = sum + 2 * f(x)
                sum = sum + f(a) + f(b)
                Ans = h / 2 * sum
                print('{:15.5f}{:15.7f}'.format(100 * h, Ans), file=out)
                h = h / 10
        else:
            print('The value of Integration is {:f}'.format(Answer), file=out)
        out.close()
    except ZeroDivisionError:
        print('Zero Division Error Encountered.')


<mask token>


def Test(f, a, b):
    out = open('trapezoidal.txt', 'w')
    print(quad(f, a, b), file=out)
    out.close()

<mask token>
import matplotlib.pyplot as plt
import math as m
import numpy as np


def trapezoidal(f, a, b, h, hvar, pltn):
    try:
        z = [0]
        sum = 0
        N = int((b - a) / h)
        out = open('trapezoidal.txt', 'w')
        for i in range(1, N):
            x = a + h * i
            z.append(x)
            sum = sum + 2 * f(x)
        sum = sum + f(a) + f(b)
        Answer = h / 2 * sum
        z.remove(0)
        w = np.array(z)
        if pltn == 1:
            plt.plot(w, f(w))
            plt.xlabel('x')
            plt.ylabel('f(x)')
            plt.title('Plot of given funtion for integration')
            plt.show()
        if hvar == 1:
            print(' ' * (15 - len('Stepsize')) + 'Stepsize' + ' ' * (15 -
                len('Integration')) + ' Integration', file=out)
            for i in range(5):
                sum = 0
                N = int((b - a) / h)
                for i in range(1, N):
                    x = a + h * i
                    sum = sum + 2 * f(x)
                sum = sum + f(a) + f(b)
                Ans = h / 2 * sum
                print('{:15.5f}{:15.7f}'.format(100 * h, Ans), file=out)
                h = h / 10
        else:
            print('The value of Integration is {:f}'.format(Answer), file=out)
        out.close()
    except ZeroDivisionError:
        print('Zero Division Error Encountered.')


from scipy.integrate import quad


def Test(f, a, b):
    out = open('trapezoidal.txt', 'w')
    print(quad(f, a, b), file=out)
    out.close()

# -*- coding: utf-8 -*-
"""trapezoidal.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/14FqIk6y_oVN6rLuk4jRLcSCQfweSmHcX
"""

############******* Integration of a function using Trapezoidal method *********#############

#This module contains other modules such as numpy, math, matplotlib.pyplot as np,m,plt respectiely.
#The module contains two function definitions : 1. trapezoidal and 2. Test
# The arguments of the function definition trapezoidal are:
  # f    (The function which is to be integrated)
  # a,b  (Initial and final values of integration)
  # h    (Stepsize of integration) 
  # hvar (for different values of h, hvar=1)
  # pltn (for plotting pltn=0)

###########**************Steps involved in the integration ************#################

  #  Import the module trapezoidal.py
  #  Define the function for integration
  #  Give the value of  a,b,h,hvar,pltn as required
  #  Call the function trapezoidal(f,a,b,h,hvar,pltn)
  #  Call the function Test(f,a,b) 

##########************ A sample example for using the module *************#################
# from trapezoidal import *
# def f(x):
#   return (3*x+5)
# a = 1
# b =  10 
# h =  1
# hvar = 0
# pltn = 1
# trapezoidal(f,a,b,h,hvar,pltn)
# Test(f,a,b)
##################################----------------------------###########################

import matplotlib.pyplot as plt 
import math as m 
import numpy as np
def trapezoidal(f,a,b,h,hvar,pltn):              # Function for estimating the integral
  try:
    z =[0]                                   # Stores the xi appended from for loop as list
    sum = 0                                  
    N = int((b-a)/h)                         # The interval is divided into N subintervals 
    out = open("trapezoidal.txt", "w")           # Generates output file
    for i in range (1,N):                    
      x = a + h*i
      z.append(x)
      sum = sum+2*f(x)
    sum = sum + f(a) + f(b)                  
    Answer = (h/2)*sum                       
    z.remove(0)
    w = np.array(z)                          # Converting list z to array w
    if pltn == 1:                            # Plot condition for plotting the function
      plt.plot(w,f(w))
      plt.xlabel('x') 
      plt.ylabel('f(x)')
      plt.title('Plot of given funtion for integration')
      plt.show()                              # Shows the plot of the function

    if hvar == 1:                            # Condition for different values of h
      print(' '*(15-len('Stepsize'))+'Stepsize' + ' ' * (15-len('Integration')) + ' Integration',file = out)
      for i in range(5):
        sum = 0
        N = int((b-a)/h)
        for i in range (1,N):
          x = a + h*i
          sum = sum+2*f(x)
        sum = sum + f(a) + f(b)
        Ans = (h/2)*sum
        print('{:15.5f}{:15.7f}'.format((100*h),Ans,),file = out)
        h = h/10 
    else:
      print('The value of Integration is {:f}'.format(Answer),file=out) 
    out.close()
  except ZeroDivisionError:                                   # When function is undefined at some value
    print('Zero Division Error Encountered.')


from scipy.integrate import quad                              # Estimating the integral by quad inbuilt function as Test
def Test(f,a,b):
  out = open("trapezoidal.txt", "w")
  print(quad(f,a,b),file = out)
  out.close()