Oct-13-2017, 12:40 PM
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.
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???
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???