Search code examples
matlablimitnumerical-integration

Matlab: how to calculate the definite integral of a function over multiple limits?


Assume:

z = [0.4 0.5 0.75]'  
function y = myfunct(x)  
y = quad(@sin, 0, x)

I'd like to calculate the definite integral of sin(x) from 0 to 0.4, to 0.5, and 0.75, using:

myfunct(z)

However, Matlab returns:

??? Error using ==> quad at 70  
The limits of integration must be scalars.  

Solution

  • You can also use quadv to do this. BUT, instead of making a vector of integration limits, make an array valued function so that when you integrate each element, the range of integration will be 0 to 1.

    To be more specific, you want to integrate sin(x) from x = 0 to z. This is the same as integrating sin(u z)*z from u = 0 to 1 (u-substitution). Make an array function

    F = @(u) [sin( .4 * u) * .4, sin( .5 * u ) * .5, sin( .75 * u ) * .75 ];
    

    Then do

    quadv(F,0,1)