jlperla
jlperla

Reputation: 201

Setting to use matlab `integral` with a (only) scalar function?

I have a function which cannot be easily written easily to take a vector input and return a vector output. The builtin integral function seems to expect this, and is evaluating a number of locations at the same time. Is there any way to turn this off?

i.e., the simplest test case is

f = @(x) x * x; % intended to be univariate
integral(f, 0, 1); %I only want to have it call with univariate inputs.

where I am purposefully not setting the function to be x .* x in order to test the univariate input. Obviously, my function is a lot more complicated than this and cannot be vectorized.

Upvotes: 1

Views: 57

Answers (1)

rlbond
rlbond

Reputation: 67739

You can use the array-valued flag that's mentioned in the help:

>> f = @(x) x * x;
>> integral(f,0,1,'ArrayValued',true)

ans =

    0.3333

The help description is a bit misleading:

Array-valued function flag, specified as the comma-separated pair consisting of 'ArrayValued' and either false, true, 0, or 1. Set this flag to true to indicate that fun is a function that accepts a scalar input and returns a vector, matrix, or N-D array output.

The default value of false indicates that fun is a function that accepts a vector input and returns a vector output.

So notice that it doesn't explicitly specify that the input is a scalar and the output is a scalar, but it's clear the intent is to use scalar input.

Upvotes: 1

Related Questions