![]() |
Trapezoidal integration method in python. Getting wrong results - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Trapezoidal integration method in python. Getting wrong results (/thread-5612.html) |
Trapezoidal integration method in python. Getting wrong results - auting82 - Oct-13-2017 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 resultIn 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??? RE: Trapezoidal integration method in python. Getting wrong results - buran - Oct-13-2017 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 numericalI get 1.92271675047 EDIT: after fixing the indent (line #7) I get the result from the book RE: Trapezoidal integration method in python. Getting wrong results - auting82 - Oct-14-2017 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 resultAgain 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 RE: Trapezoidal integration method in python. Getting wrong results - buran - Oct-14-2017 Unindent line 8 one level RE: Trapezoidal integration method in python. Getting wrong results - auting82 - Oct-14-2017 (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 ![]() 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. ![]() 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 ![]() 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) RE: Trapezoidal integration method in python. Getting wrong results - buran - Oct-14-2017 (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. |