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

2 Ansichten (letzte 30 Tage)
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 Kommentare
madhan ravi
madhan ravi am 7 Apr. 2019
What is your question? Your code works fine? But you to plot a 3D graph?
Cem Tuncer
Cem Tuncer am 7 Apr. 2019
Bearbeitet: Cem Tuncer am 7 Apr. 2019
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)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

madhan ravi
madhan ravi am 7 Apr. 2019
Bearbeitet: madhan ravi am 7 Apr. 2019
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 Kommentar
Cem Tuncer
Cem Tuncer am 7 Apr. 2019
Yes, exactly this! thank you very much Sire. It seems I was low at defining indice - value difference.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 7 Apr. 2019
Bearbeitet: dpb am 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 Kommentare
Cem Tuncer
Cem Tuncer am 7 Apr. 2019
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.
dpb
dpb am 7 Apr. 2019
Bearbeitet: dpb am 7 Apr. 2019
"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...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by