On first viewing, a couple of things stand out:
- you're giving your loop variable the same name as a MATLAB built-in (
ii
) - you're using a variable name that's also the name of a MATLAB built-in (
log
) - you're changing the loop variable inside the loop (
ii=ii+1
is not necessary) - you're not in-lining the
taylorsin
function in the loop (function calls to non-built-in functions can be hard to JIT) - you're not checking for other values of the variable
log
(this WILL bite you)
So, a quick improvement would be:
function ret = partialsum(x,n,lg)
ret = 0;
if lg == 1
for ii = 0:n
N = cumprod(2 : 2*ii+1);
ret = ret + (-1)^ii * (x^(2*ii+1))/N(end);
end
elseif lg == 0
for ii = n:-1:0
N = cumprod(2 : 2*ii+1);
ret = ret + (-1)^ii * (x^(2*ii+1))/N(end);
end
else
error('Invalid value for lg');
end
But given that the outcomes of the computations in the loop are the same (just the summation order is different), you can vectorize the whole thing:
function ret = partialsum(x,n,~)
ii = 0:n;
k = 2*ii+1;
ret = x.^k ./ factorial(k) * (-1).^ii.';
end