0% found this document useful (0 votes)
37 views

Interpolation

The document discusses polynomial interpolation in mathematics. It includes the definition of interpolation polynomials using Lagrange polynomials and divided differences. It also discusses computing the interpolation polynomial using Newton's formula recursively and using Horner's method. Examples are given of interpolating the function 1/(1+x^2) at Chebyshev nodes for increasing degrees of the polynomial.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Interpolation

The document discusses polynomial interpolation in mathematics. It includes the definition of interpolation polynomials using Lagrange polynomials and divided differences. It also discusses computing the interpolation polynomial using Newton's formula recursively and using Horner's method. Examples are given of interpolating the function 1/(1+x^2) at Chebyshev nodes for increasing degrees of the polynomial.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 54

Math.

375
Interpolation

Vageli Coutsias, Fall `05


02/04/23 2
function plot_temp()
[L,K,T] = av_temp
plot(L,T(:,1),'g',L,T(:,2),'r',L,T(:,3),'b',L,T(:,4),'k')
title(' The temperature as a function of latitude ')
xlabel(' Latitude (in degrees) ')
ylabel(' Temperature ')
legend('K=.67','K=1.5','K=2.0','K=3.0')
function [L,K,T] = av_temp
% Q&S Table 3.1
L = 65:-10:-55;
K = [.67, 1.5,2.0,3.0];
% T is 13x4
T = [-3.10, 3.52, 6.05, 9.30;...
-3.22, 3.62, 6.02, 9.30;...
-3.30, 3.65, 5.92, 9.17;...
-3.32, 3.52, 5.70, 8.82;...
-3.17, 3.47, 5.30, 8.10;...
-3.07, 3.25, 5.02, 7.52;...
-3.02, 3.15, 4.95, 7.30;...
-3.02, 3.15, 4.97, 7.35;...
-3.12, 3.20, 5.07, 7.62;...
-3.20, 3.27, 5.35, 8.22;...
-3.35, 3.52, 5.62, 8.80;...
02/04/23
-3.37, 3.70, 5.95, 9.25;... 3
-3.25, 3.70, 6.10, 9.50];
Topics
• Polynomial Interpolation in 1d:
Lagrange & Chebyshev
• POLYFIT & POLYVAL
• 2D: Matrices and Color Images
• Trigonometric Interpolation
• Sound
function runge1()
close all
a = -1; b = 1; m = 17; %Equally Spaced, m=17
x = linspace(a,b,m); y = rung(x, 16);
x0 = linspace(-1,1,100); y0 = rung(x0,16);
[P,S] = polyfit(x,y,17);
pVal = polyval(P,x0);

plot(x,y,'ro',x0,pVal,'b',x0,y0,'r--')
axis([-1,1,-2,2])

function y = rung(x,a)
y =02/04/23
1./(1+a*x.^2); 5
[P,S] = polyfit(x,y,17);
pVal = polyval(P,x0);

02/04/23 6
% least squares fit: degre 8 fit based on 17 pts.
[P,S] = polyfit(x,y,8);
pVal = polyval(P,x0);

02/04/23 7
for i = 1:m+1 % Chebyshev Nodes
x(i,1) = .5*(b+a) +.5*(b-a)*cos(pi*(i-1)/m) ; end
y =1./(1+16*x.^2);
 

[P,S] = polyfit(x,y,17);
pVal = polyval(P,x0);

02/04/23 8
Machine assignment 2.7
(a) Write a subroutine that produces the value of the interp.

polynomial pn  f ; x0 , x1 ,  , xn ; t  at any real t, where

n0 is a given integer, xi are n+1 distinct nodes,


and f is any function available in the form of a function
subroutine. Use Newton’s interpolation formula and generate
the divided differences (from the bottom up) “in place” in
single array of dim n+1 that originally contains the function
values. 1
(b) Run your routine on the function f t   2
,5  t  5
1 t
i
using xi  5  10 , i  0,1,  , n. and n=2:2:8 (Runge)
n
Plot the polynomials against the exact function.
02/04/23 9
function c = InterpNRecur(x,y)
n = length(x); c = zeros(n,1);
c(1) = y(1);
if n > 1
c(2:n) = InterpNRecur(x(2:n), ((y(2:n)-y(1))./(x(2:n)-x(1)));
end
n 1
pn x   pn 1 x   cn  x  xi 
i 0

pn x0   y0  c0 ; pn xi   yi
02/04/23 10
function c = InterpN(x,y)
n = length(x);
for k = 1:n-1
y(k+1:n) = (y(k+1:n)-y(k))/(x(k+1:n)-x(k));
end
c = y;

x0 ,, xn  f  x0 , , xn 1  f  x1 ,, xn  f


xn  x0 
x0 , x1 ,, xn y : cn ; x0 y  c0 : y0
02/04/23 11
function pVal = HornerN(c,x,z)
n = length(c);
pVal = c(n)*ones(size(z));
for k=n-1:-1:1
pVal = (z-x(k)).*pVal + c(k);
end

px   c0  x  x0 c1  x  x1 c2  

02/04/23 12
% Ma505; Gautschi M.2.7
for m=1:4
n=2*m;
x = linspace(-5,5,n+1);
y = 1./(1+x.^2);
x0 = linspace(-5,5,1000);
y0 = 1./(1+x0.^2);
a = InterpNRecur(x,y);
pVal = HornerN(a,x,x0);
subplot(2,2,m)
plot(x0,y0,x0,pVal,'--',x,y,'*')
axis([-5 5 -1 1])
end02/04/23 13
02/04/23 14
n x  xj
 k x    , k  0,  , n
j 0 xk  x j
jk

1, j  k
k  Pn , k x j    jk  
0, j  k
LAGRANGE polynomials
02/04/23 15
Lagrangian Polynomial
Nodes : x 
n
i i 0

Values :  f xi 
n
i 1

Interpolating
Polynomial :  n f xi   f xi , i  0, , n
Interpolating Polynomial is a UNIQUE
polynomial of degree n that agrees with f(x)
02/04/23 16
at the n+1 nodes
n
 n f x    f xk k x 
k 0

Error

En f x   f x    n f x  
f 
n 1
 n
 

n  1! i 0
x  xi

max f n 1
 x
max En f x   xI
h n 1
xI 4n  1
02/04/23 17
Problem 2.24
Prepare plots of the Lebesgue function for interpolation,

n x ,1  x  1, for n  5,10,20 with the nodes xi

given by:
2i 2i  1
a xi  1  , b xi  cos  , i  0,1, , n
n 2n  2
Compute n x  on a grid obtained by dividing each

interval xi 1 , xi , i  1,2, , n, into 20 equal subintervals.

Plot log10 n x  in case (a) and  n x  in case (b).

02/04/23 18
The Lebesgue function n x  and constant  n x  for a grid
are defined as:
n
 n  x    li  x   n x    n 
i 0
n x  xj
with li x  the Lagrange polynomial: li x   
j 0 xi  x j
j i

Below we give a matlab script that computes the Lebesgue


functions (and constants) for the given grids.

02/04/23 19
% Math 505; Gautchi 2.24: Lebesgue functions
fprintf(' n equispaced Chebyshev \n')
for ii=0:2
n=5*2^ii; xa(1:n+1,1)=0; xb(1:n+1,1)=0;
for i= 1:n+1
xa(i,1) = -1+2*(i-1)/n;
xb(n+2-i,1) = cos(pi*(2*(i-1)+1)/(2*n+2));
end
for i=1:n
for j=1:20
ga(20*(i-1)+j,1)=((21-j)*xa(i)+(j-1)*xa(i+1))/20;
gb(20*(i-1)+j,1)=((21-j)*xb(i)+(j-1)*xb(i+1))/20;
end
end
ga(20*n+1,1)=xa(n+1);
gb(20*n+1,1)=xb(n+1);
for k = 1:20*n+1 %compute Lebesgue functions
na(:,k)=ga(k)-xa(:); nb(:,k)=gb(k)-xb(:); %numerators
end
for i = 1:n+1 % denominators
for j=1:n+1
da(i,j) = xa(i)-xa(j); db(i,j) = xb(i)-xb(j);
end
02/04/23 20
end
for k = 1:20*n+1
for i = 1:n+1
la(i,k) = 1; lb(i,k) = 1;
for j=1:n+1
if j~=i
la(i,k)=la(i,k)*na(j,k)/da(i,j); lb(i,k)=lb(i,k)*nb(j,k)/db(i,j);
end
end
end
lama(k)=1; lamb(k)=1;
for i=1:n+1
lama(k)=lama(k)+abs(la(i,k)); lamb(k)=lamb(k)+abs(lb(i,k));
end
end
figure
subplot(2,1,1); plot(ga,log(lama))
subplot(2,1,2); plot(gb,lamb)
lamax =lama(1); lambx =lamb(1); % compute Lebesgue constants
for k = 2:20*n+1
if lama(k) > lamax; lamax= lama(k); end
if lamb(k) > lambx; lambx= lamb(k); end
end
fprintf('%9i %22.14f %22.14f \n',n,lamax,lambx); clear
02/04/23 21
end
a

n=5 n=10

Lebesgue constants
a
n equispaced Chebyshev
5 4.10496 2.68356
10 30.8907 3.06859
20 10987.5 3.47908 b

The plots give the Lebesgue fncs


(log of same for grid (a)).
02/04/23 22
n=20
Fourier
series

a0 
f t     ak coskt   bk sin kt 
2 k 1
a
 k 1 2
 coskt 
    f t  dt
02/04/23  bk   t 0  sin kt   23
a0 m 1   kn   kn 
f n     ak cos     1 am
n
  bk sin 
2 k 1   m   m 
n  0, ,2m  1

2
1 1 2 m 1  kn 
ak   f t coskt dt   f n cos 
 t 0 m n 0  m 
2
1 1 2 m 1  kn 
bk   f t sin kt dt   f n sin  
 t 0 m n 0  m 

02/04/23 24
C
1 1  1 0  0 
 
       
  n   n   nm  1  
1 cos    1n sin    sin  
  m   m   m 
       
1 cos 2m  1    12 m1  2m  1 
sin   

sin 
n 2 m  1 

 m m m
      
^
F  a, b  a0 , a1 ,, am , b1 , , bm 1 
T

f   f 0 , f1 ,, f 2 m 1 (, f 2 m  f 0 )
T

^ ^
1
02/04/23
CF  f  F C f 25
Complex Form

n 2 
f x   k
c e ikx

k   n 2 

02/04/23 26
Band Limited Functions
a0 M
f t     ak coskt   bk sinkt   aM 1 cosM  1x 
2 k 1

Complex Form
M ( 1)
f x    ce k
ikx

 
k   M ( 1)
02/04/23 27
Real to Complex conversion

a k  ck  c k
bk  i ck  c k 
k  0,  , M
 aM 1 
 cM 1  cM 1  
 2 
02/04/23 28
Equispaced Sampling
M 
f x    ce k
ikx

 
k  M 
M 
f x j    ce k
ikjh
, x j  kh
 
k  M 
n n M 

 f x e
j 0
j
imjh
  ce
 
j 0 k   M 
k
ikjh imjh
e
02/04/23 29
Discrete Orthogonality
ih k  m n 1
n
1  e 0, k  m
 e ijh k  m 
  n  1 km  
j 0 1 e ih k  m 
n  1, k  m
n M  n

 f x e
j 0
j
 imjh
 
 
k  M 
c e k
j 0
ikjh imjh
e

n M 

 f x e
j 0
j
imjh
  c n  1
 
k  M 
k mk  cm n  1
02/04/23 30
Coefficients are computed exactly
from discrete sampling
n M 

 f x e
j 0
j
imjh
  c n  1
 
k  M 
k mk  cm n  1

1 n
cm   f x j e imjh

n  1 j 0
02/04/23 31
Real form; even or odd sample sizes


m
 kj   kj 
f j  a0    ak cos   bk sin   
k 1   m   m 
j  0, ,2m


m 1
 kj   kj 
f j  a0    ak cos     1 am
j
  bk sin 
k 1   m   m 
j  0, ,2m  1

02/04/23 32
n  0, ,2m  1
f
fn

n
tn 
m

02/04/23 33
t
The Fast Fourier transform
• fft: compute Fourier coefficients
from sampled function values
• ifft: reconstruct sampled values
from (complex) coefficients
• interpft: construct trigonometric
interpolant

02/04/23 34
FFT Discrete Fourier transform.

FFT(X) is the discrete Fourier transform (DFT) of vector X.

For length N input vector x, the DFT is a length N vector X,


with elements

N
X(k) = sum x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N.
n=1

The inverse DFT (computed by IFFT) is given by

N
x(n) = (1/N) sum X(k)*exp( j*2*pi*(k-1)*(n-1)/N), 1 <= n <= N.
k=1
02/04/23 35
See also IFFT, FFT2, IFFT2, FFTSHIFT.
INTERPFT 1-D interpolation using FFT method.
Y = INTERPFT(X,N) returns a vector Y of
length N obtained by interpolation in the
Fourier transform of X.

Assume x(t) is a periodic function of t with period


p, sampled at equally spaced points,
X(i) = x(T(i)) where T(i) = (i-1)*p/M,
i = 1:M, M = length(X). Then y(t) is another
periodic function with the same period and
Y(j) = y(T(j)) where T(j) = (j-1)*p/N,
j = 1:N, N = length(Y).
If N is an integer multiple of M, then
02/04/23 36
Y(1:N/M:N) = X.
function F =CSInterp(f)
n = length(f);
m = n/2;
tau = (pi/m)*(0:n-1)';
P = [];
for j = 0:m, P = [P cos(j*tau)]; end
for j = 1:m-1, P = [P sin(j*tau)]; end
y = P\f;
F = struct('a',y(1:m+1),'b',y(m+2:n));

m 1
 k   kn 
f n  a0    ak cos     1 am
n
  bk sin 
k 1   m   m 
n  0, ,2m  1
02/04/23 37
THE FOURIER COEFFICIENTS

F.a=[y(1),…,y(m+1)]
F
F.b=[y(m+2),…,y(2m)]
A CELL array: it’s elements are STRUCTURES
(here arrays)

F = struct('a',y(1:m+1),'b',y(m+2:n));

02/04/23 38
function Fvals = CSeval(F,T,tvals)
Fvals = zeros(length(tvals),1);
tau = (2*pi/T)*tvals;
for j = 0:length(F.a)-1
Fvals = Fvals + F.a(j+1)*cos(j*tau);
end
for j = 1:length(F.b)
Fvals = Fvals + F.b(j )*sin(j*tau);
end m 1
  2kt   2kt  
f t   a0    ak cos   bk sin  
k 1   T   T 
 2mt 
  am cos 
 T 
02/04/23 39
% Script File: Pallas
A = linspace(0,360,13)';
D=[ 408 89 -66 10 338 807 1238 1511 1583 1462
1183 804 408]';
Avals = linspace(0,360,200)';
F = CSInterp(D(1:12));
Fvals = CSeval(F,360,Avals);
plot(Avals,Fvals,A,D,'o')
axis([-10 370 -200 1700])
set(gca,'xTick',linspace(0,360,13))
xlabel('Ascension (Degrees)')
ylabel('Declination (minutes)')
02/04/23 40
The Gibbs
Phenomenon
….
02/04/23 41
% Script File: square
% Trigonometric interpolant of a square wave.
M=300
A = linspace(0,360,2*M+1)';
D(2:M)=0;D(M+2:2*M)=1;
D(1)=.5;D(M+1)=.5;D(2*M+1)=.5;
D=D';
Avals = linspace(0,360,1000)';
F = CSInterp(D(1:2*M));
Fvals = CSeval(F,360,Avals);
plot(Avals,Fvals)

02/04/23 42
….
can be seen at
all discontinuities
02/04/23 43
% Script File: sawtooth_wave.
clear
M=512
A = linspace(0,360,2*M+1)';
D(2:M)=(2:M)/M;D(M+2:2*M)=-1+(2:M)/M;
D(1)=0;D(M+1)=0;D(2*M+1)=0;
D=D';
Avals = linspace(0,360,1024)';
F = CSInterp(D(1:2*M));
Fvals = CSeval(F,360,Avals);
plot(Avals,Fvals)

02/04/23 44
The overshoot
is present at
any resolution…
02/04/23 45
…although the
interpolant is
correct at the
nodes as dx->0
02/04/23 46
1
y 2
,
1  .5 sin x
1  x  1

Compare with
equispaced
polynomial
interpolation
02/04/23 47
Input-output structures
• x=input(‘string’)
• title=input(‘Title for plot:’,’string’)
• [x,y]=ginput(n)
• disp(‘a 3x3 magic square’)
• disp(magic(3))
• fprintf(‘%5.2f \n%5.2f\n’,pi^2, exp(1))

02/04/23 48
Writing to and reading from a file
fid = fopen('output','w')
a=linspace(1,100,100);
fprintf(fid,'%g degC = %g degF\
n',a,32+1.8*a);
fid=fopen('output','r')
x=fscanf(fid,'%g degC = %g degF');
x=reshape(x,(length(x)/2),2)

02/04/23 49
----------------------------------------
I.
A=[1 2 3;4 5 6;7 8 9];
y=x'; %transpose
x=zeros(1,n);
length(x);
x=linspace(a,b,n);
a=x(5:-1:2);
%SineTable (Help SineTable)
disp(sprintf('%2.0f %3.0f ',k, a(k)));

----------------------------------------
----------------------------------------------------------------
II.
VECTOR OPERATIONS:

(1) vector: scale, add, subtract (scalar*vector)


2*[10 20 30] = [20 40 60]
(2) POINTWISE vector: multiply, divide, exponentiate (pointwise vector * vector)
[2 3 4].*[10 20 30] = [20 60 120]
(for pointwise operations, dimensions must be identical!)
(3) Setting ranges for axes,
Superimposing plots:
plot(x,y,x,exp(x),'o')
axis([-40 0 0 0.1])
using "hold on/off"
extended parameter lists
(4) Subplots: subplot(m,n,k)
(5) Vector linear combos: matrix*vector
(6) Tensor Products (x is array, A is array of arrays of same shape as x)
A=[f1(x) f2(x) ... fn(x)]
A(k,:) k-th row; A(:,i) i-th column
(7) loops: loop until ~condition or for fixed no. of times
while any(abs(term) > eps*abs(y))
for term = 1:n
end
(8) size(A) gives array dimensions (array, can be used to index other arrays)
length(x) gives vector dimension
----------------------------------------------------------------
----------------------------------------------------------------
II' (Miscellaneous topics)
rem(x,n): remainder
if x == 1
(< <= == >= > ~=)
(& and; | or; ~ not; xor exclusive or)
else
endif
s3 = [s1 s2]; concatenation of strings, treated like arrays
[xmax, imax] = max(x); (max val, index)
rat = max(x)/x(1)
x(1) = input('Enter initial positive integer:')
density = sum(x<=x(1))/x(1);
Summary
• Interpolation
• vanderMonde, Newton, Least Squares
• 2d interpolation: linear and cubic
• Color figures and uint8
• Smoothing of images
References
• find m-files at
www.math.unm.edu/~vageli
• Higham & Higham,
Matlab Handbook
• C.F.van Loan,
Intro to Sci.Comp. (2-3)

You might also like