• Aucun résultat trouvé

Working with Data

Dans le document This page intentionally left blank (Page 134-140)

Graphics Primitives

3.10 Working with Data

In situations where you have numerical data, you will want to enter the data into the computer to study it. How is this most easily accomplished with Mathematica? Here is an example. These data specify the temperature of a cup of coffee as it cools over time. The first column shows the number of minutes that have elapsed, while the second column indicates the temperature of the coffee,

When recording data a spreadsheet-type interface is desirable. To enter these data into Mathematica, first type data = (any name will do, but “data” seems convenient), then select Table/Matrix#New...

from the Insert menu. A dialog box will appear. In the top left portion select Table. To the right specify the number of rows (in this case 16) and columns (in this case 2). It is possible to add and delete more rows and columns later, so these numbers need not be exact. Ignore the remaining settings and hit the OK button. A rectangular array, a sort of mini-spreadsheet of the dimensions you specified, will appear in your notebook, and a very long, vertical, blinking cursor will appear to its right. Type ; so that when you eventually enter this cell the output will be suppressed. Now click on the placeholder in the upper left corner of your table (or hit the Í key to jump there) and enter the first data value. When you are finished use the Í key to move to the next placeholder. Con-tinue to enter your data in this fashion. Additional rows or columns can be added at any time; just look in the Insert#Table/Matrix menu for Add Row or Add Column. When all the data have been typed in, enter the cell.

When you enter data in this way, Mathematica stores it as a list of ordered pairs (one pair for each row in the table). Technically, it’s a list of lists. If you don’t put a semicolon after your data table,

you will see it displayed in this form upon entering it. Or you will see it in this form if you ask for it by name:

In[2]:= data

Out[2]= 0, 149.5,2, 141.7,4, 134.7,6, 128.3,8, 122.6,10, 117.4,12, 112.7,14, 108.5, 16, 104.7,18, 101.3,20, 98.2,22, 95.4,24, 92.9,26, 90.5,28, 88.5,30, 86.6 You won’t need to work with the data in this form, but it’s good to see it once so you know how Mathematica interprets it.

The command for plotting such a list of ordered pairs is ListPlot:

In[3]:= ListPlot#data'

Out[3]=

5 10 15 20 25 30

100 110 120 130 140 150

ListPlot takes a single argument: a list of two-tuples. Each two-tuple is interpreted as a point in the coordinate plane, and these points are then plotted. If your list of points is very short, you may find it easiest to type it directly into ListPlot rather than first making a data table:

In[4]:= ListPlot#1, 1,2, 3,3, 2,4, 3'

Out[4]=

1.5 2.0 2.5 3.0 3.5 4.0

1.5 2.0 2.5 3.0

It is an annoying fact of life that ListPlot will often hide one or more of your points behind the coordinate axes. In the plot above, the point +1, 1/ is at the intersection of the two axes! One way to alleviate this masking effect is to “connect the dots.” The option Joined‘True accomplishes this.

Also, one may specify another symbol to indicate the data points using the PlotMarkers option.

Here we used the ¼ symbol (from the Shapes and Icons portion of the SpecialCharacters palette).

3.10 Working with Data 121

In[5]:= ListPlot$1, 1,2, 3,3, 2,4, 3, Joined‘True,

PlotMarkers‘¼, AxesOrigin‘0, 0, PlotRange‘0, 3.5(

Out[5]=

¼

¼

¼

¼

0 1 2 3 4

0.5 1.0 1.5 2.0 2.5 3.0 3.5

But ListPlot also accepts most of the options that the Plot command does, so it is a simple matter to produce as elaborate a graph as you desire. Here we assign the name scatterplot to our plot so that we can refer to it later. The x and y values in AxesOrigin were chosen just a bit smaller than the small-est x and y values appearing in the data; this pulls the axes off any data points. Finally, wrapping Tooltip around the data itself has the convenient effect of producing a tooltip showing a data point’s exact coordinates when you mouseover that point (you’ll have to try this to experience it).

In[6]:= scatterplot ListPlot$Tooltip#data', AxesLabel‘"min", "temperature+“F/" , PlotStyle‘Directive#PointSize#Small', Blue',

Filling‘Axis, AxesOrigin‘1, 80(

Out[6]=

You can have Mathematica find the best-fitting polynomial for your data (according to the criteria of least squares) using the Fit command. Here is the best fitting linear function for the coffee cooling data. We assign it the name fitLine:

In[7]:= fitLine Fit#data,1, x, x'

Out[7]= 141.3322.03257 x Here is the best quadratic:

In[8]:= fitQuadratic Fit$data, 1, x, x2 , x(

Out[8]= 148.4653.56107 x0.0509498 x2

The Fit command takes three arguments. The first is the data (a list of two-tuples). The second is a list of the terms requiring coefficient values in the polynomial. The last is the name of the variable,

in this case x.

Once we have named these best-fit functions, we can view them against our data. A quick and dirty way to display the Plot of a function together with data points is as follows (the Epilog option is discussed in Section 3.9 on page 117):

In[9]:= Plot$fitQuadratic,x, 0, 30, Epilog‘Point#data'(

Out[9]=

5 10 15 20 25 30

100 110 120 130 140

A more nuanced image can be had by using Plot and ListPlot to generate separate graphics, and then using Show to display them together:

In[10]:= Show$scatterplot, Plot$fitLine, fitQuadratic,x, 0, 30, Filling‘True((

Out[10]=

Here we display them individually:

In[11]:= Show$scatterplot, Plot#fitLine,x, 0, 30', PlotLabel‘Style#fitLine, 8'(

Show$scatterplot, Plot#fitQuadratic,x, 0, 30', PlotLabel‘Style#fitQuadratic, 8'(

Out[11]=

3.10 Working with Data 123

Out[12]=

A more efficient means of entering the input above, where we have a list of nearly identical Show items, is to use Table to generate the list:

In[13]:= GraphicsRow$

Table$Show$scatterplot, Plot#f,x, 0, 30', PlotLabel‘f(, f,fitLine, fitQuadratic(

(

Out[13]=

The FindFit command may be used in place of Fit when a more complex form of approximating function is sought than a polynomial (or sum of basis functions). For instance, while the quadratic above seems to fit the coffee cooling data rather well, a moment’s thought tells us that this function will fare poorly if we use it to predict the coffee’s temperature, say, when the time x is equal to 60 minutes. For quadratics with a positive coefficient on the x2 term open upward, and so will eventu-ally (for sufficiently large values of x) turn from decreasing to increasing functions. The coffee, on the other hand, is not going to get warmer as time progresses. It is a well known principle of physics that bodies cool exponentially toward the ambient temperature of the surrounding medium. Hence the form of the cooling function ought to be f+x/ abcx, where a, b, and c are positive constants with 0c1, and where a is the ambient temperature of the room.

FindFit requires four arguments. The first is the data. The second specifies the form of the fitting function (in this case abcx), the third is a list of the parameters whose values we seek in this expression (in this case a,b,c), and the last is the independent variable (in this case x). Any or all of the parameters in the third argument may be given as an ordered pair of the form {parameter, guess}, where guess is a rough estimate of the correct value of that parameter. Below we use .5 as an

initial guess for the decay parameter c, since we know that c is between 0 and 1. This helps Mathemat-ica refine its search for optimal values of the parameters in question.

In[13]:= FindFit$data, abcx,a, b,c, .5, x(

Out[13]= a‘69.348, b‘80.1489, c‘0.95015

The output of FindFit is a list of replacement rules giving the values of the parameters. Replacement rules are discussed in Section 4.2 on page 153; for now we simply read off the values of the parame-ters, and note that the fit is excellent:

In[14]:= Show$scatterplot, Plot$69.34880.1480.95015x,x, 0, 30((

Out[14]=

Moreover, this approach allows us to use the coffee data to determine that the ambient temperature of the room is approximately a 69.35“ Fahrenheit.

Exercises 3.10

1. For the data given below, find the best-fitting line (according to the criteria of least-squares), and plot this line together with the data.

x 1 2 3 4 5

y 1.2 2.3 3.6 4.9 5.9

2. For the same data used in the previous exercise, find the best-fitting power function. That is, find the best-fitting function of the form f+x/ pxn for real parameters p and n.

3. For the functions in each of the previous two exercises, find the residuals. That is, for each x-coordinate in the data, find the difference between the actual y value in the data and the value predicted by the fit function. Geometrically, these residuals indicate the vertical distance between a data point and the graph of the fit function. A residual is positive if and only if the data point lies above the graph of the fit function.

4. Enter the following input to create a command that will plot a collection of data, a fit-function, and the residuals for the data and the given function. Test the command on the data from the

3.10 Working with Data 125

first exercise and the fit-function f+x/ 3.25x.125x2.

residualPlot$dataB, functionB,xB, xminB, xmaxB, optsBBBRule(:

Show$ListPlot$data, Table#x, function,x, data##All, 1''', Filling‘1‘2, FillingStyle‘Red, Green, PlotMarkers‘"", "", opts(,

Plot#function,x, xmin, xmax'(

5. When a Manipulate has a Locator controller, it’s possible (and often desirable) to modify the Locator’s default appearance in the image pane. A simple way to do this is to display a Graphics object of your choosing at the position of the Locator, and add the option setting

Appearance‘None to the iterator for the Locator control. Enter the following input, explore the output, and then change things so that the Locator appears as a Thick, Blue, Circle.

Manipulate$

Graphics$Directive#PointSize#.03', Red', Point$pt( , Axes‘True, PlotRange‘1(, pt,0, 0 , Locator, Appearance‘None (

Dans le document This page intentionally left blank (Page 134-140)