MATLAB: Fitting Curve To Temperature Data

curve fittingpolyfit

I am trying to fit a curve to temperature data taken over over a period of 48 hours. Looking at this data it seems to osculate similar to a sin function, but I am unsure how to fit a sinus to to this data. I have tried using the polyfit function, but none of those can give me an oscillating function. I have attempted to use a sine curve fitting code found here on MATLAB answers, but have ran into errors. I have attached the data sets I am using. Here is the code I am attempting to use:
yu = max(temp1);
yl = min(temp1);
yr = (yu-yl); % Range of ‘y’
yz = temp1-yu+(yr/2);
zx = time_hours(yz .* circshift(yz,[0 1]) <= 0); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(temp1); % Estimate offset
fit = @(b,time_hours) b(1).*(sin(2*pi*time_hours./b(2) + 2*pi/b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,time_hours) - temp1).^2); % Least-Squares cost function
options = optimset('MaxFunEvals',100000,'MaxIter',100000);
s = fminsearch(fcn, [yr; per; -1; ym],options) % Minimise Least-Squares
xp = linspace(min(time_hours),max(time_hours));
figure(1)
plot(time_hours,temp1,'b', xp,fit(s,xp), 'r')
grid
I keep getting Nan for s(3) no matter how high I increase MaxFunEvals. The only toolbox I have at my disposal is the Symbolic Math Toolbox. Any suggestions to finding a good fitting oscillating function would be greatly appreciated! Thanks.

Best Answer

You have several options, depending on what you want to do. One is: Curve fitting to a sinusoidal function (link), another is the interpft (link) function.