Fitting a sin function to a sparse dataset with a known parameter.

6 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Dr. Seis
Dr. Seis am 10 Okt. 2011
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 Kommentare
Jonathan
Jonathan am 16 Okt. 2011
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 am 17 Okt. 2011
That stinks!
Can you share a set of your points so I can look into it as well?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by