Ask Your Question

symbolic and numeric double integration method

asked 2015-10-28 21:48:00 -0500

collabmath gravatar image

fidbc corrected some of my previous code for double integration. However I am only able to good a good numerical double integral. Is their a sage method that can do both symbolic ie variables as bounds and numeric integration if not what is the symbolic integration method. Using the current integrate method I have I get back the wrong results in bracketed around the word integrate.

Here is my double integration code corrected by fidbc for numerical integration does anyone know a better integration method or methods for symbolic double integration.

from sage.plot.plot3d.shapes import Box
x,y = var('x,y')

def  interplay(function= input_box(sin(x*y)),lower_x_bound= input_box(0),upper_x_bound = input_box(1),lower_y_bound=input_box(0),upper_y_bound=input_box(1),dydx = checkbox(default = False)):
    if dydx==False :              
        prev = integral(function,x,lower_x_bound,upper_x_bound)
        result = n(integral(prev,y,lower_y_bound,upper_y_bound))
        q ="$\int_%s^{%s} \int_%s^%s %s \,dx\,dy = %s $" % (lower_y_bound, upper_y_bound,lower_x_bound,upper_x_bound,function,result)
        result = n(integral(integral(function,y,lower_y_bound,upper_y_bound),x,lower_x_bound,upper_x_bound))
        q = "$\int_%s^{%s} \int_%s^%s %s \,dy\,dx = %s $" % (lower_x_bound,upper_x_bound,lower_y_bound,upper_y_bound,function,result)
    html("%s" %q)
edit retag flag offensive close merge delete


Can you define what you mean by good? Do you mean one that won't return integrate(....)? It seems that sage will always return integrate(...) when there is no closed form solution.


#No closed form
sage: integral(integral(sin(x*y),x,0,2),y,0,2)
integrate(-cos(2*y)/y + 1/y, y, 0, 2)

#has closed form solution
sage: integral(integral(sin(x)*y,x,0,2),y,0,2)
-2*cos(2) + 2

If what you want is the closed form solution when that is avaliable and a numeric solution the following function should suffice:

def sym_or_num_Integral(f,variable,lower_bound=None,upper_bound=None):
    if 'integrate' in str(result):
        return result.n()
        return result
ianhi gravatar imageianhi ( 2015-10-28 23:35:47 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-10-29 01:32:52 -0500

ianhi gravatar image

updated 2015-10-29 01:33:23 -0500

I'm interpreting your question as: Is there a sage function which will give a symbolic solution to an integral if a closed form exists and a numeric solution otherwise. To my knowledge there is not though the following function may be what you are asking for:

def sym_or_num_Integral(f,variables,lower_bound=[None],upper_bound=[None]):
    if isinstance(variables,list):
        if not isinstance(lower_bound,list):
        while len(lower_bound)!=len(variables):
            if len(lower_bound)>len(variables): break

        if not isinstance(upper_bound,list):
        while len(upper_bound) != len(variables):
            if len(lower_bound)>len(variables): break

        if len(variables)==1:
            return sym_or_num_Integral(f,variables[0],lower_bound[0],upper_bound[0])
        return sym_or_num_Integral(sym_or_num_Integral(f,*inputs_inner),*inputs_outer)

    if 'integrate' in str(result):
        return result.n()
        return result

The method of checking if the solution is a closed form is quite hacky and I'd welcome and improvement too it. This function can do any number of integrals so long as you input the parameters as a list, i.e. variables=[x,y], lower_bound=[x_lower,y_lower]. Two examples:

sage: var('x y')
sage: sym_or_num_Integral(sin(x)*y,[x,y],[0,0],[2,2])
-2*cos(2) + 2
sage: sym_or_num_Integral(sin(x*y),[x,y],[0,0],[2,2])

You can have the integral be definite in any subset (must include first variable) of the variable and not the others:

sage: sym_or_num_Integral(sin(x)*y,[x,y],[0,None],[2,None])
-1/2*y^2*(cos(2) - 1)

Was this all you were looking for?

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

1 follower


Asked: 2015-10-28 21:48:00 -0500

Seen: 295 times

Last updated: Oct 29 '15