Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用matlab中的cftool拟合非线性函数曲线_Matlab_Math - Fatal编程技术网

用matlab中的cftool拟合非线性函数曲线

用matlab中的cftool拟合非线性函数曲线,matlab,math,Matlab,Math,我需要用一个非线性函数拟合一个数据,就像这样: y=a+b*cos(c*x+d)+e*exp(f*x); 其中a、b、c、d、e和f是要求出的系数。我在cftool中使用了自定义方程方法,结果很差,那么如何调整cftool中的起始点以获得更好的结果?否则,还有其他方法来解决我的问题吗?非常感谢 这是我的数据,每个1*35: x = [1970:2004]; y = [46808,49416,53094,57237,56677,56198,59673,61826,64158,65

我需要用一个非线性函数拟合一个数据,就像这样:

y=a+b*cos(c*x+d)+e*exp(f*x);
其中a、b、c、d、e和f是要求出的系数。我在cftool中使用了自定义方程方法,结果很差,那么如何调整cftool中的起始点以获得更好的结果?否则,还有其他方法来解决我的问题吗?非常感谢

这是我的数据,每个1*35:

    x = [1970:2004];
    y = [46808,49416,53094,57237,56677,56198,59673,61826,64158,65220,63108,60944,59543,58779,59882,60087,61825,63104,64963,66902,66443,67061,67273,67372,68679,69995,71522,73292,73932,75826,76954,78105,78439,79892,82631];

好的,丹,这是我关于cftool的代码,函数是由cftool的自定义方程生成的

% This file include two functions:
% 1. Main: testForFit.m
% 2. Function generated by cftool
function testForFit
clc; clear all;
x = 1970:2004;
y = [46808,49416,53094,57237,56677,56198,59673,61826,64158,65220,63108,60944,59543,58779,59882,60087,61825,63104,64963,66902,66443,67061,67273,67372,68679,69995,71522,73292,73932,75826,76954,78105,78439,79892,82631];
plot(x, y, 'or')
[fitresult, gof] = createFit(x, y)
end

function [fitresult, gof] = createFit(x, y)
%CREATEFIT(X,Y)
%  Create a fit.
%
%  Data for 'untitled fit 1' fit:
%      X Input : x
%      Y Output: y
%  Output:
%      fitresult : a fit object representing the fit.
%      gof : structure with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.
%  Auto-generated by MATLAB on 04-Feb-2014 00:23:03
%% Fit: curve fitting of gived form of function.
[xData, yData] = prepareCurveData( x, y );

% Set up fittype and options.
ft = fittype( 'a+b*cos(c*x+d)+e*exp(f*x)', 'independent', 'x', 'dependent', 'y' );
% ft = fittype( 'a+e*exp(f*x)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';

% http://stackoverflow.com/questions/21551951/fitting-curve-with-nonlinear-function-using-cftool-in-matlab
% Firstly, give the estimation, then calculate it
% funlist={1, @(p,x) cos(p(1)*x+p(2)), @(p,x) exp(p(3)*x)};
% NLPstart = [0.605 -3159.659 0.02369];
% [cdf,abe] = fminspleas(funlist,NLPstart,x,y)
% c=cdf(1); d=cdf(2); f=cdf(3);
% a=abe(1); b=abe(2); e=abe(3);
% opts.StartPoint = [a b c d e f];

% How to give the value of StartPoint ?
opts.StartPoint = [0.957166948242946 0.485375648722841 0.8002804688888 0.141886338627215 0.421761282626275 0.915735525189067];
% opts.StartPoint = [31950 1557 0.605 -3159.659 1.183e-16 0.02369];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'curve fitting' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. x', 'curve fitting', 'Location', 'NorthEast' );
% Label axes
% 31950 1557 0.605 -3159.659 1.183e-16 0.02369
% title('31950+1557*cos(0.605*x-3159.659)+1.183e-16*exp(0.02369*x)')
xlabel( 'x' );
ylabel( 'y' );
grid on
% x = [1970:2004];
% y = [46808,49416,53094,57237,56677,56198,59673,61826,64158,65220,63108,60944,59543,58779,59882,60087,61825,63104,64963,66902,66443,67061,67273,67372,68679,69995,71522,73292,73932,75826,76954,78105,78439,79892,82631];
% Baidu Zhidao
% http://zhidao.baidu.com/question/198731054514100005.html?push=keyword
end

如果起始点不正确,则会发生一些错误…

我没有CurveFit工具箱,但假设在选择曲线类型的位置附近有一个“拟合选项”按钮,会出现一个对话框,您可以在其中选择自己的初始点

我还建议您尝试一下,这可以利用y对6个参数中的3个参数具有线性依赖性这一事实。您还只需要提供非线性参数的初始猜测
NLPstart=[c,d,f]

funlist={1, @(p,x) cos(p(1)*x+p(2)), @(p,x) exp(p(3)*x)};

[cdf,abe] = fminspleas(funlist,NLPstart,x,y);

c=cdf(1); d=cdf(2); f=cdf(3);
a=abe(1); b=abe(2); e=abe(3);

请包含您尝试使用
cftool
时的代码,并解释错误所在。您可以编辑您的问题以添加此信息。thx!因此,可以计算线性参数,但如何提供非线性参数的初始猜测?我知道如果我只拟合
f(x)=e*exp(f*x)
,就可以很容易地计算出f;但是如何得到关于c和d的初始猜测呢?如果指数项是衰减的,那么前两项渐近地服从模型。也许将数据的尾部输入cftool,以便仅半准确地估计正弦部分?