简体   繁体   English

Matlab将数据拟合到函数,其中函数是求和和周期性语法

[英]Matlab fitting data to a function, where the function is a summation and periodic, syntax

I am attempting to fit temperature data over the last 50 years for the US using the lsqcurvefit, but I am concerned that my fitting function has bad syntax. 我正在尝试使用lsqcurvefit拟合美国过去50年的温度数据,但是我担心我的拟合函数语法错误。

The fitting function itself is of the form: 拟合函数本身具有以下形式:

∑(ai+bi*t+ci*t^2)*(cos(vt+p))

Summing from i=1 to N. I would like t to correspond to the time vector called TIME. 从i = 1到N的总和。我希望t对应于称为TIME的时间向量。 Ideally the fitting should return values for a, b, c, v, and p which describe the temperature data 理想情况下,拟合应返回描述温度数据的a,b,c,v和p值

I am attempting to fit daily maximum temperatures, a vector called TMAX. 我正在尝试拟合称为TMAX的每日最高温度。

So far I have: 到目前为止,我有:

lsqcurvefit(fitFn(1,2,TIME),5,TIME,TMAX)

Where fitFn is defined in a script as 在脚本中将fitFn定义为

function f = fitFn(i,N,t)
f=0;
for i=1:N
    f =@(i,N,a,b,c,v,p,t) f + (a+b*t+c*t^2)*(cos(v*t+p));
end

However whenever I run lsqcurvefit I get the error 但是,每当我运行lsqcurvefit时,我都会收到错误消息

Error using
fitFn>@(i,N,a,b,c,v,p,t)f+(a+b*t+c*t^2)*(cos(v*t+p)) (line
4)
Not enough input arguments.

Error in lsqcurvefit (line 199)
            initVals.F =
            feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});

Caused by:
    Failure in initial user-supplied objective function
    evaluation. LSQCURVEFIT cannot continue.

I am sure I'm making a simple error but I'm somewhat at a loss. 我确定我犯了一个简单的错误,但我有些茫然。 I'm new to MatLab and don't quite understand how to use anonymous functions. 我是MatLab的新手,不太了解如何使用匿名函数。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks for reading 谢谢阅读

Your fitFn is very wrong. 您的fitFn非常错误。 I think this is what you want: 我认为这是您想要的:

f = @(x,t) (x(1)+x(2)*t+x(3)*t.^2)*cos(x(4)*t+x(5))
lsqcurvefit(f,zeros(1,5),TIME,TMAX)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM