fit function does not work correctly

조회 수: 94 (최근 30일)
Yoann Paul Daniel Félix Lafore
Yoann Paul Daniel Félix Lafore 2021년 10월 11일
편집: Matt J 2021년 10월 11일
I wanted to test a custom fit with a simple function ( cos(3.5t) ) and so I made the following code. But when I plot, the resultant fit doesn't follow the function at all.
t = [0:0.01:6]';
%Test with a cosinus function
y = cos(t*3.5);
%Create the fit type
fitmodel = @(a, x) cos(a.*x);
%Fit the curve
fitted = fit(t, y, fitmodel, 'TolX', 1E-15);
Warning: Start point not provided, choosing random start point.
%Plot the curve and the fit
figure
plot(t, y, 'b.')
hold on
plot(t, fitted(t), 'r-')
hold off
  댓글 수: 1
Mathieu NOE
Mathieu NOE 2021년 10월 11일
hi
look at the warning
Warning: Start point not provided, choosing random start point.
some fitting functions are (very) sensitive to initial conditions . you should be able to give a better start point

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2021년 10월 11일
편집: John D'Errico 2021년 10월 11일
I wrote a long question (and answer) just yesterday, that goes into depth on the why and how to solve this.
The point is, that you need to provide better starting values for the solver.
When you alow it to use its own (random) start point, for this cleass of model, you will often find a poor fit results.
t = [0:0.01:6]';
%Test with a cosinus function
y = cos(t*3.5);
%Create the fit type
fitmodel = @(a, x) cos(a.*x);
%Fit the curve
fitted = fit(t, y, fitmodel, 'TolX', 1E-15)
Warning: Start point not provided, choosing random start point.
fitted =
General model: fitted(x) = cos(a.*x) Coefficients (with 95% confidence bounds): a = 0.349 (0.325, 0.3729)
Here, a is estimated as less than 1, clearly a poor estimate. Even if I provide a start point of 2 for a, it gets it wrong.
fitted = fit(t, y, fitmodel, 'TolX', 1E-15, 'start',2)
fitted =
General model: fitted(x) = cos(a.*x) Coefficients (with 95% confidence bounds): a = 2.132 (2.1, 2.163)
try a little larger?
fitted = fit(t, y, fitmodel, 'TolX', 1E-15, 'start',3)
fitted =
General model: fitted(x) = cos(a.*x) Coefficients (with 95% confidence bounds): a = 3.5 (3.5, 3.5)
And that did it. I needed to start the solver out inside the basin of attraction before it will find the global solution.
For much more depth on the concepts here, read the question and answers I posted in that link.

추가 답변 (2개)

Chunru
Chunru 2021년 10월 11일
If the StartPoint is good enough, you can get the correct fitting.
t = [0:0.01:6]';
%Test with a cosinus function
y = cos(t*3.5);
%Create the fit type
fitmodel = @(a, x) cos(a.*x);
%Fit the curve
fitted = fit(t, y, fitmodel, 'TolX', 1E-15, 'StartPoint', 3);
%Plot the curve and the fit
figure
plot(t, y, 'b.')
hold on
plot(t, fitted(t), 'r-')
hold off

Matt J
Matt J 2021년 10월 11일
편집: Matt J 2021년 10월 11일
You shouldn't use a custom model when one is unnecessary. It is an important advantage to use one of the built-in models when possible, because the fitting algorithm can do smarter things, including the automatic generation of a StartPoint. Here, you can use 'sin1' or 'fourier1' with appropriate bounds:
t = [0:0.01:6]';
%Test with a cosinus function
y = cos(t*3.5);
opts=fitoptions('sin1','Lower',[1 -inf, pi/2],'Upper',[1 +inf pi/2]);
%Fit the curve
fitted = fit(t, y, 'sin1',opts);
%Plot the curve and the fit
figure
plot(t, y, 'b.')
hold on
plot(t, fitted(t), 'r-')
hold off

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by