Constant term in fittype
5 views (last 30 days)
Show older comments
Hello, i want to fit some data with the funktion: y= S0/2+S1/2*cos(2*x)+S2/2*sin(2*x) while i know the value of S0 and the values of S1 and S2 should be fitted by matlab.
So my current matlab code is
poincarefit=fittype({'S0/2+S1/2*cos(2*x)+S2/2*sin(2*x)'},...
'problem','S0');
f = fit(Theta, I, poincarefit,'problem',S0);
But that throws me the error
Expression a*(S0/2+S1/2*cos(2*x)+S2/2*sin(2*x)) is not a valid MATLAB expression, has non-scalar
coefficients, or cannot be evaluated:
Error in fittype linear term ==> (S0./2+S1./2.*cos(2.*x)+S2./2.*sin(2.*x))
??? Undefined function or variable 'S1'.
end
so i tried to implement the coefficients like this:
poincarefit=fittype({'S0/2+S1/2*cos(2*x)+S2/2*sin(2*x)'},...
'coefficients',{'S1','S2'},'problem','S0');
f = fit(Theta, I, poincarefit,'problem',S0);
but the error massage
Number of coefficients must match the number of linear terms.
occures...
can you maybe help me?
thanks in advance
0 Comments
Answers (1)
Ganesan S
on 27 Dec 2018
Dear Sir,
can you try this?
x=0:.1:2*pi; % Whatever your x data or time
y= cos(2*x)+sin(2*x); % Whatever your y data
[xData, yData] = prepareCurveData( x, y);
S0=1; % you said you know this number
eqn = @(S1,S2,x) S0/2+S1/2*cos(2*x)+S2/2*sin(2*x); % a different format instead of typing the equation in fittype
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.StartPoint = [1 1];%starting guess for S1 and S2
[fitresult, gof] = fit( xData, yData, eqn, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. x', 'untitled fit 1', 'Location', 'NorthEast' );
% Label axes
xlabel x
ylabel y
grid on
I hope this helps... I am really not sure it helps you as you have posted this question in the year 2015 but now it is end of 2018. I answered this because this answer is also needed for me! When I searched for an answer to this, it is looking very strange that nobody had answered your query. All the best!
Yours sincerely,
S.Ganesan.
0 Comments
See Also
Categories
Find more on Fit Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!