简体   繁体   中英

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.

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. Ideally the fitting should return values for a, b, c, v, and p which describe the temperature data

I am attempting to fit daily maximum temperatures, a vector called TMAX.

So far I have:

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

Where fitFn is defined in a script as

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

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. Any help would be greatly appreciated.

Thanks for reading

Your fitFn is very wrong. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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