I have a 1000×2 data file that I’m using for this problem.
I am supposed to fit the data with Acos(wt + phi). t is time, which is the first column in the data file, i.e. the independent variable. I need to find the fit parameters (A, f, and phi) and their uncertainties.
My code is as follows:
%load initial data file
data = load('hw_fit_cos_problem.dat');
t = data(:,1); %1st column is t (time)
x = t;
y = data(:,2); %2nd column is y (signal strength)
%define fitting function
f = fittype('A*cos(w*x + p)','coefficients','A','problem',{'w','p'});
% check fit parameters
coeffs = coeffnames(f);
%fit data
[A] = fit(x,y,f)
disp('confidence interval/errorbars');
ci = confint(A)
which yields 4 different error messages that I don’t understand.
Error Messages:
Error using fit>iAssertNumProblemParameters (line 1113)
Missing problem parameters. Specify the values as a cell array with one element for each problem parameter
in the fittype.
Error in fit>iFit (line 198)
iAssertNumProblemParameters( probparams, probnames( model ) );
Error in fit (line 109)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, …
Error in problem2 (line 14)
[A] = fit(x,y,f)
The line of code
specifies
Aas a “coefficient” in the model, and the valueswandpas “problem” parameters.Thus, the fitting toolbox expects that you will provide some more information about
wandp, and then it will varyA. When no further information aboutwandpwas provided to the fitting tool, that resulted in an error.I am not sure of the goal of this project, or why
wandpwere designated as problem parameters. However, one simple solution is to allow the toolbox to treatA,w, andpas “coefficients”, as follows:In this case, the code will not throw an error, and will return 95% confidence intervals on
A,w, andp.I hope that helps.