Custom expression for surface fitting
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am sorry, I don't have any formal Matlab training, so I might just be missing a simple point here.
I have a problem figuring out how to write a function for a custom surface-fit equation. In a simple case, I have this equation, where is a log-normal distribution, is a (known) parabola and . Some of the parameters are known, the fit has 6 coefficients to find (3 for the lognormal distribution, ).
If I simply write an expression (FitFormula) for this equation and use
ft = fittype(FitFormula, 'independent', {'x', 'y'}, 'dependent', 'z','problem',{'sigma' ,'y0'});
[fitresult, gof] = fit( [xData, yData], zData, ft, opts, 'problem',{sigma,y0} );
the fit successfully computes the unknown parameters.
But I would need to write a function for this equation rather than a simple in-line expression, because I want to use some more advanced functions involving a convolution. Here is where I cannot figure out how to do it, because the "Streak" function uses both x and y as a variable. So I made sure that it produces a matrix as output:
function y = Streak(x,tau,tau0,w)
C=max([size(tau),size(tau0)]); %number of columns of output
R=max(size(x)); %number of rows in output
if ((~isscalar(x)) && (isrow(x))) x=x'; end;
if C>1 % check if all inputs are vectors
if ((~isscalar(tau)) && (iscolumn(tau))) tau=tau'; end;
if ((~isscalar(tau0)) && (iscolumn(tau0))) tau0=tau0'; end;
if ((~isscalar(w)) && (iscolumn(w))) w=w'; end;
if isscalar(tau) tau=repmat(tau,1,C); end;
if isscalar(tau0) tau0=repmat(tau0,1,C); end;
if isscalar(w) w=repmat(w,1,C); end;
end;
XX0=repmat(x,1,C)-repmat(tau0,R,1);
TT=repmat(tau,R,1);
GG=repmat(w,R,1);
ExpDec=exp(-XX0./TT);
GGauss=exp(GG.^2./(2*TT.^2));
Erf = erf((XX0-GG.^2./TT)./(sqrt(2)*GG))+1;
y = GGauss.*ExpDec.*Erf;
end
When I use this "Streak" function as the FitFormula, Matlab's
function testCustomModelEvaluation( obj )
produces an error
Error using fittype/testCustomModelEvaluation (line 16)
Custom equations must produce an output vector, matrix, or array that is the same size and shape as the input data. This custom equation fails to meet that requirement
even though the size of the output is OK if I simply evaluate it.
Does anyone know what the correct way to define the "Streak" function is? Thaks a lot!
0 commentaires
Réponse acceptée
Walter Roberson
le 13 Mar 2019
Notice that if your input x is a row vector then you transpose it and work with that to calculate y. That will not be the same orientation as the input. If the input x is a row vector then the output must be a row vector as well.
0 commentaires
Plus de réponses (1)
kk
le 13 Mar 2019
2 commentaires
Walter Roberson
le 13 Mar 2019
The curve fitting routine will only pass in arrays of compatible size. The important point is that the output needs to be exactly the same size as the input.
Can you post your FitFormula function so that we can see how the parameters passed to it by fit() make their way to Streak ?
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!