多項式事實上是一個很好的函數, 我們完全知道他的微分以及積分.
所以通常任意一個函數我們會想要把它以多項式取代. 然後我們以此多項式的微分積分來估計原函數的微分積分值.
而事實上在分析導論這門課中也知道, 有以下這個定理告訴我們這樣的多項式一定找得到, 而且跟原函數的距離可以任意小:
If
is continuous on and if , then there is a polynomial satisfying on the interval .
Question: 以上定理中的
任意給定兩個函數
試求出一個最佳的多項式
polyfit
找到一個多項式通過或逼近這些點. 並以此多項式來當 polyfit
找到一個多項式通過或逼近這些點. 並以此多項式來當 Question 1:
Question 2:
Question 3: 任意給
均勻取點無法收斂
function InterpolateRungeFunction2
% Select interpolation points by right clicking.
% When you get bored, left or double click to finish.
F = @(x) 1./(1+25*x.^2);
% initialise
x = [];
t = linspace(-1, 1, 1000);
LW = 'LineWidth'; MS = 'MarkerSize';
fv = F(t);
afv = max(fv)+1;
ifv = min(fv)-1;
% loop
figure
plot(t,fv,'-k',LW,2);
axis([-1 1 ifv afv])
hold on
h1 = plot(t, fv, '-b', LW, 2);
h2 = plot(t, F(t)*0, '-r', LW, 2);
shg
legend('original function', 'interpolant', 'difference')
while 1 % keep clicking!
if ( ~isempty(x) )
plot(x,F(x),'.b',MS,20,'HandleVisibility','off') % plot interpolation points
plot(x,0,'*k',MS,6,'HandleVisibility','off') % plot x values alone
p = polyfit(x, F(x), length(x)-1);
f = polyval(p, t);
h1.YData = f;
h2.YData = f-fv;
axis([-1 1 ifv afv])
drawnow
shg
end
[gx, ~, button] = ginput(1); % input new interpolation point
if ( button == 3 ) break, end % if right button, stop
x = unique([x; gx]); % #ok<AGROW>
end
end
讓使用者任意在螢幕上按順序以滑鼠指定六個點, 並將此六點以直線連線. 找到一條多項式曲線