Docstoc

ORIENTATION SESSION Becoming familiar with MATLAB

Document Sample
ORIENTATION SESSION Becoming familiar with MATLAB Powered By Docstoc
					Orientation session                                                                               5

                           ORIENTATION SESSION

                      Becoming familiar with MATLAB
    MATLAB is a commercial “Matrix Laboratory” package which operates as a very sophisticated
calculator with graphical output, as well as an interactive programming environment. It is ideal for
carrying out the kinds of calculations that are common in the field of theoretical optics. Therefore,
before starting to work through the exercises which comprise the succeeding weekly sessions, it is
important that you gain at least some small degree of familiarity with Matlab.
    The following exercises are designed to get you started. They are written on the assumption
that you have no experience with using the Linux operating system, or with MATLAB itself. If you
already have some competence in this area you can go through the first exercises quickly and get to
Exercise OP0.2.
                     This session should take one week to complete.

OP0.1      MATLAB tutorial
     With the desktop open, click on the web browser icon on the task bar. This opens the web
     browser, FireFox. Go to the Computational Physics website:
                http://www.physics.usyd.edu.au/teaching/comphys2/cp2optics/
     Then go to the link MATLAB tutorial. A brief web based tutorial, entitled Just Enough
     MATLAB, comes up. You should consider this your basic reference: if you run across any
     MATLAB-related problems in this course, you should find an answer in this tutorial.
     However, right now, you are asked to work through a much shorter document entitled A
     Very Brief Guide to MATLAB which you will find on page 77 of these notes. You are
     asked to work through and understand all the commands described in this guide. Keep the
     Tutorial open while you are doing this, and if you find something you don’t understand, refer
     to the Tutorial.
     When you are working through this exercise — indeed whenever you work with MATLAB
     — we recommend that you try to keep the desktop tidy. Close windows that you don’t need
     and arrange the others so that you can see all the ones you do need without having to go
     hunting for them. In this exercise you might like to work with the browser and MATLAB
     command windows arranged like this. (Note: This picture was taken on a Macintosh with a
     student version of MATLAB.)
6                                                                            Orientation session

    If you cannot finish working through the guide within the two hours of the laboratory session,
    you will need to spend extra time before next weekly session, in which you will be expected
    to think about Optics and not get hung up on computing details.
    Incidentally, pay particular attention to the statements made under the section Command
    structures:
            Never use a loop when you can use a matrix instead.
    When you first learn MATLAB, it is very tempting to say to yourself that working with
    rectangular matrices is a luxury that you can put off till you know it all more thoroughly.
    That’s a bad way of thinking. If you want to use MATLAB efficiently you need to use it as
    it was designed. And this means thinking in terms of matrices.

    Extracting information from graphs with MATLAB

    In the succeeding exercises you will be working a lot with graphs — firstly drawing them
    and then extracting information from them in order to interpret what they are showing you.
    The MATLAB commands zoom on and grid on will help you do this. You can also use
    the Data Cursor on the graph’s menubar.
    For greater accuracy, the ginput tool is a powerful way to measure co-ordinates on Matlab
    figures. Following the procedure outlined below, but check help for variations.


                 Measuring position co-ordinates accurately
                 (1) In the command window type: ginput <Enter>
                 (2) Return the mouse to the figure. A set of crosshairs appears.
                 (3) Position these over the required point (zoom in if necessary)
                     and click the mouse. Repeat for as many points as required.
                 (4) When you are finished press <Enter> (with the mouse still
                     positioned over the figure).
                 (5) The co-ordinates of your points are displayed in the com-
                     mand window. If there aren’t enough significant figures, type
                     format long in the command window and repeat.




NOTES.
Orientation session                                                                                 7

OP0.2      Test-yourself problems
         The following are some short problems which you ought to be able to do after going through
     the guide. They by no means cover all that is in the tutorial, but concentrate on the kind of
     MATLAB skills that will be important in the Optics exercises in the following weekly sessions.
         Enter the answers in the boxes provided and at the end of the session ask the tutor to
     initial your answers and check how many problems you got done.

      (a) Let the variable A be a row matrix (2, 4, 0, -1, 3), and B be a column matrix whose
          five elements are 2, 5, 8, 3, -5, in that order. Calculate the quantity A * (B+1).
          Enter the answer in the box.
                                                                               14

      (b) Set up the vector v = (0, 1, 2, 3 . . . 50) and calculate the length of this vector, |v|, as
          given by the formula:    |v| =
                                            √
                                                v.v
                                                                             207.2

      (c) In MATLAB there is a function called eomday which calculates the quantity X, given the
          quantities M and Y. Choose the values M = 2 and Y = 8 and calculate the corresponding
          value of X.
                                                                               29

      (d) Write a small script to calculate,         n=1,000,000
                                               J =                 n2
                                                        n=1

          using a loop structure. Write your script in this box.

                                         J=0;
                                         for n=1:1000000;
                                            J = J + nˆ2;
                                         end;
                                         disp(J);
          Run your script and enter the result of the calculation in this box.

                                                                        3.333338e+17

      (e) Repeat the calculation in Problem OP0.2(d), with n3 instead of n2 . This time work
          directly in the command window without using a loop structure. (You will need to
          construct a vector for n and n3 , and use the MATLAB command sum). Write the
          necessary statements in this box.


                                         n=1:1000000;
                                         J = sum(n.ˆ3)

          Again enter the result of the calculation in this box.

                                                                   2.500005000002462e+23
8                                                                                                                                           Orientation session

    (f) When a simple harmonic wave travels in a one dimensional medium, its shape is a sine
        wave. When a simple harmonic wave travels on a two dimensional surface, radially out-
        wards from a central point, the corresponding wave shape is called a Bessel function,
        usually denoted by the symbol J0 (x).
        Write a script to do the following calculation. Construct x as a row vector consisting
        of these values: 0, 0.001, 0.002, 0.003, . . . 20.000. Then calculate the row vector for y =
        J0 (x). You can do this with the MatLab command
                     y = besselj(0,x);
        Plot the variable y against x and read off from the graph, accurate to 7 significant figures,
        the lowest value of x for which J0 (x) = 0. Note: you will need to use the zoom and grid
        facilities, and the command ginput for this exercise.

                                                                                                                                        2.404826

    (g) Consider a rectangular array of                     1       .       0




                                                                                    o           o       o           o       o           o          o           o       o




        45 points on the Cartesian plane:
                                                                                                                            o           o          o           o       o
                                                                                    o           o       o           o




                                                y
                                                        0           .       5




                                                                                    o           o       o           o       o           o          o           o       o




                                                                                    o           o       o           o       o           o          o           o       o




                                                    0           .       0




                                                                                0
                                                                                    o




                                                                                        .   0
                                                                                                o




                                                                                                    0
                                                                                                        o




                                                                                                            .   5
                                                                                                                    o




                                                                                                                        1
                                                                                                                            o




                                                                                                                                .   0
                                                                                                                                        o




                                                                                                                                               1
                                                                                                                                                   o




                                                                                                                                                       .   5
                                                                                                                                                               o




                                                                                                                                                                   2
                                                                                                                                                                       o




                                                                                                                                                                           .   0
                                                                                                                                                                                   x


        The lowest left-hand corner is coincident with the origin, and the distance between
        neighbouring points, both in the vertical and the horizontal directions, is 0.25 cm.
        You are asked to construct a matrix with 5 rows and 9 columns, called Xp, each element
        of which is the x-coordinate of the corresponding point on the plane. Write in this box
        a single MatLab statement which calculates Xp in terms of the row vector [0:1:8] and
        the column vector ones(5,1).


                                  Xp = ones(5,1)*[0:1:8]*0.25

        Write in the second box a single MatLab statement which calculates the corresponding
        y-coordinates in terms of the column vector [4:-1:0]’ and the row vector ones(1,9).


                                 Yp = [4:-1:0]’*ones(1,9)*0.25

        Plot Yp against Xp, as individual points (use plot(Xp,Yp,’o’)), and you should re-
        produce the diagram above. On the same graph (use hold on), plot the quadrant of a
        circle of radius 1 with the commands
                      >>theta = [0:pi/100:pi/2]; plot(sin(theta), cos(theta));
        Calculate the fraction of the 45 points which lie within (or on) the quadrant, and write
        your answer (as a percentage) in the first answer box.
        If you had used many more points, so that the spacing between points was much less
        than 0.25, what numerical value should this fraction tend to? Answer in the second
        box.

                                          38%                                                                           39.27%                                 (π/8)


                                                                                                                                            Tutor’s initials

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:8
posted:4/22/2010
language:English
pages:4
Description: ORIENTATION SESSION Becoming familiar with MATLAB