Ask Your Question
3

Numerical integral with multiple parameters

asked 2013-04-28 20:23:36 +0100

droppit gravatar image

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 don't give much to go on with respect to params or nintegral. Any other docs out there I am missing?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-04-30 14:12:40 +0100

slelievre gravatar image

updated 2013-05-01 07:50:01 +0100

A 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 ...
(more)
edit flag offensive delete link more
0

answered 2013-04-29 04:00:39 +0100

rickhg12hs gravatar image

Not 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."
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

4 followers

Stats

Asked: 2013-04-28 20:23:36 +0100

Seen: 4,302 times

Last updated: May 01 '13