How to plot of a double integral if error "Matrix dimensions must agree."

Hi to everybody, I didn't have any error for the first code but fir the second I obtain the error "matrix dimensions must agree". Howerver, I think the idea behind these 2 codes is the same... Why is there this error and how to solve it?
1) code without error
a=0.015;
b=0.01;
z = 0.006;
mu0 = 4*pi*10^-7;
x = linspace(0,0.016,8);
M = zeros(1,length(x));
for i=1:length(x)
f = @(theta,phi) a*b*cos(theta-phi)./sqrt((x(i)^2 + a^2 + b^2+ z^2 +2*b*x(i).*cos(phi) - 2*a*x(i).*cos(theta) - 2*a*b.*(cos(theta - phi)))) ;
M(i) = integral2(f,0,2*pi,0,2*pi);
end
figure;
plot(x,M);
2) code with error
r1=0.015;
r2=0.01;
z = 0.004;
mu0 = 4*pi*10^-7;
x = linspace(0,0.016,8);
M = zeros(1,length(x));
for i=1:length(x)
f = @(theta,phi) r1.*r2.*((1+theta.*phi).*cos(theta-phi)-(theta-phi).*sin(theta-phi))./sqrt(x.^2+(r1.*theta).^2+(r2.*phi).^2+z.^2+2.*r2.*x.*phi.*cos(phi)-2*r1.*x.*theta.*cos(theta)-2*r1.*r2.*theta.*phi.*cos(theta-phi));
M(i) = integral2(f,8.2*pi,10.2*pi,10.2*pi,15.2*pi);
end
figure;
plot(x,M);
error message :
Matrix dimensions must agree.
Error in
coil_2_archimede>@(theta,phi)r1.*r2.*((1+theta.*phi).*cos(theta-phi)-(theta-phi).*sin(theta-phi))./sqrt(x.^2+(r1.*theta).^2+(r2.*phi).^2+z.^2+2.*r2.*x.*phi.*cos(phi)-2*r1.*x.*theta.*cos(theta)-2*r1.*r2.*theta.*phi.*cos(theta-phi))
(line 9)
f = @(theta,phi)
r1.*r2.*((1+theta.*phi).*cos(theta-phi)-(theta-phi).*sin(theta-phi))./sqrt(x.^2+(r1.*theta).^2+(r2.*phi).^2+z.^2+2.*r2.*x.*phi.*cos(phi)-2*r1.*x.*theta.*cos(theta)-2*r1.*r2.*theta.*phi.*cos(theta-phi));
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 coil_2_archimede (line 10)
M(i) =
integral2(f,8.2*pi,10.2*pi,10.2*pi,15.2*pi);

 Réponse acceptée

Looking at the code, it appears that the intent of the for loop is to iterate over ‘x’ so subscripting it in each itaration appears to be appropriate. The integral2 funciton does not have an 'ArrayValued' option, so using ‘x’ as a vector will not work in it.
The code could be more efficient by creating the ‘f’ anonymous function once, and adding ‘x’ as an additional argument —
r1=0.015;
r2=0.01;
z = 0.004;
mu0 = 4*pi*1E-7;
x = linspace(0,0.016,8);
M = zeros(1,length(x));
f = @(theta,phi,x) r1.*r2.*((1+theta.*phi).*cos(theta-phi)-(theta-phi).*sin(theta-phi))./sqrt(x.^2+(r1.*theta).^2+(r2.*phi).^2+z.^2+2.*r2.*x.*phi.*cos(phi)-2*r1.*x.*theta.*cos(theta)-2*r1.*r2.*theta.*phi.*cos(theta-phi));
for i=1:length(x)
M(i) = integral2(@(theta,phi)f(theta,phi,x(i)),8.2*pi,10.2*pi,10.2*pi,15.2*pi);
end
figure;
plot(x,M);
.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by