MATLAB Answers


Numerical integration with array limits

Asked by Christopher Kodama on 29 Apr 2013
Latest activity Commented on by Ashish Bhatt on 10 Apr 2017

I'm trying to work with integrals that are functions of one of their limits:

For example,

phi = @(x) quad(@(L) besseli(1, (1+L)/(1-L)), 0, x);

What I'm trying to do is evaluate phi over an array of values, like:

phi([1,2,3,4]); %ERROR
quad(@(L) besseli(1, (1+L)/(1-L)), 0, [1,2,3,4]); %ERROR

but these return errors. I could do this in a for loop, like:

  phi_eval = phi(nums(k));

but I was wondering if there was a better way to do things. Is there a no-for-loops way of doing this?


Log in to comment.

1 Answer

Answer by Shashank Prasanna on 29 Apr 2013
 Accepted Answer

Christopher, I can't run the loop as well. But phi([1,2,3,4]) will certainly not work because the vector is being passed to quad directly as limits which is wrong syntax for quad.

You can try the following:



I too would like to specify vector valued integration limits. If anybody from mathworks is listening could you please respond? This should be really easy to implement I imagine, and hopefully will be in the next release?

Please go to my profile. Where it says "email", click on "contact Mike Hosea" and tell me about your use cases for array-valued limits. If we're talking generic array limits, where there is no a priori relationship between the different elements of the limits, then no gain in efficiency can be had over writing a loop. E.g.

Q = zeros(size(a));
for k = 1:numel(Q)
    Q(k) = integral(f,a(k),b(k));

For scalar-valued integrations, that can also be accomplished efficiently with an application of arrayfun, e.g.

Qarray = @(a,b)arrayfun(@(ak,bk)integral(f,ak,bk),a,b);
Q = Qarray(a,b);

However, if we are talking about table-building, where the limits represent a grid, then efficiency improvements are possible. The latter use case might be accomplished by some other means, however, such as providing scalar limits and a list of output points for the integrals over partial regions.

it works with both array limits as well:

phi = matlabFunction(int(x^2,x,a,b));

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!