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>This is not what you asked. However, as someone else has mentioned, sympy would probably make your life much easier. For instance, suppose you need to be able to evaluate integrals of functions of <strong>x</strong> such as f in the code below, where <strong>a</strong> and <strong>b</strong> are arbitrary constants. Here's how you can use sympy to do that.</p>

<p>Define the function, integrate it with respect to <strong>x</strong> and save the result, then evaluate the result for values of <strong>a</strong>, <strong>b</strong> and <strong>x</strong>.</p>

<p><a href="http://i.stack.imgur.com/Ul4K5.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/Ul4K5.jpg" alt="sample use of sympy"></a></p>

python python-2.7 scipy