Examples: (polynomials and non-polynomials, with program comments)
% Curve Fitting % polynomial curve fitting x =-Pi: 0.1: PI; y = sin (x); plot (x, y); % original curve p0 = polyfit (X, y, 4); % plot the polynomial regression coefficient vector Y1 = polyval (P0, x) for the fourth-order fitting curve; % polynomial evaluate % the polynomial is evaluated at x = 5, 7, and 9 with % P = [3 2 1]; % polyval (p, [5 7 9]) % which results in % ans = % 86 162 262 plot (X, Y, X, Y1, 'R'); % Plot Original and fit graphics poly2sym (P0) % print the polynomial % result: % ans = %-(2504756088051987 * x ^ 4)/2305843009213693952-(3360678413491453 * x ^ 3) /36028797018963968 + (2066488371929293 * x ^ 2)/288230376151711744 + (7714426024601503 * X)/9007199254740992-2922386864273423/576460752303423488% polynomial fitting sine function curve % original data point X0 =-Pi: 0.1: pi; Y0 = sin (x0); % 4 times polynomial fitting p0 = polyfit (x0, y0, 4); Y1 = polyval (P0, X0); plot (x0, y0, x0, Y1, 'R'); % polynomial fitting effect % higher order, not necessarily better fitting effect X1 =-1:0. 2:1; Y1 = 1. /(1 + 25 * x1. ^ 2); % use three, five, and eight Polynomials to fit P3 = polyfit (x1, Y1, 3 ); p5 = polyfit (x1, Y1, 5); P8 = polyfit (x1, Y1, 8); X =-1:0. 01:1; y = 1. /(1 + 25 * X. ^ 2); Y3 = polyval (P3, x); Y5 = polyval (P5, x); Y8 = polyval (P8, x); figureplot (X, Y, X, y3, 'r-', X, Y5,'m:', X, Y8, 'B --'); legend ('Primitive ', '3 times ', '5 Times ', '8 Times'); % non-polynomial fitting method x =-1:0. 01:1; y = 1. /(1 + 25 * X. ^ 2); % first, establish the structure of the fitting option Options = fitoptions ('method', 'nonearleastsquare '); options. lower = [-INF,-INF,-INF]; % set the maximum and minimum values of parameters A, B, and C options. upper = [INF, INF, INF]; % use fittype to establish a nonlinear fitting model type = fittype ('A/(B + C * x ^ N) ', 'problem ', 'N', 'options', options); % parameter introduction % A/(B + C * x ^ N) as the model. In this example, the fitting is: 1/(1 + 25 * x ^ 2) % 'problem', 'n' specifies that N is not a coefficient and the value of N is variable % 'optoins ', specify fitoptions % return value: % G = fittype ('a * cosh (x-x0)/A) + y0') % G = % general model: % G (A, x0, y0, x) = A * cosh (x-x0)/A) + y0 % fit [cfun gof] = fit (x', y', type, 'blem ', 2); % note: we need to use the column vector. Because problem is specified in the model, we need to give n a specific value % return value: % cfun indicates the fitting function, including the model and coefficient, various information such as the value of N % gof indicates fitting quality % fitting effect ynp = feval (cfun, x); % according to the cfun and x returned by fit, it is worth to the Y value corresponding to X, used to plot the curve figurehl = plot (X, Y, 'k'); % plot the original curve set (HL, 'linewidth', 10); % set the width of the curve, it is convenient to compare hold on % to keep the current image, and add the new image to the current image plot (x, ynp, 'R '); % plot the legend of the image after non-polynomial fitting ('original curve ', 'use non-polynomial fitting curve ');
Experimental results: (polynomial fitting of the sine function curve and non-polynomial fitting results)
MATLAB instance learning ------ polynomial and non-polynomial curve fitting (example)