# Too much calculus time on 'lsqcurvefit' + 'fsolve'

3 views (last 30 days)
Sergio Quesada on 19 Jun 2018
Commented: OCDER on 26 Jun 2018
Good afternoon everybody,
I am working with a code that combines 'fsolve' and 'lsqcurvefit' to calculate the parameters, k(1), k(2) and k(3), that better fits my function, called 'Iteor', to my experimental data, (texp, Iexp):
rteor=@(k,r) fsolve(@(r) arrayfun(@(R,T) FC.*integral(@(x) exp(-a./(x.*log(x))), 1, R)-(T-(.001.*k(1))),r,texp), inpts, options);
Iteor=@(k,r) ((.01.*k(2)./FC).*(rteor(k,texp)).*exp(a./(rteor(k,texp)).*log(rteor(k,texp))))+(Vo./(100.*k(3).*log(rteor(k,texp))));
k = lsqcurvefit(Iteor, x0, texp, Iexp,[],[],options);
The problem is my experimental data are 22000 points large, and the calculus takes too much time. I'm working on a Intel Core i7 4.00 GHz and 32.0 GB. With FORTRAN the process is much faster... Is there any way to reduce the calculus time without eliminating initial experimental points ?
Thank you so much !!

OCDER on 19 Jun 2018
Edited: OCDER on 20 Jun 2018
Your Iteor is causing a lot of issues since it's calling rteor 4 times, meaning it's doing repeated fsolve and integral - both of which are computationally expensive.
To translate what's happening:
rteor = @(k, r) HEAVY_MATH_STUFF;
Iteor = @(k, r) HEAVY_MATH_STUFF .* HEAVY_MATH_STUFF + log(HEAVY_MATH_STUFF) ./ HEAVY_MATH_STUFF;
To fix, create a function that does the heavy math only once.
function y = Iteor(k, r, ...)
b = rteor(k, r, ...);
y = b .* b + log(b) ./ b; %b was only computed once!
I wish I could give you the corrected code, but that equation is very complex with all the nested anonymous functions... I'll end up messing it up. Overall, prevent recalculating fsolve and integral. Hope this helps to speed things up a little.
OCDER on 26 Jun 2018
You're welcome!