Custom expression for surface fitting
16 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2019-3-13
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 个评论
更多回答(1 个)
kk
2019-3-13
2 个评论
Walter Roberson
2019-3-13
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 ?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!