Example Sheet 8 – More graphing, file I/O and some things to play with.

Dave Langstaff Page 9 20/11/2001

PH15010 MathCAD Example Sheet 8

The aim of this worksheet is to show more techniques for plotting results on graphs and reading and writing data to files.

By the end of the sheet you will know how to:

·  Add error bars to a graph

·  Plot one function against another

·  Read data from a file

This worksheet takes the form of a number of examples which illustrate and introduce these principles. Work through them all.

At the end of the worksheet are also a number of examples designed to introduce you to topics within MathCAD that can help you with some of the subjects that you are learning on other modules. These are not intended as complete tutorials, but more as a way of showing your some of the things that mathCAD is capable of so you can discover more for yourself.

Exercise 1 – Error Bars

When plotting graphs of experimental results, it is frequently useful to indicate on the graph the expected range of errors in each reading. In that way it is possible to tell if variations from the predicted values is within the bounds of experimental error.

In order to illustrate error bars, we will re-visit the Current vs Voltage plot we took in example sheet 5. If you have this on your M: drive, then copy and paste the relevant section of the worksheet into a fresh sheet, otherwise you will have to create the data from scratch.

In the original example, we have a series of readings of current through an unknown resistor at different applied voltages as shown in the table below:

We will assume that the current was measured on a poor quality ammeter and that all readings are subject to errors of up to 10%. Note that although this makes it pointless to quote the measured current to 4 significant figures, we will leave things as they are for the purposes of this exercise.

Start by creating 2 10-element vectors to hold the readings as before.

In order to plot error bars, we need to provide a pair of vectors to the graph, one containing the high points of the error bars and the other containing the low points. Since mathCAD will multiply a vector by a scalar quantity to produce another vector, we can quickly produce these vectors as shown below.

Vapplied / Imeasured
(Volts) / (ma)
0 / 0
0.5 / 0.465
1 / 0.923
1.5 / 1.387
2 / 1.869
2.5 / 2.337
3 / 2.811
3.5 / 3.287
4 / 3.747
4.5 / 4.177

Having created the 4 vectors, plot all the IMeasured vectors against the VApplied vector as shown:

To plot more than one vector on the y-axis, separate them with commas.

The next step is to format the traces to show points and error bars.

Select the chart and bring up the chart formatting dialog box.

Turn to the traces page and format trace 1 to have ‘o’s’ as the symbol and ‘points’ as the type of line.

Format trace 2 and trace 3 to have ‘error’ as the type of trace.

The resultant graph should look like this:

Exercise 2- Plotting one function against another.

So far we have used mathCAd’s plotting abilities to plot a simple function, such as sin(x) vs x and also seen how we can plot one vector against another. Frequently we wish to plot one function against another, for example we may have functions which describe the height and distance downrange of a projectile as a function of time. By plotting one function against the other we can plot the trajectory of the projectile.

The following exercise plots the trajectory of a cannon ball fired at a particular angle with a particular initial velocity.

The sheet starts by describing the angle of the cannon and the initial velocity and then goes on to split the velocity into components in the x- and y- directions.

Once you have created these statements, it is possible to create two functions to describe the position of the cannonball in the X- and Y- direction, using the simple equations of motion under constant acceleration. We ignore air resistance, and the position in the x-axis is readily calculated from the initial velocity in the x-axis. Similarly, the position in the y direction may be calculated, using the built-in value ‘g’ for acceleration due to gravity. This acts downwards, whereas our reference direction for the y-axis is upwards, hence the minus sign in the equation.

All that remains is to select a suitable range of time steps and to plot the functions.

A good range is from 0 to 6 seconds in steps of 0.1 second.

Note that time must have the units of seconds and see how this must be incorporated into all 3 terms defining the range variable.


I’ve added grid lines on the y-axis which allow an estimate to be made of when the cannonball will hit the ground, which in this case will be about 220m down range.

As an exercise, determine the value of a which gives the maximum range – is this value what you expected ?

Exercise 3 – Reading from a file

Manually typing in values for analysis within mathCAD is a tedious and error prone process, in many circumstances you may be dealing with large data sets where it simply not practical, especially in cases where the data has been produced by a data logger or other computerised test equipment. Fortunately MathCAD provides several tools to enable reading of data from a file.

I have created a datafile in an area of public filestore for use with this example. Before you go any further, use windows explorer to copy the file from its location on the E: drive onto your M: drive, I the same directory as your mathCAD worksheets.

The file is in the location E:\Examples\Physics\Datafile.txt

Once you have copied the file onto your M: drive, you can use notepad or another editor to look at the data in the file. Its quite big, so I’ve just shown the top few lines here. See how each line of the file consists of two numbers separated by a tab character, this is a very common form of data file, which can be understood by virtually any package for handling data on the PC.

0 7.01935240989551

0.1 7.08132951383951

0.2 7.21167855755486

0.3 7.30117175628534

We will now use the File Read/Write component to capture this data and read it into mathCAD.

·  Position the cursor on a blank piece of worksheet.

·  Select the menu option Insert|Component

·  A Component Wizard should pop up, containing a list of components

·  Select the line marked File read or Write and click the Next button

·  The next page offers a choice of reading from or writing to a file, check that Reading is selected and click the Next button

·  You now have to selet the file format and browse for the name of the file.

·  The file is in Text format, so leave that option selected.

·  Click the Browse button and use the dialog box to select Datafile.txt from where you left it earlier.

·  Click Finish to end the wizard so that mathCAD can create the file reading component.

·  MathCAD will create the Read file component and position the cursor in a placeholder associated with it, ready for you to type a name for the matrix that will be filled with the data from the file.

·  Once you have completed the component, examine the matrix and check the data has loaded correctly.

Once you have completed this, your worksheet should look a bit like this:

In this example, column 0 of the array are the times that a measurement was taken, in seconds, and column 1 represents the current, in amps, flowing into a circuit.

Use the column extract operator to create two vectors to hold the time and current, multiplying each by the correct units at this stage.

Having brought the data into mathCAD, we can now plot the data and perform other analysis.


The remaining exercises on this sheet are intended to give you a feel for what mathCAD is capable of.

Exercise 4 – Complex Numbers

MathCAD will handle complex numbers with ease. The trick to handling complex numbers with mathCAD is that to enter the imaginary part, type the number followed by i, without a space between. MathCAd will display this differently depending on whether the editing cursor is in the expression or not.

MathCAD also offers some functions to work with complex numbers as shown below:

Exercise 5 – Direction Vectors

MathCAD recognizes vectors with 3 rows and 1 column as direction vectors and allows cross products on them. This is useful for problems where the direction vector represents forces or accelerations.

Assessed Exercise

This portion of the worksheet forms the last assessed portion of this part of the module. As before, start a fresh worksheet and work through these exercises. Send your completed worksheet by email to before midnight on 10 December 2001.

As usual, marks will be given for correctness, layout style, comments, use of units creative use of the material covered in the course.

To complete the exercises, you will have to research in the mathCAD resource centre.

Q1. Calculate the surface area, volume and mass of a right circular cone of Iridium with a base radius of 6cm and a height of 10cm.

Q2. A sphere of quartz with radius 5cm is dropped from a height of 1.7m.

Calculate the surface area, volume and mass of the sphere

Calculate the velocity with which the sphere will hit the ground, (neglecting air resistance)

Calculate the kinetic energy of the sphere just before it hits the gound.

Q3. Solve the following set of simultaneous equations, using matrix inversion:

Q4. Create a plot of sin(2x) vs sin(3x) over a suitable range of x.

Comment on the shape you have produced.

Q5. Create a file read component to read the data from the file in exercise 3 above.

Draw a plot of the current against time.

The voltage across the circuit was constant at 12V throughout the experiment; calculate the average power and total energy dissipated in the circuit.

Dave Langstaff Page 9 20/11/2001