14-回归与内插

一、Polynomial curve fitting(多项式曲线拟合)

(一)Simple Linear Regression(简单线性回归)

1、A bunch of data points(

)are collected(收集一些二维数据点)

如体重(Height)和身高(Weith)

2、Assume

and
are linearly correlated(假设x和y线性相关)

(二)Linear Regression Formulation(线性回归公式)

1、Define sum of squared errors(SSE):(定义平方误差之和)

2、Given that the regression model:(假设回归模型)

(三)Solving Least-squares Problem(最小二乘问题的求解)

1、SSE is minimized when its gradient with respect to each parameter is equal to zero:(当SSE相对于每个参数的梯度等于零时,SSE最小)

联立二元一次方程式

(四)Least-squares Solution(最小二乘解)

1、Suppose there exists N data points:(假设存在N个数据点:)

只有β0和β1是未知,其他均为已知

(五)Polynomial Curve Fitting:polyfit()(多项式曲线拟合)

1、Curve fitting for polynomials of different orders(不同阶多项式的曲线拟合)

示例代码:

x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
fit = polyfit(x,y,1);%order,1次方的多项式
%poly会产生两个值fit(1)和fit(2)对应斜率和截距
%% 绘制图形
xfit = x(1):0.1:x(end);
%即xfit = [-1.2 -1.1 -1.0 ... 3.5]
yfit = fit(1) * xfit + fit(2);
%即y = ax + b
plot(x,y,'ro',xfit,yfit);
set(gca,'FontSize',14);
legend('data points','best-fit','Location','northwest');

输出代码:

(六)Excise

1、Given the table below:

(1)Find the

of the regression line(找到回归线)

(2)Plot the figure

答案代码:

TC = [0.025 0.035 0.050 0.060 0.080];
T = [20 30 40 50 60];
fit = polyfit(T,TC,1)Tfit = T(1):0.01:T(end);
TCfit = fit(1) * Tfit + fit(2);
plot(T,TC,'ko',Tfit,TCfit,'r','LineWidth',2);
set(gca,'FontSize',14);
grid on;
set(gca,'GridLineStyle','--');   %设置网格线为虚线
xlabel('Temperature(^oC)');
ylabel('TC output(mV)');
title('Calibration of TC')

输出结果:

(七)Area x and y Linearly Correlated(区域x和y是否线性相关?)

1、If not,the line may not well describe their relationship(拟合的线或许不能很好的描述他们之间关系)

2、Check the linearity by using (检查他们的线性关系使用)

(1)scatter():scatterplot(散点图)

(2)corrcoef():correlation coefficient,-1≤r≤1(相关系数,很强的正相关接近1,很强负相关-1)

示例代码:

x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
scatter(x,y);%散点图
box on;
axis square;
corrcoef(x,y)%相关系数

输出结果:

注意:

corrcoef(x,y)%相关系数

(八)Higher Order Polynomials(高阶多项式)

示例代码:

x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
figure('Position',[50 50 1500 400]);%可绘制区域的位置和大小
for i = 1:3subplot(1,3,i);p = polyfit(x,y,i);%多项式曲线拟合
%i = 1:3  分别画出了1次拟合,2次拟合,3次拟合
%拟合阶数越高,平均差值越小xfit = x(1):0.1:x(end);yfit = polyval(p,xfit);%多项式计算plot(x,y,'ro',xfit,yfit);set(gca,'FontSize',14);ylim([-17,11]);legend('Data points','Fitted curve','Location','southeast');
end

输出结果:

注意:

(1)figure('Position',[50501500400]);%可绘制区域的位置和大小

(2) p =polyfit(x,y,i);%多项式曲线拟合

(3)yfit =polyval(p,xfit);%多项式计算

(九)Excise

1、Find the

-order polynomials

2、Is it better to use higher order polynomials?(错,可能过拟合)

答案代码:

x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
figure('Position',[50 50 1500 400]);
for i = 4:6subplot(1,3,i-3);%由于与order用的是同样的变量,所以只需减掉多的3阶即可p = polyfit(x,y,i);xfit = x(1):0.1:x(end);yfit = polyval(p,xfit);plot(x,y,'ro',xfit,yfit);set(gca,'FontSize',14);ylim([-17,11]);legend('Data points','Fitted curve','Location','southeast');
end

输出结果:

二、Multiple regression(多元回归)

(一)What If There Exists More Variables?(如果有更多的变量呢?)

1、Equations associated with more than one explanatory variables: (有多个解释变量的相关方程)

2、Multiple linear regression:regress()(多元线性回归)

3、Note:the function given you more statistics(e.g.,R²)of the regression model(该函数为您提供了回归模型的更多统计信息)

(二)Multiple Linear Regression:regress()(多元线性回归)

示例代码:

load carsmall;
y = MPG;%1加仑的汽油可以走多少英里
x1 = Weight;%重量
x2 = Horsepower;%马力
X = [ones(length(x1),1) x1 x2];%增广矩阵,第一行为1向量,即常数项
%即y = a + bx1 + cx2
b = regress(y,X);%多元线性回归%% 绘制图形
x1fit = min(x1):100:max(x1);
x2fit = min(x2):10:max(x2);
[X1FIT,X2FIT] = meshgrid(x1fit,x2fit);
YFIT = b(1) + b(2) * X1FIT + b(3) * X2FIT;
%即y = a + bx1 + cx2
scatter3(x1,x2,y,'filled');
%三维散点图
%filled表示填充标记
hold on;
mesh(X1FIT,X2FIT,YFIT);
hold off;
xlabel('Weight');
ylabel('Horsepower');
zlabel('MPG');
view(50,10);

输出结果:

注意:

(1)b= regress(y,X)返回向量b,其中包含向量y中的响应对矩阵X中的预测变量的多元线性回归的系数估计值。要计算具有常数项(截距)的模型的系数估计值,请在矩阵X中包含一个由 1 构成的列。

(三)What If the Equations Area NOT Linear?(如果方程区域不是线性的呢)

1、What are linear equations?(什么是线性方程式)

2、How do we do curve fitting using nonlinear equations?(如何利用非线性方程进行曲线拟合)

(二)DC Motor System Identification(DC马达系统辨识)

1、For a typical DC motor,the velocity

and displacement
profile of a step responses of are(对一个典型的DC马达,速度和位移关于时间的关系)

2、The displacement

profile is:(求位移的方程,三个未知数

where

is the time constant(β是时间的常数)

(三)Curve Fitting Toolbox:cftool()(曲线拟合内件)

1、键入cftool

三、Interpolation(插值,或称内插)

(一)Interpolation vs Regression(插值与回归)

1、Interpolation(插值)

(1)The process of finding an approximation of a function(求函数逼近的过程)

(2)The fit does traverse all known points(拟合会遍历所有已知点)

2、Regression(回归)

(1)The process of finding a curve of best fit(寻找最佳拟合曲线的过程)

(2)The fit generally does not pass through the data points(拟合通常不通过数据点)

(二)Common Interpolation Approaches(常用插值方法)

1、piecewise linear interpolation(分段线性插值)

2、piecewise cubic polynomial interpolation(分段三次多项式插值)

3、Cubic spline interpolation(三次样条插值)

(三)Linear Interpolation:interp1()(线性插值)

示例代码:

%% 构造分散点
x = linspace(0,2 * pi,40);%在0到2pi之间生成间隔相同的40个点
x_m = x;
x_m([11:13,28:30]) = NaN;%手动将11-13,28-30这6个点变为空,制造下图中的断点
y_m = sin(x_m);
%% 绘制图形
plot(x_m,y_m,'ro','MarkerFaceColor','r');
xlim([0,2*pi]);
ylim([-1.2,1.2]);
box on;
%set(gca,'FontName','symbol','FontSize',16);高版本不需要这一句
set(gca,'XTick',0:pi/2:2*pi);
set(gca,'XTickLabel',{'0','pi/2','pi','3pi/2','2pi'});
%% 内插
m_i = ~isnan(x_m);
%'~':表示取非,这里是将右边判断空值所形成的数组取非后赋值给左边
%isnan():确定哪些数组元素为 NaN
y_i = interp1(x_m(m_i),...y_m(m_i),x);
%interp1():一维数据插值(表查找)
hold on;
plot(x,y_i,'-b',...'LineWidth',2);
hold off;

输出结果:

注意:

(1)‘~’表示取非,这里是将右边判断空值所形成的数组取非后赋值给左边

(2)TF= isnan(A)返回一个逻辑数组,其中的1(true) 对应A中的NaN元素,0(false) 对应其他元素。如果A包含复数,则isnan(A)中的1对应实部或虚部为NaN值的元素,0对应实部和虚部均非NaN值的元素。

(3)vq = interp1(x,v,xq) 使用线性插值返回一维函数在特定查询点的插入值。向量 x 包含样本点,v 包含对应值 v(x)。向量 xq 包含查询点的坐标。

如果您有多个在同一点坐标采样的数据集,则可以将 v 以数组的形式进行传递。数组 v 的每一列都包含一组不同的一维样本值。

(四)Spline Interpolation:spline()(样条插值)

示例代码:

x = linspace(0,2 * pi,40);
x_m = x;
x_m([11:13,28:30]) = NaN;
y_m = sin(x_m);
plot(x_m,y_m,'ro','MarkerFaceColor','r');%绘制散点图
xlim([0,2*pi]);
ylim([-1.2,1.2]);
box on;
%set(gca,'FontName','symbol','FontSize',16);高版本不需要这一句
set(gca,'XTick',0:pi/2:2*pi);
set(gca,'XTickLabel',{'0','pi/2','pi','3pi/2','2pi'});
%% 内插
m_i = ~isnan(x_m);
y_i1 = spline(x_m(m_i),y_m(m_i),x);%使用内插值线段连接
y_i2 = interp1(x_m(m_i),y_m(m_i),x);%使用样条差值连接
hold on;
plot(x,y_i1,'-b','LineWidth',2);%
plot(x,y_i2,'-g','LineWidth',2);
hold off;
h = legend('Original','Linear','Spline');
set(h,'FontName','Times New Roman');

输出结果:

(五)What Are Splines?

1、Piecewise polynomial functions(分段多项式函数)

(六)Excise

1、Fit the data using linear lines and cubic splines(用直线和三次样条拟合数据)

答案代码:

x = [0 0.25 0.75 1.25 1.5 1.75 1.875 2 2.125 2.25];
y = [1.2 1.18 1.1 1 0.92 0.8 0.7 0.55 0.35 0];
x_i = 0:0.1:2.5;
hold on
plot(x,y,'bo');%绘制散点图
xlim([0,2.5]);
ylim([0,1.4]);
box on;
y_i1 = interp1(x,y,x_i);%使用内插值线段连接
y_i2 = interp1(x,y,x_i,'spine');%使用样条差值连接
plot(x_i,y_i2,'r','linewidth',1);
plot(x_i,y_i1,'c');
xlabel('x(ft)');
ylabel('y(ft)');
title('Data & Fit Model');
set(gca,'fontsize',14);
legend('Data','Linear','Spline')
hold off

输出结果:

(七)Cubic Spline vs. Hermite Polynomial(三次样条曲线与埃尔米特多项式)

1、p = pchip(x,y,t);:Hermite Polynomial,埃尔米特多项式

最大的不同在于,埃尔米特多项式在点和点连接曲线不会片离连接的线段

埃尔米特多项式_百度百科​baike.baidu.com

示例代码:

x = -3:3;
y = [-1 -1 -1 0 1 1 1];
t = -3:.01:3;
s = spline(x,y,t);%spline多项式
p = pchip(x,y,t);%埃尔米特多项式
hold on;
plot(x,y,'ro','MarkerFaceColor','r');
plot(t,s,':g','LineWidth',2);
plot(t,p,'--b','LineWidth',2);
hold off;
box on;
set(gca,'FontSize',16);
h = legend('Original','Spline','Hermite','Location','northwest');
%注意,标的顺序与绘图的先后顺序相同

输出结果:

(八)2D Interpolation:interp2()(二维插值)

示例代码:

%% 原本的数据图
subplot(1,2,1);
xx = -2:.5:2;
yy = -2:.5:3;
[X,Y] = meshgrid(xx,yy);
Z = X .* exp(-X .^ 2 - Y .^ 2);
surf(X,Y,Z);
hold on;
plot3(X,Y,Z+0.01,'ok',...'MarkerFaceColor','r')
title('Origin');%% 线性内插
subplot(1,2,2);
xx_i = -2:.1:2;
yy_i = -2:.1:3;
[X_i,Y_i] = meshgrid(xx_i,yy_i);
Z_i = interp2(xx,yy,Z,X_i,Y_i);
surf(X_i,Y_i,Z_i);
hold on;
plot3(X,Y,Z+0.01,'ok',...'MarkerFaceColor','r')
title('Linear');

输出结果:

(九)2D Interpolation Using Spline(端点处圆润很多)

示例代码:

xx = -2:.5:2;
yy = -2:.5:3;
[X,Y] = meshgrid(xx,yy);
Z = X .* exp(-X .^ 2 - Y .^ 2);
xx_i = -2:.1:2;
yy_i = -2:.1:3;
[X_i,Y_i] = meshgrid(xx_i,yy_i);
Z_e = interp2(xx,yy,Z,X_i,Y_i,'cubic');
surf(X_i,Y_i,Z_e);
hold on;
plot3(X,Y,Z+0.01,'ok',...'MarkerFaceColor','r')
title('Spline');

输出结果:


第十四节结束

matlab练习_MATLAB教程-台大郭彦甫-第十四节,含练习答案相关推荐

  1. matlab求两向量夹角_MATLAB教程-台大郭彦甫-第十节,含练习答案

    10-数值微积分 一.Polynomial differentiation and integration(多项式微分与积分) (一)Differentiation(微分) 1.The derivat ...

  2. matlab求两向量夹角_MATLAB教程-台大郭彦甫-第十二节,含练习答案

    12-线性方程式与线性系统 一.Linear equation(线性方程式) (一)Linear equation 1.Suppose you are given linear equations: ...

  3. Matlab学习(台大郭彦甫)第5节-初阶绘图

    Matlab学习(台大郭彦甫)第5节-初阶绘图 第5节 初阶绘图 5.1 基础绘图 5.1.1 plot()函数 5.1.2 hold on/off 5.1.3 Plot Style(绘图风格) 5. ...

  4. 【台大郭彦甫】Matlab入门教程超详细学习笔记二:基本操作与矩阵运算(附PPT链接)

    Matlab入门教程超详细学习笔记二:基本操作与矩阵运算 前言 一.基本操作 1.把matlab当作计算器使用 2.变量 3.控制格式输出 二.矩阵运算 1.矩阵 2.矩阵索引 3.使用:创建向量 4 ...

  5. MATLAB教程_11方程式求根_台大郭彦甫课程笔记

    MATLAB教程_11方程式求根_台大郭彦甫课程笔记 一.符号寻根法(解析法) 1.使用sym()或syms()创建符号变量 2.符号根查找:solve() 3.解多重方程 4.求解用符号表示的方程 ...

  6. 【台大郭彦甫】Matlab入门教程超详细学习笔记七:数值微积分(附PPT链接)

    数值微积分 前言 一.多项式微积分 1. 多项式计算 2. 多项式微分 3. 多项式积分 二.数值微积分 1. 数值微分法 2. 高阶微分法 3. 数值积分法 三.回顾Function Handles ...

  7. 【台大郭彦甫】Matlab入门教程超详细学习笔记五:初阶绘图(附PPT链接)

    初阶绘图 前言 一.基础绘图 1.plot() 绘制二维线图 2.legend()添加图例 3.title()和*label()添加标题与坐标轴 4.text()和annotation()增加注解 二 ...

  8. 【台大郭彦甫】Matlab入门教程超详细学习笔记六:高阶绘图(附PPT链接)

    高阶绘图 前言 一.进阶二维绘图 1. 对数图 2.一图双y轴 3. 直方图 4. 条形图 5. 饼状图 6. 极坐标图 7. 阶梯图与取样图 8. 箱线图以及误差线图 9. 填充图 二.配色 1.R ...

  9. 【台大郭彦甫】Matlab入门教程超详细学习笔记四:数据类型与文件读写(附PPT链接)

    变量类型与文件读写 前言 一.变量类型 1.numeric(数值类型) 2.char(字符类型) 3.string(字符串类型) 4.structure(结构体) 5.cell(元胞数组) 5.高维数 ...

最新文章

  1. 设置tomcat服务为80端口,tomcat虚拟主机,tomcat日志
  2. python 线程之threading(五)
  3. 一款超炫酷后台权限管理系统
  4. mysql 查看函数fsync_查看MySql使用的数据库引擎
  5. Vim功能键整理(图片来自mooc)
  6. Endless Spin
  7. swift 拖动按钮_ios – Swift中可拖动的UIButton / Elements?
  8. IBM斥资20亿美元的收购,天气数据为何值钱?
  9. 什么情况下使用weak关键字,相比assign有什么不同
  10. IKM-Java SE 8评估测试题挑战,测测你的基础水平
  11. echarts饼状图去除他的划过放大效果。
  12. SysFader:IEXPLORE.EXE应用程序错误
  13. ISBN号码(c++)
  14. 儿童视力档案小程序开发,视力“云管家”
  15. 什么是智能制造成能力成熟度模型?
  16. 掌上智维隐私政策privacy
  17. FPGA Verilog md5算法实现源代码及仿真文件分享。
  18. 2007中国手机客户端软件TOP50
  19. Windows cmd命令 GeTu0529
  20. 批处理下载MODIS数据

热门文章

  1. 9.Boost之正则regex
  2. java 时间戳和PHP时间戳 的转换 php time()
  3. java 初始化二维数组_java二维数组的常见初始化
  4. 统计学怎么求加权指数_我要自学生信之统计学:统计学概述(一)
  5. 利用.swp文件恢复源文件
  6. 数据採集器服务——Socket(今天才发现AES加解密代码跟贴的时候不一样,貌似乱码,不知什么情况)...
  7. 浅谈对5G核心网演进方向的几点展望
  8. ArcMap中的名称冲突问题
  9. oracle的存储过程
  10. vim一些挺方便的功能