global datain

%load datain.txt 
%load the saved data file

init_value = [3;3.75*2*pi;1.57;0];
%choose an initial set of guess values for the function parameters
%[amplitude;frequency(Hertz*2*pi);phase;DC offset]

amplitude_guess=init_value(1)
frequency_guess=init_value(2) %Guesses
phase_guess=init_value(3)
dc_guess=init_value(4)

fcn_parameters = fminsearch('finderror',init_value);
%Get the parameters from fmins

amplitude_fit = fcn_parameters(1,1)
frequency_fit = fcn_parameters(2,1) %Fitted data
phase_fit = fcn_parameters(3,1)
dc_fit = fcn_parameters(4,1)


ycheck = amplitude_fit*sin(frequency_fit*datain(:,1) + phase_fit)+dc_fit; 
%Get the values from the set of parameters
%that fmins gives you

%Final plot of fitted curve
plot(datain(:,1),datain(:,2));
hold on
plot(datain(:,1),ycheck,'r')