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

채택된 답변

Dr. Seis
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
Jonathan
Jonathan 2011년 10월 16일
Thank you Elige for putting this together for me, and sorry that I didn't thank you earlier. My first shot at using it vastly overestimated the amplitude, but I think I can work with a few things and improve it.
Thanks,
Jonathan
Dr. Seis
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 CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by