Вы находитесь на странице: 1из 44

AP3114/Computational Methods for

Physicists and Materials Engineers


10. Fourier Series
&
Review
Nov. 16, 2015

http://rsb.info.nih.gov/nih-image/more-docs/Examples/fftexample.gif

http://imagejdocu.tudor.lu/doku.php?id=gui:process:fft

Contents
Introduction to Fourier analysis
Fourier series
Fourier Series
Applications of function fourierseries

Introduction to Fourier analysis


Fourier analysis involves the expansion of periodic
functions into infinite series of sine and cosines.
In general, any continuous function can be expanded into
a series of
a series combining sine and cosine functions (Fourier
series).
Expansion of functions into Fourier series finds
applications in areas related to any type of wave
propagation, e.g. electromagnetic waves or surface waves
in waters.
In practice, we evaluate Fourier series using only a finite

Introduction to Fourier analysis


Fourier analysis can be extended to a continuous
frequency domain through the use of Fourier transforms.
Fourier transforms have applications in the analysis of
periodic and non-periodic signals such as
communication signals, turbulence velocity variations,
etc.
Most measured signals include a finite number of
(typically) equally spaced data points suitable for analysis
through discrete Fourier transforms.
Sine and cosine functions belong to a family of so-called
orthogonal functions.

Fourier Series
A Fourier series is simply a series expansion of a function that
includes both sine and cosine series. The general form of the
Fourier series expansion in the interval [-L/2,L/2], i.e.,
assuming a period L, is:

th the coefficients calculated as:

and

General format -- Fourier Series


If the function is periodic of period L, the coefficients an and bn
are calculated as

and

Thus, if c0= -L/2, we recover the first set of equations for an and
b
An.user-defined SCILAB function to calculate Fourier series.
The following function, fourierseries, returns the coefficients
of the Fourier series and the evaluation of the same on a range
of values of x for different number of components. The general

Fourier Series
[a0 a b y] = fourierseries(nn, c0, L, x, f, tol)
where:
nn is a vector containing values of the components to be used,
e.g., nn = [5, 10, 20],
L is the period of the function,
x is a vector indicating the values of x where the Fourier series will
be evaluated,
tol is a tolerance for convergence used in function intg (see -->
help intg).
The integrals defining an and bn are calculated in the interval
[c0,c0+L]. The function returns values a0, a, b, and y, where
a0 is the coefficient a0 in the series,
a and b are vectors containing the coefficients of the cosine and
sine series, respectively, i.e., an and bn for n=1,2,,
y is a matrix with the same number of columns as in vector x and
the number of

Fourier Series
A listing of function fourierseries follows. The function
fourierseries includes a sub-function, fseries, which is
called recurrently to produce the rows of matrix y.
fseries:

ourierseries

Applications of function fourierseries


Example

1- The following example shows the fitting of the


function f(x) = in the interval [-1,1] using function
fourierseries. The Fourier series are calculated for 5, 10,
20, and 40 components. A plot of the original figure and
of the Fourier series with different number of components
is also produced.

Applications of function fourierseries

Applications of function fourierseries


Example 2- To fit the function f(x) = ln(x+2) in the interval
[-1,1],i.e., L = 2, using Fourier series.

Applications of function fourierseries

HW-10

Due: 9am, Nov 23, 2015


Late policy: 10% off per day

For the function: f(x) = exp(x):


Please produce the Fourier series for x in [-1, 1] using function
fourierseries.
Calculate each for 5, 10, 20, and 40 components, respectively.
Plots of the original figure and of the series with different number of
components are also required.

Note:
Record your intermediate results using Print Screen
Save your results and submit them through your usual way.

AP3114 Overview
Basics
WK 1
Introduction to Scilab, define variables
WK 2
Matrix & Statistics (intro)
Median
WK 3
Programming (if, select, for, while)
WK 4
2D Plots, 3D Plots & Random Numbers
Advanced
WK 6 & 8
WK 10 & 11
equations
WK 12

Solve equations, i.e., roots


Solve ordinary differentiation
Fourier series

Numerical methods solving equations and


differentiation equations
Finding roots f(x)=0
WK 6 Quadratic equations
Cubic equations
Solutions to polynomial
equations
Roots of complex variables
WK 8

non trivial equation f(x)=0


Bisection method
Newton-Raphson method

Solving ODE
WK 10
Linear ordinary differential equations
First order ODE dy/dx=f(x), where f(x)
is a polynomial
WK 11

First order ODE dy/dx=g(x,y)


Eulers forward method
Implicit method

Review of lecture 1

Creating a variable
Comments (//)/continuation(..)
Elementary mathematical functions
Boolean (%T, %F)
Complex numbers
Integer
Strings (test, +)
Dynamic variables

Matrix

Review of Lecture 2

Functions to generate matrix (eye, ones, zeros, etc)


Empty matrix []
Query matrices size
Accessing elements of matrix A(i,j)
a:b:c
$: the end of the matrix
+,-*,/,\
XB=A => X=A/B=AB-1
AX=B => X=A\B=A-1B

Element-wise operations: .
Conjugate transpose:

real number/matrix => transpose


Complex number/matrix => transpose
and conjugates
Elementary single quote . only
transpose, no conjugating the matrix,
be it real or complex.

Review of Lecture 3
Looping and branching statements
Branching: if & select/case
Looping: for & while
break: exit/interrupt a loop
continue: to go on to the next loop
Functions
Function Library
genlib = compile *.sci files into binary files and
store them
Load library

Debugging: pause, abort, resume

21

Flowchart

if
False

Conditi
on?

False Block

select
case
Case 1

True

True Block

Test
expression

Case 2

Case 3

Command

Case 4

for
Given a number
of repetitions

Block 1

while
Condition is True

Block 1

Conditi
on?

False
Block 2

Block 2

Point

plot(1,2,.r)

Bar

bar(x, n1)

Line

plot([1,3],[2,5])

Histgra
m
histplot(n,
data )

Curve

plot(x,y),

Contour

contour(x,y,z,10)

3D plot

Random Number

Numerical Methods to solve equations and


differentiation equations
Finding roots
WK 6
Quadratic equations
Cubic equations
Solutions to polynomial
equations
Roots of complex variables
WK 7

Non-linear equations f(x)=0


Bisection method
Newton-Raphson method

Solving ODE
WK 10
Linear ordinary differential equations
First order ODE dy/dx=f(x), where f(x)
is a polynomial
WK 11

First order ODE dy/dx=g(x,y)


Eulers forward method
Implicit method

1) Quadratic equations
ax2+bx+c = 0
The quantity under the square root in this result is known as
the discriminant of the equation, i.e.,
D = b2-4ac.
Possible solutions:
If D>0, the quadratic equation has two distinct real solutions:
x1,2= (-b D1/2)/(2a).
If D=0, the quadratic equation has one real (double) solution,
x1= x2= -b/(2a).
If D<0, the quadratic equation has two conjugate complex
solutions,
x1,2= (-b i(-D)1/2)/(2a),
where i = (-1)1/2 is the unit imaginary number.

2) Cubic equations
2
xy3=+ax
+bx+c = 0
x + a/3 y3+py+q = 0,

p = (3b-a2)/3,
and
q = c + 2a3/27 ab/3.
A discriminant for the reduced cubic
equation is given by
D = (q/2)2+(p/3)3.
An Italian mathematician, Gierolimo
Cardano, proposed the following
solutions to the cubic
equation:
x1= -(a/3)+u+v
x2,3= -(a/3)-(u+v)/2 i3(u-v)/2
If D > 0, one real and two complex conjugate
solutions.
If D = 0, three real solutions including a
double solution (x2 and x3).

D<0

Solutions to polynomial equations


P = a0+a1x+a2x2+a3x3++anxn,

4) Roots of complex variable


Because the argument of a complex number zhas a periodicity of 2, we
can write
z = rei(+2k) , for k = 0, 1, 2,
There are n n-th roots of z calculated as:

erval-halving or bisection method


Numerically solve
roots of f(x)=0

The Newton-Raphson method

ODE
1)
Solutio
n
where

2)

dy/dx = f(x)

he function f(x) is a polynomial, use intpoly

roots(pn
)

he function f(x) is not a polynomial, but g(x,y)

Eulers forward method


Implicit method

Euler's forward method


Consider the ordinary differential equation,
dy/dx = g(x,y),
B.C.
y(x1) = y1.

dy/dx = g(x,y) =(y(x+h)-y(x))/h


y(x+h) = y(x)+hg(x,y).
yi+1= yi + xg(xi,yi)
(x1,y1)

x2= x1+h

x3= x2+h

(x1, y1), (x2, y2), , (xn, yn)

Implicit Method
Ordinary differential equation, dy/dx = g(x,y),
B.C
y(x1) = y1.
Centered difference approximation for dy/dx,
i.e.
dy/dx = (y(x+h)-y(x-h))/(2*h).
With this approximation the ODE becomes,
(y(x+h)-y(x-h))/(2*h) = g(x,y).
In terms of sub-indexed variables, this latter equation can be
written as:
yi-1+2xg(xi,yi)-yi+1= 0, ( i = 2,3, ..., n-1 )
where y(x) = yi, y(x+h) = yi+1, y(x-h) = yi-1, and h = x

Implicit Method
yi-1+2xg(xi,yi)-yi+1= 0, ( i = 2,3, ..., n-1 )
For example, if n = 5, we have 3 equations:
y1+2x g(x2,y2)-y3= 0
y2+2x g(x3,y3)-y4= 0
y3+2x g(x4,y4)-y5= 0
Since y1 is known (it is the initial condition), there are still 4
unknowns, y2, y3, y4, and y5. We use, for example, the forward
difference equation applied to i = 1, i.e.,
(y2-y1)/x = g(x1,y1),
or
y2-x g(x1,y1)-y1= 0.
The values of xi, and n(or x), can be obtained as in the Euler
forward (explicit) solution.

Example

ODE: dy/dx = y sin(x)


Initial conditions y(0) = 1, in the interval 0 < x < 5.
Use x = 0.5,
Numerical solution:
dy/dx = (yi+1yi-1)/(2x),
yi-1 + 2xsin(xi)yi yi+1 = 0, (i = 3, , n-1).
y1=dy/dx|
1
x= 1= (y2-y1)/x = -y1sin(x1),
(1+x sin(x1))y1- y2= 0.

1. Linear Equations

Scilab

2. Free Fall
t=0, v=0,
Case 1. No air resistance
Case 2. Resistance proportional to the lin
ear power of velocity
Case 3. Resistance proportional to the sec
ond power of the velocity

3. Electric Current
V=1; R=1; L=1;
t=0:0.01:6;
i=(V/R)*(1-exp(-R.*t/L));
figure;plot(t,i,'ro');
xlabel('time,
t','fontsize',6),
ylabel('current,
i','fontsize',6)

4. Cantilever Beam
For a uniformly loaded span of a cantilever beam attached to a
wall at x = 0 with the free end at x = L, the formula for the vertic
al displacement from y = 0 under the loaded condition, with y t
he coordinate in the direction opposite that of the load, can be
written as follows:

where
E is a material property known as
the modulus of elasticity;
I is a geometric property of the
cross-section of the beam known
as the moment of inertia; L is the
length; w is the load per unit width
of the beam.

5.

Diffusion equation:
dc
2c
dt

c( x 0, t 0) 1
c( x, t 0) 0

5. Diffusion
dc
2c
dt

c(i, n 1) c(i, n) c(i 1, n) c(i 1, n) 2c(i, n)

t
x 2

x=1:1:100; dt=0.05;
c=zeros(100,1); c(1)=1;
figure;
for t=1:1:3000
for i=2:1:99
d(i)=(c(i+1)+c(i-1)2*c(i))*dt+c(i);
end
d(100)=d(99);
d(1)=1-sum(d(2:100));
c=d;
if pmodulo(t,1000)==0
then plot(x,c);
end
end

Results
t1
c(x,t)

t2
t3
x

6. Random walk (Markov proces


s)
Cafe
Home
1

P=1/3 3 P=2/3

A drunk wanders down a street that has six intersections.


His home is at interaction 1, and his favorite Internet caf
is at intersection 6.
At each intersection other than his home or the caf, he m
oves in the direction of the caf with probability 2/3 and i
n the direction of his home with probability 1/3.
We would like to know the chances of the drunk ending u
p at home or in the caf is. He starts at a given corner.

6. Random walk (Markov proces


s)
Cafe
Home
1

P=1/3 3 P=2/3

We represent the probabilities of being in these states a si


x-component state vector X(t), where Xi(t) is the probabilit
y of him being at intersection i at moment t. The compon
ents of X(t) must sum to 1, since he has to be in one of the
se states.

Transition probability

Вам также может понравиться