ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 30 Apr 2013 07:12:40 -0500Numerical integral with multiple parametershttp://ask.sagemath.org/question/10072/numerical-integral-with-multiple-parameters/I am trying to numerically integrate a function with respect to one variable, although the function is of more than one variable. An example:
var('x')
var('a')
f(x,a)=a*x
f(x,a)
integral(f(x,a),x,0,1)
produces the correct result of 1/2*a
g(x,a)=(f(x,a).nintegral(x, 0, 1))
Errors with "ValueError: Maxima (via quadpack) cannot compute the integral", but I probably don't have the syntax correct even if that function can do this. Even if a is given a value prior to the g(x,a) definition, it doesn't work.
g(x,a)=numerical_integral(f(x,a),0,1)
Errors with "ValueError: Integrand has wrong number of parameters". I can understand this, as it doesn't quite know what to do with 'a'.
g(x,a)=numerical_integral(f(x,a),0,1, params=[a])
g(x,6)
Gives an incorrect result of 0.3333
g(x,a)=numerical_integral(f(x,a),0,1, params=[6])
g(x,a)
Gives the correct result of 2.99996
h(x,a)=integral(f(x,a),x,0,1)
h(x,6)
Gives the correct result of 3
What is going on with g(x,a) and the "params" vector? Is what I am attempting to do possible?
I would like to make a plot of g(x,a) across a range of a. This is a simplified example, where I could obviously just do it by hand or with a non-numerical integral. The f(x,a) that I am really trying to work with is much more complex. I can upload a .sws workbook with these equations if that helps.
The documentation at [http://www.sagemath.org/doc/reference/calculus/sage/gsl/integration.html](http://www.sagemath.org/doc/reference/calculus/sage/gsl/integration.html) don't give much to go on with respect to params or nintegral. Any other docs out there I am missing?
Sun, 28 Apr 2013 13:23:36 -0500http://ask.sagemath.org/question/10072/numerical-integral-with-multiple-parameters/Answer by rickhg12hs for <p>I am trying to numerically integrate a function with respect to one variable, although the function is of more than one variable. An example: </p>
<pre><code>var('x')
var('a')
f(x,a)=a*x
f(x,a)
integral(f(x,a),x,0,1)
</code></pre>
<p>produces the correct result of 1/2*a</p>
<pre><code>g(x,a)=(f(x,a).nintegral(x, 0, 1))
</code></pre>
<p>Errors with "ValueError: Maxima (via quadpack) cannot compute the integral", but I probably don't have the syntax correct even if that function can do this. Even if a is given a value prior to the g(x,a) definition, it doesn't work.</p>
<pre><code>g(x,a)=numerical_integral(f(x,a),0,1)
</code></pre>
<p>Errors with "ValueError: Integrand has wrong number of parameters". I can understand this, as it doesn't quite know what to do with 'a'.</p>
<pre><code>g(x,a)=numerical_integral(f(x,a),0,1, params=[a])
g(x,6)
</code></pre>
<p>Gives an incorrect result of 0.3333</p>
<pre><code>g(x,a)=numerical_integral(f(x,a),0,1, params=[6])
g(x,a)
</code></pre>
<p>Gives the correct result of 2.99996</p>
<pre><code>h(x,a)=integral(f(x,a),x,0,1)
h(x,6)
</code></pre>
<p>Gives the correct result of 3</p>
<p>What is going on with g(x,a) and the "params" vector? Is what I am attempting to do possible?</p>
<p>I would like to make a plot of g(x,a) across a range of a. This is a simplified example, where I could obviously just do it by hand or with a non-numerical integral. The f(x,a) that I am really trying to work with is much more complex. I can upload a .sws workbook with these equations if that helps.</p>
<p>The documentation at <a href="http://www.sagemath.org/doc/reference/calculus/sage/gsl/integration.html">http://www.sagemath.org/doc/reference/calculus/sage/gsl/integration.html</a> don't give much to go on with respect to params or nintegral. Any other docs out there I am missing?</p>
http://ask.sagemath.org/question/10072/numerical-integral-with-multiple-parameters/?answer=14862#post-id-14862Not exactly sure what you're looking for, but the function help seems to have a similar example to what you're doing.
"For a Python function with parameters:
sage: f(x,a) = 1/(a+x^2)
sage: [numerical_integral(f, 1, 2, max_points=100, params=[n]) for n in range(10)] # random output (architecture and os dependent)
(0.32175055439664557, 3.5721487367706477e-15),
(0.24030098317249229, 2.6678768435816325e-15),
(0.19253082576711697, 2.1375215571674764e-15),
(0.16087527719832367, 1.7860743683853337e-15),
(0.13827545676349412, 1.5351659583939151e-15),
(0.12129975935702741, 1.3466978571966261e-15),
(0.10806674191683065, 1.1997818507228991e-15),
(0.09745444625548845, 1.0819617008493815e-15),
(0.088750683050217577, 9.8533051773561173e-16)]
Note the parameters are always a tuple even if they have one
component."
Sun, 28 Apr 2013 21:00:39 -0500http://ask.sagemath.org/question/10072/numerical-integral-with-multiple-parameters/?answer=14862#post-id-14862Answer by slelievre for <p>I am trying to numerically integrate a function with respect to one variable, although the function is of more than one variable. An example: </p>
<pre><code>var('x')
var('a')
f(x,a)=a*x
f(x,a)
integral(f(x,a),x,0,1)
</code></pre>
<p>produces the correct result of 1/2*a</p>
<pre><code>g(x,a)=(f(x,a).nintegral(x, 0, 1))
</code></pre>
<p>Errors with "ValueError: Maxima (via quadpack) cannot compute the integral", but I probably don't have the syntax correct even if that function can do this. Even if a is given a value prior to the g(x,a) definition, it doesn't work.</p>
<pre><code>g(x,a)=numerical_integral(f(x,a),0,1)
</code></pre>
<p>Errors with "ValueError: Integrand has wrong number of parameters". I can understand this, as it doesn't quite know what to do with 'a'.</p>
<pre><code>g(x,a)=numerical_integral(f(x,a),0,1, params=[a])
g(x,6)
</code></pre>
<p>Gives an incorrect result of 0.3333</p>
<pre><code>g(x,a)=numerical_integral(f(x,a),0,1, params=[6])
g(x,a)
</code></pre>
<p>Gives the correct result of 2.99996</p>
<pre><code>h(x,a)=integral(f(x,a),x,0,1)
h(x,6)
</code></pre>
<p>Gives the correct result of 3</p>
<p>What is going on with g(x,a) and the "params" vector? Is what I am attempting to do possible?</p>
<p>I would like to make a plot of g(x,a) across a range of a. This is a simplified example, where I could obviously just do it by hand or with a non-numerical integral. The f(x,a) that I am really trying to work with is much more complex. I can upload a .sws workbook with these equations if that helps.</p>
<p>The documentation at <a href="http://www.sagemath.org/doc/reference/calculus/sage/gsl/integration.html">http://www.sagemath.org/doc/reference/calculus/sage/gsl/integration.html</a> don't give much to go on with respect to params or nintegral. Any other docs out there I am missing?</p>
http://ask.sagemath.org/question/10072/numerical-integral-with-multiple-parameters/?answer=14868#post-id-14868A few observations can help explain what you are experiencing.
1. A note. When you feed `numerical_integral` with the input `f(x,a)`, it
detects that the input has two variables and knows one of them is a parameter.
The optional input `params` lets one give the *values* of the parameters.
sage: var('x a')
sage: f(x,a)=a*x
sage: numerical_integral(f(x,a),0,1, params=[1])
(0.5, 5.551115123125783e-15)
sage: numerical_integral(f(x,a),0,1, params=[6])
(2.9999999999999996, 3.330669073875469e-14)
2. What is not clear is which of the two variables in `f` is considered by
`numerical_integral` as being the parameter. Is it `x` or `a`? Which is the
main variable, the first one or the last one, or can it be specified?
3. A surprise. When you evaluate
sage: numerical_integral(f(x,a),0,1, params=[a])
(0.3333333333333333, 3.700743415417188e-15)
it seems that the parameter is transformed into the main variable before
integrating: the answer is exactly the integral of $x^2$ (or $a^2$ if the
integration is with respect to $a$) from 0 to 1.
sage: numerical_integral(x^2,0,1,)
(0.3333333333333333, 3.700743415417188e-15)
4. Weirder (if you thought `a` was the parameter and `x` the main variable):
sage: f(x,a)=a*x^2
sage: numerical_integral(f(x,a),0,1, params=[1])
(0.5, 5.551115123125783e-15)
sage: numerical_integral(f(x,a),0,1, params=[a])
(0.25, 2.7755575615628914e-15)
Now the integral with parameter set to 1 is wrong, unless integration is with
respect to $a$ rather than $x$. With `params=[a]` it now looks like `x` was set
to `a` before integrating *with respect to `a`*.
5. Since `numerical_integral(f(x,a),0,1, params=[a])` has a fixed value, when
you define
sage: g(x,a) = numerical_integral(f(x,a),0,1, params=[a])
this does not depend on `x` or `a`, so `g(anything,anything)` will always
return the same value.
6. For some reason, the "same" `g` defined using `def` doesn't behave the same:
sage: def g(x,a):
....: return numerical_integral(f(x,a),0,1, params=[a])
....:
sage: g(x,6)
(2.9999999999999996, 3.330669073875469e-14)
Here, `g(x,6)` returns `numerical_integral(f(x,6),0,1, params=[6])`, in
which the function to integrate only has one variable, `x`, and the
optional parameter `params=[6]` is just ignored.
You could simply write:
sage: def g(a):
....: return numerical_integral(f(x,a),0,1)
....:
sage: g(6)
(2.9999999999999996, 3.330669073875469e-14)
7. So which one is the main variable?
In `numerical_integral(f(x,a),0,1,params=[value])`, the function of several
variables is treated as a function of one variable with parameters, but the
main variable is `a`, not `x`.
You might think the main variable is the last of the arguments of `f(x,a)`
but it seems that it's really the first variable in the lexicographic order.
Indeed, compare
sage: f(x,y)=y*x^2
sage: numerical_integral(f(x,y),0,1, params=[1])
(0.3333333333333333, 3.700743415417188e-15)
and
sage: f(x,a)=a*x^2
sage: numerical_integral(f(x,a),0,1, params=[1])
(0.5, 5.551115123125783e-15)
8. Can you change which variable is the main variable and which are parameters?
I haven't figured that out yet.
Tue, 30 Apr 2013 07:12:40 -0500http://ask.sagemath.org/question/10072/numerical-integral-with-multiple-parameters/?answer=14868#post-id-14868