Upload
lynga
View
219
Download
0
Embed Size (px)
Citation preview
1
Computational chemistry
I. part: Basics of signal processing with Octave
notes for lecture and practice
BSc in Chemistry, computational lab in 6th semester
Gergely Tóth
Loránd Eötvös University, Institute of Chemistry
6 hours of practice
Octave introduction, functions and basic programming 2-6
8 hours of lecture and practice
Solution of a set of linear algebraic equations 7-9
Numerical integration 10-12
Interpolation 12-13
Smoothing 13-14
Numerical differentials 14-15
Data storage in matrices – conventions, statistical applications 15-16
Principal component analysis 17-19
Fourier-transformation 20-29
References 29
version 2016.02.01
"Supported by the Higher Education Restructuring Fund allocated to ELTE"
2
Octave introduction, functions and basic programming
Set working directory, logbook
> pwd
> cd mydir (Linux) cd c:\a\teaching\compchem (Windows)
> diary logbook.dat # write a logbook
> ls
> quit
Initial steps
> x=2
> y=3;
> z=x+y**3
> x=cos(z)
> disp(" order of commands ")
> y=exp(x)*log(x)-log10(x)+sqrt(x)-ceil(x)+floor(x)-
fmod(x,2)+tan(x)
> function y=f(x,z) y=x+z*3-sqrt(x);endfunction; #preparation
of a function
> f(3,4)
> x=2
> z=6
> f(x,z)
> round(2.4)
> round(2.6)
> sign(3.2)
> sign(-3.2)
> factor(12312)
> lookfor factor
> factorial(12)
> s=1
> n=5
> for i=1:n s=s*i; endfor; #writing loops
> s
> help primes
3
> primes(100)
Row and column vectors
> v=[2;34;2]
> v=[2,34,2]
> u=rand(1,3)
> size(u)
> v=resize(v,1,4)
> u=resize(u,1,4)
> v*u'
> dot(v,u)
> sum(v)
> function s=sumvtg(v,n) s=0;for i=1:n s=s+v(i); endfor;
endfunction;
> sumvtg(v,4)
> sumvtg(u,4)
> function [su,sv]=sumuvtg(u,v,n) su=0;sv=0;for i=1:n
sv=sv+v(i);su=su+u(i); endfor; endfunction;
> sumuvtg(u,v,4)
> [s1,s2]=sumuvtg(u,v,4)
s1 = 0.35839
s2 = 38
> function [su,sv]=sumuvtg(u,v,n) su=0;sv=0;for i=1:2:n
sv=sv+v(i);su=su+u(i); endfor; endfunction;
> [s1,s2]=sumuvtg(u,v,4)
> prod(v)
> sumsq(v)
> sumsq(v.*u) # . = multiply one by one element
> v
> sort(v) # sorting of vectors
Exercise:
Write a function to calculate sum of squares of vector
elements
4
Commands for programming
< > =< == & | ! !=
while( ) body endwhile
if( ) body elseif() body else body endif
do body until( ) – stops if it is true
while( ) body endwhile – runs if it is true
break – jump out from for or while
continue – jump to next in for or while, the rest is skipped
from the commands
Exercises:
Write a function to calculate sum of squares of vector
elements, as long as there is a 0 element.
Write a function to calculate the number of vector elements
smaller than a given number.
Write a function to find the roots of second order
polynomials.
Write a function to calculate cross and triple product of
vectors.
Complex numbers
> z=0.1+2i
> real(z)
> imag(z)
> abs(z)
> i=sqrt(-1) #define complex unit
Exercises:
Write a function to calculate the conjugate of a complex
number.
Write a function to calculate the conjugate of a complex
number, if the imaginary part was negative.
Coordinate transformation
> [theta,r]=cart2pol(1,0)
5
theta = 0
r = 1
> cart2pol(1,0)
> [x,y]=pol2cart(0,1)
> [x,y,z]=sph2cart(0,0,1)
Exercise:
Write a function to calculate degree to radian.
Write a function to calculate the highest common factor or
lowest common multiple. Look for Euclidean algorithm and
relation of the two numbers.
Graphics
> plot(u,v,"4+") #4 colour, + sign
> print -djpg proba.jpg
Matrices
> A=[1,2;3,4]
> A(1,2) # given element
> A(:,1) # all elements of column 1
> B=randn(2,2)
> A*B
> C=randn(2,3)
> C*A
error: operator *: nonconformant arguments (op1 is 2x3, op2 is
2x2)
> A*C
> C'*A
Exercise:
Write a function to multiply matrices (multiple loops)
> det(A)
> inv(A)
> A*inv(A)
> eig(A)
6
> [vA,eA]=eig(A)
> A==B
> v=vec(A)
> u=v'
> B=eye(2) #Diagonal Matrix
> trace(A)
Exercise:
Write a function to substitute trace command
> sortrows(A,2)
Exercise:
Create an algorithm and write a function to sort vector
elements.
> save amatrix.dat A
> A=A'
> load amatrix.dat
7
Solution of a set of linear algebraic equations
Theory
General form of the equations:
a11x1 + a12x2 + …….. + a1nxn = b1
a21x1 + a22x2 + …….. + a2nxn = b2
…………………………………………………….
an1x1 + an2x2 + …….. + annxn = bn
n equations n unknowns (xi) n×n coefficients (aii)
using matrix and column vector notation
Ax=b
Unique solution exists, if ∃ bi ≠ 0 and det(A) ≠ 0
Cramer-method
)Adet(
)Adet( k=kx , where
=
+−
+−
nknnknn
nkk
aabaa
aabaa
......
.....................
......
A
1,1,1
11,111,111
k
Inverse matrix method
Ax=b
A-1Ax=A-1b see A-1A = I (identity matrix)
x=A-1b
Gauss-elimination
Extended matrix formalism
The size of the extended matrix is n×(n+1)
8
Schematic view of the two steps:
elimination step back substitution step
Example for the elimination process
−−→
−−
−−→
−− 105200
13500
21320
15123
42760
13500
21320
15123
42760
35140
21320
15123
)) ba
a) Subtract from line III 2 times line II
b) Subtract from line IV -3 times line II
Pivoting to avoid division failure:
if aii=0 (or very small), change lines as long as to eliminate aii ≠ 0
Practice
> A=[2,4,6;2,3,1;-1,0,5]
> b=[8;7;-2]
> inv(A)*b
> B=A #for Cramer-method
> B=A
> B(:,1)=b
> det(B)/det(A)
> B=A
> B(:,2)=b
> det(B)/det(A)
Theory
Overdetermined case
n number of lines, m number of unknowns, n>m
Anxmxmx1≈bnx1
9
Derivation of the analytical form:
Ax≈b
ATAx≈ATb where AT is the transpose of A
(ATA)-1(ATA)x=(ATA)-1ATb where (ATA)-1(ATA)=I
x=(ATA)-1ATb
Overdetermined case ≈ least square regression of lines, planes and hyper-planes
Practice
> A=resize(A,4,3)
> A(4,:)=[2,3,2]
> b=resize(b,4,1)
> b(4)=4
> inv(A'*A)*A'*b
Theory
Addition of a constant term - intercept
Extend A with a column vector of 1-s
1
1
1
A
Practice
> A=resize(A,5,4)
> A(:,4)=1
> A(5,:)=[-1,5,-1,1]
> b=resize(b,5,1)
> b(5)=0
> inv(A'*A)*A'*b
10
Numerical integration
Theory
n known xi-yi pairs (e.g. from experiment); equidistant x -h=xi+1-xi
task: numerical determination of the area
Newton-Cotes equations
trapezoidal rule – approximation of the area with trapezoids
( )∫+
++≈ +
1
)(2
)( 3
1
i
i
x
x
ii hyyh
dxxf ο local form for one trapezoid
( )∫ +++++++≈ −
b
a
nni hyyyyyh
dxxf )(2.....22
)( 2
121 ο global form for the total
interval
)( 2hο ordo means in math that the error is directly proportional to h2
Simpson 1/3 rule
( )∫+
+++≈ ++
2
)(43
)( 5
21
i
i
x
x
iii hyyyh
dxxf ο local form
( )∫ ++++++++≈ −−
b
a
nnn hyyyyyyyyh
dxxf )(42...24243
)( 4
1254321 ο global form
11
n should be odd number
Practice
> u=rand(1,20);
> for i=1:20 v(i)=i*0.1; endfor;
> u
> v
> function s=trint(x,y,n) s=0; for i=1:n-1 s=s+(x(i+1)-
x(i))/2*(y(i)+y(i+1));endfor;endfunction
> trint(v,u,20)
ans = 0.87450
> trapz(v,u)
ans = 0.87450
> function s=simpint(x,y,n) s=0; for i=1:2:n-2 s=s+(x(i+1)-
x(i))/3*(y(i)+4*y(i+1)+y(i+2));endfor;endfunction
> simpint(v,u,20)
ans = 0.73852
> quad('cos',0,pi()/2)
ans = 1.0000
> quadl('cos',0,pi()/2)
ans = 1.0000
Polynomials in Octave
> c=[2,3,4,5,1]; #coefficients (last one is the
constant)
> a=[1,2,3,4];
> conv(a,c) #convolution
> polyder(a) #derivation
> q=polyder(a,c) #derivate a convolutions
> [q,r]=polyder(a,c) #rational functions
> roots(a) #roots
> for i=1:20 u(i)=v(i)+cos(v(i)*0.3); endfor;
> p=polyfit(v,u,5) #fit of polynomials
> pint=polyint(p) #integration
12
> polyval(pint,pi()/2)-polyval(pint,0) #evaluation of
polynomials
Interpolation
Theory:
n known xi-yi pairs
task: find y for a given x
basic idea fit polynomial max Pn-1(x)
interpolation xЄ[xmin ; xmax]
extrapolation x<xmin or xmax<x (dangerous, if no e.g. physical law behind it)
Lagrange-interpolation
Cubic polynomial using the 4 next neighbouring points
4
342414
321
3
432313
4212
423212
4311
413121
4323
))()((
))()((
))()((
))()((
))()((
))()((
))()((
))()(()(
yxxxxxx
xxxxxx
yxxxxxx
xxxxxxy
xxxxxx
xxxxxxy
xxxxxx
xxxxxxxp
−−−
−−−+
−−−
−−−+
−−−
−−−+
−−−
−−−=
Practice
> interp1(v,u,1.05)
> interp1(v,u,1.05,'linear')
> interp1(v,u,1.05,'cubic')
Theory
Spline interpolation
simple polynomial interpolation is not smooth at the measured points
for equidistant x -h=xi+1-xi
13
local, e.g. cubic polynomials, altogether n-1 polynomials
gi(x)=ai(x-xi)3+ bi(x-xi)
2+ ci(x-xi)+ di
gi(xi)=yi
gi(xi+1)=yi+1
gn-1(xn)=yn
g’i(xi+1)= g’i+1(xi+1)
g’’i(xi+1)= g’’i+1(xi+1)
= set of linear equations (2 extra equations are necessary, e.g. ‘natural’ spline)
spline can be differentiated (e.g. use in mechanics dr
rdUrF
)()( −= )
in real applications:
a) calculation of all coefficients (ai, bi, ci, di)
b) use for different x-s to calculate y(x)-s
Practice
> interp1(v,u,1.05,'spline')
Smoothing
Theory
Golay-Savitzky filter
n known xi-yi pairs; equidistant x -h=xi+1-xi
Fit, e.g. cubic polynomials on 5 points (k=2 case)
Replace yi with iy~ (xi and h are not used in this form)
∑+=
−=
−=kij
kij
jiji ycF
y1~
Table of Golay-Savitzky coefficients for general points and endpoints
yi c-2 c-1 c0 c1 c2 F
-3 12 17 12 -3 35
y1 c1 c2 c3 c4 c5 F
14
yn cn cn-1 cn-2 cn-3 cn-4 F
69 4 -6 4 -1 70
y2 c1 c2 c3 c4 c5 F
yn-1 cn cn-1 cn-2 cn-3 cn-4 F
2 27 12 -8 2 35
Practice
> function [w]=golay(v,n) for i=3:n-2 w(i)=1/35*(-3*v(i-
2)+12*v(i-1)+17*v(i)+12*v(i+1)-
v(i+2));endfor;w(1)=v(1);w(2)=v(2);w(n-1)=v(n-
1);w(n)=v(n);endfunction;
> golay(v,20)
Numerical differentials
Theory
Simple forms
n known xi-yi pairs; equidistant x h=xi+1-xi
)(2
)(' 211 hh
yyxf ii
i ο+−
≈ −+
)(2
)('' 2
2
11 hh
yyyxf iii
i ο++−
≈ −+
)(12
163016)('' 4
2
2112 hh
yyyyyxf iiiii
i ο+−+−+−
≈ −−++
symmetric choice of points has better performance
Differentials with Golay-Savitzky filter
∑+=
−=
−=kij
kij
jiji ydFh
y1
'~
Table of Golay-Savitzky differential coefficients
yi d-2 d-1 d0 d1 d2 F
1 -8 0 8 -1 12
Practice
> function [w]=golayder(u,v,n) h=u(2)-u(1);for i=3:n-2
w(i)=1/12*(v(i-2)-8*v(i-1)+8*v(i+1)-v(i+2));endfor;w(1)=(v(2)-
15
v(1))/h;w(2)=(v(3)-v(1))/2/h;w(n-1)=(v(n)-v(n-
2))/2/h;w(n)=(v(n)-v(n-1))/h;endfunction;
> golayder(u,v,20)
> quit
Data storage in matrices – conventions, statistical applications
Theory
n rows - n objects m columns – m variables
=
333231
232221
131211
ddd
ddd
ddd
Dnxm
mean: n
d
d
n
i
ij
j
∑== 1
variance:
( )
1
1
2
2
−
−
=∑
=
n
dd
s
n
i
jij
j
standard deviation
( )
1
1
2
−
−
=∑
=
n
dd
s
n
i
jij
j
centering jij
n
i
ij
ijij ddn
d
dc −=−=∑
=1
scaling
( )
1
1
2
−
−
==
∑=
n
dd
d
s
dc
n
i
jij
ij
j
ij
ij
standardization (z-scoring, studentization)j
jij
ijs
ddc
−=
covariance:
( )( )
1
1
−
−−
=∑
=
n
dddd
s
n
i
kikjij
jk
correlation coefficient kj
jk
jkss
sr = range [-1;1]
around -1 close to directly proportional j and k variables with negative slope
16
around 1 close to directly proportional j and k variables with positive slope
Practice
> D=rand(10,5)
> D(:,2)=cos(D(:,1))
> D(:,3)=D(:,1)*2+rand()
> D(:,4)=exp(D(:,1))
> D(:,5)=D(:,2)+D(:,4)+rand()*0.1
> mean(D)
> median(D)
> meansq(D)
> std(D)
> var(D)
> sortrows(D,2)
> statistics(D)
> help statistics
> center(D)
> A=studentize(D)
> mean(A) #mean of standardized equals 0
> std(A) #standard dev. of standardized equals 1
> cov(D) # in a matrix
> cor(D)
> cov(A) # covariance of standardized equals correlation
> anova(D)
One-way ANOVA Table:
Source of Variation Sum of Squares df Empirical Var
*********************************************************
Between Groups 142.7943 4 35.6986
Within Groups 30.2420 45 0.6720
---------------------------------------------------------
Total 173.0363 49
Test Statistic f 53.1194
p-value 0.0000
17
> p=var_test(D(:,1),D(:,2))
> p=t_test(D(:,1),0.5)
> p=t_test(D(:,1),1)
> p=t_test(D(:,1),2,”<>”)
> p=t_test(D(:,1),0.78,”<”)
> p=t_test_2(D(:,1),D(:,2),”<>”)
Principal component analysis
Theory
to reduce the number of variables
to find relationship among variables
to cluster objects or variables
to perform principal component regression
Factoring D Dnxm=TPT
=mxaP loading matrix: coefficients of old variables in the new principal
components (super variables) a<=min(n,m)
=nxaT score matrix: object coordinates in the new variables;
PPT=I and TTT=I (orthogonal matrices)
T,P first vectors contain the majority of the total variance in the data matrix. Total variance:
sum of column variances, 2
js -s.
Outer product representation of the principal component factorization:
18
Numerical methods to determine principal components
Eigenvalue determination
Cmxn=DcTDc C covariance matrix, Dc centred data matrix
C=ZΛZ-1= ZΛZT
P=Z, where Z eigenvectors of C
T= DZ Λ, where Λ eigenvalues of C (diagonal matrix, λi-s are in the diagonal)
Singular value decomposition
Dnxm=UnxmWmxm(Vmxn)T
T=UnxmWmxm
PT=(Vmxn)T
UTU=I VTV=I (U and V are orthogonal matrices)
λi=wii2
How many principal components are necessary?
explained variance of the i-th principal component ∑
=i
i
λ
λ
a) sum up to K components as long as 95% is explained
b) scree plot
19
Graphical representation of loading and score matrices
Loading plot Score plot
for variables for objects
For example x-axis = principal component 1, y-axis = principal component 2
Practice
> A=studentize(D)
> [Ve,E]=eig(A'*A)
> [U,W,V]=svd(A)
> size(U)
> size(W)
> size(V)
> trace(E)
> for i=1:5 E(i,i)/trace(E) endfor;
> s=0;
> for i=1:5 s=s+W(i,i)**2; endfor;
> for i=1:5 W(i,i)**2/s endfor;
> UW=U*W
> plot(UW(:,1),UW(:,2),"4+")
> plot(V(:,1),V(:,2),"4+")
20
Theory
Fourier transformation
Jean-Baptiste Joseph Fourier (1768—1830) French mathematician
Transformation: change, replace
In mathematics function and variable changes:
e.g., Legendre-transform in thermodynamics
U(S,V) S→T variable replacing, but keep the whole information content of U
A(T,V)=U-TS
V→p replacement: G(T,P)=U-TS+pV
Integral-transforms: Laplace, Fourier...
Why it is important for chemists?
• mathematics, physics: solution of partial differential equations
• one function is measured or observed, its transform has expressive meaning
(spectroscopy, diffraction)
Eustach
density changes of air → frequency, amplitude
(20Hz-20kHz) low and high voice at different place in the spiral
← cilia
← liquid
21
Fourier-transformational and canonical infrared spectroscopy (FTIR and IR)
Same is measured by using different variables: FTIR-in time, IR-in frequency. (non-
mathematically one function in two representation)
22
FTIR: less optic, better resolution, better signal/noise ratio, shorter measurement time,
because the whole frequency domain is measured at once, scanning in time domain is fast.
Michelson-interferometer: Replace the magnitude of the signal to a measurable time domain
vmτ = λ/2
f = 1/τ = 2νm/λ
λ = c/ν
f = (2νm/c) ν
2νm/c ≈ 10-10
vm moving speed of mirror, τ equals the time for λ/2 length, f frequency, c speed of light, v
optical frequency
23
• mathematics is different in the transformed space (theoretical chemistry / physical
chemistry calculations, medical diagnostics, image processing, image compression,
data evaluation, noise filtering)
Sum of electrostatic interactions in periodic systems:
Coulomb interaction decays 1/r, infinite amount of periodic images should be calculated.
Ewald: transform the problem to the reciprocal space (1/length unit) better convergence of the
sum
Medical diagnostics: 3D methods
The Radon transformed representation of the density of the human body is measured in layers
Image compression, noise filtering: After transformation we can select between noise and
signal, satellite images
Convolution: enhance of contrast in images
Deconvolution: e.g., kinetics
Mathematical background of Fourier series
Periodic f(t) function as sum of sines and cosines, if:
T periodic
T finite number min., max, and discontinuity
and ∞<∫T
dttf0
)(
∑∑∞
=
∞
=
++=11
0 2sin
2cos
2)(
n
n
n
nT
ntb
T
nta
atf
ππ
∫+
=
Tt
t
n dtT
nttf
T
a0
0
2cos)(
1
2
π ∫
+
=
Tt
t
n dtT
nttf
T
b0
0
2sin)(
1
2
π
∫+
=
Tt
t
dttfT
a0
0
)(1
0 usually small n is enough
24
Periodic function in exponential form:
)sin()cos( titeit +=
∑∞
−∞=
=n
ti
nectfω)(
t
nπω
2=
t
n=ν
2
nnnn
ibacc
−== n≠0
Fourier-transformation – continuous, aperiodic function
Fourier-transform: inverse Fourier-transform:
∫∞
∞−
−= dtetfF ti πνν 2)()( ∫∞
∞−
= νν πνdeFtf
ti2)()(
Attributes:
)()()()( 22112211 νν FaFatfatfa +⇔+ linear
⇔
aF
aatf
ν1)( time scaling
( )νbFb
tf
b⇔)(
1 frequency scaling
( ) 02
0)(ti
eFttfνπν⇔− time shift
( )0
2 0)( νννπ −⇔−Fetf
ti frequency shift
25
Convolution: )()()()()( ννττ HGdtthtghg ⇔−≡Ψ≡∗ ∫∞
∞−
1) g(t) (on scheme v(t)) input impulse causes h(t) response
2) many impulses – sum of many h(t)-s
3) if g(t) is a function, then response is a convolution
The Fourier transform of the convolution of two functions is the product of the individual
Fourier transforms
Image processing: f(t) is convolved with other functions, e.g. contrast
(Femto)kinetics: Convolved function is measured (kinetics + instrument), but if we know the
instrument function, we can get the kinetic function (deconvolution)
Correlation )()()()()(),( ννττ ∗
∞
∞−
⇔+≡Ω≡ ∫ HGdtthtghgcorr
autocorrelation, if )()( thtg =
Velocity autocorrelation function: memory of the particle velocity in a liquid
0 1 2 3
vacf(t)
t/ps
sebesség autokorrelációs függvény
0 50 100 150 200 250
F(v)
v/(1/cm)
spektrális sűrűség
Many measurements provide information on correlations, e.g., IR on a liquid =
autocorrelation of the total dipole moment of the system
26
Some transform
To notice:
rectangular signal:
transform ≈ sin(x)/x shape
broader signal → narrower transformed signal
Gaussian-curve:
transform is Gaussian, as well
broader Gaussian signal → narrower transformed signal
cosines:
transform: Dirac-delta pair
comb function:
transform: comb, as well
Discrete Fourier-transform
N samples ∆t frequency
notations: tN∆
=∆1
ν N
nkt
ππν
22 =
kFkFF =∆= )()( νν nftnftf =∆= )()(
Transforming equation:
∑−
=
−
≡1
0
21 N
n
N
nki
nk efN
F
π
Inverse:
∑−
=
≡1
0
2N
k
N
nki
kn eFf
π
all points are necessary for one point in the other space
28
Sampling theory: Nyquist-frequency:
tc
∆=
2
1ν
c
tν2
1=∆ the frequency depends on the frequencies in the system
advantage:
if F(ν)=0 for all ν >νc-re, then all F(ν) can be determined for all ν < νc
disadvantage:
if F(ν)≠0 for ν >νc-re, it biases F(ν) in νЄ[-νc,νc]
broadening effect:
continuous cosine ↔ Dirac-delta pair
discrete cosines ↔ new lines close to the original ones
determination of νc:
- trial and error refining of the sampling frequency
- filtering with low transmission filters
- windowing: multiplication before the transformation with a window function
Even and odd functions: cosines or sine solely
Structure of liquids: g(r) (pair-correlation function) can be calculated (relative density at r
distance from a particle), S(q) (structure function) measured in diffraction experiments, not
expressive. S(q) and g(r) related with sine transform.
∫ −+= drrgqr
qrrqS )1)((
)sin(41)( 2πρ
0
1
2
3
4
0 5 10 15 20
g(r)
r/Angstrom
párkorrelációs függvény
-1
0
1
2
3
4
0 5 10 15
S(q)
q / (1/Angstrom)
szerkezeti függvény
29
Fast Fourier-transformation (FFT)
Fk calculation scales N2
Danielson-Lánczos theory:
Fk can be calculated as special sum of (Fkeven) and (Fk
odd)
The factorization is recursive down to 1-1-elements. It can be used to build an algorithm,
where all multiplication or summation is performed only one time:
final scaling Nlog2N
N must be here power of 2 (…128, 256, 512, 1024, 2048 ,5096…). Fill with zeroes the rest up
to power number of 2, if we have less data.
Fast cosine transform, e.g., JPEG image compression
References:
Further reading:
S. Dowdy and S. Wearden: Statistics for research, Wiley 1982, New York
W.H. Press, S.A. Teukolsky, W.T. Vetterling and B.P. Flannery: Numerical recipes in FORTRAN, Cabridge Univ.
Press. 1982, Cambridge
Valkó P., Vajda S.: Műszaki-tudományos feladatok megoldása személyi számítógéppel, Műszaki könyvkiadó,
1987, Budapest.
Source and original idea of some exercises:
Deutsch T., Vajda S., Valkó P., Riedel M: Kémiai számítástechnika füzetek, ELTE TTK Kémiai Kibernetikai
Laboratórium, 1976-1987