Antalagor
Antalagor

Reputation: 438

matlab: inefficient "mysum"

in order to estimate some non-linear models, i need to derive a quite huge function numerically. one part of the target function includes a polynomial (which is created by some sums). there are quite a lot of iterations in this process and my computer takes way to much time to compute (though it yields reasonable estimates). the profiler claims, that my handwritten sum-function is by far the most time-consuming part of the algorithm. this is my first matlab project, so im pretty new to it. maybe you can help to optimize it :)

 function [output] = mysum(a,b,inputfun)
   output=0;
   for i=a:b
        output=ouput+inputfun(i);
   end

if you want to know, how i use it. this is the polynomial:

function [ weights ] = wexpo(theta)
global lag;

for i=1:lag
    weights(i) = exp(mysum(1,length(theta),@(k) theta(k)*(i-1)^k))...
                 /mysum(0,lag-1,@(j)...
                 exp(mysum(1,length(theta),@(k) theta(k)*j^k)));
end

Upvotes: 1

Views: 69

Answers (1)

Aleharu
Aleharu

Reputation: 162

If you can use Matlab functions:

function [output] = mysum(a,b,inputfun)
    output = sum(inputfun(a:b))

Upvotes: 1

Related Questions