필터 지우기
필터 지우기

Constant term in fittype

조회 수: 11 (최근 30일)
Matlab Gounad
Matlab Gounad 2015년 7월 14일
답변: Ganesan S 2018년 12월 27일
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

답변 (1개)

Ganesan S
Ganesan S 2018년 12월 27일
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.

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by