Cylindrical Coordinates Matlab: Electromagnetics Fields and Waves
Cylindrical Coordinates Matlab: Electromagnetics Fields and Waves
Cylindrical Coordinates Matlab: Electromagnetics Fields and Waves
Cylindrical Coordinates
With
MATLAB
(1)
(2)
Example 1 Sketch the graph of the equation z = x 2 + y 2 . Solution. Lets sketch the graph of z = x 2 + y 2 over the rectangular region D = { ( x, y ) : 1 x 1,1 y 1} with the following sequence of MATLAB commands
x=-1:.1:1; y=-1:.1:1; [xx,yy]=meshgrid(x,y); zz=xx.^2+yy.^2; mesh(xx,yy,zz); grid on; xlabel('x-axis') ylabel('y-axis') zlabel('z-axis') title('The graph of z = x^2+ y^2')
Figure 2: The graph of z = x 2 + y 2 over a rectangular domain Although the image in figure 2 is nice, this particular surface is not well suited to be drawn over a rectangular region. You can get a much better representation of the z = x 2 + y 2 into cylindrical surface in Figure 2 by transforming the equation coordinates. If you substitute equations 1 into z = x 2 + y 2 , you arrive at the following result.
z = x2 + y2 = ( r cos ) + ( r sin )
2 2
It is important to note that z is a function of both r & , even though is not explicitly present. In example 1 , z was a unction of x & y . Therefore, MATLABs meshgrid command was used to create a grid of ( x , y ) coordinate pairs. In the present case MATLABs meshgrid command will be used to create a grid of ( r , ) pairs.
Once you have created a grid of ( r , ) pairs, then equations 1 can be used to change to rectangular coordinates ( x , y ) . Finally, z -values are computed with z = r 2 . Note the use of array operators in all calculations.
R=0:.1:1; Phi=0:pi/12:2*pi; [r,Phi]=meshgrid(R,Phi); x=r.*cos(Phi); y=r.*sin(Phi); z=r.^2; mesh(x,y,z); grid on xlabel('x-axis') ylabel('y-axis') zlabel('z-axis') title('The graph of z=x^2+y^2')
The graph of z=x 2+y 2
1 0.8 0.6 z-axis 0.4 0.2 0 1 0.5 0 -0.5 y-axis -1 -1 -0.5 x-axis 0.5 0 1
Complex numbers
Complex number appear when you attempt to take square root of a negative number. MATLABs mesh command will fail if any input is complex. Therefore complex numbers need special attention. Example 2 Sketch the graph of the equation
z2 x2 y2 =1 4
(3)
r.
(4)
Note that r will be a complex number if the numerator of this last expression is negative. This can cause some difficulty in MATLAB. If any complex number are passed to MATLABs mesh command the routine will crash and refuse to draw your surface. Therefore, you must remove all complex numbers from your matrices before passing them to mesh command. You can do this by replacing each occurrence of a complex number
with NaN (not a number). help NaN NaN Not-a-Number. NaN is the IEEE arithmetic representation for Not-a-Number. A NaN is obtained as a result of mathematically undefined operations like 0.0/0.0 and inf-inf. See also INF. Because r is a function of z & , lets begin by creating a mesh of z & values. You will work at the command line in this example. Phi=0:pi/2:2*pi; z=-2:0.1;2; [z, Phi]=meshgrid (z, Phi);
r -values.
r=sqrt((4*z.^2-4)./(cos(Phi).^2+4*sin(Phi).^2)); MATLAB has a routine that will check if your matrix has any complex entries. help isreal ISREAL True for real array. ISREAL(X) returns 1 if all elements in X have zero imaginary part and 0 otherwise. ~ISREAL(X) detects complex arrays (i.e., arrays that have a non-zero real part). See also REAL, IMAG, I, J. Check if matrix r has any complex entries. isreal(r) ans = 0 The response 0 indicates that not all of the entries of the matrix can find the complex entries with MATLABs find command. help find FIND Find indices of nonzero elements. I = FIND(X) returns the indices of the vector X that are non-zero. For example, I = FIND(A>100), returns the indices of A where A is greater than 100. See RELOP. [I,J] = FIND(X) returns the row and column indices of the nonzero entries in the matrix X. This is often used with sparse matrices. If z = a + bi , the real part of z is a and the imaginary part of z is b. In symbol, Re(z)=a and Im(z)=b. A number z is real if and only if Re(z)=z; otherwise, the number is complex. help real REAL Complex real part. REAL(X) is the real part of X. See I or J to enter complex numbers.
See also ISREAL, IMAG, CONJ, ANGLE, ABS. Now lets find which entries of the matrix k=find(real(r)~=r); The symbol ~= is MATLABs symbol for (not equals).The vector k now contains the index of each complex entry of the matrix r . We want to replace each of these entries with NaN. r(k)=NaN; Now use the transformations 1 to change from Cylindrical coordinates to Cartesian Coordinates.
x = r.*cos(Phi); y=r.*sin(Phi);
The following command will produce an image similar o figure 4. mesh(x,y,z) Add labels and a title and, more importantly, scale the axes equally to remove distortions and see that cross sections of the surface parallel to the xy-plane are more elliptical than circular. In m-file the complete program can be written as.
Phi=0:pi/12:2*pi; Z=-2:0.1:2; [z,Phi]=meshgrid(Z,Phi); r=sqrt((4*z.^2-4)./(cos(Phi).^2+4*sin(Phi).^2)); k=find(real(r)~=r); r(k)=NaN x = r.*cos(Phi); y=r.*sin(Phi); mesh(x,y,z); xlabel('x-axis') ylabel('y-axis') zlabel('z-axis') title('the graph of z^2-x^2/4-y^2 = 1') grid on axis ([-4 4 -4 4 -2 2])
Advance Planning
With a little careful planning, complex numbers can be avoided. Lets try a different approach on the graph of the equation of example 2. Example 3 Sketch the graph of the equation.
z2 x2 y2 =1 4
(5)
Once again, substituting equations 1 into equations 5 yields the following result.
r= 4z 2 4 cos 2 + 4 sin 2
(6)
You will introduce complex numbers only if you attempt to take the square root of a negative number in equation 6. With a little advance planning, complex numbers can be
avoided. Since the denominator of the expression underneath the radical in equation 6 must always be positive, r will be complex only if the numerator 4 z 2 4 is negative, So, what values of z make the expression 4 z 2 4 non-negative ?
4 z 2 4 0 4z 2 4 z 2 1 z2 1 z 1
1 only if z 1 . Therefore, if we restrict the values of z so that they are less But z than or equal to -1 or greater than or equal to 1, r will be a real number. The following MATLAB code was used to produce the image of figure 6.
z=[-3:0.2:-1,1:0.2:3]; Phi=linspace(0,2*pi,36); [z,Phi]=meshgrid(z,Phi); r=sqrt((4*z.^2-4)./(cos(Phi).^2+4*sin(Phi).^2)); x=r.*cos(Phi); y=r.*sin(Phi); mesh(x,y,z) axis equal title('z^2-x^2/4-y^2=1') xlabel('x-axis') ylabel('y-axis') zlabel('z-axis')
The most important command in this sequence is the command z=[-3:0.2:1,1:0.2:3]. This command crafts a vector with values from -3 to -1, incremented by 0.2, then adds numbers from 1 to 3, incremented again by 0.2. This ensure that only z values less than or equal to -1 or greater than or equal to 1 are used, avoiding the complex number issue posed in example 2. Obviously his is a much better approach. It should be the approach you use in the exercise, should complex numbers need to be avoided.
Figure 6: Advance planning avoids complex numbers. The End (&) Have a nice day