Skip to content

Instantly share code, notes, and snippets.

@sloede
Created November 22, 2022 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloede/a680cf36245e1794801a6bcd4530487a to your computer and use it in GitHub Desktop.
Save sloede/a680cf36245e1794801a6bcd4530487a to your computer and use it in GitHub Desktop.
using GLMakie
function taylor_approximation(n_start=1, x0_start=0.0)
# Create figure with increased canvas and font size
fig = Figure(resolution=(1200, 1200), fontsize=30)
# Create axis for plotting
ax = Axis(fig[1, 1],
title = "Taylor series for sine/cosine",
xlabel = "x",
ylabel = "y",
xticks = MultiplesTicks(5, pi, "π"),
)
# Set axis limits
xlims!(ax, -7, 7)
ylims!(ax, -5, 5)
# Create observables (= values that, if changed, cause the plot to be updated) for the exact
# function and the Taylor polynomial
exact_function = Observable{Any}(sin)
taylor_polynomial = Observable{Any}(tsinn)
# Plot exact function
lines!(ax, -7..7, exact_function, linewidth=3, label="f(x)")
# Create sliders to control polynomial degree and expansion point
sg = SliderGrid(
fig[2, 1],
(label = "Taylor polynomial degree:", range = 0:20, startvalue = n_start, linewidth=25),
(label = "x₀:", range = -7:0.01:7, startvalue = 0, format = "{:.2f}", linewidth=25),
)
# Compute function values of Taylor polynomial. The lifting ensure that this is updated whenever
# the polynomial degree, the expansion point, or the polynomial function change
xvalues = range(-7, 7, 1000)
yvalues = @lift begin
n = $(sg.sliders[1].value)
x0 = $(sg.sliders[2].value)
$taylor_polynomial.(xvalues, Ref(n), Ref(x0))
end
# Plot taylor polynomial
lines!(ax, xvalues, yvalues, linewidth=3, label="Tₙ(x)")
# Create and plot expansion point
point = @lift begin
n = $(sg.sliders[1].value)
x0 = $(sg.sliders[2].value)
Point2f(x0, $taylor_polynomial(x0, n, x0))
end
scatter!(point, color=:red, markersize=20, label="x₀")
# Create legend
axislegend(ax, position=:rb)
# Create menu to select function to approximate
menu = Menu(fig[3, 1], options=["Sine", "Cosine"])
on(menu.selection) do selection
if selection == "Sine"
exact_function[] = sin
taylor_polynomial[] = tsinn
elseif selection == "Cosine"
exact_function[] = cos
taylor_polynomial[] = tcosn
end
end
# Return figure such that the plot is created and shown when called from the REPL
return fig
end
function tcosn(x, n, x0)
sin_x0, cos_x0 = sincos(x0)
result = cos_x0
for i in 1:4:n
result -= sin_x0 * (x - x0)^i/factorial(i)
end
for i in 2:4:n
result -= cos_x0 * (x - x0)^i/factorial(i)
end
for i in 3:4:n
result += sin_x0 * (x - x0)^i/factorial(i)
end
for i in 4:4:n
result += cos_x0 * (x - x0)^i/factorial(i)
end
return result
end
function tsinn(x, n, x0)
sin_x0, cos_x0 = sincos(x0)
result = sin_x0
for i in 1:4:n
result += cos_x0 * (x - x0)^i/factorial(i)
end
for i in 2:4:n
result -= sin_x0 * (x - x0)^i/factorial(i)
end
for i in 3:4:n
result -= cos_x0 * (x - x0)^i/factorial(i)
end
for i in 4:4:n
result += sin_x0 * (x - x0)^i/factorial(i)
end
return result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment