and was able to use it to generate the Taylor series coefficients for a problem I was working on.
I thought to myself this would be a really cool thing to implement in Symbolics, just for fun, and immediately got into trouble
the goal: create a function to generate a Taylor series by generating the derivatives of a function and returning the coefficients of that Taylor series, and for bonus points return a function which can be evaluated in code.
As i pointed out this can all be done with the TaylorSeries package, this is a learning experience for me, and I thought it might be useful to others.
I did the obvious thing, trying to generate the 1st derivative:
using Symbolics
function main()
@variables x
f(x) = log(1+x)
df = Symbolics.derivative(f(x),x)
println(df)
# @syms df(x)
println(df(2))
println(df(3))
end
main()
and saw:
(1 + x)^-1
ERROR: LoadError: Sym (1 + x)^-1 is not callable. Use @syms (1 + x)^-1(var1, var2,...) to create it as a callable.
ste code here
I then tried a bunch of stuff based what i was reading in the documentation to try and get df(2) and df(3) to yield a numeric value but the best i could ever do was get “df(2)” and “df(3)” to print.
hoping i could get some advice and how to move this further along.
`ERROR: LoadError: MethodError: no method matching haskey(::Int64, ::Num)
Closest candidates are:
haskey(::DataStructures.SwissDict, ::Any) at /home/briand/.julia/packages/DataStructures/ixwFs/src/swiss_dict.jl:534
looking at the documentation, it seems to me like substitute is meant to substitute a different expression, not to evaluate an expression numerically.
FYI, i have tried using build_function in various ways without success, thinking that I might be able to get that to return something that could be used to evaluate at a value.
edit:
The answer is… println(SymbolicUtils.substitute(df,Dict(x=>2.0)))
My goal is to of course generate a function which is compiled in as code, but give me some time to get through the documentation
using Symbolics, TaylorSeries
@variables x e
f = sin(x)
dx = Differential(x)
order = 5
taylor_seriesₓ₊ₑ = f + sum((dx^i)(f)*e^i/factorial(BigInt(i)) for i in 1:order) |> expand_derivatives
substitute(taylor_seriesₓ₊ₑ, Dict(x=>0.0)) # coefficients
# alternative
# @variables x₀
# series = substitute(f, Dict(x=>x₀)) + sum(substitute((dx^i)(f)|>expand_derivatives, Dict(x=>x₀))*(x-x₀)^i/factorial(i) for i in 1:order)
# substitute(series, Dict(x₀=>0.0))
#
t = Taylor1(5)
sin(t) # same coefficients with TaylorSeries.jl
f_taylor_5_order = Symbolics.build_function(taylor_seriesₓ₊ₑ, x, e) |> eval # callable julia function
f_taylor_5_order(0.0, 0.1) # example