Curve Graphing in MS Excel and Applications
Curve Graphing in MS Excel and Applications
Volume 2 | Issue 2
Article 6
5-10-2007
B Yushau
King Fahd University of Petroleum and Mineral
This In the Classroom Article is brought to you by the Faculty of Business at ePublications@bond. It has been accepted for inclusion in Spreadsheets in
Education (eJSiE) by an authorized administrator of ePublications@bond. For more information, please contact Bond University's Repository
Coordinator.
Curve sketching is one of the best ways to visualize and investigate the behavior of functions and equations.
Graphs convey a lot more information about functions than algebraic expressions would. In this note we shall
show how to use MS Excel to graph different types of functions and equations. Applications to zooming, root
finding, parametric studies and online curve graphing will be given.
Keywords
The authors would like to thank King Fahd University of Petroleum & Minerals for excellent research facility.
Abstract
Curve sketching is one of the best ways to visualize and investigate the behavior of
functions and equations. Graphs convey a lot more information about functions than algebraic
expressions would. In this note we shall show how to use MS Excel to graph different types of
functions and equations. Applications to zooming, root finding, parametric studies and online
curve graphing will be given.
Introduction
2007BondUniversity.Allrightsreserved.
http://www.sie.bond.edu.au
1
M.ELGEBEILY&B.YUSHAU
In this section we discuss two methods for generating sequences of x values for
graphing purposes. The first is an Excel auto-fill method and the second is a formula
copying method.
2.1.
Supposewewanttogeneratevaluesbetween5and5withastepof0.2.Then
theprocedureisasfollows(seeFigure1).
a) In cell A2 enter the first value (-5).
b) In cell A3 enter the next value (-4.8)
c) Select the range A2-A3.
d) Point the cursor to the fill handle (the little box at the lower right corner of the
selected cells). The cursor will change to a +.
e) Click with the left mouse button and drag down. As you drag, you will see a
yellow hint box containing the current value generated by Excel. Stop when
you reach the last value (5 in this case). When you release the mouse button
the desired sequence will be created.
f) Select the whole of column A by clicking on the A label (the box above cell
A1). Now in the main menu bar click Insert, Name, Define. A dialog box
appears which shows all the names defined in the workbook and the ranges
they refer to. In the Names in workbook: Box, type x to indicate the values
in the first column will be referred to as x (you will find the name x already
typed there if you generated the sequence as shown in Figure 1). Confirm this
name by pressing OK.
Figure1:SequencegenerationbytheFillMethod
2.2.
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
233
2
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
Figure2:Sequencegenerationbyformulacopying
Themostgeneralcasewheretheendpointsaandbaswellasthenumberof
divisionsnareallvariablecanbehandledasfollows:(seeFigure3)
h) Label cells A1, B1, C1 and D1 as a, b, n and h and name cells A2, B2, C2 and
D2 as a, b, n and h.
i)
In A2, B2 and C2 enter the values of a, b and n. In D2 enter the formula =(ba)/n to set the value of h.
j)
234
Published by ePublications@bond, 2007
M.ELGEBEILY&B.YUSHAU
Figure3:Sequencegenerationwithvariablenumberofpoints
2.3.
Another technique that we will be using in this paper is that of inserting new xvalues in an existing table. This technique can be used, for example to graph
piecewise defined functions or introduce finer spacing of points to better visualize
the behavior of the graph. We can start with a coarse set of points to get an idea
about how the curve generally looks like and then add points to clarify the behavior
of the curve over specific parts of the domain. The idea is simple. Generate the new
points at the end of the existing table, select the whole ser of x-values and use the
Sort Ascending button to get the new points to line up in the correct order.
In this section we show how one sketches different types of graphs using Excel.
Unless otherwise stated, Column A is always named as x.
3.1.
We begin by showing how to use Excel to draw a simple curve such as that
of y = sin x over the interval [ , ] (see also [5]). To do that we take the following
steps:
a) In A1 and B1 enter the labels x and y=sin x, respectively.
b) In A2 enter the first x-value by typing =-pi().
c) In the cell A3 enter the formula =A2+pi()/20. (Thus we are going to take 40
values in the interval [ , ] ) Generate the rest of the values as explained
above.
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
235
4
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
d) In B3 type the formula =IF(x="","",SIN(x)) and double click the fill handle.
This will automatically repeat the formula for the rest of the x-values
e) Select the range of cells A1-B42, point to the Chart Wizard icon
main menu toolbar and click. The dialog box in Figure 4 appears.
on the
f) Select XY scatter from the chart type pane and Scatter with data points
connected by smoothed Lines without markers from the Chart sub-type
pane as shown in Figure 4. If you click on Press and Hold to View Sample
button you will see a preview of how the graph will look like. At this point,
the graph is complete, you can click Finish to see the graph on the
worksheet or you can click Next to view other options you can include in
your graph.
g) By double clicking in various parts of the graph, dialog boxes appear that
enable you to control certain features of the graph like the color of the plot
area, color of the graph line and its weight, etc. Figure 6 shows the result of
some of these choices.
Figure4:ThefirstdialogboxintheChartWizard
236
Published by ePublications@bond, 2007
M.ELGEBEILY&B.YUSHAU
y=sin x
1.5
1
0.5
0
-4
-2
-0.5
-1
-1.5
Figure5:Theplottedsinefunction
y=sin x
1.5
1
0.5
0
-4
-2
-0.5
-1
-1.5
Figure6:Theeditedgraph
3.2.
There are two ways to draw more than one curve on the same chart. The first is
best used when the points for all graphs are generated before plotting and for the
same x-values. The second is best used when the points for each graph are generated
separately or when they do not have the same x-values. We illustrate the two
methods by examples.
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
237
6
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
3.2.1.
FirstMethod
Suppose we want to plot the graphs of y = x 2 and y = 1 x 2 on the interval
[ 5,5]. In this case we follow exactly the procedure explained in the previous section
except that we generate the points for the two curves simultaneously and we select
the 3-column range of cells that contains these points before clicking the Chart
Wizard. The following two figures illustrate the details.
30
20
10
y1=x^s
0
-10
-5
-10
10
y2=1-x^2
-20
-30
Figure7:Twographsonthesamechart
3.2.2.
SecondMethod
Here, we plot the graphs of y = e x over the interval [ 3,3] and y = 10 log x
over the interval [0.1, 5] . We begin by generating and plotting the graph of y = e x in
the usual manner. Next we generate the points for the graph y = 10 log x . To plot the
latter graph on the same chart, select the range of x-y points for the graph, point to
the border of the selection, drag and drop in the chart area. A dialog box appears as
shown in Figure 8.
Figure8:Thedialogboxtopasteasecondgraph
238
Published by ePublications@bond, 2007
M.ELGEBEILY&B.YUSHAU
Select: Add Cells - New Series, Values (Y) in - Columns, Series Names in First
Row, Categories (X Values) in First Column. When you press ok, the graphs below
will appear.
25
20
15
10
y1=exp(x)
y2=10*log(x)
0
-4
-3
-2
-1
-5
-10
-15
Figure9:Twographswithdifferentdomains
3.2.3.
AGeneralPurposeGrapher
We describe here a general purpose function grapher that works much like
specialized CAS graphers in the sense that the user need only insert the rule of the
function and the endpoints of the interval on which the function is to be plotted. The
procedure is as follows:
a) Type the labels a, b, n, h, x and y in cells A1, B1, C1, D1, E1 and F1,
respectively. Name A2, B2, C2, D2, E2 as a, b, n, h and x, respectively.
b) Generate a sequence of n=200 (n is fixed) x-values between the endpoints a
and b as explained in Section 2.
c) Select the range F2:F202 and type a rule of a function (say =x^2+x-1) in the
active cell. Press Ctrl+Shift+Enter. This generates the corresponding y-values.
d) Plot the corresponding graph as usual.
At this point we generated the first graph. If we wish to graph another
function, we do the following: 1) Provide the values of a and b and 2) Click in cell
F2, type the rule of the new function in the formula bar and press Ctrl+Shift+Enter
get the new graph.
Figure 10 shows the plots of the two functions e x 2x 2 and x sin(10 x ) over the
interval [2, 3] and [ , ] respectively, obtained using this procedure.
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
239
8
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
y
4
2
1
0
-3
-2
-1
4
0
-2
-4
-3
-2
-1
-1
-4
-2
-6
-3
-8
-4
-10
3.3.
( x 1)
y=
5x2 2 x 7
2
( x 1) +
y=
5x2 2 x 7
2
and
( x 1)
y=
5x2 2 x 7
2
over the interval [-8, 8], a subinterval of which is not in the allowable range of
x-values. When producing the points for these graphs you will notice that Excel
produces the error message #NUM! for the range of x-values that are not allowed.
You simply need to delete the cells that contain these errors. You should leave at
least one empty row of cells between the undeleted ones (try to see what happens if
you do not leave an empty row, you can always undo the changes by clicking the
undo button in the toolbar). The following figure shows the graph corresponding to
an x-step of 0.1.
240
Published by ePublications@bond, 2007
M.ELGEBEILY&B.YUSHAU
15
10
5
0
-10
-5
10
-5
-10
-15
Figure11:Agraphofanonfunction
You will notice that part of the graph is missing. This is because the 0.1 step
was too big for the rapidly changing graph around x = 1.5 . This brings us to the idea
of adding points to a graph. We will use this idea to add more points near 1.5. This is
done as follows.
a) Insert values between 1.401 and 1.5 in steps of 0.01, say, as explained in
Section 2.3 above. Extend the y-values correspondingly. This can be done by
copying the formulas to the corresponding cells in the usual manner. You will
need to insert an empty row again after the x-value -1.
b)
Click somewhere in the chart area to select it. The range of points used to
generate the graph will be highlighted by Excel. You will notice that the
highlighted range misses some values towards the end of the table. To extend
the highlighted range point to the little red box at the end of the highlighted
x-values. The cursor will change to a diagonal double headed arrow. Click
and drag to extend the highlighting to the end of the generated points. The
following figure shows the results.
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
241
10
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
15
10
5
0
-10
-5
10
-5
-10
-15
Figure12:Fillingthegapinthegraph
3.4.
ParametricandpolarcurvesposenospecialchallengetoExcelgraphing.Inthe
caseofaparametriccurve( x = f (t ), y = g (t ) ),anextracolumnforthevaluesoftis
needed.Thevaluesofxandyarethengeneratedusingthefunctions f and g .Fora
Figure13:Generatingpointsforaparametric(left)andapolar(right)curve
In the tables shown in Figure 13, Column A is named as t for the parametric
curve while for the polar curve, Column A is named as Theta and Column B is
named as r_. The underscore in naming Column B is necessary because the letters R
242
Published by ePublications@bond, 2007
11
M.ELGEBEILY&B.YUSHAU
and C are reserved names and cannot be used. The graph of two loops of the cycloid
is shown in Figure 14 and the graph of the butterfly curve is shown in Figure 15.
2.5
2
1.5
1
0.5
0
-1-0.5
11
13
Figure14:Twoloopsofacycloid
-4
-2
Figure15:Thebutterflycurve
3.5.
Parametric studies
Figure16:Tableforthecurve r
= a + cos
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
243
12
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
a=1
a = 1.5
1.5
1.5
1
0.5
0.5
0
-1
-0.5
0
0
-1
-0.5 0
-1
-1
-1.5
-1.5
-2
a = 0.5
a=2
3
2
0.5
1
0
-1
0
0
-1
-1
-0.5
-2
-1
-3
Figure17:Graphsforseveralvaluesoftheparametera
The values of the parameters can also be controlled by scroll bars. Figure 18
shows an implementation of this idea. In this figure, cells A2, B2, C2 and D2 are
named as cont (for counter), a, theta and r_, respectively. The scroll bar
implementation is done as follows:
a) Click View, Toolbars, Control Toolbox (the Control Toolbox bar is displayed).
b) Click on the scroll bar control. Click on the worksheet at the location where
you want to place the scroll bar. Drag the control to the size you want. (On
top of cells A3 and B3 in Figure 18.)
c) To set the necessary properties for the scroll bar to control the parameter a,
right-click the control then click Properties on the shortcut menu (or click on
the properties button in the Control Toolbox bar). Set the properties Linked
Cell, Max and Min to cont, 10 and 0, respectively. Close the properties dialog
box and click the design mode button
to quit the design mode and
activate the scroll bar control. If you click on the scroll bar arrows or drag the
scroll button, you will see the values in A2 change from 0 to 10.
d) Using the values generated in A2, we can now create parameter values in B2,
e.g., by using the formula =-(10-cont)/10+2*cont/10 to map the values 0 to 10
to the values -1 to 2.
244
Published by ePublications@bond, 2007
13
M.ELGEBEILY&B.YUSHAU
e) The rest of the table for the curve is constructed as before and the graph is
created in the usual manner. If the scroll bar is pressed or scroll button is
dragged, you will see the parameter values change and the corresponding
polar curve drown.
Figure18:Controllingtheparameterwithascrollbar
One thing to notice in Figure 18 (and also in Figure 17) is that the correct value
of the parameter always shows in the chart title. To do this automatically, insert a
Text Box from the Drawing Toolbar, click inside the text box to activate it and type
the formula =a in the formula bar. When you press enter the current value in the cell
named a (B2) will be displayed in the text box.
It has been observed in [4] that from the last quarter of the last century,
mathematics as an activity has become more experimental and more visual. The
trend now is more so, due to technological advancement than when this observation
was made. In line with this development, Excel is a tool that has the potential for
enhancing both visual and experimental features. We shall give two applications
were experimentation is encouraged and visualization is vivid in Excel. The first is
the zooming technique in order to detect a zero of a function, while the other is
online graphing, whereby a collection of data is linked to a graph that is
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
245
14
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
automatically updated as new data is generated. The graph gives a visual summary
of the history of data generation.
4.1.
-2
-200
-400
-600
-800
Figure19:Thepolynomialplottedonalargeinterval
We see from this Figure that the roots of the polynomial are located in the
interval [0,4] . So we change the values of a and b to 0 and 4. The graph in Figure 20
is generated.
y
10
0
-10 0
-20
-30
-40
-50
-60
-70
-80
-90
246
Published by ePublications@bond, 2007
15
M.ELGEBEILY&B.YUSHAU
Figure20:Zoomingcloser
Fromthisgraph,itisclearthatthepolynomialhasonlyonerootnear4,sowe
changeaandbto3.8and4.1.Figure21isgenerated.
y
40
30
20
10
0
-10
3.7
3.8
3.9
4.1
4.2
-20
-30
-40
-50
Figure21:Bracketingthezeroofthepolynomial
Atthispoint,bylookingatthetableofgeneratedyvaluesweseethattheroot
is actually between 3.992 and 3.995. This means we have bracketed the root to two
decimal places. Observe also that if you move the cursor near the root, the yellow
hintboxwillappeargivingthefollowingpoints:
and
4.2.
Sometimes need arises to graph and/or analyze data points that are generated
online. For example, the computer may be hooked to a measuring device in an
experiment, or the data is being generated by another program. Excel can be used to
graph/analyze and update the graph/analysis periodically at time intervals of as
little as one minute. We illustrate the procedure by considering the data shown in
Table 1 (see [7], page 359) which gives measurements of normal stress vs. shear
resistance for a certain metal specimen.
To simulate the online data generation, open a text file and type the first 2 or 3
values. Save the file as, say, dat.txt in my documents, or some other
convenient directory. In an Excel sheet, type the labels Stress, Shear and Ratio in
cells A1, B1 and C1, respectively. Name Columns A and B as Stress and Shear,
(x, y )
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
247
16
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
respectively. In C2 enter the formula =Shear/Stress (do not worry about the error
message that appears when you press Enter). The ratios generated in Column C
represent analysis that we want to perform on the generated data. Select cell A2 and
then click Data, Import External Data, Import Data... to open the Select Data Source
dialog box. Navigate to where you saved the dat.txt file (My Documents in this
example), select the file and then click Open to bring up the Text Import Wizard
shown in Figure 22:Step1oftheTextImportWizard.
NormalStress
ShearResistance
22.6
25.8
23.6
27.1
23.9
25.9
24.7
26.3
25.4
27.3
25.6
24.9
26.8
26.5
26.9
21.7
27.4
21.4
27.7
23.6
28.1
22.5
28.9
24.2
Table1:Dataforonlinegraphing/analysis
Figure22:Step1oftheTextImportWizard
248
Published by ePublications@bond, 2007
17
M.ELGEBEILY&B.YUSHAU
In this dialog box you can select the Original data type and at which row to
start importing. Make the selections shown and click Next. In the next dialog box,
check, the Comma box in the Delimiters sub-box to tell Excel that your data is
separated by commas. Click Finish.
TheImportDatadialogboxstarts(seeFigure23:TheImportDatadialogbox).
InthisdialogboxclickProperties.
Figure23:TheImportDatadialogbox
The External Data Range Properties appears as shown in Figure 24. Make the
selections shown in Figure 24 and set the Refresh every box to 1 minute. Click Ok.
Figure24:TheExternalDataRangePropertiesdialogbox
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
249
18
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
The imported data appears as shown in Figure 25. Observe how the formula in
Column C is extended automatically to the three imported rows.
Figure25:Theinitialimporteddata
Now with the range A1:C4 generate the two graphs shown in Figure 26.
15
Ratio
10
5
0
22
22.5
23
23.5
24
24.5
25
Figure26:Graphsoftheinitiallyimported/analyzeddata
Next, to simulate the generation of new data, open the file dat.txt and insert
some more points. Do not forget to save the changes. Figure 27 shows the addition,
analysis and plotting of the new imported data.
Figure 27 shows actually the data after it was imported into Excel. This
updating takes place within a minute of saving the changes. You will also notice that
the graph is automatically updated to include the new points.
Further simulations can be done by actually adding or deleting points. Excel
will update automatically every one minute. One point to note here is that, if you exit
Excel and reopen it again, a dialog box comes up asking you if you want to enable
automatic importing or not. If you want to continue the updating choose Enable
automatic refresh.
250
Published by ePublications@bond, 2007
19
M.ELGEBEILY&B.YUSHAU
Stress
Shear
Ratio
22.6
25.8
1.14
23.6
27.1
1.14
24.7
26.3
1.06
25.4
27.3
1.07
25.6
24.9
0.97
26.8
26.5
0.98
26.9
21.7
0.80
15
Ratio
10
5
0
22
23
24
25
26
27
28
Figure27:Moredataimported,analyzedandplotted
Conclusions
In this note we have shown how to graph curves given in rectangular and
parametric forms using MS Excel. As an application of the techniques, we have
shown how to do parametric studies and how to zoom to approximate zeros of a
function with very high precision. In addition, we have shown how Excel can be
used to continually update graphs/analysis of data generated online by an external
source. The importance of having a facility to visualize a function as a graph cannot
be overstated. While there are a few graphing packages that provide great support
for this process, the value of using Excel to make the graph is that the users arent
working with a black box. They really do create the graph for themselves, a process
which undoubtedly enhances understanding.
http://epublications.bond.edu.au/ejsie/vol2/iss2/6
251
20
CURVEGRAPHINGINMSEXCELANDAPPLICATIONS
Acknowledgement
The authors would like to thank King Fahd University of Petroleum &
Mineralsforexcellentresearchfacility.
References
[1]
[2]
[3]
Anton, H., Bivens, I., Davis, S. (2002). Calculus, 7th edition, John Wiley.
[4]
[5]
[6]
[7]
Walpole, R., Myers, R., Myers, S. and Ye, K. (2002). Probability and Statistics for
Engineers and Scientists, Pearson Education International, Prentice Hall.
252
Published by ePublications@bond, 2007
21