beacher_pille88
beacher_pille88

Reputation: 69

Python: Integration of Interpolation

I've got some question I cant solve:

#! /usr/bin/env python
import numpy as np
from scipy.interpolate import UnivariateSpline
from scipy.integrate import quad
import pylab as pl

x = ([0,10,20,30,40,50,60,70,...,4550,4560])
y = ([0,0,0,0,0,0,0,3,2,3,2,1,2,1,2,...,8,6,5,7,11,6,7,10,6,5,8,13,6,8,8,3])
s = UnivariateSpline(x, y, k=5, s=5)
xs = np.linspace(0, 4560, 4560)
ys = s(xs)

This is my code for making some Interpolation over some data. In addition, I plotted this function.

But now I want to integrate it (from zero to infinity).

I tried

results = integrate.quad(ys, 0, 99999)

but it didnt work.

Can you give me some hints (or solutions) please? thanks

Upvotes: 3

Views: 5514

Answers (2)

seberg
seberg

Reputation: 8975

As Pierre GM said, you have to give a function for quad (I think also you can use np.inf for the upper bound, though here it doesn't matter as the splines go to 0 quickly anyways). However, what you want is:

s.integral(0, np.inf)

Since this is a spline, the UnivariateSpline object already implements an integral that should be better and faster.

Upvotes: 3

Pierre GM
Pierre GM

Reputation: 20339

According to the documentation of quad, you need to give a function as first argument, followed by the lower and upper bounds of the integration range, and some extra arguments for your function (type help(quad) in your shell for more info.

You passed an array as first argument (ys), which is why it doesn't work. You may want to try something like:

results = quad(s, xs[0], xs[-1])

or

results = quad(s, 0, 9999)

Upvotes: 3

Related Questions