1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > matlab中散点图的线性拟合_Matlab直线拟合和平面拟合

matlab中散点图的线性拟合_Matlab直线拟合和平面拟合

时间:2024-03-28 00:34:56

相关推荐

matlab中散点图的线性拟合_Matlab直线拟合和平面拟合

利用

Matlab

实现直线和平面的拟合

-04-1410:45:43|

分类:

算法思想

|

举报

|

字号

订阅

直线和平面拟合是很常用的两个算法,原理非常简单。但如果

matlab

不太熟的

话,写起来也不是那么容易。搜了很久才找到这两个代码,保存之,免得日后麻

烦。

1

、直线拟合的

matlab

代码

%Fittingabest-fitlinetodata,bothnoisyandnon-noisy

x=rand(1,10);

n=rand(size(x));%Noise

y=2*x+3;%xandysatisfyy=2*x+3

yn=y+n;%xandynroughlysatisfyyn=2*x+3duetothenoise

%Determinecoefficientsfornon-noisyliney=m1*x+b1

Xcolv=x(:);%MakeXacolumnvector

Ycolv=y(:);%MakeYacolumnvector

Const=ones(size(Xcolv));%Vectorofonesforconstantterm

Coeffs=[XcolvConst]\Ycolv;%Findthecoefficients

m1=Coeffs(1);

b1=Coeffs(2);

%Tofitanotherfunctiontothisdata,simplychangethefirst

%matrixonthelinedefiningCoeffs

%Forexample,thiscodewouldfitaquadratic

%y=Coeffs(1)*x^2+Coeffs(2)*x+Coeffs(3)

%Coeffs=[Xcolv.^2XcolvConst]\Ycolv;

%Notethe.^beforetheexponentofthefirstterm

%Plottheoriginalpointsandthefittedcurve

figure

plot(x,y,'ro')

holdon

x2=0:0.01:1;

y2=m1*x2+b1;%Evaluatefittedcurveatmanypoints

plot(x2,y2,'g-')

title(sprintf('Non-noisydata:y=%f*x+%f',m1,b1))

%Determinecoefficientsfornoisylineyn=m2*x+b2

Xcolv=x(:);%MakeXacolumnvector

Yncolv=yn(:);%MakeYnacolumnvector

Const=ones(size(Xcolv));%Vectorofonesforconstantterm

NoisyCoeffs=[XcolvConst]\Yncolv;%Findthecoefficients

m2=NoisyCoeffs(1);

b2=NoisyCoeffs(2);

%Plottheoriginalpointsandthefittedcurve

figure

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。