# Find Taylor Series of a Function

I’m completely at a loss for finding the Taylor Series of a function using TaylorSeries.jl

``````using TaylorSeries
f(x)=cos(x)
t=Taylor1(f(pi/2),2)
``````

Are you looking for a symbolic expansion or an expansion evaluated at a point? I think `TaylorSeries` deals with the latter. Not really sure though, never used it

expansion evaluated at a point. So that I can approximate Cos(x), say if x = 88 degrees. I’ll redo my MWE

What is the mathematical quantity that you are trying to calculate?

The Taylor Series arout (π/2) and the approximation of cos(22π/45) (cos(88degrees).

I think you want `taylor_expand`:

``````julia> t = taylor_expand(cos, pi/2, order=2)
6.123233995736766e-17 - 1.0 t - 3.061616997868383e-17 t² + 𝒪(t³)

julia> t(22pi/45 - pi/2)
0.0349065850398867

julia> cos(22pi/45)
0.03489949670250108
``````

(note that the expansion needs to be shifted by subtracting the point it’s expanded about)

4 Likes

Here’s the way I like to think about it:

``````julia> using TaylorSeries

julia> t = Taylor1(10)    #  symbolic object that you will use to expand
1.0 t + 𝒪(t¹¹)

julia> a = π / 2    # expansion point
1.5707963267948966

julia> c = cos(a + t)   # expand in powers of t around the point a
6.123233995736766e-17 - 1.0 t - 3.061616997868383e-17 t² + 0.16666666666666666 t³ + 2.5513474982236524e-18 t⁴ - 0.008333333333333333 t⁵ - 8.504491660745509e-20 t⁶ + 0.0001984126984126984 t⁷ + 1.5186592251331266e-21 t⁸ - 2.7557319223985893e-6 t⁹ - 1.6873991390368075e-23 t¹⁰ + 𝒪(t¹¹)

julia> δ = 22π/45 - a   # distance away from the expansion point
-0.03490658503988664

julia> c(δ)   # evaluate the series there
0.03489949670250109
``````

When you use a Taylor series, you should think of the parameter t as the small deviation from the point you’re expanding at.

3 Likes

This line is really cool!

``````julia> c = cos(a + t)
6.123233995736766e-17 - 1.0 t - 3.061616997868383e-17 t² + 0.16666666666666666 t³ + 2.5513474982236524e-18 t⁴ - 0.008333333333333333 t⁵ - 8.504491660745509e-20 t⁶ + 0.0001984126984126984 t⁷ + 1.5186592251331266e-21 t⁸ - 2.7557319223985893e-6 t⁹ - 1.6873991390368075e-23 t¹⁰ + 𝒪(t¹¹)
``````
1 Like

What is even cooler is

``````julia> cos(cos(a + t))
1.0 + 6.123233995736766e-17 t - 0.5 t² - 5.102694996447305e-17 t³ + 0.20833333333333331 t⁴ + 1.8879971486855028e-17 t⁵ - 0.051388888888888894 t⁶ - 5.5522181270867114e-18 t⁷ + 0.011334325396825398 t⁸ + 1.3784363566791678e-18 t⁹ - 0.0022511574074074074 t¹⁰ + 𝒪(t¹¹)
``````
5 Likes

This worked but I can’t seem to replicate it

``````using TaylorSeries
f(x)=log(x+1) #natural log
t=Taylor1(10)
a=0
c=f(a+t)
δ=3/2
c(δ)
``````

gives me .375

I think you should expand around 1 to compute at 3/2.

``````julia> using TaylorSeries
[ Info: Precompiling TaylorSeries [6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea]

julia> t = Taylor1(10)
1.0 t + 𝒪(t¹¹)

julia> a = 1
1

julia> l = log(a + t)
1.0 t - 0.5 t² + 0.3333333333333333 t³ - 0.25 t⁴ + 0.2 t⁵ - 0.16666666666666666 t⁶ + 0.14285714285714285 t⁷ - 0.125 t⁸ + 0.1111111111111111 t⁹ - 0.1 t¹⁰ + 𝒪(t¹¹)

julia> δ = 3/2 - a
0.5

julia> l(δ)
0.4054346478174603

julia> log(3/2)
0.4054651081081644
``````
1 Like

My bad. I could see from the graph, that the radius of convergence is 1 and the interval of convergence is -1,1. I was actually doing a practice problem, and the correct answer was 3/8, or .375, so it was working fine.