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

4 vues (au cours des 30 derniers jours)
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;

Réponses (1)

Guillaume Le Goc
Guillaume Le Goc le 9 Avr 2020
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 commentaires
Carmelo Barbagallo
Carmelo Barbagallo le 9 Avr 2020
!!! 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 le 9 Avr 2020
Modifié(e) : Guillaume Le Goc le 9 Avr 2020
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by