码迷,mamicode.com
首页 > 其他好文 > 详细

Least square method using matlab

时间:2017-08-12 15:23:55      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:a*   data   oop   download   roo   abs   current   his   rom   

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

标签:a*   data   oop   download   roo   abs   current   his   rom   

原文地址:http://www.cnblogs.com/wuxibo/p/7350214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!