CS计算机代考程序代写 matlab AI Numerical Methods in Engineering (ENGR20005)

Numerical Methods in Engineering (ENGR20005)
Lecture 10 Dr. Leon Chan
lzhchan@unimelb.edu.au Department of Mechanical Engineering The University of Melbourne
Slides prepared by Prof.Andrew Ooi

L10.1: Spline interpolation
2
“Book” (Chap. 5, pg. 87)

Spline Interpolation
Splines are made up of piecewise polynomials connecting only two data points. This is different to Newton or Lagrange polynomials where you have only one polynomial connecting all data points. See figure below.
f (x) f (x3)
f(x1) f (x2)
f (x0)
f (x) f (x3)
f(x1) f (x2)
f3(x)
S1(x) x)
S2(x)
S0(
x0 x1
x2 x3 x
f (x0)
Spline function
Newton or Lagrange interpolation polynomial
x0 x1 S(x) =
x2 x3 x
S0(x) x0 ≤x≤x1
S1(x) x1 ≤x≤x2
S2(x) x2 ≤x≤x3
3

Spline Interpolation
• The spline function, S(x), is made up of several polynomial functions, Si(x).
• S(x) = Si(x) for xi ≤ x ≤ xi+1.
• Si(x) is ONLY defined between xi and xi+1. Outside of the
interval, Si(x) is not defined and has got NO meaning.
• If Si(x) are linear functions, then S(x) is called a linear spline. Si(x) quadratic → S(x) quadratic spline.
Si(x) cubic → S(x) cubic spline
4

L10.2: Linear Splines
5
“Book” (Chap. 5, pg. 88)

Linear Spline Interpolation
f(x3) f(x1)
f(x2) f(x0)
x0 x1
• All S0(x), S1(x) and S2(x) are linear functions 6
S1(x)
S2(x)
S0(x)
x2
x3
x

Linear Spline Interpolation
• Si(x) = ai + bi(x − xi)
•S(x)=Si(x)forxi xi(i)
j=j-1;
break
end end
fi(i)=a(j)+b(j)*(xi(i)-xdata(j))+c(j)*(xi(i)-xdata(j))^2;
end end
function [a,b,c]=QuadraticSplineCoeffs(xdata,fdata)
n=length(xdata)-1; a=zeros(1,n+1); a=zeros(1,n+1); a=zeros(1,n+1); for i=1:n+1
a(i)=fdata(i);
end
%/* number of intervals */
c(1) = 0.0;
h(1) = xdata(2)-xdata(1);
for i=2:n
h(i) = xdata(i+1)-xdata(i);
c(i) = (1/h(i))*(a(i+1)/h(i)-a(i)*((1/h(i-1))+(1/h(i)))+a(i-1)/h(i-1)-c(i-1)*h(i-1));
end
for i=1:n b(i)=(a(i+1)-a(i))/h(i)-c(i)*h(i);
end end
clear all
n=length(xdata)-1;
x = linspace(xdata(1),xdata(n+1),100);

What if C2 = 0 instead of C0?

a1 = f(x1) a =f(x)
b0 = a1 − a0 − c0h0 h0
b1=a2−a1 −c1h1 h1
b2 = a3 − a2 − c2h2 h2
b + 2c h = b 0 0 0 1
b1+2c1h1=b2
2 2 a3 = f(x3)
c0h0 + c1h1 = a2 − a1 1 + 1 + a0 h(hh)h
1010 c1h1+c2h2=a3 −a2(1 + 1)+a1
h2 h1 h2 h1 c0 = 0
c2 = 0 hh0ch(hh)h
0 0a2−a1+1+a0
a2−a1 1+1 +a0 1 010
100c hh0ch(hh)h
01 c0
0h1h2 1 a3−a(1+1)+a1
01 c1=1 10 1 0
=
001 c2 h2 2h1 h2 h1
0h1h2 2 a3−a2(1+1)+a1 h2 h1 h2 h1
0

ci=1 ai+1−ai 1 +1 +ai−1−ci−1hi−1 h(h[hh]h)
i i i−1 i i−1
Quadratic spline with cn = 0. Rearrange the equation below so that you have ci−1 on the
left hand side
h(h[hh]h) i−1 i i−1 i i−1
ci−1= 1 ai+1−ai 1 +1 +ai−1−cihi

c0 = 0 cn = 0

Example L10.2: Use quadratic splines to estimate the value of the function
f(x) = 1 − e−x
at x = 1.0. Use the intervals x0 = 0, x1 = 2.0, x2 = 4.0 and x3 = 6.0. Plot the spline for the interval 0 ≤ x ≤ 6.
48

Exercise – Quadratic Interpolation
Notice, spline solution linear for first segment. For this function, this is probably not where we want it to be linear (and then quadratic for larger x).
Replace condition c0 = 0 by c2 = 0 and see what happens. 49

Exercise – Quadratic Interpolation
Notice, spline solution linear for first segment. For this function, this is probably not where we want it to be linear (and then quadratic for larger x).
Replace condition c0 = 0 by c2 = 0 and see what happens. 50

Exercise – Quadratic Interpolation
Notice, spline solution linear for first segment. For this function, this is probably not where we want it to be linear (and then quadratic for larger x).
Replace condition c0 = 0 by c2 = 0 and see what happens. 51

End of Example L10.2

L10.4: Cubic Splines
53
“Book” (Chap. 5, pg. 94)

Linear spline
f (x3) f(x1)
S1(x) S2(x) f (x2)
S0(x)
f (x0)
x0x1 x2 x3
Si(x) = ai + bi(x − xi) n is the number of
itervals
Spline must pass through all data points
Si(xi) = f(xi) n + 1 equations Adjacent spline must pass through same points
Si(xi+1) = Si+1(xi+1) n − 1 equations
2n unknowns
Total: 2n equations
Quadratic spline
f (x3)
S2(x) f(x1)
S1(x) f (x2)
S0(x)
f (x0)
x0x1 x2 x3 Si(x) = ai + bi(x − xi)
+ci(x − xi)2
Spline must pass through all data points
Si(xi) = f(xi) n + 1 equations
Adjacent spline must pass through same points Si(xi+1) = Si+1(xi+1) n − 1 equations
Gradient of adjacent spline must be the same
S′(x )=S′ (x ) i i+1 i+1 i+1
n − 1 equations
Need to make 1 guess!
54
3n unknowns
Total: 3n − 1 equations
Cubic spline
f (x3)
S2(x)
S1(x) f (x2)
f(x1) f (x0)
x0x1 x2 x3 Si(x) = ai + bi(x − xi)
+ci(x − xi)2 + di(x − xi)3
Spline must pass through all data points
Si(xi) = f(xi) n + 1 equations
Adjacent spline must pass through same points Si(xi+1) = Si+1(xi+1) n − 1 equations
Gradient of adjacent spline must be the same
S′(x )=S′ (x ) i i+1 i+1 i+1
n − 1 equations
2nd derivative of adjacent spline must be the same
S′′(x )=S′′ (x ) i i+1 i+1 i+1
Need to make 2 guesses!
n − 1 equations
S0(x)
4n unknowns
Total: 4n − 2 equations

55

Cubic Spline Interpolation
At each interval, the splines now consist of a cubic polynomial. Cubic splines are usually smoother than quadratic splines. Note that
Si(x) = ai + bi(x − xi) + ci(x − xi)2 + di(x − xi)3 S(x)=Si(x)forxi