how to speed up a fit using an anonymous function?

2 vues (au cours des 30 derniers jours)
Anne-Laure Cheffot
Anne-Laure Cheffot le 16 Fév 2018
I would like to fit a signal using the following anonymous function:
function [ y ] = zeusEquation(varargin)
psi0=varargin{1};% in radian This is the delay created by the pin hole of the mask
a1=varargin{2};% parameter that depend on the piston
a2 = varargin{3};%parameter that depend on the piston
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
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
Attached to this message is the complete fitting algorithm composed of two function, one script and one data set for you to play with. The starting point is the script named testScript_zeusFit. To run this, it is required to have the curve fitting toolbox.
Now fitting using zeusEquation takes ages (~60 second with 1000 point signal and a very good starting point) and more precisely, I think, using the matlab integral function is the problem. it seems to be the most time consuming operation. I tried:
  • replacing it by sum or trapz function: but then it is no longer an anonymous function because it require necessarily to feed it with a vector (fittype said so),
  • since the sinIntegral term is not very often changing I tried to calculate it before hand and then feed it to fit: I did not find a way to do that because I am fitting a 1D signal then fit seems to only want one vector. I also tried using the 'dependent' and 'problem' key word but it did not work (I do not know if I used these option right though).
  • calculate the sinIntegrale before hand and concatenate the x and the sinIntegral in a one column vector or two column vector but it did not work either.
I am running short on ideas how to improve this function and I am also running short on ways to ask search engines for new ideas and would welcome all new point of view on how to solve this.

Réponses (0)

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by