Matlab: function handle integration with several variables

1 view (last 30 days)
My goal here is to build an array (cell array since I'm working with function handles) via a for loop and take the integral of each element, plug in a value and get an array. But I get the following error:
Input function must return 'double' or 'single' values. Found 'function_handle'.
The error occurs on the line when I'm trying to plug in the value 1 (or any scalar value) for x_2. Any tips on how to "handle" this error? Note a(1,1) and c(1,1) are both scalar values (eg. 0 and 1).
Here is the code:
FUN_1 = @(y_1,y_2,x_1,x_2)sum(heaviside(y_1-a_k(1:m,1)).*dirac(1,y_2-a_k(1:m,2))).*(-1/2.*log((x_1-y_1).^2+(x_2-y_2).^2))+(x_1-y_1).^2./((x_1-y_1).^2)+sum(dirac(y_1-a_k(1:m,1)).*dirac(y_2-a_k(1:m,2))).*(-1/2.*log((x_1-y_1).^2+(x_2-y_2).^2))+(x_1-y_1).*(x_2-y_2)./((x_1-y_1).^2+(x_2-y_2).^2);
Q_1 = @(x_1,x_2)integral2(@(y_1,y_2)FUN_1(y_1,y_2,x_1,x_2),a(1,1),c(1,1),a(1,2),c(1,2));
FUN_2 = @(y_1,y_2,x_1,x_2)sum(heaviside(y_1-a_k(1:m,1)).*dirac(1,y_2-a_k(1:m,2))).*(-1/2.*log((x_1-y_1).^2+(x_2-y_2).^2))+(x_1-y_1).*(x_2-y_2)./((x_1-y_1).^2)+sum(dirac(y_1-a_k(1:m,1)).*dirac(y_2-a_k(1:m,2))).*(-1/2.*log((x_1-y_1).^2+(x_2-y_2).^2))+(x_2-y_2).^2./((x_1-y_1).^2+(x_2-y_2).^2);
Q_2 = @(x_1,x_2)integral2(@(y_1,y_2)FUN_1(y_1,y_2,x_1,x_2),a(1,1),c(1,1),a(1,2),c(1,2));
k = zeros(1,2*M);
n=0;
for n = 0:2*M-1
S = @(x_1,x_2)Q_1(x_1,x_2)*2*n*(x_1+1i*x_2)^(n-1) + Q_2(x_1,x_2)*2*n*1i*(x_1+1i*x_2)^(n-1);
R = @(x_2)integral(@(x_1)S,a(1,1),c(1,1));
k(1,n+1) = R(1);
end
disp(k);
end

Answers (1)

Guillaume
Guillaume on 9 Jun 2017
A guess as I've not really tried to understand what your functions are doing:
R = @(x_2) integal(@(x_1) S(x_1, x_2), a, c)
The error you get makes sense. (@(x_1) S is a function that returns the function handle S regardless of the input. My modification returns the result of S(x_1, x_2) for input x_1 received from integral.
Note that if a and c are scalar, there's absolutely no point in writing them as a(1, 1) and c(1, 1) other than puzzling the reader and making them wonder if you meant to pass instead some other variable that was a 2D array.
  6 Comments
Torsten
Torsten on 12 Jun 2017
Edited: Torsten on 12 Jun 2017
Aside from the formal MATLAB error from above: it makes little sense to apply numerical integration methods to functions that are full of discontinuities (dirac, heaviside).
Did you try to plot abs((R)) ? How does it look ? Not smooth, I guess ...
Best wishes
Torsten.
Walter Roberson
Walter Roberson on 12 Jun 2017
You posted a Question about this; I replied there. You missed that integral() passes in a vector of values.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!