MATLAB Answers

Translated by

이 페이지의 내용은 영어에서 자동 번역되었습니다. 자동 번역 기능을 해제하려면 "<a class="turn_off_mt" href="#" onclick="window._kiq.push(['set', { 'event': 'Turn off MT' }]);">여기</a>"를 클릭하십시오.

0

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

Asked by Cem Tuncer on 7 Apr 2019
Latest activity Edited by dpb
on 7 Apr 2019
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 Comments

What is your question? Your code works fine? But you to plot a 3D graph?
It works but not as I want. The results are for the angle z between 1 to 160, not for -80 to 80. I couldnt give negative inputs to the integral because of 'Array indices must be positive integers or logical values.' error. Actually I want the plot of ktheta for the range of angle z as -80 to 80 degrees
for z=-80:80
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(z,ktheta)

Sign in to comment.

2 Answers

Answer by madhan ravi
on 7 Apr 2019
Edited by madhan ravi
on 7 Apr 2019
 Accepted 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)

  1 Comments

Yes, exactly this! thank you very much Sire. It seems I was low at defining indice - value difference.

Sign in to comment.


Answer by dpb
on 7 Apr 2019
Edited by dpb
on 7 Apr 2019

  1. The functional is invariant upon the the loop; remove the definition from inside the loop
  2. Use meaningful name for the degrees array; then won't confuse data with loop indices so easily
  3. 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 Comments

Thank you very much. It strangely work with variable z, instead of dg, dg is giving error. And also thank you for warning on imaginary results! variable y, argument of integral, should be only an angle value. I corrected outside variables y to yaxis, which is constant unrelated with integral.
"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...

Sign in to comment.



Translated by