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!!!

***

时间: 2024-08-12 14:38:53

Least square method using matlab的相关文章

Modified Least Square Method Fit Circle from Data

In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to fit circle, so i read some paper, and write a function to do it. template<typename _tp> struct Circle_ { _tp x; _tp y; _tp r; }; typedef Circle_<fl

MATLAB新手教程

MATLAB新手教程   1.MATLAB的基本知识 1-1.基本运算与函数    在MATLAB下进行基本数学运算,仅仅需将运算式直接打入提示号(>>)之後,并按入Enter键就可以.比如: >> (5*2+1.3-0.8)*10/25 ans =4.2000 MATLAB会将运算结果直接存入一变数ans,代表MATLAB运算後的答案(Answer)并显示其数值於萤幕上. 小提示: ">>"是MATLAB的提示符号(Prompt),但在PC中文视窗

Matlab tips and tricks

matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it grew to become my annotated Matlab reading cache. In order to motivate the DSP people out there, I am showing below how one can apply a window and scal

基于MATLAB的多项式数据拟合方法研究-毕业论文

摘要:本论文先介绍了多项式数据拟合的相关背景,以及对整个课题做了一个完整的认识.接下来对拟合模型,多项式数学原理进行了详细的讲解,通过对文献的阅读以及自己的知识积累对原理有了一个系统的认识.介绍多项式曲线拟合的基本理论,对多项式数据拟合原理进行了全方面的理论阐述,同时也阐述了曲线拟合的基本原理及多项式曲线拟合模型的建立.具体记录了多项式曲线拟合的具体步骤,在建立理论的基础上具体实现多项式曲线的MATLAB实现方法的研究,采用MATLAB R2016a的平台对测量的数据进行多项式数据拟合,介绍了M

西瓜书第三章 线性模型

读书笔记 周志华老师的<机器学习> 因为边看边记,所以写在随笔里,如果涉及版权问题,请您联系我立马删除,[email protected] 3.1 基本形式 给定d个属性描述的示例 x = (x_1;x_2;...;x_3), 其中x_i是X在第i个属性上的取值,线性模型视图学得一个通过属性的线性组合来进行预测的函数,即 f(x) = w_1*x_1 + w_2*x_2 + ... + w_d*x_d + b, 向量形式 其中 w = (w_1;w_2;...;w_d). w直观表达了各属性在

周志华 机器学习 笔记

首先的阶段由卷积层和池化层组成,卷积的节点组织在特征映射块(feature maps)中,每个节点与上一层的feature maps中的局部块通过一系列的权重即过滤器连接.加权和的结果被送到非线性函数中如ReLU.一个feature maps中所有的节点分享相同的过滤器,即共享权重.这种结构的原因是双重的,第一,图像中一个值附近的值是高度相关的,第二,不同区域的值是不相干的.换句话说,一个图像某部分出现的特征会在其他部分出现,因此可以实现权值共享并且检测到相同模式在矩阵的不同部分,这种操作在数学

7 Types of Regression Techniques you should know!

7 Types of Regression Techniques you should know! Introduction Linear and Logistic regressions are usually the first algorithms people learn in predictive modeling. Due to their popularity, a lot of analysts even end up thinking that they are the onl

【转】c++调用java

转:http://blog.sina.com.cn/s/blog_62b2318d0101h5j1.html c调用java走的也是jni,具体流程:1.初始化jvm2.加载你要调用的java类3.获取类中的函数4.调用函数我们一步步来,首先编写一个java类(没有它什么都是扯淡~~),我写了一个最简单的package com.cjni;public class CJniTest{    public CJniTest() {        super();    }    public sta

因素空间发展评述

因素空间发展评述      刘海涛1,包研科1,郭嗣琮1,何华灿2, 何平3 (1. 辽宁工程技术大学 智能工程与数学研究院, 辽宁 阜新 123000: 2. 西北工业大学 计算机学院,陕西 西安 710072: 3. 辽宁警官学院 信息系,辽宁 大连 116036) ——2016年3月 摘 要:为了适应信息革命和大数据时代的需要,模糊数学要更多地切入智能数据的领域. 在这方面,我国早期学者汪培庄教授提出了因素空间的数学理论,从服务于模糊数学的研究开始,进而转向认知描述,建立了知识表示的数学描