Plotting graph of negative values of array in integral2 with 3 variables
조회 수: 3 (최근 30일)
이전 댓글 표시
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)
댓글 수: 2
채택된 답변
madhan ravi
2019년 4월 7일
편집: madhan ravi
2019년 4월 7일
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)
추가 답변 (1개)
dpb
2019년 4월 7일
편집: dpb
2019년 4월 7일
- The functional is invariant upon the the loop; remove the definition from inside the loop
- Use meaningful name for the degrees array; then won't confuse data with loop indices so easily
- Loop over the array, don't use data arrays as looping indices:
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))
dg =-80:80;
ktheta=zeros(size(dg),1);
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 i=1:numel(dg)
ktheta(i) = K1*integral2(@(x,y)fun(x,y,dg(i)),0,2*pi,0,2*pi);
end
plot(dg,ktheta)
You have an issue in the functional in that over the range of 0:2*pi for the y argument, it returns complex values. I didn't try to dig into such a lot of stuff to try to figure out which term, specifically is the culprit, but
>> fun(0,0:2*pi,-80)
ans =
0.0016 + 0.0000i 0.0007 + 0.0000i -0.0004 + 0.0000i -0.0012 + 0.0000i 0.0000 + 0.0009i 0.0000 - 0.0002i 0.0000 - 0.0020i
>> fun(0,0:pi,-80)
ans =
0.0016 0.0007 -0.0004 -0.0012
>>
댓글 수: 2
dpb
2019년 4월 7일
편집: dpb
2019년 4월 7일
"It strangely work[s] with variable z, instead of dg,"
Oh! Indeed; I neglected to change the z variable in the anonymous function. The z vector is built into the function detinition when it is defined and so needed
fun=@(x,y,z)(r1.*r2.*sin(x).*sin(y)+r1.*r2.*cos(x).*cos(y).*cosd(dg))./ ...
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(dg)+2.*r2.*d.*sin(y).*sind(dg));
It was that oversight that caused the imaginary result..so, in the end, it's the same fix as madhan's...
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!