# Definite Integral
# Lauri Ruotsalainen, 2010
@interact
def definite_integral(
f = input_box(default = 3*x),
g = input_box(default = x^2),
interval = input_box(default=(0,3), label="Interval"),
x_range = input_box(default=(0,3), label = "Plotting range (x)"),
selection = selector(["f", "g", "f and g", "f - g"], label="Select")
):
f(x) = f; g(x) = g
f_plot = Graphics(); g_plot = Graphics(); h_plot = Graphics();
text = ""
# Plot function f.
if selection != "g":
f_plot = plot(f(x), x, x_range, color="blue", thickness=1.5)
# Color and calculate the area between f and the horizontal axis.
if selection == "f" or selection == "f and g":
f_plot += plot(f(x), x, interval, color="blue", fill=True, fillcolor="blue", fillalpha=0.15)
text += "$\int_%s^%s(\color{Blue}{f(x)})\,dx=\int_%s^%s(%s)\,dx=%s$" % (
interval[0], interval[1],
interval[0], interval[1],
latex(f(x)),
f(x).nintegrate(x, interval[0], interval[1])[0]
)
# Plot function g. Also color and calculate the area between g and the horizontal axis.
if selection == "g" or selection == "f and g":
g_plot = plot(g(x), x, x_range, color="green", thickness=1.5)
g_plot += plot(g(x), x, interval, color="green", fill=True, fillcolor="yellow", fillalpha=0.5)
text += "\n$\int_%s^%s(\color{Green}{g(x)})\,dx=\int_%s^%s(%s)\,dx=%s$" % (
interval[0], interval[1],
interval[0], interval[1],
latex(g(x)),
g(x).nintegrate(x, interval[0], interval[1])[0]
)
# Plot function f-g. Also color and calculate the area between f-g and the horizontal axis.
if selection == "f - g":
g_plot = plot(g(x), x, x_range, color="green", thickness=1.5)
g_plot += plot(g(x), x, interval, color="green", fill=f(x), fillcolor="red", fillalpha=0.15)
h_plot = plot(f(x)-g(x), x, interval, color="red", thickness=1.5, fill=True, fillcolor="red", fillalpha=0.15)
text = "$\int_%s^%s(\color{Red}{f(x)-g(x)})\,dx=\int_%s^%s(%s)\,dx=%s$" % (
interval[0], interval[1],
interval[0], interval[1],
latex(f(x)-g(x)),
(f(x)-g(x)).nintegrate(x, interval[0], interval[1])[0]
)
show(f_plot + g_plot + h_plot, gridlines=True)
html(text)
|
|
Click to the left again to hide and once more to show the dynamic interactive window
|