直線擬合和平面擬合_第1頁(yè)
直線擬合和平面擬合_第2頁(yè)
直線擬合和平面擬合_第3頁(yè)
直線擬合和平面擬合_第4頁(yè)
直線擬合和平面擬合_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、利用Matlab實(shí)現(xiàn)直線和平面的擬合 2011-04-14 10:45:43| 分類: 算法思想 |舉報(bào)|字號(hào) 訂閱直線和平面擬合是很常用的兩個(gè)算法,原理非常簡(jiǎn)單。但如果matlab不太熟的話,寫起來(lái)也不是那么容易。搜了很久才找到這兩個(gè)代碼,保存之,免得日后麻煩。1、直線擬合的matlab代碼% Fitting a best-fit line to data, both noisy and non-noisyx = rand(1,10);n = rand(size(x); % Noisey = 2*x + 3; % x and y satisfy y = 2*x + 3yn = y + n;

2、% x and yn roughly satisfy yn = 2*x + 3 due to the noise% Determine coefficients for non-noisy line y=m1*x+b1Xcolv = x(:); % Make X a column vectorYcolv = y(:); % Make Y a column vectorConst = ones(size(Xcolv); % Vector of ones for constant termCoeffs = Xcolv ConstYcolv; % Find the coefficientsm1 =

3、Coeffs(1);b1 = Coeffs(2);% To fit another function to this data, simply change the first % matrix on the line defining Coeffs% For example, this code would fit a quadratic % y = Coeffs(1)*x2+Coeffs(2)*x+Coeffs(3)% Coeffs = Xcolv.2 Xcolv ConstYcolv; % Note the . before the exponent of the first term%

4、 Plot the original points and the fitted curvefigureplot(x,y,'ro')hold onx2 = 0:0.01:1;y2 = m1*x2+b1; % Evaluate fitted curve at many pointsplot(x2, y2, 'g-')title(sprintf('Non-noisy data: y=%f*x+%f',m1,b1)% Determine coefficients for noisy line yn=m2*x+b2Xcolv = x(:); % Make

5、 X a column vectorYncolv = yn(:); % Make Yn a column vectorConst = ones(size(Xcolv); % Vector of ones for constant termNoisyCoeffs = Xcolv ConstYncolv; % Find the coefficientsm2 = NoisyCoeffs(1);b2 = NoisyCoeffs(2);% Plot the original points and the fitted curvefigureplot(x,yn,'ro')hold onx2

6、 = 0:0.01:1;yn2 = m2*x2+b2;plot(x2, yn2, 'g-')title(sprintf('Noisy data: y=%f*x+%f',m2,b2)2、平面擬合matlab代碼x = rand(1,10);y = rand(1,10);z = (3-2*x-5*y)/4; % Equation of the plane containing % (x,y,z) points is 2*x+5*y+4*z=3Xcolv = x(:); % Make X a column vectorYcolv = y(:); % Make Y a

7、column vectorZcolv = z(:); % Make Z a column vectorConst = ones(size(Xcolv); % Vector of ones for constant termCoefficients = Xcolv Ycolv ConstZcolv; % Find the coefficientsXCoeff = Coefficients(1); % X coefficientYCoeff = Coefficients(2); % X coefficientCCoeff = Coefficients(3); % constant term% Us

8、ing the above variables, z = XCoeff * x + YCoeff * y + CCoeffL=plot3(x,y,z,'ro'); % Plot the original data pointsset(L,'Markersize',2*get(L,'Markersize') % Making the circle markers largerset(L,'Markerfacecolor','r') % Filling in the markershold onxx, yy=meshg

9、rid(0:0.1:1,0:0.1:1); % Generating a regular grid for plottingzz = XCoeff * xx + YCoeff * yy + CCoeff;surf(xx,yy,zz) % Plotting the surfacetitle(sprintf('Plotting plane z=(%f)*x+(%f)*y+(%f)',XCoeff, YCoeff, CCoeff)% By rotating the surface, you can see that the points lie on the plane% Also,

10、 if you multiply both sides of the equation in the title by 4,% you get the equation in the comment on the third line of this example如何用matlab最小二乘法進(jìn)行平面擬合MATLAB軟件提供了基本的曲線擬合函數(shù)的命令:多項(xiàng)式函數(shù)擬合: a = polyfit(xdata,ydata,n)其中n表示多項(xiàng)式的最高階數(shù),xdata,ydata 為要擬合的數(shù)據(jù),它是用數(shù)組的方式輸入。輸出參數(shù)a為擬合多項(xiàng)式y(tǒng) = a1xn + + anx + an+1的系數(shù)a = a1

11、, , an, an+1。多項(xiàng)式在x處的值y可用下面程序計(jì)算。y = polyval (a, x)一般的曲線擬合: p = curvefit(Funp0,xdata,ydata)其中Fun表示函數(shù)Fun (p, xdata)的M-文件,p0表示函數(shù)的初值。curvefit命令的求解問(wèn)題形式是:minp sum (Fun (p, xdata)-ydata).2若要求解點(diǎn)x處的函數(shù)值可用程序f = Fun(p, x) 計(jì)算。例如已知函數(shù)形式 y = ae - bx + ce dx ,并且已知數(shù)據(jù)點(diǎn)(xi, yi), i = 1,2, n,要確定四個(gè)未知參數(shù)a, b, c, d。使用curvefit

12、命令,數(shù)據(jù)輸入xdata = x1,x2, , xn; ydata = y1,y2, , yn;初值輸入p0 = a0,b0,c0,d0; 并且建立函數(shù)y = ae - bx + ce dx的M-文件(Fun.m)。若定義p1 = a, p2 = b, p3 = c, p4 = d , 則輸出p = p1, p2, p3, p4。引例求解: t=1:16; %數(shù)據(jù)輸入 y=4 6.4 8 8.4 9.28 9.5 9.7 9.86 10 10.2 10.32 10.42 10.5 10.55 10.58 10.6; plot(t,y,'o') %畫散點(diǎn)圖 p=polyfit(t

13、,y,2) (二次多項(xiàng)式擬合)計(jì)算結(jié)果: p = -0.0445 1.0711 4.3252 %二次多項(xiàng)式的系數(shù)從而得到某化合物的濃度y與時(shí)間t的擬合函數(shù):y = 4.3252+1.0711t 0.0445t2對(duì)函數(shù)的精度如何檢測(cè)呢?仍然以圖形來(lái)檢測(cè),將散點(diǎn)與擬合曲線畫在一個(gè)畫面上。 xi=linspace(0,16,160); yi=polyval(p,xi); plot(x,y,'o',xi,yi) 在MATLAB的NAG Foundation Toolbox中也有一些曲面擬合函數(shù),如e02daf,e02cf,e02def可分別求出矩形網(wǎng)格點(diǎn)數(shù)據(jù)、散點(diǎn)數(shù)據(jù)的最小平方誤差雙三

14、次樣條曲面擬合,e02def等可求出曲面擬合的函數(shù)值。用matlab的regress命令進(jìn)行平面擬合(2011-08-16 22:00:38)轉(zhuǎn)載標(biāo)簽: 教育分類: 數(shù)學(xué)軟件 以少量數(shù)據(jù)為例x = 1 5 6 3 7'y = 2 9 3 5 8'z = 4 3 5 11 6'scatter3(x,y,z,'filled')hold on即可將散點(diǎn)繪制出來(lái)我們繼續(xù) X = ones(5,1) x y; /5為size(x) b = regress(z,X) /擬合,其實(shí)是線性回歸,但可以用來(lái)擬合平面。regress命令還有其它用法,但一

15、般這樣就可以滿足要求了。 于是顯示出 b =    6.5642   -0.1269   -0.0381這就表示 z = 6.5643 - 0.1269 * x - 0.0381 * y 是擬合出來(lái)的平面的方程下面把它繪制出來(lái)xfit = min(x):0.1:max(x);  /注 0.1表示數(shù)據(jù)的間隔yfit = min(y):0.1:max(y);XFIT,YFIT= meshgrid (xfit,yfit); /制成網(wǎng)格數(shù)據(jù)ZFIT = b(1) + b(2) * XFIT + b(3) * YFIT;mesh (XFIT,YFIT,ZFIT)這樣,圖就出來(lái)啦%r p q就是你的x,y,zr = randi(10,20,1);p = randi(10,20,1);q = randi(10,20

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論