Is there a way to specify a constant using the fit() function?

41 Ansichten (letzte 30 Tage)
Audrey
Audrey am 5 Feb. 2015
Kommentiert: Caio Vaz Rimoli am 28 Nov. 2018
I am using the fit() function with a 'fourier4' model. Is there a way I can specify one of the parameters as a constant rather than allowing it to be fit? Specifically, I would like to set the frequency (ie. fundamental period) to a known value and/or specify the offset (a0) as 0 since the mean is already subtracted from the dataset. Thank you in advance!
  1 Kommentar
Caio Vaz Rimoli
Caio Vaz Rimoli am 28 Nov. 2018
Well, one way to go is to set the Upper and the Lower bounds with the same value, the value of your constant ( k ).
For example, gauss2 (which is for 2 gaussian peaks):
% General model Gauss2:
% f(x) = a1*exp(-((x-b1)/c1)^2) + a2*exp(-((x-b2)/c2)^2)
k = 0 %%% k is my constant, let's say it is zero
fOps = fitoptions('gauss2');
fOps.Lower=[a1, b1, c1, k, b2, c2]; %%%% lower bounds
fOps.Upper=[A1, B1, C1, k, B2, C2]; %%%% upper bounds
f = fit(x,y,'gauss2',fOps)
Thus the amplitude of the second gaussian is set zero, and the other parameters are free.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Audrey
Audrey am 9 Feb. 2015
I posed this question directly to Matlab support and they came back with the following answer, which, paraphrased, says, "No, you can not specify parameters as constants with built-in model options."
Copied from email:
I understand that you would like to use "fit" function with custom "fourier4" model. You can supply a custom "fit" function to get the required type of model.
To fit a custom model, you can use a MATLAB expression, a cell array of linear model terms, an anonymous function or create a fittype using "fittype" function and use this fittype as a parameter to "fit" function.
Please take a look at the following example to fit a custom model using an anonymous function: http://www.mathworks.com/help/curvefit/fit.html#bto2vuv-11
You can also take a look at the following examples on creation of custom linear and non-linear models: http://www.mathworks.com/help/curvefit/fittype.html

Weitere Antworten (4)

Julia Gala
Julia Gala am 17 Jul. 2018
Easy but maybe a bit dodgy way to do it: remember you can specify both the Lower and Upper limits of your parameters! Just force the lower and upper of that parameter to be the same and to be the value of your constant and thus you can fix it. You can redefine your anonymous function, of course, but it is something annoying to do when you are still playing with the data :)

Tom Lane
Tom Lane am 9 Feb. 2015
The answer you got is correct, so here's how you might do it:
% Get some data
x = sort(10*rand(100,1));
y = 0 + 1*cos(x/2) + 1*sin(x/2) + 2*cos(x) - 3*sin(x) + randn(100,1);
% Suppose we know the first 0 and 1 coefficients. First fit a fourier model.
f1 = fit(x,y,'fourier2')
% Use the results from that as starting values. In the function below
% we omit the constant term and we subtract the known part
fit(x,y-cos(x/2), 'a*sin(x*w) + b*cos(2*x*w) + c*sin(2*x*w)','start',[f1.b1,f1.a2,f1.b2,f1.w])
However, your problem is easier. If you know the frequency, then you just transform the problem into linear regression.
[cos(.5*x),sin(.5*x),cos(x),sin(x)]\y
You can fit this with backslash as above, or with various methods from the Curve Fitting or Statistics Toolbox if you want more statistical results.

Cody Tishchler
Cody Tishchler am 19 Jun. 2018
In case anyone comes across this I had to devise a work around for my application. Since literals will be accepted as constants simply convert the desired constant into a string with num2str and strcat it into the model. Then pass it all as one string into the fit function. Example:
const = num2str(6.283185307*wave_vector/box);
model = strcat('a*sin(', const,'*x+b)');
f = fit(x_data,y_data,model,'StartPoint',[1,0]);

Steven Lord
Steven Lord am 17 Jul. 2018
If you build your own fittype object with a custom equation to be fitted, use the 'problem' parameter to specify that a particular variable in the equation to be fitted should be constant. When you call fit with your fittype you need to specify a value for that 'problem' parameter. Alternately you can specify your equation to be fitted as an anonymous function.
See the last two examples on the fittype documentation page for illustrations of those two techniques.

Kategorien

Mehr zu Linear and Nonlinear Regression 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