Error using + Matrix dimensions must agree. I want to find the value of Mt. Kindly help me clear the error.

CODE:
a=0.1
s=0.01
N=5
wd=0.002906
d=5
for a1=a-((N-1)*(wd+s)):(wd+s):a
for b1=a-((N-1)*(wd+s)):(wd+s):a
r1=@(x,y) sqrt((x+a1).^2+(y+b1).^2+d.^2)
r2=@(x,y) sqrt((a1-x).^2+(y+b1).^2+d.^2)
r3=@(x,y) sqrt((a1-x).^2+(y-b1).^2+d.^2)
r4=@(x,y) sqrt((x+a1).^2+(y-b1).^2+d.^2)
C1=@(x) a1+x
C4=@(x) -a1-x
C2=@(x) a1-x
C3=@(x) -a1+x
d1=@(y) y+b1
d2=@(y) y+b1
d3=@(y) y-b1
d4=@(y) y-b1
fun=@(x,y) ((((u./(4.*pi)).*((((((-1).^1).*d1(y))./(r1(x,y).*(r1(x,y)+((-1).^(1.+1)).*C1(x))))-(C1(x)./(r1(x,y).*(r1(x,y)+d1(y)))))+(((((-1).^2).*d2(y))./(r2(x,y).*(r2(x,y)+(((-1).^(2.+1))).*C2(x))))-(C2(x)./(r2(x,y).*(r2(x,y)+d2(y)))))+(((((-1).^3).*d3(y))./(r3(x,y).*(r3(x,y)+(((-1).^(3.+1))).*C3(x))))-(C3(x)./(r3(x,y).*(r3(x,y)+d3(y)))))+(((((-1).^4).*d4(y))./(r4(x,y).*(r4(x,y)+((-1).^(4.+1)).*C4(x))))-(C4(x)./(r4(x,y).*(r4(x,y)+d4(y)))))))));
M = integral2(fun,-b1,b1,-b1,b1)
Mt=Mt+M
end
end
ERROR:
Error using +
Matrix dimensions must agree.
Error in @(x,y)sqrt((x+a1).^2+(y+b1).^2+d.^2)
Error in
@(x,y)((((u./(4.*pi)).*((((((-1).^1).*d1(y))./(r1(x,y).*(r1(x,y)+((-1).^(1.+1)).*C1(x))))-(C1(x)./(r1(x,y).*(r1(x,y)+d1(y)))))+(((((-1).^2).*d2(y))./(r2(x,y).*(r2(x,y)+(((-1).^(2.+1))).*C2(x))))-(C2(x)./(r2(x,y).*(r2(x,y)+d2(y)))))+(((((-1).^3).*d3(y))./(r3(x,y).*(r3(x,y)+(((-1).^(3.+1))).*C3(x))))-(C3(x)./(r3(x,y).*(r3(x,y)+d3(y)))))+(((((-1).^4).*d4(y))./(r4(x,y).*(r4(x,y)+((-1).^(4.+1)).*C4(x))))-(C4(x)./(r4(x,y).*(r4(x,y)+d4(y)))))))))
Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in test (line 110)
M = integral2(fun,-b1,b1,-b1,b1)

5 Comments

Your code is not formatted and fairly complex. Can you explain why you expect this to work?
Your code does not assign to d so d will have whatever value happens to be in the workspace.
You can simulate what we will see by using clearvars before you run the code that you pasted here.

Sign in to comment.

Answers (2)

Hi,
You can try declaring and defining variables like u and Mt to see if the code is working . Error is occuring due to variable u and Mt not being defined.
thanks.
a=1.5; % make good guess
s=0.01;
N=5;
wd=0.002906;
d=5;
u=2; % guess
i = 1;
a1=a-((N-1)*(wd+s)):(wd+s):a;
b1=a-((N-1)*(wd+s)):(wd+s):a
b1 = 1×5
1.4484 1.4613 1.4742 1.4871 1.5000
for a1=a-((N-1)*(wd+s)):(wd+s):a
for b1=a-((N-1)*(wd+s)):(wd+s):a
r1=@(x,y) sqrt((x+a1).^2+(y+b1).^2+d.^2);
r2=@(x,y) sqrt((a1-x).^2+(y+b1).^2+d.^2);
r3=@(x,y) sqrt((a1-x).^2+(y-b1).^2+d.^2);
r4=@(x,y) sqrt((x+a1).^2+(y-b1).^2+d.^2);
C1=@(x) a1+x;
C4=@(x) -a1-x;
C2=@(x) a1-x;
C3=@(x) -a1+x;
d1=@(y) y+b1;
d2=@(y) y+b1;
d3=@(y) y-b1;
d4=@(y) y-b1;
fun=@(x,y) ((((u./(4.*pi)).*((((((-1).^1).*d1(y))./(r1(x,y).*(r1(x,y)+((-1).^(1+1)).*C1(x))))-(C1(x)./(r1(x,y).*(r1(x,y)+d1(y)))))+(((((-1).^2).*d2(y))./(r2(x,y).*(r2(x,y)+(((-1).^(2+1))).*C2(x))))-(C2(x)./(r2(x,y).*(r2(x,y)+d2(y)))))+(((((-1).^3).*d3(y))./(r3(x,y).*(r3(x,y)+(((-1).^(3+1))).*C3(x))))-(C3(x)./(r3(x,y).*(r3(x,y)+d3(y)))))+(((((-1).^4).*d4(y))./(r4(x,y).*(r4(x,y)+((-1).^(4+1)).*C4(x))))-(C4(x)./(r4(x,y).*(r4(x,y)+d4(y)))))))));
M(i)= integral2(fun,-b1,b1,-b1,b1); % integral value for each iteration
i=i+1;
end
end
M
M = 1×25
0.1347 0.1379 0.1411 0.1444 0.1477 0.1358 0.1390 0.1422 0.1455 0.1488 0.1368 0.1401 0.1433 0.1466 0.1500 0.1379 0.1411 0.1444 0.1478 0.1511 0.1389 0.1422 0.1455 0.1489 0.1523
Mt = sum(M)
Mt = 3.5840
plot(M);

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Asked:

on 4 Feb 2021

Answered:

on 29 Oct 2021

Community Treasure Hunt

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

Start Hunting!