1

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?

2
  • Are you doing this numerically or symbolically? Because you appear to be mixing symbolic functions (heaviside and dirac) with numeric ones (integral). Commented Jun 9, 2017 at 13:58
  • I made this program symbolically, but the symbolic integrations take a really long time when the m in the sum for FUN_1 and FUN_2 increases. I am trying to make it numerically, but I can only plug in for x_2 at the end. Commented Jun 9, 2017 at 16:25

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure I understand. a(1,1) and c(1,1) are both scalar values.
@HypatieD'Alexandrie: Those are arguments passed to the integral function. The first argument to integral is an anonymous function, which you have defined as @(x_1) S. Your function S expects 2 inputs, but you are providing it none, so when @(x_1) S is evaluated it just returns the function handles S.
I understand now, thank you! I've tried what you suggested. However, now when I try to plug in a value for x_2 in R (ie. line: k(1,n+1) = R(1)), I am getting an error in matrix dimensions: "Matrix dimensions must agree".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.