Search code examples
octave

Integral of integral function


I am struggling trying to evaluate numerically on Octave a double integral of a user defined function on one variable.

Here is the function:

function result = Sigma1(ec)
    ec2 = -0.001;
    fcd = 20e6;
    if(ec >= 0)
        result = 0;
        return;
    endif
    if(ec > ec2)
        result = -fcd.*(1-(1-ec./ec2).^2);
        return;
    endif
    result = -fcd;
endfunction

The function works as expected:

>> integral(@Sigma1, 0, -0.003)
ans = 6.0000e+04

but if I try to integrate two times I get:

>> integral(integral(@Sigma1, 0, -0.003), 0, -0.003)
error: feval: first argument must be a string, inline function, or a function handle
error: called from
    integral at line 123 column 36

I tried also a double integral:

>> integral2(@Sigma1, 0, e, 0, e)
error: Sigma1: function called with too many inputs

Edit1:

I also tried integral2 with:

function result = Sigma2(ec, foo)
    ec2 = -0.001;
    fcd = 20e6;
    if(ec >= 0)
        result = 0;
        return;
    endif
    if(ec > ec2)
        result = -fcd.*(1-(1-ec./ec2).^2);
        return;
    endif
    result = -fcd;
endfunction

but the result is looks wrong:

>> integral2(@Sigma2, 0, -0.003, 0, -0.003)
ans = -180

What am I doing wrong?


Solution

  • Finally I came up with this solution:

    function y = Sigma1Int(x)
      y = integral(@Sigma1, 0, x, 'ArrayValued', true);
    endfunction
    
    function y = Sigma1IntInt(x)
      y = integral(@Sigma1Int, 0, x, 'ArrayValued', true);
    endfunction
    
    function y = Sigma1IntInt(x)
      y = integral(@Sigma1Int, 0, x, 'ArrayValued', true);
    endfunction
    

    and the results are as expected:

    >> Sigma1IntInt(-0.003)
    ans = -56.667
    >> Sigma1IntIntInt(-0.003)
    ans = 0.047333
    >>