In Matlab I am using the combination of fit
and fittype
function (from the curve fitting toolbox) to fit a signal using an anonymous function.
My issue is that, this anonymous function use the integral
function of matlab, and this is extremely time consuming. I am therefore trying to get rid of this function inside the anonymous function.
An amazing improvement would be to pre-calculate these integral and pass it to the fit
function, but I do not know how to do this.
I tried to define the vector that contain these integration values and pass it to the fit
by defining it as 'dependent'
or 'problem'
variable in fittype
but that did not do the trick. I also tried concatenating the coordinate and the result of this integral. When doing a two column vector it tried to fit a surface (which it is not the case). When concatenating in a one column vector it complain that the data set and the coordinate system should be the same size.
As an alternative I also tried using sum
and trapz
instead of integral
but fittype
told me that then the function did not fulfill the criteria of an anonymous function.
I am running short on ideas and on ways to ask search engines for new ideas and would welcome all new point of view on how to solve this.
For curious peoples here is the original anonymous function I am using:
function [ y ] = zeusEquation(varargin)
psi0=varargin{1};% in radian
a1 = varargin{2};% parameter to be fitted
a2 = varargin{3};%parameter to be fitted
a3 = varargin{4};% average of background signal/ offset in gray level
a4 = varargin{5};% slope on the signal background
a5 = varargin{6};% decentering of the signal (in meter)
a6 = varargin{7};% width of the signal in radian
x = varargin{8};%Coordinate system in meter!!!!
y = zeros(size(x));
%fundamental bloc equation needed
sinIntegral = @(t) 2.*sin(t)./(pi.*t);
u = @(f, e, x) f.*abs(x-e);
%Actual signal calculation
for i = 1:max(size(x))
if u(a6,a5,x(i))~=0
y(i) = a3...
+a4.*(x(i)-a5)...
+(1-integral(sinIntegral,0,u(a6,a5,x(i))))...
.*(a1.*sign(x(i)-a5).*sin(psi0)-...
(1-a2).*integral(sinIntegral,0,u(a6,a5,x(i))).*(1-cos(psi0)));
else
y(i) = a3+a4.*(x(i)-a5);
end
end
end
Note: I cross asked this question on mathwork.
zeusEquation
and hence not an anonymous function. An anonymous function in MATLAB is a lambda, and it can capture variables of the scope it is defined in. However, regular named functions defined inside another function can do so too, sort of combining the lambda and named function concepts. If I have time later I'll try to write an answer to describe the capture concept. \$\endgroup\$