Interpolation
?
Fitting
Fitting data: linear least square method
Sea un conjunto de n puntos del plano
M = {(x1,y1); (x2,y2); …;(xn,yn)} con i=1,…,n
Interpolation
(*)
Fitting data: linear least square method
If:
є Rn
U U┴
U||
L
(**)
(*)
(**) Theorem:
(**) Theorem:
Fitting data: linear least square method
If:
Example: linear fitting
Z Y
a*=(ZT.Z)-1. ZT.Y
Example: linear fitting
e = Y - Z . a*
A Fortran code for linear fitting
program linear_least_squares
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
implicit none
integer(4) :: i,n,m ! n: number of data; m=number of fitting parameters
real(8), allocatable :: x(:),y(:) ! data
real(8), allocatable :: a(:) ! fitting parameters
real(8),allocatable :: z(:,:),zt(:,:),ztz(:,:),inv_ztz(:,:),prodm(:,:),identity(:,:)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
open(10,file='data_linear.in',status='old') ! Start reading data
read(10,*) n
print *, 'Number of data:',n
allocate (x(n),y(n))
print *,' Data:'
do i=1,n
read(10,*)x(i),y(i)
print *,x(i),y(i)
enddo
read(10,*) m
print *, 'Number of parameters to be optimized:',m
close(10) ! End reading data
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
allocate(z(n,m)) ; call zmatrix(n,x,m,z) ! computing Z_(nxm)
allocate(zt(m,n)) ; zt=transpose(z) ! computing the transpose of Z_(nxm)
allocate(ztz(m,m)) ; ztz=matmul(zt,z) ! computing Z_(nxm)^t . Z_(nxm)
Tema: Interpolación
- como caso particular de fitting (m=n)
- polinomial: Lagrange y Newton
- segmentaria: splines
Interpolation vs. fitting methods
Interpolation
Fitting
Interpolation
Interpolation can be viewed as a VERY particular case of fitting, possible in SOME cases:
BUT often, finding the inverse of a big matrix entails large round errors
Polynomial Interpolation
FORMA DE LAGRANGE:
El matemático francés J. L. Lagrange descubrió que se puede encontrar
el polinomio interpolador usando un método distinto.
Turín, Italia
Nacionalidad: Francesa
Conjunto de
n+1 polinomios
de grado n
Polinomios
En forma general: coeficientes de
Lagrange
Propiedades de los polinomios Lk(n)(x):
El polinomio no necesariamente va a ser de grado n, sino que puede ser de grado menor que n
Ventajas:
• No es necesario resolver un sistema lineal de ecuaciones que
generalmente está mal condicionado
• Se puede conocer el valor del polinomio interpolante en un punto
intermedio cualquiera xp sin necesidad de conocer sus coeficientes ai
Desventajas (de la forma de Lagrange):
* Si queremos agregar un nuevo dato, hay que generar desde cero un nuevo polinomio
Fórmula de Newton
Para funciones como sin(x) o e-x, cuyas derivadas están acotadas por
una misma constante M, la respuesta es sí.
EJEMPLO
Isaac Newton
Nacimiento: 04/01/1643
Woolsthorpe Manor,
Reino Unido
Fallecimiento: 31/03/1727
Kingston, Londres
Entierro: Abadía de
Westminster, Londres
Polynomial Interpolation
FORMA DE NEWTON:
Isaac Newton
Nacimiento: 04/01/1643
Woolsthorpe Manor,
Reino Unido
Fallecimiento: 31/03/1727
Kingston, Londres
Entierro: Abadía de
Westminster, Londres
Polynomial Interpolation
FORMA DE NEWTON:
Polynomial Interpolation
FORMA DE NEWTON EN TERMINO DE DIFERENCIAS DIVIDIDAS:
Polynomial Interpolation
n+1 data
n+1 data
If the extra condition involves the polinomial of the j-th interval, we can find first: aj,bj,cj, and
then move backward and forward (using the continuity conditions) to find all the coeficients
(i=j-1, j-2,…,1 and j+1,j+2,…,n) WITHOUT SOLVING ANY SET OF LINEAR EQUATIONS
1D cubic splines
Splines interpolation:
a3x3+b3x2+c3x+d3
Cubic splines
(the most popular ones) a1x +b1x +c1x+d1
3 2
a2x3+b2x2+c2x+d2
n+1 data
4n unknown coefficients (4 per interval)
n+1 data
4n unknown coefficients (4 per interval)
n+1 data
4n unknown coefficients (4 per interval)
Solving a 4nx4n set of linear equations would be the BRUTE-FORCE option: Not recommended !!
What to do to (at least) reduce the set of equations to solve ?
(if the extra conditions do not involve a single polynomial)
1D cubic splines
1D cubic splines
1D cubic splines
1D cubic splines
Splines interpolation:
a3x3+b3x2+c3x+d3
Cubic splines
Implementation a1x +b1x +c1x+d1
3 2
a2x3+b2x2+c2x+d2
i=1,…,n
1D cubic splines
Splines interpolation:
a3x3+b3x2+c3x+d3
Cubic splines
Implementation a1x +b1x +c1x+d1
3 2
a2x3+b2x2+c2x+d2
n+1 data
Unique solution
fi(x)=Ai(x)f(xi-1)+Bi(x)f(xi)+Ci(x)f’’(xi-1)+Di(x)f’’(xi) i=1,…,n
Ai(x)=(xi-x)/(xi-xi-1)
IMPORTANT:
The matrix of coefficients depends on: Bi(x)=1-Ai(x)
- the set of knots: xi i=0,1,…,n
Ci(x)=[Ai(x)3-Ai(x)](xi-xi-1)2/6
- f(xi) i=0,1,…,n
Di(x)=[Bi(x)3-Bi(x)](xi-xi-1)2/6
1D cubic splines
Splines interpolation:
a3x3+b3x2+c3x+d3
Cubic splines
Implementation a1x +b1x +c1x+d1
3 2
a2x3+b2x2+c2x+d2
n+1 data
ONLY ONCE !
splines1d.in
compile_splines1d
./splines1d.x
splines1d.out
Cubic Splines
How to proceed in 2D ?
allocate(f(ndatx,ndaty),f2x(ndatx,ndaty),f_int_x(ndaty),f2_int_x(ndaty))
do i=1,ndatx
do j=1,ndaty
f(i,j)=fun(x(i),y(j)) ! compute and store the function in the knots
enddo
enddo
do j=1,ndaty
call spline(x, f(:,j), ndatx, yp1, ypn, f2x(:,j)) ! construction (ndaty times)
enddo
…
2D Cubic Splines exercise
splines2d.f90
function fun(x,y)
implicit none
real (8) :: x,y,fun
fun=exp(-4._8*(x**2+y**2))
end function fun
y
2D Cubic Splines example
4x4=16 knots
1
0
0 1 x
Gaussian function
centered at the origin
y
2D Cubic Splines example
4x4=16 knots
1
0
0 1 x
7x7=49 knots Gaussian function
centered at the origin
y
x
How much can we increase the number of variables ?
N atoms → 3N coordinates E.g. N=2 atoms (H2 in an external potential)
Is this possible ?
Some coordinates can be uncoupled one from the other (symmetry properties)
How much can we increase the number of variables ?
Coupling between coordinates: what does it mean ?
We say that there is a strong coupling between two coordinates (x,y) when we deal with a
function f(x,y) if the y-dependence of f change a lot with the value of x and vice versa
4 points is ENOUGH !
If we set f’(0)=0
Is this possible ?
Some coordinates can be uncoupled one from the other due to a symmetry property !
But the coordinates convenient for two symmetry properties of the potential are not the same !
FIN NOTAS DE CLASE