Calculation of straightness with Matlab endpoint method and least squares algorithm

Source: Internet
Author: User
Tags min relative

First, the straightness of the relevant description:

In engineering practice, the method of evaluating the straightness error of guide rail is commonly used both ends point connection method and least square. Two ends point connection method, is to connect the error curve, and then through the highest and lowest points of the curve, respectively, as two parallel to the end of the line, the number of lines along the ordinate measurement, through data processing, that is, the straightness error of the guide rail; Two points connected, too low (high) point as a straight line and the parallel, two parallel lines along the longitudinal standard sitting measured value, through data processing, that is, the linear error value of the guide rail.

the core of solving straightness error is to determine understanding linear equation Two, the two ends of the point connection method to calculate straightness:

x, y to replace with your actual data

Clc,clear X=1:5;

Y=[2,5,16,8,20];        Len=length (x);        % gets the length of the data x_start=x (1);
% gets the endpoint value of x and Y x_end=x (end);
Y_start=y (1);

Y_end=y (end);
% linear equation: y=kx+b Convenient subsequent calculation k= (Y_end-y_start)/(X_end-x_start);
B=y_end-k*x_end;  % xx=[x_start,x_end];  % get X's first end point Yy=[y_start,y_end]; % gets Y's first end point plot (x, y, '-* ', ' linewidth ', 2)% draws the original data graph hold on plot (Xx,yy, '-rs ', ' MARKERFACEC
Olor ', ' r ', ' LineWidth ', 2)% draw endpoint Wiring diagram% str=[' ideal linear equation for: ', ' y= ', Num2str (k), ' x ', ' + ', ' (', num2str (b), ') '];

% text (3,16,str, ' Color ', ' red ', ' fontsize ', ') set (GCA, ' XTick ', [min (x): 1:max (x)], ' Ytick ', [min (y): 1:max (y)])% control scale            Grid on for I=1:len D (i) =y (i)-(K*x (i) +b);                    % cycle Scan the difference between the point on the curve and the endpoint end L=max (d)-min (d);            % linearity error of the calculated curve L=dmax-dmin [D_max,max_index]=max (d);            % find the point of maximum deviation (both positive and negative, is relative) [D_min,min_index]=min (d); % find the minimum deviation of point hold on plot ([Max_index,max_index],[y (Max_index), k*max_index+b], ' b--')% Plot deviation line plot ([Min_index,min_inde X],[y(Min_index), k*min_index+b], ' k--') Xlabel (' x (measured point ordinal) ') Ylabel (' Y (measured value) ') title ({"The ideal line equation is: ', ' y= ', Num2str (k), ' x ', ' + '), ' (', num2str (b), ') ';
 [' Endpoint method to find straightness value: ', Num2str (L)]});

Result diagram:

third, least squares calculation of straightness:

The core of the straightness error is to determine the ideal linear equation clc,clear% build data test x=1:5;

Y=[2,5,16,8,20];        Len=length (x); % get the length of the data percent formula method for calculating the least squares ideal line k= (5*sum (x.*y)-sum (x) *sum (y))/(5*sum (x.^2)-(sum (x)) ^2) B=mean (y)-k*mean (x) percent percent
The Polyfit function is used to determine the least squares ideal straight line% p=polyfit (x,y,1)% X1=x;
% Y1=polyval (p,x1);             % k=p (1);             % one-time coefficient% b=p (2);
The percent constant term coefficient of two methods are feasible xx=x;

Yy=k*x+b; Plot (x, y, '-* ', ' linewidth ', 2)% draws the original data graph hold on plot (Xx,yy, '-rs ', ' Markerfacecolor ', ' R ',


' LineWidth ', 2)% draw endpoint Wiring diagram set (GCA, ' XTick ', [min (x): 1:max (x)], ' Ytick ', [min (y): 1:max (y)])% control scale grid on            For I=1:len D (i) =y (i)-(K*x (i) +b);                    % cycle Scan the difference between the point on the curve and the endpoint end L=max (d)-min (d);            % linearity error of the calculated curve L=dmax-dmin [D_max,max_index]=max (d);            % find the point of maximum deviation (both positive and negative, is relative) [D_min,min_index]=min (d); % find the minimum deviation of point hold on plot ([Max_index,max_index],[y (Max_index), k*max_index+b], ' b--')% Plot deviation line plot ([Min_index,min_inde X],[y (Min_index), k*min_index+b], ' k--') XlabeL (' x (measured point ordinal) ') Ylabel (' Y (measured value) ') title ({' Ideal linear equation: ', ' y= ', Num2str (k), ' x ', ' + ', ' (', num2str (b), ') '];[' Least squares to find straightness value: ', Num2str (L)]})

The result diagram is as follows:

Two ways, replace x y for your own data, you can get the results that match your own data

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.