




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、利用 Matlab 實現(xiàn)直線和平面的擬合2011-04-14 10:45:43| 分類: 算法思想 |舉報|字號 訂閱 直線和平面擬合是很常用的兩個算法,原理非常簡單。但如果 matlab 不太熟的 話,寫起來也不是那么容易。搜了很久才找到這兩個代碼,保存之,免得日后麻 煩。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 =
2、 y + n; % x and yn roughly satisfy yn = 2*x + 3 due to the noise % Determine coefficients for non-noisy line y=m1*x+b1 Xcolv = x(:); % Make X a column vector Ycolv = y(:); % Make Y a column vectorConst = ones(size(Xcolv); % Vector of ones for constant termCoeffs = Xcolv ConstYcolv; % Find the coeffi
3、cients m1 = 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)*xA2+Coeffs (2) *x+Coeffs(3)% Coeffs = Xcolv.A2 Xcolv Con stYcolv;% Note the .a before the exp onent of
4、 the first term% Plot the original points and the fitted curve figure plot(x,y,'ro') hold onx2 = 0:0.01:1;y2 = m1*x2+b1; % Evaluate fitted curve at many points plot(x2, y2, 'g-') title(sprintf('Non-noisy data: y=%f*x+%f',m1,b1) % Determine coefficients for noisy line yn=m2*x+
5、b2 Xcolv = x(:); % Make X a column vector Yncolv = yn(:); % Make Yn a column vectorConst = ones(size(Xcolv); % Vector of ones for constant term NoisyCoeffs = Xcolv ConstYncolv; % Find the coefficients m2 = NoisyCoeffs(1);b2 = NoisyCoeffs(2);% Plot the original points and the fitted curve figureplot(
6、x,yn,'ro') hold on x2 = 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 colum
7、n vector Ycolv = y(:); % Make Y a column vector Zcolv = z(:); % Make Z a column vectorConst = ones(size(Xcolv); % Vector of ones for constant term Coefficients = Xcolv Ycolv ConstZcolv; % Find the coefficients XCoeff = Coefficients(1); % X coefficientYCoeff = Coefficients(2); % X coefficient CCoeff
8、= Coefficients(3); % constant term% Using the above variables, z = XCoeff * x + YCoeff * y + CCoeff L=plot3(x,y,z,'ro'); % Plot the original data points set(L,'Markersize',2*get(L,'Markersize') % Making the circle markers larger set(L,'Markerfacecolor','r') %
9、Filling in the markers hold onxx, yy=meshgrid(0:0.1:1,0:0.1:1); % Generating a regular grid for plotting zz = XCoeff * xx + YCoeff * yy + CCoeff;surf(xx,yy,zz) % Plotting the surface title(sprintf('Plotting plane z=(%f)*x+(%f)*y+(%f)',XCoeff, YCoeff, CCoeff) % By rotating the surface, you ca
10、n see that the points lie on the plane% Also, 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 最小二乘法進行平面擬合MATLAB 軟件提供了基本的曲線擬合函數(shù)的命令: 多項式函數(shù)擬合: a = polyfit (xdata,ydata, n) 其中n表示多項式的最高階數(shù),xdata, ydata為要擬合的數(shù)據(jù),它
11、是用數(shù)組的方 式輸入。輸出參數(shù)a為擬合多項式y(tǒng) = a1xn + ax + an+i的系數(shù)a =,,用 an+1 。多項式在x處的值y可用下面程序計算。y = polyval (a, x)一般的曲線擬合: p = curvefit(Fun'0,pxdata, ydata)其中Fun表示函數(shù)Fun (p, xdata)的 M-文件,p0表示函數(shù)的初值。curvefit命令的 求解問題形式是:min p sum (F un (p, xdata)-ydata).A2若要求解點 x 處的函數(shù)值可用程序 f = Fun(p, x) 計算。例如已知函數(shù)形式 y = ae - bx + ce -dx
12、,并且已知數(shù)據(jù)點(xi, yi), i = 1,2,n要確定四個未知參數(shù) a, b, c, d。使用 curvefit 命令,數(shù)據(jù)輸入 xdata = x1,x2,xn; ydata = y1,y2,初值輸n;入 p0 = aO,bO,cO,dO;并且建立函數(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 1O 1O.2 1O.32 1O.42 1O
13、.5 1O.55 1O.58 1O.6;plot(t,y,'o') %畫散點圖p=polyfit(t,y,2) (二次多項式擬合 )計算結(jié)果:p = -O.O4451.O7114.3252%二次多項式的系數(shù)從而得到某化合物的濃度y與時間t的擬合函數(shù):y = 4.3252+1.O711t -O.O445t2對函數(shù)的精度如何檢測呢?仍然以圖形來檢測, 將散點與擬合曲線畫在一個畫面 上。xi=linspace(O,16,16O);yi=polyval(p,xi); plot(x,y,'o',xi,yi) 在MATLAB的NAG Foundation Toolbox中也
14、有一些曲面擬合函數(shù),如e02daf,eO2cf, eO2def 可分別求出矩形網(wǎng)格點數(shù)據(jù)、散點數(shù)據(jù)的最小平方誤差雙三次樣 條曲面擬合,e02def等可求出曲面擬合的函數(shù)值。用matlab的regress命令進行平面擬合2011-08-16 22:00:38)專載標簽:分類:數(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即可將散點繪制出來我們繼續(xù)X = ones(5,1) x y; 5 為 size(x)b = regress(
15、z,X) /擬合,其實是線性回歸,但可以用來擬合平面。regress命令還有其它用法, 但一般這樣就可以滿足要求了。于是顯示出6.5642-0.1269-0.0381這就表示 z = 6.5643 - 0.1269 * x - 0.0381 * y 是擬合出來的平面的方程 下面把它繪制出來xfit = min(x):0.1:max(x);注 0.1 表示數(shù)據(jù)的間隔yfit = mi n(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)這樣,圖就出來啦%r p q就是你的x,y,z r = ran di(10,20,1);p = ran di(10,20,1);q = ran di(10,20,1);b = re
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 法律服務(wù)行業(yè)法律顧問服務(wù)協(xié)議
- 產(chǎn)業(yè)園物業(yè)服務(wù)合同
- 古詩文登高解讀與教學(xué)方案設(shè)計
- 個人權(quán)益保護網(wǎng)絡(luò)平臺使用協(xié)議
- 企業(yè)級網(wǎng)絡(luò)安全預(yù)防預(yù)案
- 裝修工程擔(dān)保合同
- 《宋代書法欣賞:大學(xué)書法藝術(shù)課程教案》
- 在線教育行業(yè)分析模擬試題集
- 股權(quán)擔(dān)保協(xié)議書規(guī)范
- 企業(yè)社會責(zé)任年度演講致辭草稿
- 服裝倉庫管理制度及流程
- 架子工安全教育培訓(xùn)試題(附答案)
- 《高血壓5項化驗》課件
- 一中師德考核評估制度
- 肋骨骨折護理個案查房
- 分布式網(wǎng)絡(luò)處理方案
- CNAS-CL02-A001:2023 醫(yī)學(xué)實驗室質(zhì)量和能力認可準則的應(yīng)用要求
- 血管外科護理課件
- 鐵路機車檢修坑施工方案
- 數(shù)字化轉(zhuǎn)型中的知識管理
- 安徽高中畢業(yè)生登記表
評論
0/150
提交評論