Difference between revisions of "EGR 103/Fall 2018/Lab 2"
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | <div class="noautonum">__TOC__</div> | |
+ | == Introduction == | ||
+ | Among other things, you will learn how to use Spyder with your CIFS drive as well as how to upload assignments for the class. | ||
+ | |||
+ | == Errors / Additions == | ||
+ | None so far for Fall 2018! | ||
+ | |||
+ | == 2.1 Introduction == | ||
+ | The main purpose of this lab is to go through the process of writing a complete program to load a data set, manipulate the values, perform some analysis, and create a graph. The following is a companion piece to the lab handout itself. The sections in this guide will match those in the handout. | ||
+ | |||
+ | == 2.2 Resources == | ||
+ | You will want to have a browser open with the [[Python:Script]] and [[Python:Plotting]] pages available to you. | ||
+ | |||
+ | == 2.3 Getting Started == | ||
+ | As with last week, you will want to use the | ||
+ | ssh -XY netid@login-teer.oit.duke.edu | ||
+ | command in MobaXterm to connect to a Teer machine. Follow the directions to connect, change into your EGR103 directory, make a new folder, change into it, get the tar file, expand it, then make a copy of your lab. You will also have some startup to do in terms of mounting your CIFS drive and starting Spyder. | ||
+ | |||
+ | === 2.3.1 Preview === | ||
+ | Read and work through this section, taking notes as you go. Type in the commands at the lettered sections to see how Python responds. | ||
+ | |||
+ | === 2.3.2 .py Files === | ||
+ | Read and work through this section, taking notes as you go. | ||
+ | |||
+ | == 2.4 Cantilever Beam Analysis == | ||
+ | Read and work through this section, taking notes as you go. Note especially the process of rearranging the equation to get displacement as a function of force based on the fact that - for this experiment - the independent variable is the force. | ||
+ | |||
+ | To see the data set, go to UNIX (i.e. the MobaXterm window) and type | ||
+ | more Cantilever.dat | ||
+ | You will see an 8x2 array of data. The first column of numbers is the amount of ''mass'' in kg placed on the end of the beam - for this experiment, it ranged from 0 to about 0.8 kg (i.e. about 8 N). The second column of numbers is the displacement measured in ''inches,'' because that is what the device I used measured in. You should always take data in the original units of the device; you can convert it later. | ||
+ | |||
+ | Typically, data sets should have descriptions such as the items in the data set, units, perhaps the equipment and the person who took the measurements. For this week, however, I wanted to give you a rectangular array of data that Python can easily load. | ||
+ | |||
+ | == 2.5 Creating the Script == | ||
+ | Note when you open a second scripts that there are tabs in the editing window; you can choose which one to make active by clicking its tab. Be careful not to accidentally click the part of the tab that closes that tab (the X). | ||
+ | |||
+ | === 2.5.1 Lab Manual Syntax === | ||
+ | For this lab, there will be several times you will simply type some items into the command window to see what they do before actually adding codes to the script. Command line only items are surrounded by a box whereas code that goes in the script has a shadowbox. | ||
+ | |||
+ | === 2.5.2 Comments === | ||
+ | While you are not explicitly required to include comments in your labs -- other than the header and community standard information at the top -- comments are a great way to tell yourself and your TAs what you meant to do with particular chunks of code. Also, note that a line starting with #%% will set off a section in the editor window. It will have no effect on the way the program runs, just what it looks like in your editing window. By creating cells that way, you can also run your code a cell at a time. | ||
+ | |||
+ | === 2.5.3 Importing Modules === | ||
+ | Read and work through this section, taking notes as you go. These lines will import two modules with nicknames. If you want to see what a module has in it, once imported, type help(NAME) - for instance, | ||
+ | help(np) | ||
+ | will show everything in np. If you want help on a particular command, put the the command in the argument - for instance, | ||
+ | help(plt.plot) | ||
+ | |||
+ | === 2.5.4 Loading and Manipulating the Data === | ||
+ | Read and work through this section, taking notes as you go. Note that when you slice arrays, you need to append the .copy() to duplicate the data rather than have two different variables pointing at the same data. | ||
+ | |||
+ | === 2.5.5 Generating Plots === | ||
+ | Read and work through this section, taking notes as you go. Note that the code for this is pretty far under what you've written so far; you will be filling in the blank space later, just like Taylor Swift. | ||
+ | |||
+ | For the "During the lab, the instructor will have you create several different graphs in the command window..." this semester the instructor is going to let you navigate this part on your own. | ||
+ | go ahead and look at the following alternate plotting commands by typing them in the command window only; at the end, there will be a plot command that you paste into your document: | ||
+ | <!-- | ||
+ | ==== Plotting a matrix ==== | ||
+ | plot(Cantilever) | ||
+ | Plots the matrix itself - the data points are plotted by columns with the ''y'' value coming from the data points and the ''x'' value coming from the row of the matrix. That is why there are two lines (two columns of data) and the domain goes from 1 to 8. | ||
+ | ==== Plotting with two inputs ==== | ||
+ | plot(Force, Displacement) | ||
+ | If MATLAB gets two arguments for the plot command, the first will be the independent (typically <math>x</math> coordinates and the second will be the dependent (typically <math>y</math> coordinates). The default case is to connect the dots with blue line segments. Note for this experiment that this plot would receive no credit - among other things, the axes are not labeled and there is no title. Perhaps worse than that, you have connected eight discrete experimental data points with lines. This is not appropriate for this experiment - instead, the data points should be plotted as individual points. | ||
+ | ==== Plotting different ways ==== | ||
+ | Take a look at the help file for plot in MATLAB: | ||
+ | help plot | ||
+ | and in particular the table in the middle of it. The table is also available on the Pundit page for plot at [[MATLAB:Plotting#The_plot_Function]]. The table shows that, for a particular set of independent and dependent values, you can change how MATLAB makes the plot by specifying a color, a symbol, and a line style. In the absence of a color, MATLAB will default to its built in color wheel which starts with blue in the table and works its way down - skipping white. If you give a symbol but no line style, there will be no line. If you give a line style but no symbol, no symbol will appear at each point. If you give neither, solid lines with no symbol will appear. Note that in almost all cases, the order of color, symbol, and line style does not matter - <code>ro-</code> will produce the same as <code>-ro</code>. The ''only'' case where the order matters is when it comes to specifying a dash-dot line versus trying to specify a situation where there are large dots at each point and a solid line connecting the points. Take a look at | ||
+ | plot(Force, Displacement, 'k-.') | ||
+ | versus | ||
+ | plot(Force, Displacement, 'k.-') | ||
+ | as an example | ||
+ | ==== Things to avoid ==== | ||
+ | Here are a few examples to avoid: | ||
+ | |||
+ | plot(Force, Displacement, 'mp') | ||
+ | Uses purple pentagrams. It's adorable, though perhaps not professional. | ||
+ | |||
+ | plot(Force, Displacement, 'bh') | ||
+ | Using symbols that have cultural or religious meanings should be avoided unless wholly appropriate to the data set. | ||
+ | |||
+ | plot(Force, Displacement, 'k+') | ||
+ | The problem here isn't necessarily that the + look like crosses but rather that the data point in the bottom left corner looks like it might be the origin instead of a data point. Later, you will learn to use a command that zooms out so no point is on the figure boundary. For today, just avoid using the +. | ||
+ | ==== The right answer ==== | ||
+ | For this lab, the right answer will be to use black ink and to use circles at the points with no connecting lines, so: | ||
+ | plot(Force, Displacement, 'ko') | ||
+ | --> | ||
+ | |||
+ | === 2.5.6 Polynomials in NumPy=== | ||
+ | Read and work through this section, taking notes as you go. Be sure you and your neighbor understand how Python - using NumPy - interprets polynomials and which commands use them. | ||
+ | |||
+ | === 2.5.7 Generate Predictions === | ||
+ | Read and work through this section, taking notes as you go. This part creates a new set of data points based on your equation so that you can actually plot it. You will thus have your original eight data points in <code>force</code> and <code>disp</code>. You will also have 100 linearly spaced values between the minimum and maximum force value in <code>force_model</code> and the 100 calculated estimates for the displacement, based on your best straight line, in <code>disp_model</code>. | ||
+ | |||
+ | === 2.5.8 Generating Plots (revisited) === | ||
+ | Read and work through this section, taking notes as you go. This part adds the new line to your old graph. | ||
+ | |||
+ | '''CRITICALLY IMPORTANT PART''' - never put .py in a savefig argument! Your graph might override your program! | ||
+ | |||
+ | == 2.6 Uploading PDFs == | ||
+ | In lab, you will upload the PDF of the graph you have now to Sakai. You do '''not''' need to upload an updated graph later - the one from lab (missing the grid and title and such) is fine for the Cantilever assignment. | ||
+ | |||
+ | == 2.7 The Assignment == | ||
+ | Basically, once you get this script running perfectly, you are going to replicate it three times and change it to use three different data sets. '''Note:''' these three data sets are "cooked" - that is, I produced them; they did not come from an experiment. | ||
+ | |||
+ | === 2.7.1 What the .py-files Should Do === | ||
+ | Read and work through this section, taking notes as you go. | ||
+ | |||
+ | === 2.7.2 The Lab Report === | ||
+ | Read and work through this section, taking notes as you go. Note that the data sets have different numbers of points so the bottoms of the data tables will not line up. Also note that you should use the original data (masses and displacements in inches) in the tables -- do not put in the forces or the displacements in meters. You are ''using'' the latter in your calculations but you are presenting the original data sets here. | ||
+ | |||
+ | Here's the code for getting the beam_data numbers to show up on the screen in a way that is easy to copy to emacs: | ||
+ | <source lang=python> | ||
+ | for a in range(len(beam_data/2)): | ||
+ | print('%0.4e' % beam_data[a][0], ' %0.4e' % beam_data[a][1]) | ||
+ | </source> | ||
+ | If you choose to add this to your script - you are certainly welcome to leave it in there when you turn your code it. | ||
+ | |||
+ | === 2.7.3 Processing the Lab Report === | ||
+ | Seriously - spell check! | ||
+ | |||
+ | {{Protected Class Document}} |
Latest revision as of 18:37, 5 September 2018
Introduction
Among other things, you will learn how to use Spyder with your CIFS drive as well as how to upload assignments for the class.
Errors / Additions
None so far for Fall 2018!
2.1 Introduction
The main purpose of this lab is to go through the process of writing a complete program to load a data set, manipulate the values, perform some analysis, and create a graph. The following is a companion piece to the lab handout itself. The sections in this guide will match those in the handout.
2.2 Resources
You will want to have a browser open with the Python:Script and Python:Plotting pages available to you.
2.3 Getting Started
As with last week, you will want to use the
ssh -XY netid@login-teer.oit.duke.edu
command in MobaXterm to connect to a Teer machine. Follow the directions to connect, change into your EGR103 directory, make a new folder, change into it, get the tar file, expand it, then make a copy of your lab. You will also have some startup to do in terms of mounting your CIFS drive and starting Spyder.
2.3.1 Preview
Read and work through this section, taking notes as you go. Type in the commands at the lettered sections to see how Python responds.
2.3.2 .py Files
Read and work through this section, taking notes as you go.
2.4 Cantilever Beam Analysis
Read and work through this section, taking notes as you go. Note especially the process of rearranging the equation to get displacement as a function of force based on the fact that - for this experiment - the independent variable is the force.
To see the data set, go to UNIX (i.e. the MobaXterm window) and type
more Cantilever.dat
You will see an 8x2 array of data. The first column of numbers is the amount of mass in kg placed on the end of the beam - for this experiment, it ranged from 0 to about 0.8 kg (i.e. about 8 N). The second column of numbers is the displacement measured in inches, because that is what the device I used measured in. You should always take data in the original units of the device; you can convert it later.
Typically, data sets should have descriptions such as the items in the data set, units, perhaps the equipment and the person who took the measurements. For this week, however, I wanted to give you a rectangular array of data that Python can easily load.
2.5 Creating the Script
Note when you open a second scripts that there are tabs in the editing window; you can choose which one to make active by clicking its tab. Be careful not to accidentally click the part of the tab that closes that tab (the X).
2.5.1 Lab Manual Syntax
For this lab, there will be several times you will simply type some items into the command window to see what they do before actually adding codes to the script. Command line only items are surrounded by a box whereas code that goes in the script has a shadowbox.
2.5.2 Comments
While you are not explicitly required to include comments in your labs -- other than the header and community standard information at the top -- comments are a great way to tell yourself and your TAs what you meant to do with particular chunks of code. Also, note that a line starting with #%% will set off a section in the editor window. It will have no effect on the way the program runs, just what it looks like in your editing window. By creating cells that way, you can also run your code a cell at a time.
2.5.3 Importing Modules
Read and work through this section, taking notes as you go. These lines will import two modules with nicknames. If you want to see what a module has in it, once imported, type help(NAME) - for instance,
help(np)
will show everything in np. If you want help on a particular command, put the the command in the argument - for instance,
help(plt.plot)
2.5.4 Loading and Manipulating the Data
Read and work through this section, taking notes as you go. Note that when you slice arrays, you need to append the .copy() to duplicate the data rather than have two different variables pointing at the same data.
2.5.5 Generating Plots
Read and work through this section, taking notes as you go. Note that the code for this is pretty far under what you've written so far; you will be filling in the blank space later, just like Taylor Swift.
For the "During the lab, the instructor will have you create several different graphs in the command window..." this semester the instructor is going to let you navigate this part on your own. go ahead and look at the following alternate plotting commands by typing them in the command window only; at the end, there will be a plot command that you paste into your document:
2.5.6 Polynomials in NumPy
Read and work through this section, taking notes as you go. Be sure you and your neighbor understand how Python - using NumPy - interprets polynomials and which commands use them.
2.5.7 Generate Predictions
Read and work through this section, taking notes as you go. This part creates a new set of data points based on your equation so that you can actually plot it. You will thus have your original eight data points in force
and disp
. You will also have 100 linearly spaced values between the minimum and maximum force value in force_model
and the 100 calculated estimates for the displacement, based on your best straight line, in disp_model
.
2.5.8 Generating Plots (revisited)
Read and work through this section, taking notes as you go. This part adds the new line to your old graph.
CRITICALLY IMPORTANT PART - never put .py in a savefig argument! Your graph might override your program!
2.6 Uploading PDFs
In lab, you will upload the PDF of the graph you have now to Sakai. You do not need to upload an updated graph later - the one from lab (missing the grid and title and such) is fine for the Cantilever assignment.
2.7 The Assignment
Basically, once you get this script running perfectly, you are going to replicate it three times and change it to use three different data sets. Note: these three data sets are "cooked" - that is, I produced them; they did not come from an experiment.
2.7.1 What the .py-files Should Do
Read and work through this section, taking notes as you go.
2.7.2 The Lab Report
Read and work through this section, taking notes as you go. Note that the data sets have different numbers of points so the bottoms of the data tables will not line up. Also note that you should use the original data (masses and displacements in inches) in the tables -- do not put in the forces or the displacements in meters. You are using the latter in your calculations but you are presenting the original data sets here.
Here's the code for getting the beam_data numbers to show up on the screen in a way that is easy to copy to emacs:
for a in range(len(beam_data/2)):
print('%0.4e' % beam_data[a][0], ' %0.4e' % beam_data[a][1])
If you choose to add this to your script - you are certainly welcome to leave it in there when you turn your code it.
2.7.3 Processing the Lab Report
Seriously - spell check!