Curve Fitting Using External Function

Using the Curve Fitting Toolbox, is it possible to put the fitting function outside of the fittype? A general example of the way I want this to be is:
FitOpt=fitoptions('Method', 'NonlinearLeastSquares','Lower',0.1*Y00, 'Upper',10*Y00,'StartPoint',Y00);
XYFit=fittype(MyFitFunction(a,X),'dependent', {'y'}, 'independent' , {'x'},'coefficient', {'E'});
f1 = fit(x0,y0,XYFit,FitOpt)
.
.
.
function F0=MyFitFunction(a,x)
F0=a*x.^2-1./x);
end
Where I would like MyFitFunction to be outside the fittype definition, but when I try this I get this error message:
Unrecognized function or variable 'a'
If I take the function into the fittype and write:
XYFit=fittype(@(a,X) a*x.^2-1./x),'dependent', {'y'}, 'independent' , {'x'},'coefficient', {'E'});
then it will work, but this is not the form that I want.
How is this possible?

Antworten (1)

Image Analyst
Image Analyst am 7 Okt. 2021

0 Stimmen

Did you try
XYFit=fittype(@MyFitFunction(a,X),..........................

4 Kommentare

Saeid
Saeid am 7 Okt. 2021
If I do that, I get this error message:
File: FitElong.m Line: 8 Column: 33
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Sorry I don't have that toolbox so I can't try anything. Maybe try
XYFit=fittype(@MyFitFunction,..........................
Saeid
Saeid am 18 Okt. 2021
Hi,
whenI do that, I see a new error message:
Error using fittype>iCreateFittype (line 362)
First input argument must be a string scalar or character vector, cell array or anonymous function.
Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );
Image Analyst
Image Analyst am 18 Okt. 2021
Call tech support because I don't have that function.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Gefragt:

am 6 Okt. 2021

Kommentiert:

am 18 Okt. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by