keywords: SageMath, Definite and Indefinite Integral, Riemann Sum, Trapezoidal Rule for Numerical Integration, Latex.
Copy the
selected example and insert it into the Sage
box below; then click on "Evaluate".
@interact
def integral(
f = input_box(default =
x^3-3*x^2+2*x),auto_update=False):
f(x)=f
r=LatexExpr("\\int(")+latex(f(x))+LatexExpr(")dx=")
q=indefinite_integral(f(x),x)
show(r+latex(q)+" + C")
# Definite Integral
- an example
# loading library "definite_integral"
from sage.symbolic.integration.integral import definite_integral
# inputs : a
function and an interval
p=sin(x)*sin(x)
# Show task in Latex
r=LatexExpr("\\int_0^{\\pi}sin^2(x)dx=")
# Computation and result
q=definite_integral(p,x,0,pi)
show(r+latex(q))
# Definite Integral
- an example with interact and graph
# loading library
"definite_integral"
from sage.symbolic.integration.integral import definite_integral
# inputs : a
function f and an interval [a,b].
# outputs : the value
of a definite integral f at [a,b],
the area under (above) f
# author: P. Szabó, 12 March 2018
@interact
def integral(
f = input_box(default = x^3-3*x^2+2*x) ,
a = input_box(default = -0.5) ,
b = input_box(default = 2.5),
gr = selector(values = ["Yes", "No"],
label = "Graph", default = "No" ), auto_update=False):
show("Integral computation")
f(x)=f
r=LatexExpr("\\int_a^{b}(")+latex(f(x))+LatexExpr(")dx=")
q=definite_integral(f(x),x,a,b)
show(r+latex(q))
if (gr == "Yes"):
r=plot(f(x),x,a,b,gridlines='minor', title="\$
f(x)="+str(f(x))+" \$", fillcolor = "yellow", fill = true)
r.show()
# Riemann Sum and
Sage Interact - an example
@interact
def RiemannSum(
f = input_box(default = x^3-3*x^2+2*x) ,
a = input_box(default = 0) ,
b = input_box(default = 2) ,
n = slider(vmin = 1, vmax = 20, default = 8,
step_size=1) ):
f(x)=f
#plot the function
p = plot(f, x, a,b, color = "red", thickness
= 4, \
alpha = 0.5, fillcolor = "blue", fill = true)
dx = (b-a)/n
#plot each trapezoid
for i in [1,2,..,n]:
#for the shading ...
xbegin = a+(i-1)*dx
xend =
a+i*dx
p += polygon(
[(xbegin,0),
(xend,0),
\
(xend, f(xend)), (xbegin,f(xend))], \
color="yellow",alpha = 0.5, aspect_ratio=
"automatic")
#these are optional for a
better graph:
p += line([(xend,0),
(xend, f(xend))], color = "black")
p += line([(xend,
f(xend)), (xbegin,f(xend))], color = "black")
p += line([(xbegin,
f(xend)),(xbegin,0) ], color = "black")
p.show(title = "Riemann Sum with Right
Endpoints", ticks = dx)
# Trapezoidal Rule
for Numerical Integration - an example
import numpy
@interact
def TrapezoidByNumPy(
f = input_box(default = e^x),
a = input_box(default = 0),
b = input_box(default = 3),
n = slider(vmin=2, vmax=50, default = 4,
step_size = 1)):
#needed to avoid the warning message
f(x)=f
#compute the size of each subinterval
delta_x = (b-a)/n
#compute the intermediate points
xi_values = [a,a+delta_x,..,b]
#compute the corresponding y values
fxi_values = [f(i) for i in xi_values]
#compute the Trapezoidal Rule value
print "approximation = ",
numpy.trapz(fxi_values ,
dx=delta_x).n(), \
"\t exact
value = ", integrate(f(x), x, a, b).n()
p = plot(f, x, a,b, color = "red", thickness
= 4)
#plot each trapezoid
p += line( [(a,0), (a,f(a))] , color =
"black")
for i in [1,2,..,n]:
p += line(
[(a+(i-1)*delta_x,f(a+(i-1)*delta_x)),
(a+i*delta_x,f(a+i*delta_x))] , \
color = "black")
p += line(
[(a+i*delta_x,0), (a+i*delta_x,f(a+i*delta_x))] , \
color = "black")
#for the shading ...
p += polygon(
[(a+(i-1)*delta_x,0),
\
(a+(i-1)*delta_x,f(a+(i-1)*delta_x)), \
(a +i*delta_x,
f(a+i*delta_x)), \
(a+
i*delta_x,
0)], \
color="yellow",
\
aspect_ratio="automatic")
p.show()
Type your
own Sage computation below and click “Evaluate”.