Azzera filtri
Azzera filtri

Calling a custom function in fittype()

114 visualizzazioni (ultimi 30 giorni)
Oisín Conway
Oisín Conway il 7 Ott 2022
Commentato: Oisín Conway il 7 Ott 2022
Does anyone know if one can define a custom function and then use fittype() with the custom function rather than the custom equation?

Risposte (1)

John D'Errico
John D'Errico il 7 Ott 2022
Modificato: John D'Errico il 7 Ott 2022
Easy peasy.
ft = fittype('a + b*exp(c*x)','indep','x')
ft =
General model: ft(a,b,c,x) = a + b*exp(c*x)
You can now use this as you wish.
Oh wait. Maybe what you are asking is if you can define a function, perhaps as an anonymous function, and then use fittype wih it?
help fittype
FITTYPE Fittype for curve and surface fitting A FITTYPE encapsulates information describing a model. To create a fit, you need data, a FITTYPE, and (optionally) fit options and an exclusion rule. LIBRARY MODELS FITTYPE(LIBNAME) constructs a FITTYPE for the library model specified by LIBNAME. Choices for LIBNAME include: LIBNAME DESCRIPTION 'poly1' Linear polynomial curve 'poly11' Linear polynomial surface 'poly2' Quadratic polynomial curve 'linearinterp' Piecewise linear interpolation 'cubicinterp' Piecewise cubic interpolation 'smoothingspline' Smoothing spline (curve) 'lowess' Local linear regression (surface) or any of the names of library models described in "List of Library Models for Curve and Surface Fitting" in the documentation. CUSTOM MODELS FITTYPE(EXPR) constructs a FITTYPE from the MATLAB expression contained in the string, cell array or anonymous function EXPR. The FITTYPE automatically determines input arguments by searching EXPR for variable names (see SYMVAR). In this case, the FITTYPE assumes 'x' is the independent variable, 'y' is the dependent variable, and all other variables are coefficients of the model. If no variable exists, 'x' is used. All coefficients must be scalars. You cannot use the following coefficient names in the expression string EXPR: i, j, pi, inf, nan, eps. If EXPR is a string or anonymous function, then the toolbox uses a nonlinear fitting algorithm to fit the model to data. To use a linear fitting algorithm, use a cell array of terms. ANONYMOUS FUNCTIONS If EXPR is an anonymous function, then the order of inputs must be correct. The input order enables the FITTYPE class to determine which inputs are coefficients to estimate, problem-dependent parameters and independent variables. The order of the input arguments to the anonymous function must be: EXPR = @(<coefficients>, <problem parameters>, <x>, <y>) expression There must be at least one coefficient. The problem parameters and y are optional. The last arguments, x and y, represent the independent variables: just x for curves, but x and y for surfaces. If you don't want to use x and/or y as the names of the independent variables, then you can specify different names by using the 'independent' property name/value pair. However, whatever name or names you choose, these arguments must be the last arguments to the anonymous function. Anonymous functions make it easier to pass other data into the fittype and fit functions. For example, to create a fittype using an anonymous function and variables xs and ys from the workspace: % Variables in workspace xs = (0:0.1:1).'; ys = [0; 0; 0.04; 0.1; 0.2; 0.5; 0.8; 0.9; 0.96; 1; 1]; % Create fittype ft = fittype( @(b, h, x) interp1( xs, b+h*ys, x, 'pchip' ) ) % Load some data xdata = [0.012;0.054;0.13;0.16;0.31;0.34;0.47;0.53;0.53;... 0.57;0.78;0.79;0.93]; ydata = [0.78;0.87;1;1.1;0.96;0.88;0.56;0.5;0.5;0.5;0.63;... 0.62;0.39]; % Fit the curve to the data f = fit( xdata, ydata, ft, 'Start', [0, 1] ) LINEAR MODELS To use a linear fitting algorithm specify EXPR as a cell array of terms. That is, to specify a linear model of the following form: coeff1 * term1 + coeff2 * term2 + coeff3 * term3 + ... (where no coefficient appears within any of term1, term2, etc) use a cell array where each term, without coefficients, is specified in a cell of EXPR, as follows: EXPR = {'term1', 'term2', 'term3', ... } For example, the model a*x + b*sin(x) + c is linear in 'a', 'b' and 'c'. It has three terms 'x', 'sin(x)' and '1' (since c=c*1) and so EXPR is EXPR = {'x','sin(x)','1'} ADDITIONAL PROPERTIES FITTYPE(EXPR,PROP1,VALUE1,PROP2,VALUE2,....) uses the property name/value pairs PROP1-VALUE1, PROP2-VALUE2 to specify property values other than the default values. PROPERTY DESCRIPTION 'independent' Specifies the independent variable name 'dependent' Specifies the dependent variable name 'coefficients' Specifies the coefficient names (in a cell array if there are two or more). Note excluded names above. 'problem' Specifies the problem-dependent (constant) names (in a cell array if there are two or more) 'options' Specifies the default 'FITOPTIONS' for this equation Defaults: The independent variable is x. The dependent variable is y. There are no problem dependent variables. Everything else is a coefficient name. Multi-character symbol names may be used. EXAMPLES g = fittype('a*x^2+b*x+c') g = fittype('a*x^2+b*x+c','coeff',{'a','b','c'}) g = fittype('a*time^2+b*time+c','indep','time') g = fittype('a*time^2+b*time+c','indep','time','depen','height') g = fittype('a*x+n*b','problem','n') g = fittype({'cos(x)','1'}) % linear g = fittype({'cos(x)','1'}, 'coefficients', {'a','b'}) % linear g = fittype( @(a,b,c,x) a*x.^2+b*x+c ) g = fittype( @(a,b,c,d,x,y) a*x.^2+b*x+c*exp(-(y-d).^2), ... 'independent', {'x', 'y'}, ... 'dependent', 'z' ); % for fitting surfaces See also fit, fitoptions. Documentation for fittype doc fittype
You can see in there how to use fittype with an anonymous function. For example:
fun = @(a,b,c,x) a + b*exp(c*x);
ft2 = fittype(fun,'indep','x')
ft2 =
General model: ft2(a,b,c,x) = a+b*exp(c*x)
I think this is what you are asking.
  5 Commenti
John D'Errico
John D'Errico il 7 Ott 2022
Do you find it odd that in this line of code:
fun = @(G, K, k1, k2, gamma, lam1) sigma_cauchy_fun(G, K, k1, k2, lam1)
that the anonymous call has six (count them 6) arguments? Yet the function sigma_cauchy_fun(G, K, k1, k2, lam1) only has 5 arguments as you called it? I find it an odd coincidence that MATLAB had a problem. Maybe it is just me of course. ;-)
Anyway, using the name gamma is just a bad idea, when the function named gamma is a useful one in MATLAB. But that is NOT your problem here, at least not yet. NEVER USE VARIABLE NAMES THAT ARE THE SAME AS EXISTING FUNCTION NAMES IN MATLAB.
Oisín Conway
Oisín Conway il 7 Ott 2022
Yes apologies, i had ran the function that you gave me that was commented above as well (with the other two lines comments) and there remains an error. Yes good point about the local and global vairables. I am not sure where else the problem is.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by