Matlab double integral of dot product error

86 Views Asked by At

Say I wanted to integrate

f=@(x) [1,0]; 
integral2( @(x,y) f(x)*f(y)' ,0,1,0,1 ); 

But this gives the error

Integrand output size does not match the input size.

Even though f is a vector, I am taking the dot product so the input size is 1x1. Ideally I want to use vector notation to compute dot products in the integrand, because I have a much more complex inner product which would be a pain to write out by hand. For 1D integrals you can set arrayvalued to true, but not for 2D. How do I numerically double integrate a function containing a dot product?

1

There are 1 best solutions below

3
Ander Biguri On

While your question provides wrong code, something like this reproduces your error:

integral2( @(x,y) f(x)*f(y)' ,0,1,0,1 );

The error is simply because your integral function is not in the format that integral2 accepts. Your function needs to accept array inputs, and return array outputs of corresponding value.

i.e.

fun=@(x,y) f(x)*f(y)'
size(fun(0,1)) % returns 1, should return 1
size(fun([0,0],[1,1])) % returns 1, should return 2, because it has 2 inputs

% in other words, this must hold for it to be a valid function for integral2:
fun([0,0],[1,1]) == [fun(0,1), fun(0,1)]

Just rewrite your function so it can be evaluated in more than one [x,y] pair at the same time, make it a vectorized function. Remember that the input does not need to be an anonymous function defined in one line, you are free to write a full function in a separate file, and use that for integral2.