Least square method using matlab

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Least square method using matlab相关的知识,希望对你有一定的参考价值。

download linux-matlab:

技术分享

after the installing step:

1), change the current folder for working folder:

add the content to ***/installed folder/toolbox/local/matlabrc.m

cd ‘cd ‘/home/groot/Music/java_2017/hadoop_r-base/matlab_plot‘

2), deal with error after begining matlab:

change the libstdc++.so.6 to libstdc++.so.6.old in ***/installed folder/sys/os/glnxa64

 3), my matlab current folder is like this:

技术分享

 

 

 

load_scatter.dat:

-2.19 -1.12
-1.56 -0.72
-1.35 -0.31
-0.8 0.34
-0.46 0.52
0.57 1.42
0.97 1.66
1.49 2.52

 

point_to_line.m:

 

% caculate the distance from any point to a line;
function d = point_to_line(x,y,z,p,q)
d=abs(x*p+y*q+z)/sqrt(x*x+y*y);

 

load_scatter_xy.m:

***

 

 

 

% x=-3:0.01:2;
% y=x+1;
% plot(x,y, ‘r‘)


% load_data=load(‘load_scatter.dat‘); % load the data in the file:load_scatter.dat
% x=load_data(:,1);
% y=load_data(:,2);
% scatter(x,y)
%
% p=polyfit(x,y,1); % fit the line from the data in the file:load_scatter.dat
% plot(x,y, ‘o‘, x,polyval(p,x),‘r‘)


% point_to_line(1,-1,1,3,1) % caculate the data using the fuction in the file:point_to_line.m


% the function(x,y):x+y=10, x-y=2 % one method:solve the function(x,y);
% A=[1,1;1,-1]
% B=[10;2]
% A\\B


% S=solve(‘x*y=10‘, ‘x/y=2‘)% another method: solve the fucntion(x,y);
% disp(‘S.x‘), disp(S.x), disp(‘S.y‘), disp(S.y) % method 1: show the solution for function(x,y)

% disp(S.x), disp(S.y) % method 2: show the solution for function(x,y)

% x_solve=S.x, y_solve=S.y % method 3: show the solution for fuction(x,y)


% y=solve(‘-0.0945*x^2+2.9289*x-6.4553-13.1==0‘) % solve the function(x);


% x=-10:0.01:10;
% y=-x.^2+3*x+10;
% plot(x, y) % plot the line

%% method: LSM (least square method)
% set the line: y=ax+b;
% M=(ax+b-y)^2
% axi^2 + bxi = xi*yi
% axi + b*N = yi

% x2=sum(xi.^2);
% x1=sum(xi);
% x1y1=sum(xi.*yi);
% y1=sum(yi);  

% a=(n*x1y1-x1*y1)/(n*x2-x1*x1);  
% b=(y1-a*x1)/n;
% plot(x,y,‘r‘)
%%

% plot the line using LSR method above
load_data=load(‘load_scatter.dat‘); % load the data in the file:load_scatter.dat
xi=load_data(:,1);
yi=load_data(:,2);

x2=sum(xi.^2);
x1=sum(xi);
x1y1=sum(xi.*yi);
y1=sum(yi);

a=(8*x1y1-x1*y1)/(8*x2-x1*x1) % caculate the line gradient using LSM method
b=(y1-a*x1)/8 % caculate the line intercept using LSM method

x_x=-2:0.1:2;
y_y=a*x_x+b;
plot(x_x,y_y,‘r‘) % plot the line after the caculation using LSM method


% begin here!!!

 

 

 

***




















































































以上是关于Least square method using matlab的主要内容,如果未能解决你的问题,请参考以下文章

《机器学习——数学公式推导合集》1. 最小二乘法(least square method)求解线性模型

《机器学习——数学公式推导合集》1. 最小二乘法(least square method)求解线性模型

《机器学习——数学公式推导合集》1. 线性模型之最小二乘法(least square method)求解线性模型

自动驾驶 8-1: 平方误差准则和最小二乘法 (上) Squared Error Criterion and the Method of Least Squares (Part 1)

自动驾驶 8-2: 平方误差准则和最小二乘法 (下) Squared Error Criterion and the Method of Least Squares (Part 2)

V-rep学习笔记:机器人逆运动学数值解法(Damped Least Squares / Levenberg-Marquardt Method)