Fitting a sin function to a sparse dataset with a known parameter.
6 ビュー (過去 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 件のコメント
Dr. Seis
2011 年 10 月 17 日
That stinks!
Can you share a set of your points so I can look into it as well?
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Interpolation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!