필터 지우기
필터 지우기

Matlab fitting an anonymous function

조회 수: 42 (최근 30일)
Tim
Tim 2014년 4월 14일
편집: Matt J 2021년 10월 13일
I have fitted my data using the matlab build in fitting function, such as 'exp2'. Since those build in functions did not work perfectly for all of my data, I tried using anonymous functions to fit my data. I could not get good results, so i tried to add the 'exp2' function as an anonymous function and compare it to the original build in 'exp2' function:
xdata = [5 10 20 40 80 100 150 200 250];
ydata = [2 6 12 20 30 40 50 70 100];
% My function
fitfun = @(a,b,c,d,x) a*exp(b*x) + c*exp(d*x);
[f,goodness] = fit( xdata, ydata, fitfun, fitoptions('exp2'))
coeffs = coeffvalues(f)
In combination with:
% Exponential function
[f2,goodness] = fit( xdata, ydata, 'exp2')
coeffs = coeffvalues(f2)
The results were almost always very different and the fit of the anonymous function was almost always very crappy. The fit with the build in 'exp2' function was fine.
So I want to know:
  • 1) What is the essential different between my anonymous 'exp2' function and the build in 'exp2' function
  • 2) What do I have to do to get good fitting values with my anonymous functions? Not only this one, but also other functions I want to try.
  댓글 수: 2
Royi Avital
Royi Avital 2014년 4월 14일
What do you mean "he results were almost always very different"?
Given a vectors the algorithm should be deterministic.
Tim
Tim 2014년 6월 12일
Ah! I almost forgot about my question. So, I basically get a random fit if I do not provide my own a start point. This fit can look very similar to the build in one, but it could also be one big mess. I want to know why this difference occurs, since I use the same fitoptions and fit-function as the build in 'exp2' fit.

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

채택된 답변

Matt J
Matt J 2021년 10월 13일
편집: Matt J 2021년 10월 13일
The difference is that, with an anonymous function, you now have the responsibility of providing the StartPoint for the iterative search in the fitoptions. If you don't, then fit() will interpret the model as a custom model and default to a random StartPoint (which would explain why the fits you're getting are so poor). See also,
  댓글 수: 2
Tim
Tim 2021년 10월 13일
This is probably it. FYI, everytime you edit your answer, I get an email notification.
Matt J
Matt J 2021년 10월 13일
편집: Matt J 2021년 10월 13일
You should probably set your email notification frequency to "Low".

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

추가 답변 (1개)

Julia Gala
Julia Gala 2021년 10월 13일
Hi Tim,
I just came accross this, it has been years, so you may never check this. In your question, you don't share the fitoptions you are using for your fit. I believe the main differences will be in the fitoptions here, and that is likely why you are getting different fitting results with the same definition of functions. If you are still interested on this, I would run the anonymous function vs the built in function with the same fitting options (add fitting options to the built in function). Feel free to share what you get.
Sincerely,
Julia

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by