MathCAD worksheet 4 – Drawing Graphs

© dpl 2001,5,61

PH15720 MathCAD Example Sheet 4

The aim of this worksheet is to introduce you to graphs in MathCAD.

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

  • Draw graphs of mathematical functions
  • Select the range over which plots are taken
  • Change the appearance of plots

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

Exercise 1 – Plot of sin(x)

As an introduction to plotting functions in mathCAD, we will start by making a plot of sin(x) vs x. This is a function with which you should all be familiar and forms a good place to start. We will use a facility in mathCAD called ‘quickplot’, which makes it very easy to take simple plots, you have to type the bare minimum of information and mathCAD makes guesses as to the other things it needs.

Start with a blank worksheet and select the menu command:

Insert|Graph|X-Y plot

MathCAD will create a blank plot and insert it in the worksheet. When the graph is first inserted, it shows 2 placeholders, one for the x-axis and one for the y-, as shown below:

  • Select the –x-axis placeholder and type the name of the independent variable, in this case ‘x’.
  • Then select the y-axis placeholder and type the expression we wish to plot, in this case ‘sin(x). MathCAD will now create a plot of sin(x) vs x, it has selected a range of –10 to +10 for x, we will see how to change this later.
  • The default plot in mathCAD is rather small, so position the cursor over the sizing grab-handle at the bottom right hand corner of the plot (the cursor will change to a double ended arrow) and drag the grab-handle to make the plot fill most of the screen.

Your completed plot should look like the figure below:

We will use this graph as the basis for the next few exercises.

Exercise 2 – Changing the appearance of the plot

MathCAD allows us to change the appearance of our basic plot. We can add grid lines to the axes, change the style of plotting and change the range of both axes. We can also plot more than one curve on the same graph.

To change the appearance of the graph, select Format|Graph|X-Y Plot…. MathCAD displays a tabbed dialog box with many options to control the appearance of the graph.

  • On the X-Y Axes page, select the checkboxes for gridlines on both axes.
  • Select the tab for ‘Traces’
  • In the list box, against the entry for ‘trace 1’, change the line colour to black.
  • You can set other properties the traces on the plot if you wish, you may need to scroll the box of trace options to see all of them.
  • Press the OK button to apply the changes to the graph.

Your graph should now look like the following:

Exercise 3 – Adding more curves and setting axes limits

For this exercise, we will add curves for the cos(x) and tan(x) functions to our plot and then adjust the plot ranges to scale our plot.

  • Select the graph of sin(x)
  • Click the mouse on the y-axis placeholder, against sin(x)
  • Press <space> until the selection box encloses the sin(x) expression, with the insertion bar after the closing bracket of (x)
  • Press <comma> to add a new curve
  • Type cos(x),tan(x) to add curves for cos and tan to the graph

Your result should look like the following:

The y-axis will have automatically scaled to quite large values as it tries to accommodate the tan curve as it shoots towards infinity. Up until now we have let mathCAD scale the axes automatically. We will now give mathCAD values to use for the maximum and minimum points on each axis.

  • Select the scale maximum placeholder on the y-axis and type ‘2’
  • Select the scale minimum placeholder on the y-axis and type ‘-2’
  • Select the scale maximum placeholder on the x-axis and type ‘4’
  • Select the scale maximum placeholder on the x-axis and type ‘-4’

When you click the mouse outside the graph area, the plot should update to show the 3 curves plotted with the new axes.

It is often useful to plot horizontal or vertical lines called markers on the graph to show important features. We will add markers on the x-axis at – and .

  • Double click in the graph to bring up the formatting dialog box
  • Switch to the ‘X-Y axes page
  • Select and check the checkbox against ‘Show Markers’ in the x-axis column
  • Select the OK button to close the dialog box.
  • Two placeholders should have appeared below the x-axis on the graph.
  • Select the left hand placeholder and type [-][ctrl-shift-p] to put –p in the placeholder
  • Select the right hand placeholder and type [ctrl-shift-p] to put p in the placeholder
  • Click out of the graph to re-draw the plot with the markers added.

Your graph should now look like the following:

You have now completed the part of this worksheet dealing with creating and formatting graphs. If you have free time and are feeling confident, try the remaining two exercises on this worksheet, otherwise move onto worksheet 5.

Exercise 4 – Plotting your own functions – The Sine approximation (Taylor series)

Providing x is in radians, a good approximation for sin(x) is x itself. This is valid for small values of x.

A better approximation is given by:

where 3! is the factorial of 3 (=3x2x1 = 6)

A better approximation yet is given by:

This exercise will plot values for the first 3 approximations of sin(x) with the aid of some user defined functions.

  • Start on a fresh worksheet by defining a function for the first approximation. Although the function simply returns the value of its argument, we will still define this as a function in order to make our graph clearer.
  • Call the function SinApp1(x) and define it thus:
    SinApp1(x):x<return>
  • Similarly, define the second approximation:
    SinApp2(x):x-x^3<space>/3!<return>
    watch out that you get the division bar in the correct place.
  • In the same way, define the third approximation, SinApp3(x)
  • Create an X-Y plot.
  • Put x on the x axis and set the axis limits to 4
  • Plot sin(x), SinApp1(x), SinApp2(x) and SinApp3(x) on the y-axis.
  • Set the y-axis limits to 2

Your worksheet should look like the one on the next page:

Exercise 5 – The Cosine Approximation

In the same way as the sin(x) function may be approximated by a polynomial, it is possible to approximate the cosine function.

which can be re-written, using the identity x0=1, as

  • Define functions to evaluate the first three approximations of cos(x), where CosApp1(x):= 1
  • Produce a graph showing cos(x) and the three approximations over the range of x-values 4.

It is frequently important to know how much error is introduced into a problem by using an approximation to sin(x) or cos(x) by using a simple approximation. Create a new graph showing the error in the approximation. In this case we will define the error in the approximations as the difference between our approximations and the built-in sin() and cos() functions.

  • Define 3 new functions, sinErr1(x), sinErr2(x) and sinErr3(x) which calculate the error in using the approximate functions sinApp1(x), sinApp2(x) and sinApp3(x) respectively.
  • Plot the 3 error functions on a new graph over the range 4, limiting the y-axis scaling to 2.

If you have time and want to explore further…

Mathematicians will use a summation notation to write down expressions such as we have explored for the sin and cosine approximations. Perhaps not surprisingly, MathCAD does offer such a facility, and it may be found on the Calculus toolbar (View|Toolbars|Calculus). Using this, you can define a function to give the first N terms of the Taylor approximation to the sine function as shown below…

Plot a graph of your new function compared with the built in sin() function.

© dpl 2001,5,61