www.gusucode.com > curvefit 案例源码程序 matlab代码 > curvefit/FitFourierModelsExample.m
%% Fit Fourier Models Using the fit Function % % This example shows how to use the |fit| function to fit a Fourier % model to data. % % The Fourier library model is an input argument to the |fit| and |fittype| % functions. Specify the model type |fourier| followed by the number of % terms, e.g., |'fourier1'| to |'fourier8'| . % % This example fits the El Nino-Southern Oscillation (ENSO) % data. The ENSO data consists of monthly averaged atmospheric pressure % differences between Easter Island and Darwin, Australia. This difference % drives the trade winds in the southern hemisphere. % % The ENSO data is clearly periodic, which suggests it can be described by % a Fourier series. Use Fourier series models to look for periodicity. % Copyright 2015 The MathWorks, Inc. %% Fit a Two-Term Fourier Model % Load some data and fit an two-term Fourier model. load enso; f = fit(month,pressure,'fourier2') plot(f,month,pressure) %% % The confidence bounds on |a2| and |b2| cross zero. For linear terms, you % cannot be sure that these coefficients differ from zero, so they are not % helping with the fit. This means that this two term model is probably no % better than a one term model. %% Measure Period % The |w| term is a measure of period. |2*pi/w| converts to the period in % months, because the period of |sin()| and |cos()| is |2*pi| . w = f.w 2*pi/w %% % |w| is very close to 12 months, indicating a yearly period. Observe this % looks correct on the plot, with peaks approximately 12 months apart. %% Fit an Eight-Term Fourier Model f2 = fit(month,pressure,'fourier8') plot(f2,month,pressure) %% Measure Period w = f2.w (2*pi)/w %% % With the |f2| model, the period |w| is approximately 7 years. %% Examine Terms % Look for the coefficients with the largest magnitude to find the most % important terms. % % * |a7| and |b7| are the largest. Look at the |a7| term in the model % equation: |a7*cos(7*x*w)|. |7*w| == 7/7 = 1 year cycle. |a7| and |b7| % indicate the annual cycle is the strongest. % * Similarly, |a1| and |b1| terms give 7/1, indicating a seven year cycle. % * |a2| and |b2| terms are a 3.5 year cycle (7/2). This is stronger than the % 7 year cycle because the |a2| and |b2| coefficients have larger magnitude % than a1 and b1. % * |a3| and |b3| are quite strong terms indicating a 7/3 or 2.3 year cycle. % * Smaller terms are less important for the fit, such as |a6|, |b6|, |a5|, % and |b5|. % % Typically, the El Nino warming happens at irregular intervals of two to % seven years, and lasts nine months to two years. The average period % length is five years. The model results reflect some of these periods. %% Set Start Points % The toolbox calculates optimized start points for Fourier fits, based on % the current data set. Fourier series models are particularly sensitive to % starting points, and the optimized values might be accurate for only a % few terms in the associated equations. You can override the start points % and specify your own values. % % After examining the terms and plots, it looks like a 4 year cycle might % be present. Try to confirm this by setting |w|. Get a value for |w|, % where 8 years = 96 months. w = (2*pi)/96 %% % Find the order of the entries for coefficients in the model ('f2') by % using the |coeffnames| function. coeffnames(f2) %% % Get the current coefficient values. coeffs = coeffvalues(f2) %% % Set the last ceofficient, |w|, to 0.065. coeffs(:,18) = w %% % Set the start points for coefficients using the new value for |w|. f3 = fit(month,pressure,'fourier8', 'StartPoint', coeffs); %% % Plot both fits to see that the new value for |w| in |f3| does not produce % a better fit than |f2| . plot(f3,month,pressure) hold on plot(f2, 'b') hold off legend( 'Data', 'f3', 'f2') %% Find Fourier Fit Options % Find available fit options using |fitoptions( _modelname_ )|. fitoptions('Fourier8') %% % If you want to modify fit options such as coefficient starting values and % constraint bounds appropriate for your data, or change algorithm % settings, see the options for NonlinearLeastSquares on the |fitoptions| % reference page.