Using Matlab to fit curve

Source: Internet
Author: User
Tags scalar

Software Environment: MATLAB2013A

I. Polynomial-fitting

Polynomial fitting is the best fitting of the observed data by polynomial, which makes the error squared and the smallest at the observed data points.

In Matlab, polynomial fitting is performed using function Ployfit and ployval.

The function ployfit a polynomial representation of a smooth curve based on the observed data and the user-specified polynomial order, and the Polyfit general invocation format is: P = Polyfit (x,y,n). where x is an argument, y is the dependent variable, and n is a polynomial order.

The input of the polyval can be a scalar or a matrix, calling the format

    • PV = Polyval (p,a)
    • PV = Polyval (p,a)

where p is a polynomial representation, a is a scalar and a is a matrix. When the input parameter is m*n matrix A, the function return value PV is also the M*n matrix, and PV (i,j) = Polyval (P,a (i,j)).

1, polynomial fitting example: polynomial fitting of ln (1+x) in [0,1] sampled data

(1) Sampling of ln (1+x) in [0,1] to obtain the observed data x, Y.

0:0.1:1.0; >> y = log (1+x);

(2) Call function Polyfit to fit the observed data x, y as the third order polynomial.

>> P = Polyfit (x, Y,3)

The results of the operation are as follows:

P corresponds to a polynomial of 0.1079-0.3974x + 0.9825x2 + 0.004x3.

(3) Fitting curve and theoretical curve respectively

>> XI =0:0.01:1.0;>> Yi = polyval (p,xi); %Polynomial evaluation>> plot (x, Y,'ro'); %Observing data points>>Hold on ;>> Plot (Xi,yi,'k'); %To fit a curve>> Plot (Xi,log (1+XI),'g'); %Theoretical Curve>> Xlabel ('x');>> Ylabel ('y');>> Legend ('sampled Data','Fit Curve','Precise Curve');

The effect is as follows:

Second, the exponential function fitting 1, Gong fitting Example: 1-√x in [0,1] sampled data for exponential function fitting.

(1) 1-√x is sampled in [0,1] to obtain the observed data x, Y.

0:0.01:0.99; 1 -sqrt (x);

(2) Call function Polyfit the first order polynomial fitting of x and Lny.

>> P = Polyfit (X,log (y),1)

The results of the operation are as follows:

(3) The fitting curve is obtained.

>> Yi = exp (polyval (p,x));

(4) The observation data points, fitting curves and theoretical curves respectively.

>> Yi =exp (Polyval (p,x));>> plot (x, Y,'K.');>>Hold on ;>> Plot (X,yi,'R');>> Xlabel ('x');>> Ylabel ('y');>> Legend ('sampled Data','Fit Curve');>> hold off;

The results of the operation are as follows:

(5) Analyze the fitting error.

>> e = yi- y; >> plot (x,e); >> xlabel ('x'); >> ylabel (' error ');

The results of the operation are as follows:

Third, interactive curve fitting tool

MATLAB provides the user with an interactive curve fitting tool, Basic Fitting interface. With this tool, we can do some common curve fitting without writing code.

(1) Load census data.

>> Load Census

At this point, the MATLAB basic workspace generates two double column vectors cdate and pop,cdate represent the year of 10 in the 1790~1990, and the pop is the population for the corresponding year in the United States.

>> whos

The results of the operation are as follows:

(2) Make census data point graph.

>> plot (Cdate,pop,'ko');

The results of the operation are as follows:

(3) in the figure of MATLAB Select Tool→basic Fitting, that is, the Basic Fitting interface interface.

The user chooses different curve fitting methods through the plot fits panel, in order to facilitate the comparison, we can choose a variety of fitting methods, so as to select the best fit.

If the effect of a fit is poor, Matlab gives a warning, and the user can try to improve the fit with Center and scale X data.

If the Show equations check box is selected, the graphics window displays the Fit equation, and if the plot residuals check box is selected, the Fit effect displays an error margin. In addition, you can select different display types, such as bar plot (histogram), scatter plot (scatter plot), line plot (graph).

If the show norm of residuals check box is selected, the error margin graph displays the norm for the margin of error.

Click to get the following interface, through which we can see the results of the fitted values:

Click again to get the following interface. Through the Panel on the right side of the interface, we can get the value of the fitting function at any point, such as entering 2000:10:2080 in the edit box and clicking the Evaluate button, the result of the calculation will be displayed in the list box. If the plot evaluated result check box is selected, the calculation results are displayed in the Fit curve.

Using Matlab to fit curve

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.