Homework09

A MathWikiből
A lap korábbi változatát látod, amilyen Imran (vitalap | szerkesztései) 2021. május 5., 17:23-kor történt szerkesztése után volt.
(eltér) ←Régebbi változat | Aktuális változat (eltér) | Újabb változat→ (eltér)

Numerical Integral

write a function called num_int that calculates the numerical integral of sin(x)/x using trapezoidal rule.

  • The function should have three parameters: a, b and x
  • The endpoints of the interval to integrate over: 0 < a < b
  • The past parameter will be either a positive integer or a numpy array. This will represent the partition of the interval [a, b].
  • Return the numerical approximation of the integral (a real number).

If the last parameter is a number, then divide the interval to that many trapezoids with equal width (use linspace).

If it is a numpy array, that will contain the points in the partition. The first number will be a and the last one b and the points will be increasing.

Example

Suppose that f(x) = sin(x)/x.
num_int(1, 2, 1)
Then the result should be: (f(1) + f(2))/2
num_int(1, 2, 2)
Then the result should be: (f(1) + f(1.5))/4 + (f(1.5) + f(2))/4
Since the partition is: [1, 1.5, 2]
In fact, the latter should be the same as:
num_int(1, 2, numpy.array([1,1.5,2]))

Hint: There are one less trapezoids in the partition than points. "

Személyes eszközök