# Difference Quotient
# Lauri Ruotsalainen, 2010
@interact
def difference_quotient(
f = input_box(default=sin(x)),
range = range_slider(0, 10, 0.1, default=(0.0,10.0),
label="Range"),
a = slider(0, 10, None, 5.5),
x0 = slider(0, 10, None, 2.5)
):
f(x) = f
fmax = f.find_maximum_on_interval(range[0], range[1])[0]
fmin = f.find_minimum_on_interval(range[0], range[1])[0]
f_height = fmax - fmin
measure_y = fmin - 0.1*f_height
measure_0 = line2d([(x0, measure_y), (a, measure_y)], rgbcolor="black")
measure_1 = line2d([(x0, measure_y + 0.02*f_height), (x0, measure_y-0.02*f_height)], rgbcolor="black")
measure_2 = line2d([(a, measure_y + 0.02*f_height), (a, measure_y-0.02*f_height)], rgbcolor="black")
text_x0 = text("x0", (x0, measure_y - 0.05*f_height), rgbcolor="black")
text_a = text("a", (a, measure_y - 0.05*f_height), rgbcolor="black")
measure = measure_0 + measure_1 + measure_2 + text_x0 + text_a
tanf(x) = (f(x0)-f(a))*(x-a)/(x0-a)+f(a)
fplot = plot(f(x), x, range[0], range[1])
tanplot = plot(tanf(x), x, range[0], range[1], rgbcolor="#FF0000")
points = point([(x0, f(x0)), (a, f(a))], pointsize=20, rgbcolor="#005500")
dashline = line2d([(x0, f(x0)), (x0, f(a)), (a, f(a))], rgbcolor="#005500", linestyle="--")
show(fplot + tanplot + points + dashline + measure, xmin=range[0], xmax=range[1], ymin=fmin-0.2*f_height, ymax=fmax)
html("<br>$\\text{Line's equation:}$")
html("$y = %s$<br>"%tanf(x))
html("$\\text{Slope:}$")
html("$k = \\frac{f(x0)-f(a)}{x0-a} = %s$<br>" % (N(derivative(tanf(x), x), digits=5)))