When I try to run this code I get the following error: "Undefined operator '.*' for input arguments of type 'cell'." 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 of the resulting array. The error occurs on the last line. I'm trying to plug in the value 1.5 for every element in the array. Any tips on how to "handle" this error?
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 = cell(1,2*M-1);
n=0;
for n = 0:2*M-1
k{1,n+1} = @(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)]);
end
R = @(x_2)integral(@(x_1)k,a(1,1),c(1,1),'ArrayValued',true);
x= 1.5;
R{x}
I've updated the code as follows:
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);
but I'm still getting the following error: "Input function must return 'double' or 'single' values. Found 'function_handle'. for the line
k(1,n+1) = R(1);
Any tips?
In this line:
R = @(x_2) integral(@(x_1) S, a(1, 1), c(1, 1));
You aren't passing any values to S
within your anonymous function @(x_1) S
, so that anonymous function is just returning a function handle S
instead of evaluating S
for a set of inputs. I'm guessing you want to define it like this:
R = @(x_2) integral(@(x_1) S(x_1, x_2), a(1, 1), c(1, 1));