Ask Your Question

Numerical Integral with variable params

asked 2018-01-15 16:24:26 +0100

jepstra gravatar image

I am trying to write some code to plot a function of an integral whose integrand depends on two variables, much like the following:

a = var('a')
b = var('b')
plot3d(numerical_integral((x+a+b), 0, 1, params=[a,b])[0],(a,0,1),(b,0,1))

This code raises an error NotImplementedError: free variable: b. Which would be the correct way to implement this code?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-01-15 17:07:33 +0100

eric_g gravatar image

updated 2018-01-15 17:12:14 +0100

The simplest thing is to define the integral as a Python function (you don't even need to declare a and b as symbolic variables):

def f(a, b):
    return numerical_integral(x+a+b, 0, 1)[0]
plot3d(f, (0,1), (0,1), 
       axes_labels=('a', 'b', 'f'), viewer='threejs')

Another option is to use Python's lambda function (again no need to declare a and b as symbolic variables in this case):

plot3d(lambda a,b: numerical_integral(x+a+b, 0, 1)[0], (0,1), (0,1), 
       axes_labels=('a', 'b', 'f'), viewer='threejs')
edit flag offensive delete link more


In the above answer, I used the optional argument viewer='threejs' because it allows for labels of the axes, contrary to SageMath default viewer (Jmol). It is also faster.

eric_g gravatar imageeric_g ( 2018-01-15 17:10:20 +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

1 follower


Asked: 2018-01-15 16:24:26 +0100

Seen: 519 times

Last updated: Jan 15 '18