條件數(Condition number)
matlab內建cond(A)計算出Ax=b的問題有多容易計算,條件數越大代表b的誤差會越大。
牛頓多項式差值(Newton Interpolating Polynomial)
最簡單的形式,兩個資料點有一個直線,兩點的斜率相等,以此寫出下式並轉換。

二次差值的話可以寫成f2(x) = b1 + b2(x-x1) + b3(x-x1)(x-x2)。
因此各係數為:
b1 = f(x1)
b2 = [f(x2) - f(x1)]/(x2-x1)。

以此方式可以推廣到n次,其過程可以用迴圈方便電腦計算,用疊代的方式一層一層計算(如下)


下面有課本範例的牛頓多項式插值程式:- function yint = Newtint(x,y,xx)
- % Newtint: Newton interpolating polynomial
- % yint = Newtint(x,y,xx): Uses an (n - 1)-order Newton
- % interpolating polynomial based on n data points (x, y)
- % to determine a value of the dependent variable (yint)
- % at a given value of the independent variable, xx.
- % input:
- % x = independent variable
- % y = dependent variable
- % xx = value of independent variable at which
- % interpolation is calculated
- % output:
- % yint = interpolated value of dependent variable
- % compute the finite divided differences in the form of a
- % difference table
- n = length(x);
- if length(y)~=n, error('x and y must be same length'); end
- b = zeros(n,n);
- % assign dependent variables to the first column of b.
- b(: 1) = y(:); % the (:) ensures that y is a column vector.
- for j = 2:n
- for i = 1:n-j+1
- b(i,j)=(b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
- end
- end
- % use the finite divided differences to interpolate
- xt = 1;
- yint = b(1,1);
- for j = 1:n-1
- xt = xt*(xx-x(j));
- yint = yint+b(1,j+1)*xt;
- end
複製代碼
拉格朗日插值法(Lagrange interpolating polynomial)
將f(x)拆解成∑Li(x)f(xi),其中Li(x)=Π(x-xj)(xi-xj)。
下面有課本範例的拉格朗日多項式插值程式:- function yint = Lagrange(x,y,xx)
- % Lagrange: Lagrange interpolating polynomial
- % yint = Lagrange(x,y,xx): Uses an (n - 1)-order
- % Lagrange interpolating polynomial based on n data points
- % to determine a value of the dependent variable (yint) at
- % a given value of the independent variable, xx.
- % input:
- % x = independent variable
- % y = dependent variable
- % xx = value of independent variable at which the
- % interpolation is calculated
- % output:
- % yint = interpolated value of dependent variable
- n = length(x);
- if length(y)~=n, error('x and y must be same length'); end
- S = 0;
- for i = 1:n
- product = y(i);
- for j = 1:n
- if i ~= j
- product = product*(xx-x(j))/(x(i)-x(j));
- end
- end
- s = s+product;
- end
- yint = s;
複製代碼 |