How can I use parameters from workspace in custom equation using fittype function?

조회 수: 4 (최근 30일)
I'd like to write the coefficient, in the custom equation, in literal form, keeping A as the solution of the curve fitting
function A = coffin_coefficient(X,Y)
[xData, yData] = prepareCurveData( X, Y );
ft = fittype( 'A*x^(-6.9397)*exp(0.8084/((8.6173e-05)*(120+273.15)))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Levenberg-Marquardt';
opts.Display = 'Off';
opts.StartPoint = 0.1;

답변 (1개)

Guillaume Le Goc
Guillaume Le Goc 2020년 4월 9일
If you want to fit y(x) to find the parameter A, now you have set the fittype and fitoptions for the fit, you just need to perform the fit :
f = fit(xData, yData, ft, opts);
And for your function to return the found A, you can access it with :
A = f.A;
You might want to get the 95% confidence interval for the value :
ci = confint(f);
  댓글 수: 3
Carmelo Barbagallo
Carmelo Barbagallo 2020년 4월 9일
!!! I DID IT !!!
I have used "eval" function
function A = coffin_coefficient(deltaT,Nf,n,Ea,kb,Tm)
[xData, yData] = prepareCurveData( deltaT, Nf );
eval(['ft = fittype( ''A*x^(-',num2str(n),')*'...
'exp(',num2str(Ea),'/((',num2str(kb),')*(',num2str(Tm),'+273.15)))'','...
' ''independent'', ''x'', ''dependent'', ''y'' );'])
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Levenberg-Marquardt';
opts.Display = 'Off';
opts.StartPoint = 0.1;
Probably there are better solution, but it seems to work.
However I am interested in a simpler solution
Guillaume Le Goc
Guillaume Le Goc 2020년 4월 9일
편집: Guillaume Le Goc 2020년 4월 9일
Sorry, I did not understand what your problem was.
I generally read that the eval function is generally not recommended, especially here where it is of no use.
Since your variables n, Ea... are defined in the workspace from which you call your coffin_coefficient function, you can simply pass them as input arguments in your function, as you did. Now, those variables exist within the function, and you can simply define your fitting function normally as you did in the first place, replacing numerical values by their variable names.
In fact,
eval(['ft=a+b+c;']);
does the same thing than
ft=a+b+c;
but is way slower.
In short, now that you pass all the variables to your function, you can use them as you would in a script/command window, ie. using the workspace you have access to.

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

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by