Function that returns an equation
<p>I am writing a script to calculate the definite integral of an equation. I'm writing a helper function that would take in coefficients as parameters and return a function of x.</p>

<pre><code>def eqn(x, k, c, a):
    return ((k*x + c**(1-a))
</code></pre>

<p>Next, I define a function that calculates the definite integral, using quad imported from scipy:</p>

<pre><code>from scipy.integrate import quad
def integral(eqn, c_i, y_i):
    integral_i, integral_err = quad(eqn, c_i, y_i)
    print integral_i
</code></pre>

<p>Then I call the function by passing in parameters</p>

<pre><code>k = calc_k(7511675,1282474,0,38,2)
eqn = carbon_path_eqn(x, k, 7511675, 2)
carbon_path_def_int(eqn,0,38)
</code></pre>

<p>However, I get an error saying that 'name x is not defined'. I understand that <code>x</code> isn't defined globally, but I'm wondering how I can write a helper function, that takes in parameters, and still returns a function of x that can be used in <code>quad</code>?</p>

<p>Thank you!</p>

<p>PS -
<a href="http://stackoverflow.com/users/4317531/bpachev">@bpachev</a>, this is a follow up from the other post</p>

<p>The mistake here is that the function 'eqn' does not return a function, it returns the <em>value</em> of a function at some point x, given parameters k,c,a.</p>

<p>quad should be passed a function (in your case, eqn) where the first argument (in your case, x) is assumed to be the variable over which the function is integrated. You also need to pass quad a tuple of the remaining parameters (in your case (k,c,a)) and two limits (in your case, c_i,y_i). In other words, call quad like this:</p>

<pre><code>quad(eqn,c_i,y_i,args=(k,c,a))
</code></pre>

<p>This is all explained in the scipy documentation <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html" rel="nofollow">http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html</a>.</p>

python python-2.7 scipy