Undefined operator '*' for input arguments of type 'function_handle' error in Integral2. Kindly help me clear this error.

CODE:
for d=0.1:0.1:1
A=0
r1=@(ai,aj) sqrt((x+ai).^2+(y+aj).^2+z.^2)
r2=@(ai,aj) sqrt((ai-x).^2+(y+aj).^2+z.^2)
r3=@(ai,aj) sqrt((ai-x).^2+(y-aj).^2+z.^2)
r4=@(ai,aj) sqrt((x+ai).^2+(y-aj).^2+z.^2)
C1=@(ai) ai+x
C4=@(ai) -C1
C2=@(ai) ai-x
C3=@(ai) -C2
d1=@(aj) y+aj
d2=@(aj) d1
d3=@(aj) y-aj
d4=@(aj) d3
Bz= @(ai,aj) (u0/(4*pi))*(((((-1)^1*d1)/(r1*(r1+((-1)^(1+1))*C1)))-(C1/(r1*(r1+d1))))+((((-1)^2*d2)/(r2*(r2+((-1)^(2+1))*C2)))-(C2/(r2*(r2+d2))))+((((-1)^3*d3)/(r3*(r3+((-1)^(3+1))*C3)))-(C3/(r3*(r3+d3))))+((((-1)^4*d4)/(r4*(r4+((-1)^(4+1))*C4)))-(C4/(r4*(r4+d4)))))
for ai=[a-2*(N-1)*wd-2*(N-1)*s a-2*(N-2)*wd-2*(N-2)*s a-2*(N-3)*wd-2*(N-3)*s a-2*(N-4)*wd-2*(N-4)*s a-2*(N-5)*wd-2*(N-5)*s]
for aj=[a-2*(N-1)*wd-2*(N-1)*s a-2*(N-2)*wd-2*(N-2)*s a-2*(N-3)*wd-2*(N-3)*s a-2*(N-4)*wd-2*(N-4)*s a-2*(N-5)*wd-2*(N-5)*s]
M=integral2(Bz,0,ai,0,aj)
A=A+M
hold on
end
end
end
ERROR: Undefined operator '*' for input arguments of type 'function_handle'.
Error in FPSSConlyM3 (line 62)
M=integral2(Bz,0,ai,0,aj)

 Réponse acceptée

You have
d1=@(aj) y+aj
and
Bz= @(ai,aj) (u0/(4*pi))*(((((-1)^1*d1)/(r1*(r1+((-1)^(1+1))*C1)))-(C1/(r1*(r1+d1))))+((((-1)^2*d2)/(r2*(r2+((-1)^(2+1))*C2)))-(C2/(r2*(r2+d2))))+((((-1)^3*d3)/(r3*(r3+((-1)^(3+1))*C3)))-(C3/(r3*(r3+d3))))+((((-1)^4*d4)/(r4*(r4+((-1)^(4+1))*C4)))-(C4/(r4*(r4+d4)))))
When it reaches the sub-expression (-1)^1*d1 then it encounters a reference to d1, which is the handle to an anonymous function. d1 is after an * operator, so you are trying to multiply something by the anonymous function d1. Your code is not a request to execute d1 with some argument and multiply based upon the return value: it is a request to multiply the anonymous function itself. Which is not defined.
MATLAB will not look at the d1 and see that it was defined in terms of dummy argumented aj and so try to look to see if there is an active variable named aj that it should use as the argument for executing d1 .
Dummy argument names to an anonymous function are effectively in their own namespace, as if you had written something like.
function Q93277CEA4_result = Q93277CEA4(aj_of_function_Q93277CEA4)
Q93277CEA4_result = y + aj_of_function_Q93277CEA4
end
d1 = @Q93277CEA4
and
function N76783_result = N76783(ai_of_function_N76783, aj_of_function_N76783)
N76783_result = (u0/(4*pi))*(((((-1)^1*d1)/(r1*(r1+((-1)^(1+1))*C1)))-(C1/(r1*(r1+d1))))+((((-1)^2*d2)/(r2*(r2+((-1)^(2+1))*C2)))-(C2/(r2*(r2+d2))))+((((-1)^3*d3)/(r3*(r3+((-1)^(3+1))*C3)))-(C3/(r3*(r3+d3))))+((((-1)^4*d4)/(r4*(r4+((-1)^(4+1))*C4)))-(C4/(r4*(r4+d4)))))
end
Bz = @N76783
Under no circumstances will N76783 associate aj_of_function_N76783 with aj_of_function_Q93277CEA4 by itself.
IF you want d1 to be invoked with a particular argument in Bz, then you need to say so specifically, like
Bz= @(ai,aj) (u0/(4*pi))*(((((-1)^1*d1(aj))/(r1(ai,aj)*(r1(ai,aj)+((-1)^(1+1))*C1(ai)))) and so on

9 commentaires

Swathi S
Swathi S le 14 Août 2019
Modifié(e) : Swathi S le 14 Août 2019
Thank you, Sir for your explanation. The error is cleared. But now am getting an error as 'Matrix dimension must agree' at M=integral2(Bz,0,ai,0,aj). Can you guide me as where am making mistake?
Torsten
Torsten le 14 Août 2019
Modifié(e) : Torsten le 14 Août 2019
The ai and aj passed to your function by integral2 are vectors of the same length.
So multiplication, division and exponentiation in your function definition have to be done elementwise, e.g.
Bz= @(ai,aj) (u0/(4*pi))*(((((-1)^1*d1(aj))./(r1(ai,aj).*(r1(ai,aj)+((-1)^(1+1))*C1(ai))))
and so on.
Sir, I tried this already but still getting the same error.
Please include the modified code.
CODE:
for d=0.1:0.1:1
A=0
r1=@(ai,aj) sqrt((x+ai).^2+(y+aj).^2+z.^2)
r2=@(ai,aj) sqrt((ai-x).^2+(y+aj).^2+z.^2)
r3=@(ai,aj) sqrt((ai-x).^2+(y-aj).^2+z.^2)
r4=@(ai,aj) sqrt((x+ai).^2+(y-aj).^2+z.^2)
C1=@(ai) ai+x
C4=@(ai) -C1
C2=@(ai) ai-x
C3=@(ai) -C2
d1=@(aj) y+aj
d2=@(aj) d1
d3=@(aj) y-aj
d4=@(aj) d3
Bz= @(ai,aj) ((u0./(4.*pi)).*((((((-1).^1).*d1(aj))./(r1(ai,aj).*(r1(ai,aj)+((-1).^(1+1)).*C1(ai))))-(C1(ai)./(r1(ai,aj).*(r1(ai,aj)+d1(aj)))))+(((((-1).^2).*d2(aj))./(r2(ai,aj).*(r2(ai,aj)+(((-1).^(2+1))).*C2(ai))))-(C2(ai)./(r2(ai,aj).*(r2(ai,aj)+d2(aj)))))+(((((-1).^3).*d3(aj))./(r3(ai,aj).*(r3(ai,aj)+(((-1).^(3+1))).*C3(ai))))-(C3(ai)./(r3(ai,aj).*(r3(ai,aj)+d3(aj)))))+(((((-1).^4).*d4)./(r4(ai,aj).*(r4(ai,aj)+((-1).^(4+1)).*C4(ai))))-(C4(ai)./(r4(ai,aj).*(r4(ai,aj)+d4(aj)))))))
for ai=[a-2.*(N-1).*wd-2.*(N-1).*s a-2.*(N-2).*wd-2.*(N-2).*s a-2.*(N-3).*wd-2.*(N-3).*s a-2.*(N-4).*wd-2.*(N-4).*s a-2.*(N-5).*wd-2.*(N-5).*s]
for aj=[a-2.*(N-1).*wd-2.*(N-1).*s a-2.*(N-2).*wd-2.*(N-2).*s a-2.*(N-3).*wd-2.*(N-3).*s a-2.*(N-4).*wd-2.*(N-4).*s a-2.*(N-5).*wd-2.*(N-5).*s]
M=integral2(Bz,0,ai,0,aj)
A=A+M
hold on
end
end
end
ERROR:
Matrix dimensions must agree.
Error in FPSSConlyM3 (line 63)
M=integral2(Bz,0,ai,0,aj)
Torsten
Torsten le 14 Août 2019
Modifié(e) : Torsten le 14 Août 2019
a, N, wd, s are undefined.
Where do you use d ?
clear all;
close all;
x=0
y=0
a=0.15;
N=5
wd=0.001
s=0.0006
for z=0.1:0.1:1
A=0
r1=@(ai,aj) sqrt((x+ai).^2+(y+aj).^2+z.^2)
r2=@(ai,aj) sqrt((ai-x).^2+(y+aj).^2+z.^2)
r3=@(ai,aj) sqrt((ai-x).^2+(y-aj).^2+z.^2)
r4=@(ai,aj) sqrt((x+ai).^2+(y-aj).^2+z.^2)
C1=@(ai) ai+x
C4=@(ai) -C1
C2=@(ai) ai-x
C3=@(ai) -C2
d1=@(aj) y+aj
d2=@(aj) d1
d3=@(aj) y-aj
d4=@(aj) d3
Bz= @(ai,aj) ((u0./(4.*pi)).*((((((-1).^1).*d1(aj))./(r1(ai,aj).*(r1(ai,aj)+((-1).^(1+1)).*C1(ai))))-(C1(ai)./(r1(ai,aj).*(r1(ai,aj)+d1(aj)))))+(((((-1).^2).*d2(aj))./(r2(ai,aj).*(r2(ai,aj)+(((-1).^(2+1))).*C2(ai))))-(C2(ai)./(r2(ai,aj).*(r2(ai,aj)+d2(aj)))))+(((((-1).^3).*d3(aj))./(r3(ai,aj).*(r3(ai,aj)+(((-1).^(3+1))).*C3(ai))))-(C3(ai)./(r3(ai,aj).*(r3(ai,aj)+d3(aj)))))+(((((-1).^4).*d4)./(r4(ai,aj).*(r4(ai,aj)+((-1).^(4+1)).*C4(ai))))-(C4(ai)./(r4(ai,aj).*(r4(ai,aj)+d4(aj)))))))
for ai=[a-2.*(N-1).*wd-2.*(N-1).*s a-2.*(N-2).*wd-2.*(N-2).*s a-2.*(N-3).*wd-2.*(N-3).*s a-2.*(N-4).*wd-2.*(N-4).*s a-2.*(N-5).*wd-2.*(N-5).*s]
for aj=[a-2.*(N-1).*wd-2.*(N-1).*s a-2.*(N-2).*wd-2.*(N-2).*s a-2.*(N-3).*wd-2.*(N-3).*s a-2.*(N-4).*wd-2.*(N-4).*s a-2.*(N-5).*wd-2.*(N-5).*s]
M=integral2(Bz,0,ai,0,aj)
A=A+M
hold on
end
end
plot(z,A)
end
ERROR:
Undefined operator '.*' for input arguments of type 'function_handle'.
Error in FPSSConlyM3 (line 57)
M=integral2(Bz,0,ai,0,aj)
C4=@(ai) -C1
This has the same problem we discussed: C1 is a function handle.
You have the same problem on other lines.
Yes, Sir. I could finally clear the error. Thank you, Walter Roberson Sir and Torsten Sir.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by