44
AP3114/Computational Methods for Physicists and Materials Engineers 10. Fourier Series & Review Nov. 16, 2015

AP3114 Lecture WK12 Fourier-Analysis Review

Embed Size (px)

DESCRIPTION

Physics

Citation preview

Page 1: AP3114 Lecture WK12 Fourier-Analysis Review

AP3114/Computational Methods for Physicists and Materials Engineers

10. Fourier Series&

ReviewNov. 16, 2015

Page 2: AP3114 Lecture WK12 Fourier-Analysis Review

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

Page 4: AP3114 Lecture WK12 Fourier-Analysis Review

Contents

Introduction to Fourier analysis Fourier series

Fourier SeriesApplications of function fourierseries

Page 5: AP3114 Lecture WK12 Fourier-Analysis Review

Introduction to Fourier analysisFourier 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 number of sine and/or cosine functions representing a discrete number of frequencies.

Page 6: AP3114 Lecture WK12 Fourier-Analysis Review

Introduction to Fourier analysisFourier 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.

Page 7: AP3114 Lecture WK12 Fourier-Analysis Review

Fourier SeriesA 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:

with the coefficients calculated as:

and

Page 8: AP3114 Lecture WK12 Fourier-Analysis Review

General format -- Fourier SeriesIf 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 bn. A 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 call to the function is

Page 9: AP3114 Lecture WK12 Fourier-Analysis Review

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 • rows equal to the number of elements in vector nn.

Thus, the i-th row of matrix y contains the values of the Fourier series with the number of components indicated by the i-th component of nn.

Page 10: AP3114 Lecture WK12 Fourier-Analysis Review

Fourier SeriesA 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:

Page 11: AP3114 Lecture WK12 Fourier-Analysis Review

fourierseries

Page 12: AP3114 Lecture WK12 Fourier-Analysis Review

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.

Page 13: AP3114 Lecture WK12 Fourier-Analysis Review

Applications of function fourierseries

Page 14: AP3114 Lecture WK12 Fourier-Analysis Review

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.

Page 15: AP3114 Lecture WK12 Fourier-Analysis Review

Applications of function fourierseries

Page 16: AP3114 Lecture WK12 Fourier-Analysis Review

HW-10For 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.

Due: 9am, Nov 23, 2015Late policy: 10% off per day

Page 17: AP3114 Lecture WK12 Fourier-Analysis Review

BasicsWK 1 Introduction to Scilab, define variablesWK 2 Matrix & Statistics (intro)

MedianWK 3 Programming (if, select, for, while)WK 4 2D Plots, 3D Plots & Random Numbers

AdvancedWK 6 & 8 Solve equations, i.e., rootsWK 10 & 11 Solve ordinary differentiation equationsWK 12 Fourier series

AP3114 Overview

Page 18: AP3114 Lecture WK12 Fourier-Analysis Review

Finding roots f(x)=0WK 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 ODEWK 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) Euler’s forward method Implicit method

Numerical methods solving equations and differentiation equations

Page 19: AP3114 Lecture WK12 Fourier-Analysis Review

Review of lecture 1

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

Page 20: AP3114 Lecture WK12 Fourier-Analysis Review

Review of Lecture 2• Matrix

• 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.

Page 21: AP3114 Lecture WK12 Fourier-Analysis Review

21

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

Page 22: AP3114 Lecture WK12 Fourier-Analysis Review

FlowchartCondition?

True Block

True

False Block

False

Test expression

Case 2

Command

Case 1 Case 4Case 3

if

selectcase

Page 23: AP3114 Lecture WK12 Fourier-Analysis Review

for while

Block 1

Block 2

Condition?

Block 1

Block 2

False

Condition is TrueGiven a number of repetitions

Page 24: AP3114 Lecture WK12 Fourier-Analysis Review

Point Line Curve

ContourHistgramBar

plot(1,2,”.r”) plot([1,3],[2,5]) plot(x,y),

bar(x, n1) histplot(n, data ) contour(x,y,z,10)

Page 25: AP3114 Lecture WK12 Fourier-Analysis Review

3D plot

Page 26: AP3114 Lecture WK12 Fourier-Analysis Review

Random Number

Page 27: AP3114 Lecture WK12 Fourier-Analysis Review

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 ODEWK 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) Euler’s forward method Implicit method

Numerical Methods to solve equations and differentiation equations

Page 28: AP3114 Lecture WK12 Fourier-Analysis Review

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.

Page 29: AP3114 Lecture WK12 Fourier-Analysis Review

2) Cubic equations x3+ax2+bx+c = 0

y3+py+q = 0,y = x + a/3

p = (3b-a2)/3,and

q = c + 2a3/27 – a b/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 cubicequation:

x1= -(a/3)+u+vx2,3= -(a/3)-(u+v)/2 ± i √3 (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).If D < 0, three distinct real solutions.

D<0

Page 30: AP3114 Lecture WK12 Fourier-Analysis Review

4) Roots of complex variableBecause the argument θ of a complex number zhas a periodicity of 2π, we can write

z = r e⋅ i (⋅ θ+2kπ) , for k = 0, 1, 2, …

There are n n-th roots of z calculated as:

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

3) Solutions to polynomial equations

Page 31: AP3114 Lecture WK12 Fourier-Analysis Review

The Newton-Raphson method

Interval-halving or bisection method

Numerically solve roots of f(x)=0

Page 32: AP3114 Lecture WK12 Fourier-Analysis Review

ODE

Solution

where

dy/dx = f(x)

roots(pn)

If the function f(x) is a polynomial, use intpoly

1)

2)

Page 33: AP3114 Lecture WK12 Fourier-Analysis Review

Euler’s forward method Implicit 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)+h g(x,y). ⋅

Euler's forward method

(x1,y1) x2= x1+h x3= x2+h (x1, y1), (x2, y2), …, (xn, yn)

yi+1= yi + ∆x g(x⋅ i,yi)

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

Page 34: AP3114 Lecture WK12 Fourier-Analysis Review

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+2 ∆x g(x⋅ ⋅ i,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

Page 35: AP3114 Lecture WK12 Fourier-Analysis Review

Implicit Method

For example, if n = 5, we have 3 equations:

y1+2 ∆x g(x⋅ ⋅ 2,y2)-y3= 0 y2+2 ∆x g(x⋅ ⋅ 3,y3)-y4= 0 y3+2 ∆x g(x⋅ ⋅ 4,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(x⋅ 1,y1)-y1= 0. The values of xi, and n(or ∆x), can be obtained as in the Euler forward (explicit) solution.

yi-1+2 ∆x g(x⋅ ⋅ i,yi)-yi+1= 0, ( i = 2,3, ..., n-1 )

Page 36: AP3114 Lecture WK12 Fourier-Analysis Review

Example

ODE: dy/dx = y sin(x)

Initial conditions y(0) = 1, in the interval 0 < x < 5. Use ∆x = 0.5,

dy/dx = (yi+1−yi-1)/(2 ∆x), ⋅

Numerical solution:

yi-1 + 2 ∆x sin(x⋅ ⋅ i) y⋅ i− yi+1 = 0, (i = 3, …, n-1).

y1= 1 (1+∆x sin(x1)) y⋅ 1- y⋅ 2= 0.

dy/dx|x= 1= (y2-y1)/∆x = -y1sin(x1),

Page 37: AP3114 Lecture WK12 Fourier-Analysis Review

1. Linear Equations

Scilab

Page 38: AP3114 Lecture WK12 Fourier-Analysis Review

2. Free Fall• t=0, v=0,• Case 1. No air resistance

• Case 2. Resistance proportional to the linear power of velocity

• Case 3. Resistance proportional to the second power of the velocity

Page 39: AP3114 Lecture WK12 Fourier-Analysis Review

3. Electric CurrentV=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)

Page 40: AP3114 Lecture WK12 Fourier-Analysis Review

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 vertical displacement from y = 0 under the loaded condition, with y the coordinate in the direction opposite that of the load, can be written as follows:

whereE 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.

Page 41: AP3114 Lecture WK12 Fourier-Analysis Review

5.

cdt

dc 2

Diffusion equation:1)0,0( txc

0)0,( txc

Page 42: AP3114 Lecture WK12 Fourier-Analysis Review

5. Diffusion

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

2

),(2),1(),1(),()1,(

x

nicnicnic

t

nicnic

c(x,t)

x

t1

t3

t2

Results

cdt

dc 2

Page 43: AP3114 Lecture WK12 Fourier-Analysis Review

6. Random walk (Markov process)

• 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 moves in the direction of the café with probability 2/3 and in the direction of his home with probability 1/3.

• We would like to know the chances of the drunk ending up at home or in the café is. He starts at a given corner.

Home Cafe

3 621 4 5P=1/3 P=2/3

Page 44: AP3114 Lecture WK12 Fourier-Analysis Review

6. Random walk (Markov process)

• We represent the probabilities of being in these states a six-component state vector X(t), where Xi(t) is the probability of him being at intersection i at moment t. The components of X(t) must sum to 1, since he has to be in one of these states.

Home Cafe

3 621 4 5P=1/3 P=2/3

Transition probability matrix