matlab非线性拟合求参数_matlab拟合自定义函数

(1) 2024-09-24 18:23

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
matlab非线性拟合求参数_matlab拟合自定义函数,希望能够帮助你!!!。

曲线拟合的原理:对于y = f(x),通过构造一个函数g(x)取逼近未知函数f(x),使得误差在某种意义下达到最小。对于非线性拟合时需要使用fittype()函数,该函数可以指定所要构造的拟合函数。fittype()函数与fit函数相结合,可以拟合简单的非线性函数的未知参数。


1.fittype()函数语法

f=fittype('公式具体表达','independent','自变量名','coefficients',{'待定参数1','待定参数2',......}); aFittype=fittype(libraryModelName) aFittype=fittype(expression) aFittype=fittype(expression,Name,Value) aFittype=fittype(linearModelTerms) aFittype=fittype(linearModelTerms,Name,Value) aFittype=fittype(anonymousFunction) aFittype=fittype(anonymousFunction,Name,Value) fit()函数语法: fitobject=fit(x,y,fitType) fitobject=fit([x,y],z,fitType) fitobject=fit(x,y,fitType,fitOptions) fitobject=fit(x,y,fitType,Name,Value) [fitobject,gof]=fit(x,y,fitType) [fitobject,gof,output]=fit(x,y,fitType)

2.实例1

matlab非线性拟合求参数_matlab拟合自定义函数_https://bianchenghao6.com/blog__第1张

程序

clc; clear all; close all; syms t x=[0;0.4;1.2;2;2.8;3.6;4.4;5.2;6;7.2;8;9.2;10.4;11.6;12.4;13.6;14.4;15]; y=[1;0.85;0.29;-0.27;-0.53;-0.4;-0.12;0.17;0.28;0.15;-0.03;-0.15;-0.071;0.059;0.08;0.032;-0.015;-0.02]; f=fittype('a*cos(k*t)*exp(w*t)','independent','t','coefficients',{'a','k','w'}) cfun=fit(x,y,f) xi=0:1:20; yi=cfun(xi); plot(x,y,'r*',xi,yi,'b-');

运行结果

f = General model: f(a,k,w,t) = a*cos(k*t)*exp(w*t) cfun = General model: cfun(t) = a*cos(k*t)*exp(w*t) Coefficients (with 95% confidence bounds): a = 0.9987 (0.9836, 1.014)%此处a,k,w为上述拟合函数的系数最优值 k = 1.001 (0.9958, 1.006) %此处括号里显示的为置信区间 w = -0.2066 (-0.2131, -0.2002)
matlab非线性拟合求参数_matlab拟合自定义函数_https://bianchenghao6.com/blog__第2张


3.实例2

matlab非线性拟合求参数_matlab拟合自定义函数_https://bianchenghao6.com/blog__第3张

程序

clc; clear all; close all; x = [1.0 1.5 2.0 2.5 3.0]'; y = [0.9 1.7 2.2 2.6 3.0]'; p = fittype('a*x+b*sin(x)+c'); f = fit(x,y,p) plot(f,x,y);

运行结果

 f = General model: f(x) = a*x+b*sin(x)+c Coefficients (with 95% confidence bounds): a = 1.249 (0.9856, 1.512) b = 0.6357 (0.03185, 1.24) c = -0.8611 (-1.773, 0.05094) 
matlab非线性拟合求参数_matlab拟合自定义函数_https://bianchenghao6.com/blog__第4张


4.实例3

程序

 clc; clear all; close all; x = linspace(1,100); y = 5 + 7*log(x); myfittype = fittype('a + b*log(x)',... 'dependent',{'y'},'independent',{'x'},... 'coefficients',{'a','b'}) myfit = fit(x',y',myfittype) plot(myfit,x,y)

运行结果

myfit = General model: myfit(x) = a + b*log(x) Coefficients (with 95% confidence bounds): a = 5 (5, 5) b = 7 (7, 7)
matlab非线性拟合求参数_matlab拟合自定义函数_https://bianchenghao6.com/blog__第5张


5.实例4

程序

 clc; clear all; close all; x = [0.81;0.91;0.13;0.91;0.63;0.098;0.28;0.55;... 0.96;0.96;0.16;0.97;0.96]; y = [0.17;0.12;0.16;0.0035;0.37;0.082;0.34;0.56;... 0.15;-0.046;0.17;-0.091;-0.071]; ft = fittype( 'piecewiseLine( x, a, b, c, d, k )' ) f = fit( x, y, ft, 'StartPoint', [1, 0, 1, 0, 0.5] ) plot( f, x, y ) 

piecewiseLine.m

 function y = piecewiseLine(x,a,b,c,d,k) % PIECEWISELINE A line made of two pieces % that is not continuous. y = zeros(size(x)); % This example includes a for-loop and if statement % purely for example purposes. for i = 1:length(x) if x(i) < k, y(i) = a + b.* x(i); else y(i) = c + d.* x(i); end end end

运行结果

ft = General model: ft(a,b,c,d,k,x) = piecewiseLine( x, a, b, c, d, k ) f = General model: f(x) = piecewiseLine( x, a, b, c, d, k ) Coefficients (with 95% confidence bounds): a = -0.03779 b = 1.352 c = 1.237 d = -1.301 k = 0.5 
matlab非线性拟合求参数_matlab拟合自定义函数_https://bianchenghao6.com/blog__第6张

本文内容来源于网络,仅供参考学习,如内容、图片有任何版权问题,请联系处理,24小时内删除。

作 者 | 郭志龙
编 辑 | 郭志龙
校 对 | 郭志龙

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复