matlab之回归与内插

本文详细介绍了在MATLAB中如何进行回归分析和内插操作。针对线性回归,通过polyfit()和regress()函数确定相关性及高阶方程;在非线性情况下,利用cftool定制方程寻找参数。对于内插,讲解了interp1()和interp2()函数在1D和2D数据中的应用,以及如何通过spline()实现更平滑的插值效果。

一、回归

找到回归方程,使SSE最小

用matlab解:

线性回归图:

polyfit()

>> 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);
>> fit

fit =

    4.9897   -4.4491

>> xfit = [x(1):0.1:x(end)]; yfit = fit(1)*xfit+fit(2);
>> plot(x,y,'ro',xfit,yfit); set(gca,'FontSize',14);
>> legend(2,'data points','best-fit');

确定两者是否有线性关系:

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

ans =

    1.0000    0.9202
    0.9202    1.0000

>> 

得到线性相关性:r = 0.9202

用更高阶方程式表示相关性:

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:3
    subplot(1,3,i); p = polyfit(x,y,i);
    xfit = x(1):0.01:x(end); yfit = polyval(p,xfit);
    plot(x,y,'ro',xfit,yfit); set(gca,'FontSize',14);
    ylim([-17,11]); legend(4,'Data points','Fitted curve');
end

>> 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]; 
>> for i = 4:6
subplot(1,3,1); p = polyfit(x,y,i);
xfit = x(1):0.01:x(end); yfit=polyval(p,xfit);
plot(x,y,'ro',xfit,yfit); set(gca,'FontSize',14);
ylim([-17,11]);legend(4,'Data point', 'Fitted curve','location','SouthEast');
end

 

 有两个变数时:

使用regress()

>> load carsmall;
>> y  = MPG;
>> x1 = Weight; x2 = Horsepower;
>> X = [ones(length(x1),1 ) x1 x2];
>> 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;
>> scatter3(x1,x2,y,'filled');hold on;
>> mesh(X1FIT,X2FIT,YFIT); hold off;
>> xlabel('Weught'); ylabel('Horsepower'); zlabel('MPG'); view(50,10);
>> 

[x,y]=meshgrid(向量1,向量2)绘制二维坐标系。x轴取值为向量1,y轴取值为向量2.

x,y为矩阵,对应各个点的横坐标与纵坐标所构成的矩阵。

z=f(x,y)

mesh(x,y,z)可以绘制f(x,y)的函数图像。

什么是线性方程式:

非线性时的情形:

 

怎样找到这些参数?

利用matlab的

>> load carsmall;
>> cftool
>> 

加载carsmall后的数据:

加载数据后,命令行输入cftool,就会跳出窗口:

xdata和ydata中选择数据。

 再选择Custom Equation可以输入自己想要的形式

然后就能在Results中得到相应的参数。

二、内插

 

常用函数(指令):

线性内插:interp1()

>> 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,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'});

 

上图可看出某些值被我丢失了。

使用线性内插:

>> m_i = ~isnan(x_m);
>> y_i = interp1(x_m(m_i),y_m(m_i),x);
>> hold on;
>> plot(x,y_i,'-b','LineWidth',2);
>> hold off;
>> 

 

用较平滑的线:

spline():

>> m_i = ~isnan(x_m);
>> y_i = spine(x_m(m_i),y_m(m_i),x);
>> y_i = spline(x_m(m_i),y_m(m_i),x);
>> hold on; 
>> plot(x,y_i,'-g','LineWidth',2);
>> h = legend('Original','Linear','Spline');
>> set(h,'FontName','Times New Roman');
>> 

 

二维:

>> 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')
>> 

 

interp2做线性内插:

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')

>> Z_c = interp2(xx,yy,Z,X_i,Y_i,'cubic');
>> surf(X_i,Y_i,Z_c);
>> hold on;
>> plot3(X,Y,Z+0.01,'ok','MarkerFaceColor','r')
>> 

2维的spline(): Z_c = interp2(xx,yy,Z,X_i,Y_i,'cubic');

 

【相对圆滑许多】 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值