MATLAB: Plotting graph of negative values of array in integral2 with 3 variables

integral2numerical integration

I am new with Matlab please help me regarding plotting a graph of double integral with respect to third variable. The third variable is an angle, in range of -80 to 80 degrees. I want to plot the graph also for negative values but, matlab does not allow negative indices of matrix. The last version of the code is as follows, but it does not give the solution of negative values of n as I want, just shifting z values of 1:numel(n) to -80 to 80.
u0= 4*pi*10.^-7;N1 = 15; N2 = 15 ; L1 = 8.215*10.^-6;L2 = 8.215*10.^-6;
r1 = 0.03; r2=0.03; d=0.1; yaxis=0;
K1 =(N1.*N2.*u0)./(4.*pi.*sqrt(L1.*L2))
n=-80:80;
ktheta=zeros(size(n))
for z=1:numel(n)
fun=@(x,y,z)(r1.*r2.*sin(x).*sin(y)+r1.*r2.*cos(x).*cos(y).*cosd(z))./sqrt((r1.^2)+(r2.^2)+(yaxis.^2)+(d.^2)-2.*r1.*y.*sin(x)-2.*r1.*r2.*cos(x).*cos(y)+(2.*r2.*y.*sin(y)-2.*r1.*r2.*sin(x).*sin(y)).*cosd(z)+2.*r2.*d.*sin(y).*sind(z));
ktheta(z) = K1*integral2(@(x,y)fun(x,y,z),0,2*pi,0,2*pi)
end
plot(n,ktheta)

Best Answer

Not sure what you are trying to do but see if the below does what you want:
u0= 4*pi*10.^-7;
N1 = 15;
N2 = 15 ;
L1 = 8.215*10.^-6;
L2 = 8.215*10.^-6;
r1 = 0.03;
r2=0.03;
d=0.1;
yaxis=0;
K1 =(N1.*N2.*u0)./(4.*pi.*sqrt(L1.*L2));
z=-80:80;
ktheta=zeros(size(n));
fun=@(x,y,z)(r1.*r2.*sin(x).*sin(y)+r1.*r2.*cos(x).*cos(y).*cosd(z))./sqrt((r1.^2)+(r2.^2)+(yaxis.^2)+(d.^2)-2.*r1.*y.*sin(x)-2.*r1.*r2.*cos(x).*cos(y)+(2.*r2.*y.*sin(y)-2.*r1.*r2.*sin(x).*sin(y)).*cosd(z)+2.*r2.*d.*sin(y).*sind(z));
for k=1:numel(n)
ktheta(k) = K1*integral2(@(x,y)fun(x,y,z(k)),0,2*pi,0,2*pi)
end
plot(z,ktheta)