Академический Документы
Профессиональный Документы
Культура Документы
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 MATLAB code 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.m b. 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
Student Number
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 d is the diameter of the particles, and nv is the number of particles per unit volume.
Question 1
Using the ideal gas law, Equation (2), calculate nv for the number of particles per unit volume at standard atmospheric pressure (1.01x105Nm-2), and room temperature (293K).
2.4979e+025
(1 mark)
Where P is pressure, V is volume, N is the number of molecules, kB is the Boltzman constant (1.38x10-23JK-1) and T is temperature.
3.1858e-007
(1 mark)
Question 3.
Use MATLAB to calculate the mean free path for particles in the range suitable 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. m using a
1.4
x 10
-6
(4 marks)
1.2
0.8
0.6
0.4
0.2
9 x 10
10
-10
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
Question 5
(1 mark)
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 rand function 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 rand function 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 the MarkerType 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.
(5 marks)
x 10 4
0 4 3 x 10
-8
4 2 1 Y 0 0 1 X 3 2 x 10
-8
%% Question 5 a = 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')
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 the box falls within the sphere. For each particle with position x,y,z you will need to do the following logical
(6 marks)
x 10 4
0 4 3 x 10
-8
4 2 1 Y 0 0 1 X 3 2 x 10
-8
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 MATLAB function 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 imread to 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.
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 the colour 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 are going 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 to change the image data.
10
0.8
0.6
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 the same.
0.4
0.2
300
Consider figure 5 which is generated from the same image matrix as figure1 but using a different colormap.
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 imread colormap Displays an image in a figure window Load an image and store in a MATLAB matrix. Manage colormaps.
11
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 saturated and 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.
Use the function file PixelHighlight.m as 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).
12
function [myMap, studentNumber] = PixelHighlight(im) %PixelHighlight Displays the specified image and highlights saturated pixels. %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.
(8 marks)
% 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)
13
0.8
0.6
0.4
0.2
300
Use the function file LogImageDisplay.m as your template and do the following
1. Create a logarithmic colormap and apply it to the image passed to the function in the variable im. You will need to generate a 256 x 1 array of values from 0 to 1 which follow a logarithmic curve as shown in figure 7. That array will act as the lookup table for each colour in the 256 x 3 colormap. 2. Display the image with the new colormap in a new figure window (use the figure command). 3. Return the colormap in the output variable myMap. 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.
Once you have completed your function, try it on IMAGE002. You should get a result like that shown in figure 9.
14
Copy and paste the text from LogImageDisplay.m in the box below (Remember, you must also submit the function file along with this document).
(10 marks)
15