L-20 MCS 320 Wednesday 12 October 2005
| > | restart; |
1. Truncated Series
Working with series is a way to symbolically compute approximately.
| > | tf := taylor(f(x),x=a,10); |
![]()
| > | whattype(tf); |
A series is different from a polynomial, in the sense that a series goes on for ever, whereas a polynomial has a bounded degree.
| > | dismantle(tf); # Maple has a separate type for serie |
SERIES(24)
SUM(5)
NAME(4): x
INTPOS(2): 1
NAME(4): a
INTNEG(2): -1
FUNCTION(3)
NAME(4): f
EXPSEQ(2)
NAME(4): a
[0]
FUNCTION(3)
FUNCTION(3)
NAME(4): D #[protected, _syslib]
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
[1]
SUM(3)
FUNCTION(3)
FUNCTION(3)
FUNCTION(3)
NAME(4): `@@` #[protected, _syslib]
EXPSEQ(3)
NAME(4): D #[protected, _syslib]
INTPOS(2): 2
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
RATIONAL(3): 1/2
INTPOS(2): 1
INTPOS(2): 2
[2]
SUM(3)
FUNCTION(3)
FUNCTION(3)
FUNCTION(3)
NAME(4): `@@` #[protected, _syslib]
EXPSEQ(3)
NAME(4): D #[protected, _syslib]
INTPOS(2): 3
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
RATIONAL(3): 1/6
INTPOS(2): 1
INTPOS(2): 6
[3]
SUM(3)
FUNCTION(3)
FUNCTION(3)
FUNCTION(3)
NAME(4): `@@` #[protected, _syslib]
EXPSEQ(3)
NAME(4): D #[protected, _syslib]
INTPOS(2): 4
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
RATIONAL(3): 1/24
INTPOS(2): 1
INTPOS(2): 24
[4]
SUM(3)
FUNCTION(3)
FUNCTION(3)
FUNCTION(3)
NAME(4): `@@` #[protected, _syslib]
EXPSEQ(3)
NAME(4): D #[protected, _syslib]
INTPOS(2): 5
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
RATIONAL(3): 1/120
INTPOS(2): 1
INTPOS(2): 120
[5]
SUM(3)
FUNCTION(3)
FUNCTION(3)
FUNCTION(3)
NAME(4): `@@` #[protected, _syslib]
EXPSEQ(3)
NAME(4): D #[protected, _syslib]
INTPOS(2): 6
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
RATIONAL(3): 1/720
INTPOS(2): 1
INTPOS(2): 720
[6]
SUM(3)
FUNCTION(3)
FUNCTION(3)
FUNCTION(3)
NAME(4): `@@` #[protected, _syslib]
EXPSEQ(3)
NAME(4): D #[protected, _syslib]
INTPOS(2): 7
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
RATIONAL(3): 1/5040
INTPOS(2): 1
INTPOS(2): 5040
[7]
SUM(3)
FUNCTION(3)
FUNCTION(3)
FUNCTION(3)
NAME(4): `@@` #[protected, _syslib]
EXPSEQ(3)
NAME(4): D #[protected, _syslib]
INTPOS(2): 8
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
RATIONAL(3): 1/40320
INTPOS(2): 1
INTPOS(2): 40320
[8]
SUM(3)
FUNCTION(3)
FUNCTION(3)
FUNCTION(3)
NAME(4): `@@` #[protected, _syslib]
EXPSEQ(3)
NAME(4): D #[protected, _syslib]
INTPOS(2): 9
EXPSEQ(2)
NAME(4): f
EXPSEQ(2)
NAME(4): a
RATIONAL(3): 1/362880
INTPOS(2): 1
INTPOS(2): 362880
[9]
FUNCTION(3)
NAME(4): O #[protected]
EXPSEQ(2)
INTPOS(2): 1
[10]
| > | s |
With Taylor series we can define functions:
| > | g := z/(1-z-z^2); # a generating functio |
| > | n |
| > | fib := taylor(g,z=0,10) |
| > | ; |
The coefficients of the Taylor series of g define the Fibonacci numbers.
| > | coeff(fib,z^6); |
| > | coeff(fib,z^30); |
The coeff command gives us the coefficient with a certain degree, just as a polynomial.
But the series "fib" is a truncated series, which goes here only to degree 9. This the reason why we get 0 back as coefficient with z^30.
| > | coeftayl(g,z=0,30); |
The coeftayl computes the coefficients of a Taylor series, without first having to expand a Taylor series up to this given degree. This is more efficient than first computing the full Taylor series and then selecting the desired coefficient.
| > | fibfun := n -> coeftayl(g,z=0,n): |
| > | fibfun(30); |
This illustrates how we may define new functions from the Taylor expansions of generating functions.
There is also the multivariate Taylor approximation:
| > | mtaylor(f(x,y),[x=a,y=b],4); |
![]()
The above command gave us a fourth order Taylor approximation of any function at (a,b).
2. Approximations of Functions
We may approximate a complicated function by a truncated Taylor series.
As example we take a function defined by an integral:
| > | intfun := int(exp(sin(x)),x=0..t); |
The independent variable is the t. The function gives us the area under exp(sin(x)), for x ranging between 0 and t.
To approximate this function, we may expand the integral as a series around t = 2:
| > | serfun := series(intfun,t=2,3); |
This will lead to a second-degree polynomial in t. Before we can use this polynomial function, we must convert the series into a polynomial.
| > | polfun := convert(serfun,`polynom`); # important intermediate ste |
| > | p |
Only now, we can convert the expression for polfun into a polynomial function.
| > | pf := unapply(polfun,t); |
This is still a symbolic polynomial:
| > | pf(2.34); |
| > | myfun := (n,t) -> evalf(pf(t),n); |
| > | myfun(30,2.34); |
We just computed a 30-digit approximation of the function at t = 2.34.
| > | evalf(subs(t=2.34,intfun),30); # exact value with 30 digits |
| > | (2.34-2)^3; # evaluation of the truncated term O((t-2)^3) |
We can replace complicated functions by second-degree polynomials, but their accuracy is limited.
3. Formal Power Series
We can define power series formally, without every having to worry about their convergence.
| > | with(powseries); |
![]()
![]()
| > | powcreate(defexp(n) = 1/n!); |
This command defines the n-th term, with the exponent x^n for the exponential function.
We can expand to a series:
| > | serexp := tpsform(defexp,x,10); |
This is one of the definitions for the exponential function.
As before, we may convert this series into a function.
| > | polexp := convert(serexp,`polynom`); |
The error between the actual series (and thus the exp() function) and the polynomial is of order x^10.
| > | funexp := unapply(polexp,x); |
| > | funexp(1.1); exp(1.1); |
| > | truncation_error := .1^10; |
A more interesting series is a series to compute Pi:
| > | powcreate(f(n)=(-1)^n/(2*n+1)); |
| > | fseries := tpsform(f,n,7); |
What is interesting here is the sum of the series:
4. Limits
| > | s := Sum('(-1)^n/(2*n+1)','n'=0..infinity); |
| > | value(s); |
| > | s1 := k -> sum('(-1)^n/(2*n+1)','n'=0..k); |
We can see how fast this series approximates Pi/4 by evaluating this function:
| > | for i from 1 to 20 do |
| > | evalf(s1(i),30); |
| > | end do; |