0% found this document useful (0 votes)
3 views

trapezoidal dev

The document contains Python code implementing the Trapezoidal rule to approximate the definite integral of the function f(x) = 1/(1+x^3) over the interval [0, 1]. It defines a function to calculate the integral and outputs the result, which is approximately 0.8339. The code is designed for use with Python 3.7.4.

Uploaded by

Dextro leo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

trapezoidal dev

The document contains Python code implementing the Trapezoidal rule to approximate the definite integral of the function f(x) = 1/(1+x^3) over the interval [0, 1]. It defines a function to calculate the integral and outputs the result, which is approximately 0.8339. The code is designed for use with Python 3.7.4.

Uploaded by

Dextro leo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

@author: Devesh Deshmukh

PRN: 10303320181161210034

BATCH: B 2nd year mechanical Engineering

"""

INPUT:

# Python3 code to implement Trapezoidal rule

# A sample function whose definite

# integral's approximate value is

# computed using Trapezoidal rule

def y( x ):

# Declaring the function

# f(x) = 1/(1+x*x*x*x)

return (1 / (1 + x*x*x))

# Function to evalute the value of integral

def trapezoidal (a, b, n):

# Grid spacing

h = (b - a) / n

# Computing sum of first and last terms

# in above formula

s = (y(a) + y(b))

# Adding middle terms in above formula

i=1
while i < n:

s += 2 * y(a + i * h)

i += 1

# h/2 indicates (b-a)/2n.

# Multiplying h/2 with s.

return ((h / 2) * s)

# Driver code to test above function

# Range of definite integral

x0 = 0

xn = 1

#Number of grids. Higher value means

#more accuracy

n=6

print("Value of integral is","%.4f"%trapezoidal(x0,xn,n))

OUTPUT:

Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]

Type "copyright", "credits" or "license" for more information.

IPython 7.8.0 -- An enhanced Interactive Python.

runfile('D:/dev_python/trapezoidal.py', wdir='D:/dev_python')

Value of integral is 0.8339

You might also like