# Numerical integration in a function

f(x,y)=numerical_integral(1/d*2*x*y,.01,Infinity)[0]
Error


So what I want is to the integration wait until after the variables have been substituted so that it is able to numerically integrate. (Yes I need to numerically integrate. This is a simplified form that reproduces the same result.)

f(3,1)=numerical_integral(1/d*2*3*1,.01,Infinity)[0]
406.69135669845798


I thought there might be a way using a lambda defined function, but I was unable to find one.

edit retag close merge delete

Sort by ยป oldest newest most voted

This works for me:

sage: var('d')
d
sage: f = lambda x,y: numerical_integral(1/d*2*x*y,.01,Infinity)[0]
sage: f(3,1)
406.69135669845832

more

This worked great!

( 2010-09-01 12:32:51 +0200 )edit

Great! However, you won't be able to take the derivative of this function, as far as I know. Maybe our f(x,y) should make a lambda function when it's not possible to make a symbolic function... but that sounds hard.

( 2010-09-01 14:28:06 +0200 )edit

This isn't really an answer, but ...

This is one of many things about the integration interface that needs to be improved. We have two fundamentally different syntaxes for symbolic versus numerical integration. Would you find it possible to collate all your ask.sagemath.org questions about this into one collection and open a Trac ticket for "Improve numerical integration syntax" or something like that? I don't think it would be terribly difficult to fix many of these things, but it's much harder because the requests (and possibly tickets - over the last few years) are sort of scattered.

more

Please have a concrete suggestion for the syntax before opening the ticket. Tickets without specific goals tend to be ignored by the developers, and are generally hard to resolve. The sage-devel list is the right place for a discussion on how the improved syntax might be.

( 2010-09-01 11:46:23 +0200 )edit

Maybe what's also (or alternatively) needed is a ticket for "improve documentation for numerical and symbolic integration", since these questions seem (to me) to get resolved, but not always in obvious ways. @willmwade: could you identify the things you've learned which are not documented?

( 2010-09-01 11:46:28 +0200 )edit

The main area I think that could use some documentation is the use of the Python inline lambda function. Most if not all of the integration issues I have had, have been solved with using this. However there is little in how to use it with Sage specifically.

( 2010-09-01 12:34:38 +0200 )edit

For example in the answer to this one. I did not know that f=lambda x: x^2 would allow me to do f(2) result 4. Nor has any of the python docs on it been the most help. Mind I do more in Java :( and PHP :) than the little I have done in Python, but still some Sage documentation pointing at others?

( 2010-09-01 12:36:57 +0200 )edit

The only other item would be a mess I keep running into at defining the variable of integration. integral(f(x),x,0,1) verses numerical_integral(f(x),0,1). Numerical integral errors when the variable of integration is declared. If it would not do that, it would help bring the two into uniformity.

( 2010-09-01 12:39:45 +0200 )edit