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
#2
well, with this

from math import exp
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



v = lambda t: 3*(t**2)*exp(t**3)
n = 4
numerical = trapezoidal(v, 0, 1, n)
print numerical
I get 1.92271675047
EDIT: after fixing the indent (line #7)  I get the result from the book
Reply
#3
Hi Buran and thanks for reply.
When I put the code in the editor and run, I also get the correct result but not when I enter it in Ipython console as an interactive session.
However I have the another issue with the Midpoint method. I have calculated by hand the integral of x^2-2x by midtpoint method shall be 36.I run the program and get 24??
from math import exp
def midpoint(f, a, b, n):
    h = float(b-a)/n
    result = 0
    for i in range(n):
        result += f((a + h/2.0) + i*h)
        result *= h
        return result
Again I type this in IPython console and end up with a weird result:
from midpoint import midpoint

from math import exp

v = lambda t: 3*(t**2)*exp(t**3)

n=4

numerical = midpoint(v, 0, 1, n)

numerical
Out[87]: 0.011741660549894565
Reply
#4
Unindent line 8 one level
Reply
#5
(Oct-14-2017, 03:50 PM)buran Wrote: Unindent line 8 one level

Thanks, that worked.
As I am new to python, I am a bit confused on how to know if its an identation error as the program dosen't tell me Think ??

I have another script copied from the book to be tested and gives wrong results, I suspect its the same issue. However Python dosent alarm me that there is an error. Wall
In this script I am importing the functions midpoint and trapezoidal and comparing.
However I am getting wrong result, because as the number of rectangles and trapezoids increase(larger n) I should be getting answers closer to the true value of the integral Think
from trapezoidal import trapezoidal
from midpoint import midpoint
from math import exp
g = lambda y: exp(-y**2)
a = 0
b = 2
print ’ n midpoint trapezoidal’
for i in range(1, 21):
n = 2**i
m = midpoint(g, a, b, n)
t = trapezoidal(g, a, b, n)
print ’%7d %.16f %.16f’ % (n, m, t)
Reply
#6
(Oct-14-2017, 05:20 PM)auting82 Wrote: Thanks, that worked.

Actually looking at it for second time, I think you need to unindent also line 7 in order to implement midpoint method

(Oct-14-2017, 05:20 PM)auting82 Wrote: As I am new to python, I am a bit confused on how to know if its an identation error as the program dosen't tell me Think ??

There is no indentation error in your code - it is valid python code and thus the interpreter does not raise any error. However it is incorrect in the sense that it does not implement the desired algorithm - because the return statement is within the for loop body, it actually exit the loop and the function after the first iteration, i.e. it does not finish the iteration. Understanding how your code works and what you want to do/implement will help you to identify such bugs. This comes with the experience and in more professional sense - the proper tests will help identify such errors in the code.

Now, your code in the post # 4 will indeed raise IndentationError because Python expects indented block after line 8. In fact you need to indent lines 9-12 one level, thus they will be executed for each iteration of the loop.
Reply


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