# Matlab integral with vectorized bounds, without using a loop

I have a simple question that I wasn't able to find an answer to. I have a function f(x) where x is some vector, for example:

function f=f(x)
f=x.^4;
end


Then I want to integrate with bounds as vectors:

x=1:0.1:10;
x_0=2*ones(size(x));
g=integral(@f,x_0,x);


and I want to get g as a vector g(i)=integral(@f,x_0(i),x(i)). However, this will return an error that the bounds have to be scalars.

Obviously I can use a loop:

x=1:0.1:10;
x_0=2*ones(size(x));
for i=1:length(x)
g(i)=integral(@f,x_0(i),x(i));
end


but that can be very inefficient if I have big vectors that are repeatedly integrated.

Is there a simple efficient way to do this vectorized without a loop?

• I assume that your function f is just an example. A for loop is the way to do this. As the documentation indicates, integral only handles scalar bounds. This makes sense as the integration might be quite different for different bounds, which would potentially diminish any benefits from parallelization. for loops aren't terribly inefficient in current versions of Matlab. However, you should be sure to pre-allocate your output g. And if the lower bound x_0 is constant, then you don't need to turn it into a vector. – horchler Mar 21 '16 at 16:37
• @horchler ok, so your 100% sure that there isn't a way to "vectorize" this with efficiency? arrayfun for example is a way to vectorize but is less efficient than the for loop – TensoR Mar 23 '16 at 18:13
• What do you think arrayfun is other than a for loop in disguise? ;-) And, as you indicate, it's often less efficient for applications like this. If you want faster, you'll need to implement your own quadrature routine, or solve the integral symbolically (or come up with a suitable approximation) and vectorize the evaluation of the result. – horchler Mar 23 '16 at 18:56
• i ended up using a parfor loop, which gave the best efficiency – TensoR Mar 28 '16 at 14:00
• Is performance the only metric of interest? Have you considered writing a small c++ code and calling it from Matlab with coder.ceval('cfun_name'). This may give you the performance your looking for. – Charles Apr 26 '16 at 4:53

arrayfun(@(x_1, x_2) integral(@(y) y.^4, x_1, x_2), x_0, x)