You are on page 1of 11

Assignment 3

Introduction
The objective of this assignment is to t the functions ex and cos(cos(x)) over
the interval [0,2 ) using the fourier series.
a0 +

{ak cos (kx) + bk sin (kx)}

k=1

This is attempted using two numerical methods:

Integration We use the quad function to evaluate the following integrals.


a0

ak

bk

2
1
f (x)dx
2 0

1 2
f (x) cos(kx)dx
0
2
1
f (x) sin(kx)dx
2 0

(1)
(2)
(3)

Least Squares Fitting We nd a least squares solution of the following system of equations:

1 cos(x1 )
sin(x1 )
cos(2x1 )
1 cos(x2 )
sin(x
)
...
2

1 cos(x399 ) sin(x399 )
...
1 cos(x400 ) sin(x400 ) cos(2x400 )

Code
import numpy as np
from s c i p y . i n t e g r a t e import

quad
1

sin(25x1 )
sin(25x2 )

..

..

..

...
cos(25x399 ) sin(25x399 )
sin(2x400 ) cos(25x400 ) sin(25x400 )
sin(2x1 )
...

cos(25x1 )
cos(25x2 )

a0
a1
b1
a2
b2






=


..

b25

f (x1 )
f (x2 )
f (x3 )
f (x4 )

..
.
..
.

f (x400 )

import m a t p l o t l i b . p y p l o t as p l t
def epow ( x ) :
return np . exp ( x ) #defining exp funtion to be
passed
def coco ( x ) :
return np . c o s ( np . c o s ( x ) )#defining cos ( cos (x ) ) ,
vector output allowed
def u ( x , f , k ) :
return np . c o s ( k x ) f ( x ) #defining u(x , k )= f ( x )
cos ( k x ) , to be passed to integration
def v ( x , f , k ) : #defining v (x , k ) = f (x ) sin ( k x ) , also
passed to integration
return np . s i n ( k x ) f ( x )
def i n t e g k a ( u , f , k ) :#definingintegrating function , c a l l s
functions above
return ( 1 . 0 / np . p i ) quad ( u , 0 , 2 np . pi , a r g s =(f , k ) )
[0]
def c o e f f ( f , n=25) : # defining function that generates

c o e f f i c i e n t s , for given f and n using functions above


a =0.5 np . a r r a y ( [ i n t e g k a ( u , f , 0 ) ] )
i
( 1 , n+1) :
a= np . v s t a c k ( ( a , i n t e g k a ( u , f , i ) ) )
a = np . v s t a c k ( ( a , i n t e g k a ( v , f , i ) ) )
a
getcos (v , s i ze ) :
v [ 1 : 2 s i z e +1:2]
getsin (v , s i z e ) :
v [ 2 : 2 s i z e +1:2]

for

def
def

in range

return
return
return

n=25 # number of coeff


numeqs= 400 # number of
c o s c o= c o e f f ( coco )
epoco= c o e f f ( epow )
ep_co = g e t c o s ( epoco , n )
ep_si = g e t s i n ( epoco , n )
co_co = g e t c o s ( cosco , n )
co_si = g e t s i n ( cosco , n )
nvec= np . arange ( 1 , 2 6 )

values ( training examples )

y= np . arange (2 n+1)
x= np . l i n s p a c e ( 0 , 2 np . pi , numeqs , endpoint = F a l s e )
b= np . z e r o s ( ( numeqs , 2 n+1) )
fcalc ( coeff ) :
b[: ,0]= 1
i
( 1 , n+1) :
b [ : , 2 i 1]= np . c o s ( i x )

def

for

in xrange

def

return

b [ : , 2 i ]= np . s i n ( i x )
np . dot ( b , c o e f f )

leastsq ( f , x) :
b= np . z e r o s ( ( numeqs , 2 n+1) )
b[: ,0]= 1
i
( 1 , n+1) :
b [ : , 2 i 1]= np . c o s ( i x )
b [ : , 2 i ]= np . s i n ( i x )
c= np . z e r o s (2 n+1)
c=np . l i n a l g . l s t s q ( b , f ( x ) ) [ 0 ]
c
e x c o f 2 = l e a s t s q ( epow , x )
c o c o f 2 = l e a s t s q ( coco , x )

for

in xrange

return

ep_co2
ep_si2
co_co2
co_si2

=
=
=
=

g e t c o s ( excof2
g e t s i n ( excof2
getcos ( cocof2
g e t s i n ( cocof2

, n)
, n)
, n)
, n)

abs
abs

p l t . s e m i l o g y ( nvec ,
( co_co ) , ' ro ' , l a b e l= ' by
integration ' )
p l t . s e m i l o g y ( nvec ,
( co_co2 ) , ' go ' , l a b e l= ' by l e a s t
squares ' )
plt . legend ()
p l t . t i t l e ( ' $a_n$ vs n f o r $\ c o s (\ c o s ( x ) ) $ ' )
p l t . y l a b e l ( ' $a_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n ' )
p l t . show ( )

abs
abs

p l t . l o g l o g ( nvec ,
( co_co ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n
' )
p l t . l o g l o g ( nvec ,
( co_co2 ) , ' go ' , l a b e l= ' by l e a s t
squares ' )
plt . legend ()
p l t . t i t l e ( ' $a_n$ vs n f o r $\ c o s (\ c o s ( x ) ) $ ' )
p l t . y l a b e l ( ' $a_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n in l o g s c a l e ' )
p l t . show ( )

abs
abs

p l t . s e m i l o g y ( nvec ,
( co_si ) , ' ro ' , l a b e l= ' by
integration ' )
p l t . s e m i l o g y ( nvec ,
( co_si2 ) , ' go ' , l a b e l= ' by l e a s t
squares ' )
plt . legend ()
p l t . t i t l e ( ' $b_n$ vs n f o r $\ c o s (\ c o s ( x ) ) $
')
3

p l t . y l a b e l ( ' $b_n$ i n l o g s c a l e ' )


plt . xlabel ( 'n ' )
p l t . show ( )

abs
abs

p l t . l o g l o g ( nvec ,
( co_si ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n
')
p l t . l o g l o g ( nvec ,
( co_si2 ) , ' go ' , l a b e l= ' by l e a s t
squares ' )
plt . legend ()
p l t . t i t l e ( ' $b_n$ vs n f o r $\ c o s (\ c o s ( x ) ) $
')
p l t . y l a b e l ( ' $b_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n in l o g s c a l e ' )
p l t . show ( )

abs
abs

p l t . s e m i l o g y ( nvec ,
( ep_co ) , ' ro ' , l a b e l= ' by
integration ' )
p l t . s e m i l o g y ( nvec ,
( ep_co2 ) , ' go ' , l a b e l= ' by l e a s t
squares ' )
plt . legend ()
p l t . t i t l e ( ' $a_n$ vs n f o r $e^x$ ' )
p l t . y l a b e l ( ' $a_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n ' )
p l t . show ( )

abs
abs

p l t . l o g l o g ( nvec ,
( ep_co ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n
')
p l t . l o g l o g ( nvec ,
( ep_co2 ) , ' go ' , l a b e l= ' by l e a s t
squares ' )
plt . legend ()
p l t . t i t l e ( ' $a_n$ vs n f o r $e^x$ ' )
p l t . y l a b e l ( ' $a_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n in l o g s c a l e ' )
p l t . show ( )

abs
abs

p l t . s e m i l o g y ( nvec ,
( ep_si ) , ' ro ' , l a b e l= ' by
integration ' )
p l t . s e m i l o g y ( nvec ,
( ep_si2 ) , ' go ' , l a b e l= ' by l e a s t
squares ' )
plt . legend ()
p l t . t i t l e ( ' $b_n$ vs n f o r $e^x$ ' )
p l t . y l a b e l ( ' $b_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n ' )
p l t . show ( )
p l t . l o g l o g ( nvec ,

abs ( ep_si ) ,

' ro ' , l a b e l= ' by i n t e g r a t i o n


4

')
p l t . l o g l o g ( nvec ,
( ep_si2 ) , ' go ' , l a b e l= ' by l e a s t
squares ' )
plt . legend ()
p l t . t i t l e ( ' $b_n$ vs n f o r $e^x$ ' )
p l t . y l a b e l ( ' $b_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n in l o g s c a l e ' )
p l t . show ( )

abs

abs
abs

plt . semilogy (
( c o s c o ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n ' )
plt . semilogy (
( c o c o f 2 ) , ' go ' , l a b e l= ' by l e a s t s q u a r e s '
)
plt . legend ()
p l t . t i t l e ( ' $a_n$ vs n f o r $e^x$ ' )
p l t . y l a b e l ( ' $a_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n ' )
p l t . show ( )

abs
abs

plt . loglog (
( c o s c o ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n ' )
plt . loglog (
( c o c o f 2 ) , ' go ' , l a b e l= ' by l e a s t s q u a r e s ' )
plt . legend ()
p l t . t i t l e ( ' $a_n$ vs n f o r $e^x$ ' )
p l t . y l a b e l ( ' $a_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n in l o g s c a l e ' )
p l t . show ( )

abs
abs

plt . loglog (
( epoco ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n ' )
plt . loglog (
( e x c o f 2 ) , ' go ' , l a b e l= ' by l e a s t s q u a r e s ' )
plt . legend ()
p l t . t i t l e ( ' $a_n$ vs n f o r $e^x$ ' )
p l t . y l a b e l ( ' $a_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n in l o g s c a l e ' )
p l t . show ( )

abs
abs

plt . semilogy (
( epoco ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n ' )
plt . semilogy (
( e x c o f 2 ) , ' go ' , l a b e l= ' by l e a s t s q u a r e s '
)
plt . legend ()
p l t . t i t l e ( ' $a_n$ vs n f o r $e^x$ ' )
p l t . y l a b e l ( ' $a_n$ i n l o g s c a l e ' )
plt . xlabel ( 'n ' )
p l t . show ( )
p l t . p l o t ( x , f c a l c ( c o s c o ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n ' )#

p l o t t i n g cos ( cos ( x)

p l t . p l o t ( x , f c a l c ( c o c o f 2 ) , ' go ' , l a b e l = ' by l e a s t s q u a r e s '


)
p l t . p l o t ( x , coco ( x ) , ' b ' , l a b e l = ' f u n c t i o n ' )
p l t . t i t l e ( ' e s t i m a t e d f u n c t i o n and f u n c t i o n ' )
plt . legend ()
plt . ylabel ( ' value ' )
p l t . x l a b e l ( ' $x$ ' )
p l t . show ( )
p l t . p l o t ( x , f c a l c ( epoco ) , ' ro ' , l a b e l= ' by i n t e g r a t i o n ' )#

p l o t t i n g exp ( x)

p l t . p l o t ( x , f c a l c ( e x c o f 2 ) , ' go ' , l a b e l = ' by l e a s t s q u a r e s '


)
p l t . t i t l e ( ' e s t i m a t e d f u n c t i o n and f u n c t i o n ' )
p l t . p l o t ( x , epow ( x ) , ' b ' , l a b e l = ' f u n c t i o n ' )
plt . legend ()
plt . ylabel ( ' value ' )
p l t . x l a b e l ( ' $x$ ' )
p l t . show ( )

print " f o r c o s ( c o s ( x ) ) , the max e r r o r i s ) " , np . max( abs (


cosco c o c o f 2 . r e s h a p e (2 n+1 ,1) ) )
print " f o r e^x , the max e r r o r i s " , np . max( epoco e x c o f 2 .
r e s h a p e (2 n+1 ,1) )

Observations
Coecients

the following graphs are obtained for coecients an and bn :

1.

2.

3.

4.

Error between methods the max error between the two sets of coecients
7

are
for cos(cos(x)) the max error is 2.57586713575 x 1015
for ex the max error is 1.33273087034

Results and Discussion


Integration
cos(cos(x))

In the cos(cos(x)) case we see that bn are all nearly zero(the values displayed in
the graph are due to the nite precision of the numerical methods used. ). This
is because cos(cos(x)) is an even function.
ex

In the ex case, we see that the semilogy plot(only y-axis in log scale) does not
decay as fast as the log-log plot. This is because the log log plot also shortens
the xrange. The log log plot also looks linear because in the log-log case the
coecients approximate to power laws for large values of n.
This is easily seen from the equations below
ak =

bk =

1 e2 1
k2 + 1

k e2 1
k2 + 1

further we see that the bn are n times the an . Thus the bn are larger, this is
also seen in the slope of the graph below:

Least squares
cos(cos(x))

The least squares tting produces a coecients very near those obtained by
integration. Note that the series coecients do not always agree(as seen in the
case of the exponential function).
ex

The least squares approximation produces a signicant error as seen in the


graphs 3 and 4. this is because the previously found coecients(by integration)
have signicant contribution from higher index terms, unlike the cosine case.

Graphs

Figure 1:

10

Figure 2:

Conclusion
Fourier series coecients of periodic extensions of funcions can be found using
numerical methods such as integration and least squares approximations.

11

You might also like