Fitting a sin function to a sparse dataset with a known parameter.
4 次查看(过去 30 天)
显示 更早的评论
Question #4 in the last few days... Thank you all for the help so far!
I have a sparse dataset (4 points, soon 5) of an environmental variable whose concentration changes as a function of time of year, and whose shape I know approximates a sin wave, i.e.
var(t) = a1 * (sin(2*pi/365days)*t+b1)+c1
I know the frequency of the function, but I don't know the amplitude (a1), phase offset (b1) or offset in y (c1), so I'd like to fit a sin function to it.
Question: Is there a function that will allow me to specify a given known parameter (frequency) and that will solve for the remaining unknowns (amp, phase, y offset)? I've tried using cftool, but as far as I can tell, it doesn't allow me to specify frequency. And I've started to try to use the fit function, but I'm having a hard time sorting out how to format my input variables.
TIA, Jonathan
0 个评论
采纳的回答
Dr. Seis
2011-10-10
Having only a few data points to work with is a little worrisome, but something like this may work:
function estimates = fitsinewave(xdata, ydata, start_point)
% Call fminsearch with a starting point.
model = @sinefun;
options = optimset('MaxFunEvals',3000);
estimates = fminsearch(model, start_point, options);
% sinefun accepts curve parameters as inputs, and outputs sse,
% the sum of squares error for A*sin(2*pi/365*xdata-B)+C-ydata.
function sse = sinefun(params)
A = params(1);
B = params(2);
C = params(3);
ErrorVector = A*sin(2*pi/365*xdata-B)+C - ydata;
sse = sum(ErrorVector .^ 2);
return
end
return
end
Here is an example to test it out:
A = 3;
B = 3*pi/4;
C = -1.5;
xdata = sort(rand(1,5)*365);
ydata = A*sin(2*pi/365*xdata-B)+C;
start_point = [0, 0, 0]; % Zeros unless you have a good guess for A, B, and C
ABCestimates = fitsinewave(xdata, ydata, starting_point)
ABCestimates =
3.0001 2.3562 -1.5000
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!