Physics 141 Plotting on a Spreadsheet

Size: px
Start display at page:

Download "Physics 141 Plotting on a Spreadsheet"

Transcription

1 Physics 141 Plotting on a Spreadsheet Version: Fall 2018 Matthew J. Moelter (edited by Jonathan Fernsler and Jodi L. Christiansen) Department of Physics California Polytechnic State University San Luis Obispo, CA Physics: Spreadsheet (version Fall 2018) Page 1 of 5

2 INTRODUCTION TO SPREADSHEETS: Calculations and Graphing This exercise will guide you through the steps necessary to generate data and plot a function using a spreadsheet. There are several spreadsheet programs (other types of programs Mathematica, Maple, Matlab, Mathcad, etc.) would also suffice, and sometimes might be preferable. However, this exercise should be done using a spreadsheet program. To get started, I suggest that you read this worksheet and just try to get a sense of what is going on. Start your spreadsheet and wander around in it for awhile. Go over the menu items to see what is possible, Talk with people already familiar with the program, and as a last resort read the Help menu. Practice getting the program to generate columns of data determined by functions of your choice, and plotting your data. All of this will take some time as there is a fair amount to become familiar with. As an initial exercise, plot the quadratic, x(t) = -4.9t 2 + 4t + 2 (with x in meters and t in seconds). This could be the position as a function of time for some particle. Later you will be faced with more complicated tasks. This exercise is intended to be easy and will help you to become familiar with the spreadsheet program. Analyze: We will first create a time series, t n = t 0 + ndt. The input data are the starting time, t 0, the step size, Dt, and the constants, a = 4.9, b = 4, and c = 2 of the quadratic equation. Then we can generate t n and x n = x(t n ) from theses. Lets test our program by verifying that x(1) = 1.1. Lets also let the computer plot x n vs t n to see with our eyes that it s a quadratic. Stop to think: Could you make a table with columns x n vs t n using this model and input data? Program in Excel: The following discussion gives details of how to solve this problem using Microsoft Excel (other spreadsheet programs will differ only slightly). If you are already familiar with another spreadsheet, feel free to stick with it. MICROSOFT EXCEL There are a few things to know to get started: you can move around the worksheet using the arrow keys or the mouse to enter a number or a formula into a cell, select the cell by pointing at it with the mouse and clicking once, then type in your entry, what you type will appear on a line just above the worksheet below the formatting toolbar when entering formulas you can use either lower case or upper case to make a menu choice point and click on your selection on the menu bar and, while holding the mouse down, drag down to the operation you want and release To copy a formula from one cell to others, you highlight the cell you want to copy (point at it and click), and then pull down 'Copy' from the 'Edit' menu. Then highlight the cell you want to copy it into, and pull down 'Paste' from the 'Edit' menu - it should now appear in the new cell. If you want to copy a formula or a number to a group of cells, you do it the same way except you need to highlight all of the cells you want to copy into. To do this, point at the first cell, and while holding the mouse button down, drag over all the cells you want to highlight, and then let go of the button. There is an important distinction between a relative address and an absolute address. What usually appears in a formula are constants, built-in functions, and numbers which are 141Physics: Spreadsheet (version Spring 2018) Page 2 of 5

3 located in some other cells of the spreadsheet. When you copy a formula from one cell to another, sometimes you want to refer to a very specific spot, say the cell A4 ( at the intersection of column A and row 4). To specify that you mean this exact spot, you give the absolute address as $A$4 (note the dollar signs). Sometimes a formula refers to a number in some cell at a particular relative location - say the adjacent cell on the left. For instance, we might have a formula in B4 which involves the number in A4. If I copy the formula from cell B4 to the cell B5, I want the formula there to look for the number in A5. If I write the relative address A4 in the formula instead of the absolute address $A$4, copying to other cells will automatically give the correct relative address. (You can also give an address like $A4, which is absolute in the column designation, but relative in the row designation. Or A$4 which is absolute in the row designation but relative in the column designation.) This is one of the very powerful features of spreadsheets, as you shall see. Play around and check out how it works. You can quickly alternate between relative and absolute addresses by pressing [command-t] on the Mac or F4 on a PC. Fill in your spreadsheet as follows (also see the picture on the next page): A B C 1 Plot quadratic: Moelter 2 3 (m/s^2) quad term coefficient (m/s) linear term coefficient (m) constant dt (s) t n(s) x n (m) 9 0 =$A$4*A9^2+$B$4*A9+$C$4 10 =A9+$A$6 copy cell B9 11 copy A10 to these to these cells cells 20 down to here down to here The top few rows are used for information about you and the assignment. Also constants are entered with labels and units when appropriate. First we ll have to provide the input data. Notice that A4 is 4.9 (the coefficient of the squared term in the formula), B4 is 4 (the coefficient of the linear term) and C4 is 2 (the constant term), Dt is in A6 and t 0 is in A9. Now, lets make the t n array. Look at cell A10. The "=" sign at the start tells the program to expect a mathematical formula. The formula means take the number in A9 and add to it the number in A6. Again, the use of A4 (no dollar signs) is a relative address which, since we are in A10, means take the number immediately above. $A$6 is an absolute address which means go get the number in A6 no matter which cell you are asking from. So what have we got in column A? A sequence of numbers starting at zero, in intervals of 0.25: this will be our t n data. Later on if we wanted to change it we could adjust cells A9, A6, and perhaps how far down in column A we choose to go. Finally, what were we trying to calculate? (Recall our function x(t) = -4.9t 2 + 4t + 2) Take a look at B9? It says take the number in the cell to the left (A9 since we are in B9), square it (^2), multiply (*) by A4, add it to the product of B4 and the cell to the left, and finally add it to C4. When you copy this to the cells below it the relative addresses will change, but the absolute addresses will remain the same. For example cell B11 should be: =$A$4*A11^2+$B$4*A11+$C$4. Move there and check (cell contents appear near the top 141Physics: Spreadsheet (version Spring 2018) Page 3 of 5

4 of screen in the formula bar). Evaluate: Does the program pass the test? x(1) = 1.1? Row 13 has the time = 1, and the corresponding x value. If B13 is not 1.1, then please go through the bullets again to fix a typo. If this seems frustrating, it s because of the computer s rigid logic. You, being smarter than the computer, will have to figure out what s wrong from the computer s view point. Current Cell Chart gallery If everything looks ok then lets plot x n vs t n. Columns A and B, from rows 9 to 20, now contain the data we would like to plot - column A for the variable t, in increments of 0.25, and column B for the value of the quadratic function for each input t. How do we plot the data? Highlight your data by clicking in A9 and dragging to B20 and let go, a rectangular region should be highlighted. Now you can use the Chart Gallery tab (see picture) that lets you do it pretty simply. On the top of your screen under the menu bar there should be a toolbar, with a bunch of tabs in it. If there isn't, choose Elements Gallery in the 'View' menu. One of the tabs is the Charts icon; click on it to get it going: Choose "X-Y Scatter" for chart type, do not pick "line". If you don t see X-Y Scatter immediately, click the >> and select it there. add a title and axes labels using the formatting palette. If you don t see the formatting palette, click View, then Formatting Palette. With the design, implementation, testing, and visualization done, now its time to have fun with the program. If you have gotten this far, you can now see how things change for different 141Physics: Spreadsheet (version Spring 2018) Page 4 of 5

5 quadratic functions. Move to cell A4 and change 4.9 to -10, or to whatever. Notice that the values in B9 to B20 changed by themselves. This is because the formula in those cells referred to $A$4 and you have changed A4. Notice also that the graph has changed, even rescaling the axes if necessary. Pretty slick, eh!? Try changing the values in B4 and C4, or A6, and see how your graph changes. Now that you can do this for a simple function, you can do it for any function that you can write out as an equation, and even for some where you can't. Play around with it! While doing assignments, remember to: Analyze: What s the physical model? What are the input data? What are the derived data? What data will you want to plot? Even though the problems on this page are probably easy enough that you ll be able to answer all these questions in your head, take time to think the questions through completely. The tests should be written on a piece of paper, though, including the predicted answers before you open the blank spreadsheet. Trivial tests, like cos(0) are virtually useless. Program in Excel: Write the spreadsheet (NOTE: In Excel the argument of a trigonometric function must be in radians. Also, to use p in an Excel formula, try using the PI() function which is very accurate. ) Evaluate: Does the spreadsheet pass its test? Do the plots all look reasonable? What makes them reasonable? Revise: If the tests or plots look odd, then go back and think it through again carefully. When everything is working properly, write up the report. It should include a statement of the problem, the numerical approach that you took, the test that the program passes and then the results, including the verification of the test, plots of interest, and any interesting features you discovered by adjusting the code.. 141Physics: Spreadsheet (version Spring 2018) Page 5 of 5