Ask Your Question
1

Numerical integration in a function

asked 2010-09-01 09:20:26 +0100

willmwade gravatar image
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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

answered 2010-09-01 11:35:01 +0100

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

Comments

This worked great!

willmwade gravatar imagewillmwade ( 2010-09-01 12:32:51 +0100 )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.

kcrisman gravatar imagekcrisman ( 2010-09-01 14:28:06 +0100 )edit
1

answered 2010-09-01 09:54:20 +0100

kcrisman gravatar image

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.

edit flag offensive delete link more

Comments

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.

burcin gravatar imageburcin ( 2010-09-01 11:46:23 +0100 )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?

niles gravatar imageniles ( 2010-09-01 11:46:28 +0100 )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.

willmwade gravatar imagewillmwade ( 2010-09-01 12:34:38 +0100 )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?

willmwade gravatar imagewillmwade ( 2010-09-01 12:36:57 +0100 )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.

willmwade gravatar imagewillmwade ( 2010-09-01 12:39:45 +0100 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2010-09-01 09:20:26 +0100

Seen: 3,488 times

Last updated: Sep 01 '10