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;
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!