Python Forum
Trapezoidal integration method in python. Getting wrong results
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trapezoidal integration method in python. Getting wrong results
#1
Hi, I am pretty new to Python so I want to appoligize ahead if my question is a bit silly.
I copied som finished code to test a well known numerical method Trapezoidal for computation of Integrals of functions:
The code worked , at least I thinki it did but I am getting the wrong result, and I just dont know why?

I create a function as seen below in the Python editor. I am using Spyder by the way.
def trapezoidal(f, a, b, n):
h = float(b-a)/n
result = 0.5*f(a) + 0.5*f(b)
for i in range(1, n):
result += f(a + i*h)
result *= h
return result
In the IPython Console I type this:

>>> from trapezoidal import trapezoidal
>>> from math import exp
>>> v = lambda t: 3*(t**2)*exp(t**3)
>>> n = 4
>>> numerical = trapezoidal(v, 0, 1, n)
>>> numerical


Instead of getting the result: 1.9227167504675762 as noted in the book I am reading. I get this:1.1621952071921449

Does anybody have an idea where the error is???
Reply


Messages In This Thread
Trapezoidal integration method in python. Getting wrong results - by auting82 - Oct-13-2017, 12:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Active Directory integration dady 2 466 Oct-13-2023, 04:02 AM
Last Post: deanhystad
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,387 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Help with Integration Pandas excel - Python Gegemendes 5 1,728 Jun-05-2022, 09:46 PM
Last Post: Gegemendes
  How to save some results in .txt file with Python? Melcu54 4 7,243 May-26-2021, 08:15 AM
Last Post: snippsat
Photo Integration of apache spark and Kafka on eclipse pyspark aupres 1 3,701 Feb-27-2021, 08:38 AM
Last Post: Serafim
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,191 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Tableau Time Series Prediction using Python Integration tobimarsh43 0 1,888 Jul-24-2020, 10:38 AM
Last Post: tobimarsh43
  R-PYTHON INTEGRATION RELATED PROBLEM arnab93 0 1,420 Jun-05-2020, 02:07 PM
Last Post: arnab93
  STATA/Python Integration jprender 0 1,804 May-03-2020, 09:38 PM
Last Post: jprender
  How to append one function1 results to function2 results SriRajesh 5 3,073 Jan-02-2020, 12:11 PM
Last Post: Killertjuh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020