# Numerical Integral with variable params

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]),(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 close merge delete

Sort by » oldest newest most voted

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)
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,1), (0,1),
axes_labels=('a', 'b', 'f'), viewer='threejs')

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.