




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Lab04?曲線擬合的最小二乘法實(shí)驗(yàn)【實(shí)驗(yàn)?zāi)康暮鸵蟆孔寣W(xué)生體驗(yàn)曲線擬合的最小二乘法,加深對(duì)曲線擬合的最小二乘法的理解;掌握函數(shù)ployfit和函數(shù)lsqcurvefit功能和使用方法,分別用這兩個(gè)函數(shù)進(jìn)行多項(xiàng)式擬合和非多項(xiàng)式擬合。【實(shí)驗(yàn)內(nèi)容】在Matlab命令窗口,用help命令查詢函數(shù)polyfit和函數(shù)lsqcurvefit功能和使用方法。用多項(xiàng)式y(tǒng)=X3-6x2+5x-3,產(chǎn)生一組數(shù)據(jù)(x,y)(i=1,2,…,n),再在y上添加隨機(jī)干擾(可用rand產(chǎn)生(0,1)均勻分布隨機(jī)數(shù),或用randn產(chǎn)生N(0,1)均勻分布隨機(jī)數(shù)),然后對(duì)x和添加了隨機(jī)干擾的y用Matlabii提供的函數(shù)ployfit用3次多項(xiàng)式擬合,將結(jié)果與原系數(shù)比較。再作2或4次多項(xiàng)式擬合,分析所得結(jié)果。用電壓V=10伏的電池給電容器充電,電容器上t時(shí)刻的電壓為上v(t)二V-(V-V)e,,其中V是電容器的初始電壓,T是充電常數(shù)。對(duì)00于下面的一組t,v數(shù)據(jù),用Matlab提供的函數(shù)lsqcurvefit確定V。禾口t。t(秒)0.51234579v(伏)6.366.487.268.228.668.999.439.63【實(shí)驗(yàn)儀器與軟件】CPU主頻在1GHz以上,內(nèi)存在128Mb以上的PC;Matlab6.0及以上版本。實(shí)驗(yàn)講評(píng):實(shí)驗(yàn)成績(jī):評(píng)閱教師:200年月日問(wèn)題及算法分析:1、利用help命令,在MATLAB中查找polyfit和lsqcurvefit函數(shù)的用法。2、在一組數(shù)據(jù)(x,y)(i=1,2,…,n)上,對(duì)y上添加隨機(jī)干擾,運(yùn)用多項(xiàng)式iii擬合函數(shù),對(duì)數(shù)據(jù)進(jìn)行擬合(分別用2次,3次,4次擬合),分析擬合的效果。3、根據(jù)t和V的關(guān)系畫散點(diǎn)圖,再根據(jù)給定的函數(shù)運(yùn)用最小二乘擬合函數(shù),確定其相應(yīng)參數(shù)。第一題:(1)>>helppolyfitPOLYFITFitpolynomialtodata.P=POLYFIT(X,Y,N)findsthecoefficientsofapolynomialP(X)ofdegreeNthatfitsthedataYbestinaleast-squaressense.PisarowvectoroflengthN+1containingthepolynomialcoefficientsindescendingpowers,P(1)*XAN+P(2)*XA(N-1)+...+P(N)*X+P(N+1).[P,S]=POLYFIT(X,Y,N)returnsthepolynomialcoefficientsPandastructureSforusewithPOLYVALtoobtainerrorestimatesforpredictions.Scontainsfieldsforthetriangularfactor(R)fromaQRdecompositionoftheVandermondematrixofX,thedegreesoffreedom(df),andthenormoftheresiduals(normr).IfthedataYarerandom,anestimateofthecovariancematrixofPis(Rinv*Rinv')*normrA2/df,whereRinvistheinverseofR.[P,S,MU]=POLYFIT(X,Y,N)findsthecoefficientsofapolynomialinXHAT=(X-MU(1))/MU(2)whereMU(1)=MEAN(X)andMU(2)=STD(X).Thiscenteringandscalingtransformationimprovesthenumericalpropertiesofboththepolynomialandthefittingalgorithm.WarningmessagesresultifNis>=length(X),ifXhasrepeated,ornearlyrepeated,points,orifXmightneedcenteringandscaling.ClasssupportforinputsX,Y:float:double,singleSeealsopoly,polyval,roots.ReferencepageinHelpbrowserdocpolyfit>>(2)>>helplsqcurvefitLSQCURVEFITsolvesnon-linearleastsquaresproblems.LSQCURVEFITattemptstosolveproblemsoftheform:minsum{(FUN(X,XDATA)-YDATA)42}whereX,XDATA,YDATAandthevaluesXreturnedbyFUNcanbevectorsormatrices.X=LSQCURVEFIT(FUN,XO,XDATA,YDATA)startsatX0andfindscoefficientsXtobestfitthenonlinearfunctionsinFUNtothedataYDATA(intheleast-squaressense).FUNacceptsinputsXandXDATAandreturnsavector(ormatrix)offunctionvaluesF,whereFisthesamesizeasYDATA,evaluatedatXandXDATA.NOTE:FUNshouldreturnFUN(X,XDATA)andnotthesum-of-squaressum((FUN(X,XDATA)-YDATA).A2).((FUN(X,XDATA)-YDATA)issquaredandsummedimplicitlyinthealgorithm.)X=LSQCURVEFIT(FUN,XO,XDATA,YDATA,LB,UB)definesasetoflowerandupperboundsonthedesignvariables,X,sothatthesolutionisintherangeLB<=X<=UB.UseemptymatricesforLBandUBifnoboundsexist.SetLB(i)=-InfifX(i)isunboundedbelow;setUB(i)=InfifX(i)isunboundedabove.X=LSQCURVEFIT(FUN,XO,XDATA,YDATA,LB,UB,OPTIONS)minimizeswiththedefaultparametersreplacedbyvaluesinthestructureOPTIONS,anargumentcreatedwiththeOPTIMSETfunction.SeeOPTIMSETfordetails.UsedoptionsareDisplay,TolX,TolFun,DerivativeCheck,Diagnostics,FunValCheck,Jacobian,JacobMult,JacobPattern,LineSearchType,LevenbergMarquardt,MaxFunEvals,MaxIter,DiffMinChangeandDiffMaxChange,LargeScale,MaxPCGIter,PrecondBandWidth,TolPCG,OutputFcn,andTypicalX.UsetheJacobianoptiontospecifythatFUNalsoreturnsasecondoutputargumentJthatistheJacobianmatrixatthepointX.IfFUNreturnsavectorFofmcomponentswhenXhaslengthn,thenJisanm-by-nmatrixwhereJ(i,j)isthepartialderivativeofF(i)withrespecttox(j).(NotethattheJacobianJisthetransposeofthegradientofF.)[X,RESNORM]=LSQCURVEFIT(FUN,XO,XDATA,YDATA,...)returnsthevalueofthesquared2-normoftheresidualatX:sum{(FUN(X,XDATA)-YDATA).A2}.[X,RESNORM,RESIDUAL]=LSQCURVEFIT(FUN,XO,...)returnsthevalueofresidual,FUN(X,XDATA)-YDATA,atthesolutionX.[X,RESNORM,RESIDUAL,EXITFLAG]=LSQCURVEFIT(FUN,XO,XDATA,YDATA,...)returnsanEXITFLAGthatdescribestheexitconditionofLSQCURVEFIT.PossiblevaluesofEXITFLAGandthecorrespondingexitconditionsareLSQCURVEFITconvergedtoasolutionX.ChangeinXsmallerthanthespecifiedtolerance.Changeintheresidualsmallerthanthespecifiedtolerance.Magnitudeofsearchdirectionsmallerthanthespecifiedtolerance.0Maximumnumberoffunctionevaluationsorofiterationsreached.-1Algorithmterminatedbytheoutputfunction.-2Boundsareinconsistent.-4Linesearchcannotsufficientlydecreasetheresidualalongthecurrentsearchdirection.[X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT]=LSQCURVEFIT(FUN,XO,XDATA,YDATA,...)returnsastructureOUTPUTwiththenumberofiterationstakeninOUTPUT.iterations,thenumberoffunctionevaluationsinOUTPUT.funcCount,thealgorithmusedinOUTPUT.algorithm,thenumberofCGiterations(ifused)inOUTPUT.cgiterations,thefirst-orderoptimality(ifused)inOUTPUT.firstorderopt,andtheexitmessageinOUTPUT.message.[X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT,LAMBDA]=LSQCURVEFIT(FUN,XO,XDATA,YDATA,...)returnsthesetofLagrangianmultipliers,LAMBDA,atthesolution:LAMBDA.lowerforLBandLAMBDA.upperforUB.[X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT,LAMBDA,JACOBIAN]=LSQCURVEFIT(FUN,XO,XDATA,YDATA,...)returnstheJacobianofFUNatX.ExamplesFUNcanbespecifiedusing@:xdata=[5;4;6];%examplexdataydata=3*sin([5;4;6])+6;%exampleydatax=lsqcurvefit(@myfun,[27],xdata,ydata)
wheremyfunisaMATLABfunctionsuchas:functionF=myfun(x,xdata)F=x(l)*sin(xdata)+x(2);FUNcanalsobeananonymousfunction:x=lsqcurvefit(@(x,xdata)x(1)*sin(xdata)+x(2),[27],xdata,ydata)IfFUNisparameterized,youcanuseanonymousfunctionstocapturetheproblem-dependentparameters.Supposeyouwanttosolvethecurve-fittingproblemgiveninthefunctionmyfun,whichisparameterizedbyitssecondargumentc.HeremyfunisanM-filefunctionsuchasfunctionF=myfun(x,xdata,c)F=x(1)*exp(c*xdata)+x(2);Tosolvethecurve-fittingproblemforaspecificvalueofc,firstassignthevaluetoc.Thencreateatwo-argumentanonymousfunctionthatcapturesthatvalueofcandcallsmyfunwiththreearguments.Finally,passthisanonymousfunctiontoLSQCURVEFIT:xdata=[3;1;4];%examplexdataydata=6*exp(-1.5*xdata)+3;%exampleydatac=-1.5;%defineparameterx=lsqcurvefit(@(x,xdata)myfun(x,xdata,c),[5;l],xdata,ydata)Seealsooptimset,lsqnonlin,fsolve,@,inline.ReferencepageinHelpbrowserdoclsqcurvefit>>三次線性擬合clearallx=0:0.5:5;y=x.A3-6*x.A2+5*x-3;y1=y;fori=1:length(y)y1(i)=y1(i)+rand;enda=polyfit(x,y1,3);b=polyval(a,x);plot(x,y,'*',x,b),aa=1.0121-6.10335.1933-2.4782②二次線性擬合clearallx=0:0.5:20;y=x.A3-6*x.A2+5*x-3;y1=y;fori=1:length(y)y1(i)=y1(i)+rand;enda=polyfit(x,y1,2);b=polyval(a,x);plot(x,y,'*',x,b),aa=23.9982-232.017923.9982-232.0179367.9756③四次線性擬合cl
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 項(xiàng)目部駐地裝修工程合同范本
- 卡車承運(yùn)合同范本
- 農(nóng)業(yè)路燈采購(gòu)合同范本
- 分包土方合同范本
- 化工車間采購(gòu)合同范本
- 動(dòng)漫聯(lián)名服務(wù)合同范本
- 公對(duì)公借貸合同范本
- 冰棍合同范本
- 賣酒銷售合同范例
- 分期付款購(gòu)車合同范本
- 二副工作心得體會(huì)實(shí)習(xí)感觸
- 土壤肥料全套課件
- 旅游消費(fèi)者行為學(xué)整套課件完整版電子教案課件匯總(最新)
- 學(xué)前兒童發(fā)展心理學(xué)(第3版-張永紅)教學(xué)課件1754
- 特氣供應(yīng)系統(tǒng)的規(guī)劃與設(shè)計(jì)
- 中職《機(jī)械基礎(chǔ)》全套課件(完整版)
- 勞技-中國(guó)結(jié)PPT通用課件
- 溫庭筠《望江南》ppt課件
- 口腔正畸學(xué)單詞
- 內(nèi)襯修復(fù)用HTPO管材企標(biāo)
- 部編教材一年級(jí)下冊(cè)生字筆順筆畫
評(píng)論
0/150
提交評(píng)論