Introduction to scientific Computing A Matrix Vector Approach Using Matlab Written by Charles FVan Loan 陈文斌 Wbchen(fudan. edu. cn 复日大学
Introduction to Scientific Computing -- A Matrix Vector Approach Using Matlab Written by Charles F.Van Loan 陈 文 斌 Wbchen@fudan.edu.cn 复旦大学
Chapter3 Piecewise polynomial Interpolation Piecewise Linear Interpolation Piecewise Cubic Hermite Interpolation Cubic splines
Chapter3 Piecewise Polynomial Interpolation • Piecewise Linear Interpolation • Piecewise Cubic Hermite Interpolation • Cubic Splines
Piecewise Linear Interpolation 0.8 0.6 0.4 0.2 0.2 -0.4 -0.6 -0.8
0 1 2 3 4 5 6 7 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 Piecewise Linear Interpolation
(x,y) L(=)=a+b(-2;) y+1-y i+1 Piecewise Linear L(=)ifx1≤z≤x l()=」L2()x,sz∠3 (=)ifxn-1≤z≤x
( ) ( ) i i i i ( , ) L z = a +b z − z i i x y i i i i i i i x x y y a y b − − = = + + 1 1 , = n− n− n L z x z x L z x z x L z x z x L z 1 1 2 2 3 1 1 2 ( ) if ( ) if ( ) if ( ) Piecewise Linear
for i=1 b(1)=(y(i+1)-y(1)/(x(i+1)-x(1) function [a, b]=pwL(x,y) end n=length(x) a=y(1:n-1) b=(y(2n)y(1:n-1)/(x(2n)-X(1n b= diff(y). /diff(x) b=diff(y). /diff(x) Test code zlinspace(0, 1, 9) la, b=pwL(z, sin(2*pi*z))
function [a,b] = pwL(x,y) n = length(x); a = y(1:n-1); b = diff(y) ./ diff(x); z=linspace(0,1,9); [a,b]=pwL(z,sin(2*pi*z)); for i=1:n-1 b(i)=(y(i+1)- y(i))/ (x(i+1)- x(i)); end b=(y(2:n)-y(1:n-1))./(x(2:n)-x(1:n- 1)) b=diff(y)./diff(x) Test code
Evalution la, B L(z)=? Problem:z∈[x,x if==x(n) - n else =sum(X<=z) end
Evalution z [, ] L(z) = ? Problem: [ , ], ? z xi xi+1 i = if z==x(n) i =n-1; else i=sum(x<=z); end
Binary search mid= floor((Left+ Right ) /2) linspace(1,100,10000),z=48.598 If z<x(mid) Right=mid Else Left =mid end N=10000一n=log2N≤13.3
Binary search mid = floor((Left+Right)/2); If z<x(mid) Right=mid; Else Left =mid; end N =10000 n = log2 N 13.3
function 1=Locate(x, z, g, %og(1-g<-=n-1)is an optional input parameter search for i begins, the value i-g is tried g·guss if nargin==3 if(x(g)<=z)&(z<x(g+1) return end: end n= length(x) ifz==x(n) else Left=1: Right=n while right Left+l Binary search end
function i = Locate(x,z,g) % g (1 Left+1 Binary_search end g: guss
function LVals- pWLEval(a, b, x, Z Vals) o Evaluates the piecewise linear polynomial defined by the column /o(n-1)-vectors m= length( Vals) m-vector LVals= zeros(m, 1) for j=l: m i=Locate(x, z vals(, g) LVals(=a(1+ b(i)*(zVals(-x(i) g end
function LVals = pwLEval(a,b,x,zVals) % Evaluates the piecewise linear polynomial defined by the column %(n-1)-vectors m = length(zVals); LVals = zeros(m,1); g = 1; for j=1:m i = Locate(x,zVals(j),g); LVals(j) = a(i) + b(i)*(zVals(j)-x(i)); g = i; end m-vector
Interpolation of humps(x) with pwL, n= 5 (x-.3)2+.01(x-92+0~6
6 ( .9)^2 .04 1 ( .3) .01 1 2 − − + + x − + x