当前位置:文档之家› Matlab 线性拟合 & 非线性拟合

Matlab 线性拟合 & 非线性拟合

Matlab 线性拟合& 非线性拟合分类:Computer Vision MATLAB 2012-06-22 21:24 46022人阅读评论(5) 收藏举报matlabplotrandomc图像处理目录(?)[+]使用Matlab进行拟合是图像处理中线条变换的一个重点内容,本文将详解Matlab中的直线拟合和曲线拟合用法。

关键函数:fittypeFit type for curve and surface fittingSyntaxffun = fittype(libname)ffun = fittype(expr)ffun = fittype({expr1,...,exprn})ffun = fittype(expr, Name, Value,...)ffun= fittype({expr1,...,exprn}, Name, Value,...)/***********************************线性拟合***********************************/线性拟合公式:coeff1 * term1 + coeff2 * term2 + coeff3 * term3 + ...其中,coefficient是系数,term都是x的一次项。

线性拟合Example:Example1: y=kx+b;法1:[csharp]view plaincopyprint?1.x=[1,1.5,2,2.5,3];y=[0.9,1.7,2.2,2.6,3];2.p=polyfit(x,y,1);3.x1=linspace(min(x),max(x));4.y1=polyval(p,x1);5.plot(x,y,'*',x1,y1);结果:p = 1.0200 0.0400即y=1.0200 *x+ 0.0400法2:[csharp]view plaincopyprint?1.x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];2.p=fittype('poly1')3.f=fit(x,y,p)4.plot(f,x,y);运行结果:[csharp]view plaincopyprint?1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];2.p=fittype('poly1')3.f=fit(x,y,p)4.plot(f,x,y);5.6.p =7.8. Linear model Poly1:9. p(p1,p2,x) = p1*x + p210.11.f =12.13. Linear model Poly1:14. f(x) = p1*x + p215. Coefficients (with 95% confidence bounds):16. p1 = 1.02 (0.7192, 1.321)17. p2 = 0.04 (-0.5981, 0.6781)Example2:y=a*x + b*sin(x) + c法1:[csharp]view plaincopyprint?1.x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];2.EXPR = {'x','sin(x)','1'};3.p=fittype(EXPR)4.f=fit(x,y,p)5.plot(f,x,y);运行结果:[csharp]view plaincopyprint?1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];2.EXPR = {'x','sin(x)','1'};3.p=fittype(EXPR)4.f=fit(x,y,p)5.plot(f,x,y);6.7.p =8.9. Linear model:10. p(a,b,c,x) = a*x + b*sin(x) + c11.12.f =13.14. Linear model:15. f(x) = a*x + b*sin(x) + c16. Coefficients (with 95% confidence bounds):17. a = 1.249 (0.9856, 1.512)18. b = 0.6357 (0.03185, 1.24)19. c = -0.8611 (-1.773, 0.05094)法2:[csharp]view plaincopyprint?1.x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];2. p=fittype('a*x+b*sin(x)+c','independent','x')3.f=fit(x,y,p)4.plot(f,x,y);运行结果:[csharp]view plaincopyprint?1.x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];2. p=fittype('a*x+b*sin(x)+c','independent','x')3.f=fit(x,y,p)4.plot(f,x,y);5.6.p =7.8. General model:9. p(a,b,c,x) = a*x+b*sin(x)+c10.Warning: Start point not provided, choosing random start11.point.12.> In fit>iCreateWarningFunction/nThrowWarning at 73813. In fit>iFit at 32014. In fit at 10915.16.f =17.18. General model:19. f(x) = a*x+b*sin(x)+c20. Coefficients (with 95% confidence bounds):21. a = 1.249 (0.9856, 1.512)22. b = 0.6357 (0.03185, 1.24)23. c = -0.8611 (-1.773, 0.05094)/***********************************非线性拟合***********************************/ Example:y=a*x^2+b*x+c法1:[cpp]view plaincopyprint?1.x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];2. p=fittype('a*x.^2+b*x+c','independent','x')3.f=fit(x,y,p)4.plot(f,x,y);运行结果:[csharp]view plaincopyprint?1.p =2.3. General model:4. p(a,b,c,x) = a*x.^2+b*x+c5.Warning: Start point not provided, choosing random start6.point.7.> In fit>iCreateWarningFunction/nThrowWarning at 7388. In fit>iFit at 3209. In fit at 10910.11.f =12.13. General model:14. f(x) = a*x.^2+b*x+c15. Coefficients (with 95% confidence bounds):16. a = -0.2571 (-0.5681, 0.05386)17. b = 2.049 (0.791, 3.306)18. c = -0.86 (-2.016, 0.2964)法2:[csharp]view plaincopyprint?1.x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];2.%use c=0;3.c=0;4.p1=fittype(@(a,b,x) a*x.^2+b*x+c)5.f1=fit(x,y,p1)6.%use c=1;7.c=1;8.p2=fittype(@(a,b,x) a*x.^2+b*x+c)9.f2=fit(x,y,p2)10.%predict c11.p3=fittype(@(a,b,c,x) a*x.^2+b*x+c)12.f3=fit(x,y,p3)13.14.%show results15.scatter(x,y);%scatter point16.c1=plot(f1,'b:*');%blue17.hold on18.plot(f2,'g:+');%green19.hold on20.plot(f3,'m:*');%purple21.hold off•上一篇特征提取方法SIFT,PCA-SIFT,GLOH,SURF •下一篇浙大计算机研究生复试上机考试-2009年。

相关主题