Fitting data with custom equation

조회 수: 164 (최근 30일)
Francesco Panico
Francesco Panico 2018년 12월 9일
댓글: Zeyu Ma 2023년 8월 31일
Hi,
I have some datas to fit with by the following equation : d*atan(b*(x+a))+c . I've done it by the " curve fitting tool" how I can incorporate this process in a matlab script ? I'd like to obtain the parameter a,b,c,d as an answer.
Thanks.
math.png

채택된 답변

Stephan
Stephan 2018년 12월 9일
편집: Stephan 2018년 12월 9일
Hi,
you can use this code:
% Some data - replace it with yours (its from an earlier project)
x = [177600,961200, 2504000, 4997000, 8884000]';
y = [6.754, 24.416, 58.622, 107.980, 154.507]';
% Define Start points, fit-function and fit curve
x0 = [1 1 1 1];
fitfun = fittype( @(a,b,c,d,x) d*atan(b*(x+a))+c );
[fitted_curve,gof] = fit(x,y,fitfun,'StartPoint',x0)
% Save the coeffiecient values for a,b,c and d in a vector
coeffvals = coeffvalues(fitted_curve);
% Plot results
scatter(x, y, 'r+')
hold on
plot(x,fitted_curve(x))
hold off
Due to, that this code is taken from an older project and the values dont represent your function the result here is a very bad fit - but to show how it works with a custom function inside a script it should be good enough.
Best regards
Stephan
  댓글 수: 5
Torsten
Torsten 2023년 8월 19일
편집: Torsten 2023년 8월 19일
You can insert the point in the custom equation thereby reducing the number of unknown parameters:
d*atan(b*(0+a)) + c = -0.18
-> d*atan(b*a) + c = -0.18
-> c = -0.18 - d*atan(b*a)
Thus the modified custom equation to be fitted reads:
d*(atan(b*(x+a)) - atan(b*a)) - 0.18
which now has only 3 instead of 4 unknown parameters.
Zeyu Ma
Zeyu Ma 2023년 8월 31일
Hi there, I wonder how to restrict the values of the parameters.
For example, force parameter a to be 0<a<1.
Thank you!

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

추가 답변 (1개)

Valrhona
Valrhona 2020년 9월 28일
I have a question regarding the curve fitting tool itself.
How can I change the z=f(x,y) to f(x).
I cannot change the x, y, or z data in this curve fitting tool.
I would like to use a custom f(x) function (not f(x,y))
Many thanks!
  댓글 수: 2
Koral Villalobos
Koral Villalobos 2021년 4월 27일
I need help with this too
sandeep
sandeep 2022년 8월 3일
is there a way to find the appropriate intial guess?

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

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by