Polynomial interpolation function
%% polynomial interpolation %% Description: precision of accuracy, the larger the finer the image, attribute is an attribute value, when it is known that the unknown function expression function value is 1, otherwise 0 function Polynomial_interpolation the PI = (F, X-, Precision, attribute) X-Sort = (X-); IF attribute == 0 [m, n-] = size (X-); max = mAX ([m, n-]); X-the RESHAPE = (X-,. 1, mAX); error = []; for I =. 1: mAX the Y (I) = SUBS (F, X-(I)); End Y_value = Double (the Y); A = min (X-); B = max (X-); T = A : (BA) / Precision: B; T = zeros (. 1, Precision +. 1); Yreal = SUBS (F, T); Coe = VPA (Polynomial_interpolation_cofficient (F, X-, attribute),. 4); for I =. 1: . 1: Precision. 1 + T (I) = Polynomial_value (Coe, T (I)); End for I =. 1: MAX error (I) = ABS (the Y (I) -Polynomial_value (Coe, X-(I))); End %% plotted H = Figure; SET (H, 'Color', 'W'); [HAX, hLine1, hLine2] = plotyy (T, T, X-, the Y, 'Plot', 'STEM'); title ( 'polynomial interpolation'); the xlabel ( 'Variable X'); ylabel (HAX (. 1), 'Variable'); ylabel (HAX (2), 'Variable'); Grid ON HOLD ON Plot (T, Yreal); Legend ( 'Yreal: real image', 'Y: fitting polynomial image', 'T: actual data'); % % display coordinates for I =. 1: MAX text (X-(I), Y_value (I), [ '(', num2str (X-(I)), ',', num2str (Y_value (I)), ')'] , 'Color', [0.02 0.79 0.99]); End DISP ( 'Error value '); error ELSEIF attribute. 1 == [m,n] = size(X);MAX = max([m,n]);X = reshape(X,1,MAX);f = reshape(f,1,MAX); a = min(X);b = max(X); t = a:(b-a)/precision:b; T = zeros(1,precision+1); Coe = vpa(Polynomial_interpolation_cofficient(f,X,attribute),4); for i = 1:1:precision+1 T(i) = Polynomial_value(Coe,t(i)); end h=figure; set(h,'color','w'); plot(t,T,'b',X,f,'g*'); grid on title('多项式插值'); xlabel('Variable x'); ylabel('Variable y'); legend('Y:拟合多项式图像','T:已知数据'); for i = 1:MAX text(X(i),f(i),['(',num2str(X(i)),',',num2str(f(i)),')'],'color',[0.02 0.79 0.99]); than than syms x; PI=vpa(Polynomial_value(Coe,x),4); than
2. The polynomial function value
%%多项式函数值 function PV = Polynomial_value(P,t) [m,n] = size(P); MAX = max([m,n]); sum = 0; for i = MAX:-1:1 sum = sum+P(i)*Power_function(i-1,t); end PV = sum; %%幂函数 function pf = Power_function(index,t) pf = t.^index; end end
3. polynomial coefficients
%% This function of the given number of nodes minus one polynomial coefficients %% Description: attribute is an attribute value, when it is known that the unknown function expression function value is 1, otherwise 0 function Polynomial_interpolation_cofficient the PIC = (F, X- , attribute) Global mAX; Global m; n-Global; Global I; X-Sort = (X-); IF attribute == 0 [m, n-] = size (X-); max = mAX ([m, n-]); X- the RESHAPE = (X-,. 1, MAX); A = zeros (MAX, MAX); the Y = zeros (. 1, MAX); for I =. 1: MAX . A (:, I) = (X-') ^ (I- . 1); the Y (I) = SUBS (F, X-(I)); End Coefficient = VPA (the RESHAPE (A \ (the Y '),. 1, MAX),. 4); ELSEIF attribute. 1 == [m, n-] = size (X-); mAX = max ([m, n-]); the PIC = Cell (. 1, mAX +. 1); X-= the RESHAPE (X-,. 1, mAX); A = zeros (mAX, mAX); the Y = the RESHAPE (F,. 1, MAX); for I =. 1: MAX A (:, I) = (X-') ^ (I-. 1);. End Coefficient = VPA (the RESHAPE (A \ (the Y'),. 1, MAX),. 4); End DISP ( 'the maximum number of n =' ); mAX. 1- the PIC = Coefficient; %% value polynomial function Polynomial_value the PV = (P, T) [m, n-] = size (P); mAX = max ([m, n-]); SUM = 0; for MAX = I: -1:. 1 SUM = SUM + P (I) * Power_function (. 1-I, T); End the PV = SUM; %% power function function Power_function PF = (index, T) PF = T ^ index. ; End End End
4. An example
All Clear CLC Precision = 500; X-=. 1:. 1:. 6; Rl = the RESHAPE (RAND (. 6), 1,36); R2 = the RESHAPE (RAND (12 is), 1,144); R & lt = zeros (1,6); = I. 1 for:. 6 R & lt (I) Rl = (I *. 6) R2 * (24 * I) * 100; End %% known function disp ( 'known polynomial fit function'); syms X; F * SiN X = (X ^ 2) * exp (the -X-^ 2) + log (ABS (SiN (X))); Polynomial_interpolation (F, X-, Precision, 0) %% known function value disp ( 'known fitting a polynomial function value '); Polynomial_interpolation (R & lt, X-, Precision,. 1)
result
Known function of polynomial fitting maximum number = n- ANS = . 5 error value error = 1.0E-08 * 0.0066 0.0092 0.0027 0.0473 0.1507 .3463 ANS = 0.1248 ^. 5 * X - 2.291 * 15.64 * + X ^ X ^. 4. 3 - 48.61 * x ^ 2 + 66.56 * x - 31.29 polynomial fitting a known function of the value of the maximum number of times = n- ANS = . 5 ANS = - 1.993 * X ^ X ^ *. 5. 4 31.02 + - 175.7 + 444.1 * X * X ^. 3 ^ 2 - 491.6 * x + 201.5