lab4 document

Upload: ali-ahmad

Post on 08-Aug-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/22/2019 Lab4 Document

    1/12

    Electromagnetics Fields and Waves

    Cylindrical CoordinatesWith

    MATLAB

    Ahmed Junaid OmerDepartment of Electronics

    Air University

  • 8/22/2019 Lab4 Document

    2/12

    Quadric surfaces in Cylindrical Coordinates

    If a point is described in cylindrical coordinates as in Figure 1, the equations of

    transformation between cylindrical coordinates and Cartesian coordinates are as follows:

    Figure 1: The geometry of cylindrical coordinates

    zz

    ry

    rx

    =

    =

    =

    sin

    cos

    (1)

    Note further that the relation between x, y and r is given by

    22

    yxr += (2)

    Example 1

    Sketch the graph of the equation 22 yxz += .

    Solution. Lets sketch the graph of 22 yxz += over the rectangular region

    ( ){ }11,11: = yxyx,D 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')

    y

    r

    x

  • 8/22/2019 Lab4 Document

    3/12

    Figure 2: The graph of 22 yxz += 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

    surface in Figure 2 by transforming the equation 22 yxz += into cylindricalcoordinates.

    If you substitute equations 1 into 22 yxz += , you arrive at the following result.

    ( ) ( )

    ( )2

    222

    2222

    22

    22

    sincos

    sincos

    sincos

    r

    r

    rr

    rr

    yxz

    =

    +=

    +=

    +=

    +=

    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 yx& . Therefore, MATLABsmeshgrid command was used to create a grid of ( )yx, coordinate pairs. In the

    present case MATLABs meshgrid command will be used to create a grid of ( ),r pairs.

  • 8/22/2019 Lab4 Document

    4/12

    Once you have created a grid of ( ),r pairs, then equations 1 can be used to change

    to rectangular coordinates ( )yx, . Finally, z -values are computed with 2rz= . Notethe 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')

    -1

    -0.5

    0

    0.5

    1

    -1

    -0.5

    0

    0.5

    10

    0.2

    0.4

    0.6

    0.8

    1

    x-axis

    The graph of z=x2+y2

    y-axis

    z-axis

    Figure 3: The graph of 22 yxz += in cylindrical coordinates.

    Complex numbers

  • 8/22/2019 Lab4 Document

    5/12

    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

    14

    2

    2

    2= y

    xz (3)

    Substitute equations 1 into equations 3 and solve for r.

    ( )( )

    ( )

    22

    2

    2222

    22222

    2

    2

    2

    2

    2

    2

    sin4cos

    44

    sin4cos44

    4sin4cos4

    1sin4

    cos

    14

    +

    =

    +=

    =

    =

    =

    zr

    rz

    rrz

    rr

    z

    yx

    z

    (4)

    Note that rwill 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 ris a function of &z , lets begin by creating a mesh of &z values. You willwork at the command line in this example.

    Phi=0:pi/2:2*pi; z=-2:0.1;2;

    [z, Phi]=meshgrid (z, Phi);

    Use equation 4 to compute the r-values.

  • 8/22/2019 Lab4 Document

    6/12

    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 rare real numbers. Youcan 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 arenon-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 biaz += , 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.

  • 8/22/2019 Lab4 Document

    7/12

    See also ISREAL, IMAG, CONJ, ANGLE, ABS.

    Now lets find which entries of the matrix rare complex numbers.

    k=find(real(r)~=r);

    The symbol ~= is MATLABs symbol for (not equals).The vector k now contains theindex of each complex entry of the matrix r. We want to replace each of these entrieswith NaN.

    r(k)=NaN;

    Now use the transformations 1 to change from Cylindrical coordinates to CartesianCoordinates.

    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 distortionsand 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])

  • 8/22/2019 Lab4 Document

    8/12

    Figure 4: A hyperboloid with two sheets

  • 8/22/2019 Lab4 Document

    9/12

    Figure 5: Formatting the image

    Advance Planning

    With a little careful planning, complex numbers can be avoided. Lets try a differentapproach on the graph of the equation of example 2.

    Example 3

    Sketch the graph of the equation.

    14

    2

    2

    2= y

    xz (5)

    Once again, substituting equations 1 into equations 5 yields the following result.

    22

    2

    sin4cos

    44

    +

    =

    zr (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

  • 8/22/2019 Lab4 Document

    10/12

    avoided. Since the denominator of the expression underneath the radical in equation 6

    must always be positive, rwill be complex only if the numerator 44 2 z is negative, So,what values of zmake the expression 44 2z non-negative ?

    1

    1

    1

    44

    044

    2

    2

    2

    2

    z

    z

    z

    z

    z

    But 1z only if 1z . Therefore, if we restrict the values of zso that they are less

    than or equal to -1 or greater than or equal to 1, rwill be a real number. The followingMATLAB 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.

  • 8/22/2019 Lab4 Document

    11/12

    Figure 6: Advance planning avoids complex numbers.

    The End (&)

    Have a nice day

  • 8/22/2019 Lab4 Document

    12/12