course work assignment paper 2012

Upload: silvi-valimana

Post on 04-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Course Work Assignment Paper 2012

    1/15

    School of Physics, Astronomy and Mathematics

    4PAM1008 MATLAB

    Coursework2012, Semester B

    tr11abf

    March 2012

  • 7/30/2019 Course Work Assignment Paper 2012

    2/15

    4PAM1008 MATLAB Coursework 2012

    2

    InstructionsYou will need the following files all of which can be accessed through the StudyNet assignment page:

    1. Coursework assignment documentthis file.2. PixelHighlight.m3. LogImageDisplay.m4. IMAGE001.JPG5. IMAGE002.JPG

    Enter your answers in this document as directed It is strongly recommended that you develop your code using m-files. Once the code is finished and

    de-bugged you can copy that code into your answer script and also re-generate data and figures

    instantly if required.

    Not every part of a question will necessarily require MATLAB. You must include the MATLAB code you have written with your answers. Always include any plots you are asked to generate. If you have been using the diary feature, do NOT simply copy and paste your diary file and submit

    that as an answer. You are required to provide well presented and formatted MATLABcode in your

    answers where directed.

    When you have completed your coursework you will need to submit the following online:

    1. This document with completed answers.2. Your completed function files for Task 2

    a. PixelHighlight.mb. LogImageDisplay.m

    To submit your work, go to the 4PAM1008 module page on StudyNet, and select Assignments from the

    menu on the left of the page. Then press Submit assignment next to the MATLAB coursework, and follow

    the on-screen instructions.

    Student Details

    Please enter your name (as it appears on your student card) and student number below:

    Student Name THOMAS ROBERTS

    Student Number 11568941

  • 7/30/2019 Course Work Assignment Paper 2012

    3/15

    4PAM1008 MATLAB Coursework 2012

    3

    Task 1: Mean Free Path

    The mean free path is the average distance a particle can travel before colliding with another.

    To determine this, we can use the equation given below, which assumes all the particles have the same

    diameter and that the particle moving through the volume is moving very fast with respect to the others.

    Where dis the diameter of the particles, and nvis the number of particles per unit volume.

    Question 1

    Using the ideal gas law, Equation (2), calculate nvfor the number of particles per unit volume at standard

    atmospheric pressure (1.01x105Nm

    -2), and room temperature (293K).

    Type your answer in the shaded box below.

    2.4979e+025 (1 mark)

    Where P is pressure, Vis volume,Nis the number of molecules, kB is the Boltzman constant (1.38x10-23

    JK-1

    )

    and Tis temperature.

  • 7/30/2019 Course Work Assignment Paper 2012

    4/15

    4PAM1008 MATLAB Coursework 2012

    4

    Question 2

    Use your answer to question 1 to calculate the mean free path, l, in metres of a molecule in air assuming an

    average diameter of a molecule is 2x10-10

    m3.

    3.1858e-007 (1 mark)

    Question 3.

    Use MATLAB to calculate the mean free path for particles in the range m using asuitable interval to generate at least 100 values. Assume room temperature and pressure

    Plot a graph showing how the mean free path changes with particle size.

    In the box below:

    1. Copy the graph created in MATLAB (in the figure window select edit -> Copy Figure) and paste.2. Copy and paste the code used to answer Q3.

    x = (1*10^(-10)):0.05*(10^(-10)):(1*10^(-9));Lx = 1./(pi*(x.^2)*N);plot(x,Lx)

    (4 marks)

    1 2 3 4 5 6 7 8 9 10

    x 10-10

    0

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4x 10

    -6

  • 7/30/2019 Course Work Assignment Paper 2012

    5/15

    4PAM1008 MATLAB Coursework 2012

    5

    Question 4

    Consider now 1000 particles at standard temperature and pressure. Using the ideal gas equation, calculate the

    volume occupied.

    4.0034e-023 (1 mark)

    Consider this volume as a tiny cube and calculate the length of the sides

    3.4209e-008 (1 mark)

    Question 5

    In this question you are going to create 1000 random x,y,z coordinates to represent the positions of these

    particles inside the cube whose size you determined in Q4 and then plot them in a 3-D scatter chart

    Use the randfunction to generate a 1000 x 3 matrix of x,y,z coordinates in the range specified by the size of

    the cube calculate in Q4. Remember that the randfunction by default generates random numbers between 0

    and 1 so you will have to scale the results to be between 0 and the length of the side of the cube. We will

    consider column 1 to be x-values, column 2 to be y-values and column 3 to be z-values.

    Now plot those locations using the scatter3 function to plot each particle location as a point(look at theMarkerType property in the help documentation for scatter3. Label the axes as X, Y and Z and add

    a title to the plot: Particle Locations.

    In the box below:

    1. Copy the graph created in MATLAB (in the figure window select edit -> Copy Figure) and paste.2. Copy and paste the code used to answer Q5.

  • 7/30/2019 Course Work Assignment Paper 2012

    6/15

    4PAM1008 MATLAB Coursework 2012

    6

    %% Question 5a = Side.*rand(1000,3);

    X = a(:,1);Y = a(:,2);Z = a(:,3);scatter3(X,Y,Z, 'g.')title('Particle Locations')xlabel('X')ylabel('Y')zlabel('Z')

    (5 marks)

    Question 6

    Imagine a sphere at the centre of the cube containing the particles whose locations you generated in Q5.

    In this question you are going to plot the particle locations again as you did in Q5 and then highlight all the

    particles occupying a sphere of radius 0.3 width of the cube centred on the specified particle.

    To do this you need to carry out the following steps.

    1) Determine the radius of the sphere and put your answer below.

    1.0263e-008 (1 mark)

    2) You will need to find all the particles in your 1000 x 3 matrix of position values whose location in thebox falls within the sphere. For each particle with positionx,y,z you will need to do the following logical

    0

    1

    2

    3

    4

    x 10-8

    0

    1

    2

    3

    4

    x 10-8

    0

    1

    2

    3

    4

    x 10-8

    X

    Particle Locations

    Y

    Z

  • 7/30/2019 Course Work Assignment Paper 2012

    7/15

    4PAM1008 MATLAB Coursework 2012

    7

    comparison.

    ris the radius and the centre of the cube (ie the location of the reference particle) is given by .

    To do this, implement the above expression as MATLAB code to return indices to each row in your 1000

    x 3 matrix of particle positions which satisfy the above comparison.

    Hint

    Section 3.2.2 in your notes shows you how to access whole rows or columns of a matrix. Section 3.6

    shows you how to do logical operations

    Use the indices to extract all particle locations from your matrix satisfying the inequality above to createa new n x 3 matrix containing the n particles inside the sphere.

    Now plot the original 1000 particles exactly as you did in Q5. To highlight the particles within the sphere

    you simply plot your subset of particles in the same scatter plot but use a different colour.

    Hint

    Look at section 4.1 of your notes to see how to plot multiple data sets in the same axes.

    Copy and paste your figure in the space below.

  • 7/30/2019 Course Work Assignment Paper 2012

    8/15

    4PAM1008 MATLAB Coursework 2012

    8

    (6 marks)

    0

    1

    2

    3

    4

    x 10-8

    0

    1

    2

    3

    4

    x 10-8

    0

    1

    2

    3

    4

    x 10-8

    X

    Particle Locations with Central Sphere

    Y

    Z

  • 7/30/2019 Course Work Assignment Paper 2012

    9/15

    4PAM1008 MATLAB Coursework 2012

    9

    Task 2: Image Enhancement

    Background

    For many years researchers at the university have been developing techniques to study microscopicatmospheric particles. They use a technique known as laser light scattering. Particles which are only a few

    microns across are difficult to image conventionally (ie take a photo) because one is approaching the

    resolution limit. However, if a small particle passes through a laser beam, light is scattered by that particle and

    if you capture an image (with a CCD camera) of the scatteringpattern, its structure will depend on the shape

    and other characteristics of the particle. Below is an example scattering pattern of a tiny spherical drop.

    Figure 1. Typical scattering pattern from a spherical droplet

    As you can see the light is scattered in such a way as to produce concentric rings. A fibre would produce a

    completely different pattern.

    For this question you will be required to complete two tasks. In each case you will need to write a MATLABfunction which changes the way the image is displayed in a manner which assists in the analysis of the

    images. In order to be able do this you must understand how images are handled in MATLAB.

    Images in MATLAB

    You will have been supplied with two images to work with called IMAGE001.jpg (figure 1) and

    IMAGE002.jpg. These are grayscale, ie no colour, and are represented in MATLAB as a matrix of integer

    values between 0 and 255. You can use the MATLAB function imreadto load an image into the MATLAB

    workspace. When you do, if you double-click on the matrix containing the image in the Workspace window it

    will open the matrix in the Variable editor(a spread sheet view of the data). Figure 2 shows a few of the pixel

    values of one of the images. Each element of the matrix is one image pixel and the value can be between 0 and

    255.

  • 7/30/2019 Course Work Assignment Paper 2012

    10/15

    4PAM1008 MATLAB Coursework 2012

    10

    Figure 2 Variable Editor showing part of an image matrix

    Those values are indices which specify a row in a 256 x 3 matrix called a colormap (ie a lookup table)an

    example of which is shown in figure 3. In MATLAB the brightness of a pixel is represented by a value

    between 0 and 1 (0 is black and 1 is brightest). The colormap contains 3 columns, one each for specifying the

    colours red, green and blue.

    As an example, consider the pixel value at element 18, 218 in figure 2.

    The value is 10. This means that the pixel will be displayed with thecolour specified in row 11 (figure 3)red = 0.0392, blue = 0.0392,

    green = 0.0392. Note two important points.

    1. You must add 1 to the index value to get the correct row.

    Thats because in MATLAB the first row (or column) is number 1 but

    the lowest index value in the image can be 0.

    2. You will notice that each row in the table contains identical values

    That is because this is a grayscale colormap, (which is what you aregoing to create). To display an image as grayscale, simply have equal

    values for read, green and blue channels in the colormap.

    The key point to remember is this. The colour / brightness of a displayed image is determined by the

    colormap. You can change the way an image looks by using different colormaps, you do not have tochange the image data.

    Figure 3 A grey-scale lookup table

    (colormap)

  • 7/30/2019 Course Work Assignment Paper 2012

    11/15

    4PAM1008 MATLAB Coursework 2012

    11

    0 50 100 150 200 250 3000

    0.2

    0.4

    0.6

    0.8

    1Standard colormap function

    Image Pixel Intensity Value (0 - 255)

    ImagedisplayIntensityValue(0-1)

    Thus the color map is a function (figure 4) which

    maps the input pixel value (x-axis) to the output

    display value (y-axis). Figure 4 specifies a graylevel

    colormap completely since all color channels are thesame.

    Consider figure 5 which is generated from the same image matrix as figure1 but using a different colormap.

    Figure 5 Scattering image with false colour

    This question requires you to create two MATLAB functions which will change the way images are

    displayed. In both cases it involves creating and modifying a colormap. You will need to make use of several

    built in MATLAB functions

    image Displays an image in a figure window

    imread Load an image and store in a MATLAB

    matrix.

    colormap Manage colormaps.

    Figure 4: Standard colormap function

  • 7/30/2019 Course Work Assignment Paper 2012

    12/15

    4PAM1008 MATLAB Coursework 2012

    12

    Use the MATLAB documentation to learn how to use these and any other functions you require.

    Question 1

    If you look at the image in figure 1 you can see that near the centre some of the pixels appear completely

    white. That is, it appears that some of the pixels have a value of 255. Such pixels are said to be saturatedand

    this means the image is overexposed. This can be a problem when trying to analyse the scattering patterns

    because some of the pattern structure is lost. A quick way to see if an image does contain saturated pixels is to

    use a colormap which colours all saturated pixels red.

    In this task you must write the code for the function PixelHighlight.m to colour all saturated pixels red.

    Figure 6 shows the results you should expect for IMAGE001.

    Figure 6: IMAGE001 with saturated pixels highlighted

    Use the function file PixelHighlight.mas your template and do the following

    1. Create and apply the colormap to highlight saturated pixels in the image.You can use the colormap function to create the standard grayscale colormap and then you

    only need to do a very slight alteration to highlight saturated pixels.

    2. Display the image with the new colormap in a new figure window (use the figure command).

  • 7/30/2019 Course Work Assignment Paper 2012

    13/15

    4PAM1008 MATLAB Coursework 2012

    13

    3. Return the colormap in the output variablemyMap.When you create the colormap, assign it to the variable myMap then it will be returned automatically

    by the function.

    4. Return your student number in the output variable studentNumber.5.

    Write comments to explain how to use the function. Have a look at how comments are written forother functions.

    So, when the function is called, the user should see a new figure window with the image displayed and the

    saturated pixels highlighted in red as in figure 6. Try it out on the other image, see what happens.

    Copy and paste the text from your function PixelHighlight.m in the box below (Remember,you must also

    submit the function file along with this document).

    function [myMap, studentNumber] = PixelHighlight(im)

    %PixelHighlight Displays the specified image and highlights saturatedpixels.%In the first line, change 'IMAGE001.jpg' to your own image final%The second line loads the colormap gray(256) - this is a 256 bit

    greyscale%colormap - and renames is ukcolourmap (with 'colour' spelt the English

    way)%The third line change the fully saturated pixels to display RED only.%The fourth line renames the finished colormap to 'myMap'%The fifth line tells MATLAB to display the image using the modified%colormap.

    % Thomas Roberts% 25 / 04 / 2012

    % Replace the number below with your student number.studentNumber = 11568947;

    %% Add your code below.

    im = imread ('IMAGE001.jpg');ukcolourmap = (gray(256));ukcolourmap (256,:) = [1 0 0];myMap = colormap(ukcolourmap);image(im)

    (8 marks)

  • 7/30/2019 Course Work Assignment Paper 2012

    14/15

  • 7/30/2019 Course Work Assignment Paper 2012

    15/15

    4PAM1008 MATLAB Coursework 2012

    15

    Figure 8: IMAGE002, standard colormap Figure 9: IMAGE002, Logarithmic colormap

    Copy and paste the text from LogImageDisplay.min the box below (Remember,you must also submit the

    function file along with this document).

    (10 marks)