Application of Least squares (Matlab)

Source: Internet
Author: User
Tags cos rand sin
In conjunction with an example, direct example code (source code according to Steven J.leon "linear Algebra (original book seventh edition)" in the least squares of the application of their own coding and modification)Amway: Strongly recommend Steven J.leon, "Linear Algebra (original book seventh edition)" as a linear algebra primer or review textbook (all Can), although a part of the following is not read, however, this book is really better than the line of Tongji's too much. Optimal least squares fitting curve
In fact, it is not necessary to use code to fit the curve quickly: first draw the source data scatter plot, in the Matlab figure pop-up frame-tool (tools)-Basic fitting (basic fitting) inside there are many interpolation methods, such as "spline interpolation", "polynomial interpolation" and so on.

Personally, by learning the linear algebra-steven J.leon, it is found that the least squares method is actually "polynomial interpolation"

% optimal least squares fitting curve% of examples have a data table, as follows:% P 1 2 3 4 5 6 7 8 9 ten T 222 227 223 233 244 253 260 266 270 266
Requirements: Optimal minimum multiplication fitting curve% code P=[1:10] ';
T=[222,227,223,233,244,253,260,266,270,266] ';
A=vander (P);
                                                            % build Vandermonde matrix, i.e. to x= (x1,x2,..., x (n+1)) ', with vander (x) = [X1^n x1^ (n-1) ... x1 1]
                                                            %[x2^n x2^ (n-1) ... x2 1]
                                                            %[... ... ... ... ... ...               ]    %[x (n+1) ^n x (n+1) ^ (n-1) ... x (n+1) 1] v=a (:, 9:10);          % take a of the 9th, 10 column, here can be changed, through the tune to V=a (:, 8:10), V=a (:, 7:10) and so on to improve the accuracy of the fitting precision c=v\t;     The% calculates C, which is a q=1:0.1:10 of the polynomial power-down coefficients of the fitted curve; % set up the value range for 1,1.1,1.2,..., z=polyval (C,Q); The%polyval function, that is, has z=c (0) *q^n+c (1) *q^ (n-1) +......+c (n-1) *q+c (n), that is, the matrix C each value as an n-th polynomial power-down order coefficient, and the corresponding power of the matrix Q multiplied Figure;plot (q,z,p       , T, ' X ');                                         
 % Draw Q-z graph, as the fitting curve of scatter plot; at the same time, draw p,t The original problem data scatter plot (no curve), if there is no ' x ', it is the drawing p-t scatter connection diagram
Least squares circle (using least squares to fit the circle equation)
% Least squares circle (best least squares fitting circle)
% of examples
have round parametric equation: x=3+2cost,y=1+2sint
t=0:0.5:6;
X=3+2*cos (t); Y=1+2*sin (t);
% above is the original circle
% below is the added disturbance (noise) of the circular
x=x+0.1*rand (1,13);
Y=y+0.1*rand (1,13);
% below is the center C and radius of the circle with the least squares fitting to add the data after the disturbance r,ac=b
a=[2*x ', 2*y ', Ones (size (x)) '];
B=diag ((Diag (x.^2)) + (Diag (y.^2)));
c=a\b;
% below is the figure t1=0:0.1:6.3 for making scatter plots and fitting circles
;
R= (c (3) +c (1) ^2+c (2) ^2) ^ (a);
X1=c (1) +r*cos (t1);
Y1=c (2) +r*sin (t1);
Plot (X1,y1,x,y, ' x ')

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.