function [fitresult, gof] = createFit(x, y)
[xData, yData] = prepareCurveData( x, y) % Set up fittype and options.
ft = fittype( 'gauss1' )
opts = fitoptions( 'Method', 'NonlinearLeastSquares' )
opts.Display = 'Off'
opts.Lower = [-Inf -Inf 0]
opts.StartPoint = [y(ceil(length(x)/2)) x(ceil(length(x)/2)) 0.1] % Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts ) % Plot fit with data.
h = plot( fitresult, xData, yData)
xlabel( 'β' )
ylabel( 'R' )
legend off
grid onhold on
plot(fitresult.b1,fitresult.a1,'^','markersize',6)
运行它,然后在命令行窗口输入:createFit(x,y)
可以用lsqcurvefit函数或nlinfit函数,来得到高斯拟合的函数。实现方法:1、已知数据
x=【。。。】;
y=【。。。】;
2、自定义拟合函数模型,如fun = @(a,x) a(1)*exp(-((x- a(2))/a(3))^2) + a(4)*exp(-((x-a(5))/a(6))^2)
式中:a(1)—a1; a(2)—b1; a(3)—c1; a(4)— a2; a(5)—b2; a(6)—c2
3、利用lsqcurvefit函数或nlinfit函数,求出系数
p = lsqcurvefit(fun,a0,x,y)
或
p= nlinfit(x,y,fun,a0)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)