using fit function,Error using fittype>iD​educeCoeff​icients

How do I get a string properly input into a fit function?
I'm trying to fit the wave equation to find the amp and phase for each row of 'zs'-x and 'data'-y (both 401x20). I have input a string that will vary with the changing frequency ('wn', 401x1) for each row of 'zs'. I get the following error:
---------------------------------------------------------------
Error using fittype>iDeduceCoefficients (line 621)
The independent variable x does not appear in the equation expression.
Use x in the expression or indicate another variable as the independent variable.
Error in fittype>iCreateCustomFittype (line 477)
obj = iDeduceCoefficients(obj);
Error in fittype>iCreateFittype (line 353)
obj = iCreateCustomFittype( obj, varargin{:} );
Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );
Error in fit>iFit (line 165)
model = fittype( fittypeobj, 'numindep', size( xdatain, 2 ) );
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in z_fit (line 11)
ft = fit(z1(:), ampRow(:), 'a * sin(',num2str(wn),' * x + c)', 'StartPoint', [5, 0], 'Lower',[0,-Inf],
'Upper',[Inf,Inf]);
------------------------------------------
Below is the code:
n = length(zs);
for i = 1:n
ampRow = data(i,:);
z1 = zs(i,:);
wn = 2.*pi.*Freq(:)./299.79;
ft = fit(z1(:), ampRow(:), 'a * sin(',num2str(wn),' * x + c)', 'StartPoint', [5, 0], 'Lower',[0,-Inf], 'Upper',[Inf,Inf]);
vals = coeffvalues(ft);
ampFixedFreq_nd(i) = vals(1);
phaseFixedFreq_nd(i) = vals(2);
end

1 Kommentar

Nevermind It was solved...just needed brakets around the eqaution

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 24 Sep. 2019

1 Stimme

Rather than building the equation using the text form of your problem parameter, I recommend creating a fittype with a 'problem' parameter and specifying a value for that 'problem' parameter when you call fit. See the "Create Fit Options and Fit Type Before Fitting" example on the documentation page for the fit function for an illustration of this technique. The n parameter from the example fills the same role as the wn parameter in your equation.

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 24 Sep. 2019

Beantwortet:

am 24 Sep. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by