Matlab least squares method to achieve trajectory positioning method (original: huh! ( ̄▽ ̄)")

Trajectory positioning method-Matlab simulation method

Known plane coordinates of four points (x1,y1)=(0,0), (x2,y2) =(1,0), (x3,y3) =(0,1), (x4,y4) =(0 ,2), someone walks along a straight line, and the coordinates of the points to be located during walking are (a1,b1),……(a5,b5), and the distances from each coordinate point to be located to the known four points are measured separately For (0.71, 0.78, 0.64, 1.52), (1.4, 0.95, 1.05, 1.5), (2.12, 1.61, 1.55, 1.52), (2.87, 2.24, 2.31, 2.1), (3.61, 3, 2.97, 2.57) Find the linear equation for this person to walk.

Positioning method - multi-point positioning

Problem-solving ideas:

(1) Linear regression equation

 

(2) Least squares method

Program realization:

Location.m

function kk=Location(xx,yy,ll)

xxx = xx(1:end-1);

yyy = yy(1:end-1);

lll = ll(1:end-1);



A = [2*(xxx-xx(end)) 2*(yyy-yy(end))];

b = (xxx.^2-xx(end)^2) + (yyy.^2-yy(end)^2) - (lll.^2-ll(end)^2);

kk=(A.'*A)^(-1)*A.'*b;

drawline.m (linear regression equation matlab realizes drawing a straight line)

function drawline(x,y)

n=length(x);

xb=mean(x);

yb=mean(y);

x2b=sum(x.^2)/n;

xyb=x*y'/n;

b=(xb*yb-xyb)/(xb^2-x2b);

a=yb-b*xb;

y1=a+b.*x;

plot(x,y,'*',x,y1);

serror=sum((y-y1).^2)

The first conventional program: (Disadvantage: you have to call the Location() function every time you find an anchor point, the code is a lot of redundant)

heheda_matlab_changui.m

tic

xx=[0 1 0 0].';

yy=[0 0 1 2].';

l1=[0.71 0.78 0.64 1.52].';

l2=[1.4 0.95 1.05 1.5].';

l3=[2.12 1.61 1.55 1.52].';

l4=[2.87 2.24 2.31 2.1].';

l5=[3.61 3 2.97 2.57].';

k1=Location(xx,yy,l1)

k2=Location(xx,yy,l2)

k3=Location(xx,yy,l3)

k4=Location(xx,yy,l4)

k5=Location(xx,yy,l5)



X=[k1(1) 1

    k2(1) 1

    k3(1) 1

    k4(1) 1

    k5(1) 1]

Y=[k1(2)

    k2(2)

    k3(2)

    k4(2)

    k5(2)]

X1=[k1(1) k2(1) k3(1) k4(1) k5(1)]

Y1=[k1(2) k2(2) k3(2) k4(2) k5(2)]



drawline(X1,Y1)

toc

The second type: simplified procedures

heheda_chengxu.m

tic

xx=[0 1 0 0].';

yy=[0 0 1 2].';

l1=[0.71 0.78 0.64 1.52].';

l2=[1.4 0.95 1.05 1.5].';

l3=[2.12 1.61 1.55 1.52].';

l4=[2.87 2.24 2.31 2.1].';

l5=[3.61 3 2.97 2.57].';

sum=[l1 l2 l3 l4 l5];

kk=[]

i=1

for i=1:5

    kk(:,i)=Location(xx,yy,sum(:,i));

end;

k_x=kk(1,:)

k_y=kk(2,:)



X=[]

Y=[]

for i=1:5

    X(:,i)=[k_x(i),1]

    Y(:,i)=k_y(i)

end

X=X.'

Y=Y.'

a = -5:0.1:2;

b = 0:0.1:10;

drawline(k_x,k_y)

toc

The third type: realize code optimization

code_fast.m

tic

xx=[0 1 0 0].';

yy=[0 0 1 2].';

l1=[0.71 0.78 0.64 1.52].';

l2=[1.4 0.95 1.05 1.5].';

l3=[2.12 1.61 1.55 1.52].';

l4=[2.87 2.24 2.31 2.1].';

l5=[3.61 3 2.97 2.57].';

sum=[l1 l2 l3 l4 l5];

kk=[]

i=1

for i=1:5

    kk(:,i)=Location(xx,yy,sum(:,i));

end;

k_x=kk(1,:)

k_y=kk(2,:)

X=[]

Y=[]

for i=1:5

    X(:,i)=[k_x(i),1]

    Y(:,i)=k_y(i)

end

X=X.'

Y=Y.'

a = -5:0.1:2;

b = 0:0.1:10;

drawline(k_x,k_y)

toc

Fourth: the implementation code is more concise

true_line.m

tic

xx=[0 1 0 0].';

yy=[0 0 1 2].';

l1=[0.71 0.78 0.64 1.52].';

l2=[1.4 0.95 1.05 1.5].';

l3=[2.12 1.61 1.55 1.52].';

l4=[2.87 2.24 2.31 2.1].';

l5=[3.61 3 2.97 2.57].';

sum=[l1 l2 l3 l4 l5];

kk=[]

i=1

for i=1:5

    kk(:,i)=Location(xx,yy,sum(:,i));

end;

k_x=kk(1,:)

k_y=kk(2,:)

X=[]

Y=[]

for i=1:5

    X(:,i)=[k_x(i),1]

    Y(:,i)=k_y(i)

end

X=X.'

Y=Y.'

%a = -5:0.1:2;

%b = 0:0.1:10;

EK=(X.'*X)^(-1)*X.'*Y

a=EK(1)

b=EK(2)

y1=a.*k_x+b;

plot(k_x,k_y,'*',k_x,y1);

toc

Find the coordinates


X1 =

    0.4483    1.0283    1.4512    2.1122    2.5165


Y1 =

    0.5487    0.9273    1.5460    1.9580    2.6070

That is, the coordinates of 5 pending points can be determined as a straight line.

The least square method determines the coefficients a and b:

 

Same idea as above

(Thanks to the Internet of Things teacher, let me go from entering the pit to giving up, hahaha! The editor will continue to work hard and strive to publish more meaningful original articles)

Hope to help you

Thank you very much for watching. If you like it, please like it?

One who likes cats and mice!

 

 

Guess you like

Origin blog.csdn.net/weixin_41987016/article/details/91869143