# Find Taylor Series of a Function

**URL:** <https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847>\
**Category:** Optimization (Mathematical)\
**Tags:** question, package\
**Created:** [December 15, 2020, 1:46am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847 "2020-12-15T01:46:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 15, 2020, 1:46am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/1 "2020-12-15T01:46:05Z")

</div>

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

```julia
using TaylorSeries
using QuadGK
f(x)=cos(x)
t=Taylor1(f(pi/2),2)
quadgk(x->cos(t), 22pi/45,pi/2;order=2)

```

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [December 15, 2020, 2:07am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/2 "2020-12-15T02:07:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 15, 2020, 2:09am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/3 "2020-12-15T02:09:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 15, 2020, 3:31am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/4 "2020-12-15T03:31:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 15, 2020, 3:42am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/5 "2020-12-15T03:42:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 15, 2020, 3:54am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/6 "2020-12-15T03:54:54Z")

</div>

I think you want [`taylor_expand`](http://www.juliadiff.org/TaylorSeries.jl/latest/api/#TaylorSeries.taylor_expand):

```julia
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)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 15, 2020, 4:17am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/7 "2020-12-15T04:17:36Z")

</div>

> [@stillyslalom](#):
>
> `22pi/45 - pi/2`

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [December 15, 2020, 4:28am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/8 "2020-12-15T04:28:08Z")

</div>

This line is really cool!

```julia
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¹¹)

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 15, 2020, 4:29am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/9 "2020-12-15T04:29:12Z")

</div>

What is even cooler is

```julia
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¹¹)

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 16, 2020, 2:25am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/10 "2020-12-16T02:25:20Z")

</div>

This worked but I can’t seem to replicate it

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

```

gives me .375

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [December 16, 2020, 3:09am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/11 "2020-12-16T03:09:33Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 16, 2020, 3:52am UTC](https://discourse.julialang.org/t/find-taylor-series-of-a-function/51847/12 "2020-12-16T03:52:52Z")

</div>

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.
