Example Sheet 9 – The if() function, Complex numbers and Symbolic Algebra.

© DPL 2002,5 1/10


PH15720 MathCAD Example Sheet 9

This worksheet introduces some more aspects of mathCAD which may be useful to you.

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

· Create discontinuous functions with the if() function

· Use mathCAD to handle complex numbers

· Have an awareness of symbolic algebra in mathCAD

Modelling a discontinuous universe – The if() function

MathCAD provides an if() function for making logical decisions. The format of the if() function is shown below:

The arguments of the if() function are:

· cond is a logical expression usually involving a logical operator, some examples are:

o (x>3) True if x greater than 3

o (x≤7) True if x less than or equal to 7

o (x=3) True if x is equal to 3. The bold equals sign is different from either the assignment operator (:=) or the print operator (=). You can find the bold, boolean equals sign on the evaluation palette.

o (x<1) ∙ (x>0) True if x>0 and x<1. Use multiplication to get a logical AND.

o (x<7) + (x>9) True if x<7 or x>9. Use addition to get a logical OR

· Tval is the value returned when cond is true.

· Fval is the value returned when cond is false

MathCAD’s if() function behaves in a very similar fashion to the if() function used in spreadsheets such as Excel.

Having introduced the if() function, we can now use it to model some real-life situations.

Magnetic field in and around a conductor.

We will start by considering the magnetic field inside and outside a long straight wire. You can read about the physics involved in Keller, Gettys & Skove pp676, 683

We will create a function B(r) which gives the magnetic field B at a distance r from the centre of the wire.

At points outside the wire, where r>a, the magnetic field is given by the equation:

where:

m0 is the Permeability of vacuum

I is the current in the wire

r is the distance from the centre of the wire.

Inside the wire, where r≤a, the magnetic field is given by:

We will draw a graph of the magnetic field for a current of 1 Amp flowing in a wire of radius 1mm. We will plot the field at a distance of up to 10mm from the centre of the wire.

Start a fresh MathCAD worksheet and create variables for the current, the wire diameter and the permeability of vacuum (which you can copy from the resource centre)


As a first go, we will create two separate functions, Boutside(R) and Binside(R) to model the magnetic field and plot them on a graph.

Note that the Boutside(r) function will zoom off to infinity when r = 0 so we set the limits on the x-axis to 0.1mm and 10mm.

Note that MathCAD has scaled the x-axis in metres.

Having looked at the separate functions for the magnetic field inside and outside the wire, we can combine the two into a single function that covers all cases. As in many aspects of MathCAD, there are several ways of achieving the same result. Having got the definitions of Binside(r) and Boutside(r), we could combine them with an if() functions as shown below:

B(r):= if(r<a,Binside(r),Boutside(r))

We can now graph this function for values of r between 0 and 10mm as shown below:

Although this is a solution to the problem it is not particularly elegant, we have had to define the two ‘auxiliary’ functions before combining them together. We can skip this step and produce a single function that does all the calculation in a single step.

Note how both Binside(r) and Boutside(r) have a number of terms in common which we can pull outside of the if() function to produce a more elegant solution.

Use the last definition of B(r) to plot the magnetic field from 0mm to 10mm. Add a marker to your x-axis to mark the point where r=a.

What happens if you try and use the above function for negative values of r?

Does this make sense?

Can you modify your definition to give a sensible result for all values of r?

(Hint: The absolute value operator is on the arithmetic palette. You will need to use it 3 times)

Complex Numbers

MathCAD understands and can operate on complex and imaginary numbers with the same ease it handles ordinary numbers. As with many aspects of MathCAD, the trick is to understand how to enter complex numbers in the MathCAD environment.

To start with, ask MathCAD to calculate the square root of –1 by typing \-1=. MathCAD will display the result as shown:

If you move the selection box out of the equation the display will change, to show the complex number in a more usual form:

To enter imaginary numbers in MathCAD, simply put the character i or j immediately after the digits of the number. For example to enter the complex number 1.2+3.4j, simply type the digits as shown. The only confusing example is when you have to enter i which should be entered as 1i

Note that MathCAD understands that i (or j) is different from a variable called i (or j), but you may get confused so it is good practice to avoid using i or j for variable names in a worksheet where you are using complex numbers.

The modulus of a complex number may be found by using the absolute value operator, which is the same as the determinant operator, which is the vertical bar on the keyboard (shift \)

The argument of a complex number may be found using the arg() function.

If you prefer to use j instead of i in the display of complex numbers, this can be changed form the format|Result… dialog box.

To show how easy MathCAD can make life, we will revisit some examples from a first year example sheet. Calculate the following:

j3 j8 j14

(5+4j)∙(3+7j)∙(2-3j)

Symbolic Evaluation

Up to now we have been using MathCAD to come up with numeric answers to problems. We have created equations and plugged numbers into these equations to give answers in terms of numbers. In addition to its tools for handling numeric answers, MathCAD also has a powerful set of tools for the algebraic manipulation of equations.

This section of worksheet is intended as an introduction to symbolic processing. It is by no means a comprehensive tutorial but rather a starting point for your own explorations. For further information about symbolic processing look in the following places in MathCAD’s resource centre:

Quicksheets|Arithmetic and Algebra|Live Symbolic Algebra

Quicksheets|Arithmetic and Algebra|Advanced Symbolic Keywords – Algebra

Quicksheets|Calculus and Differential Equations|Computing a Symbolic Derivative

Quicksheets|Calculus and Differential Equations|Symbolic Integration of a Function

Quicksheets|Solving Equations|Symbolic Solution of Equations

Advanced Topic|Solving and Optimazation|Solving Equations Symbolically

When working with the symbolic processor, it is helpful to bring up the symbolic toolbar.

To see how the symbolic processor can solve equations and evaluate expressions symbolically, start a fresh worksheet and enter the following exercises.

Simplifying and Expanding expressions

The symbolic processor can simplify and expand expressions. To see it in action, try the following.

Enter the expression:

Now, with the selection rectangle still in the expression, select ‘simplify’ from the symbolic toolbar. MathCAD will tidy up the expression and remove simplify it as much as possible.

Note how MathCAD uses the right facing arrow as a ‘symbolic equals’ sign.


Try and simplify the following expressions:

Expanding Expressions

To see how the symbolic processor can expand expressions, put the selection rectangle in an expression and press ‘expand’ from the symbolic palette.

The expand keyword has a placeholder after it, you should either fill in the variable you wish to expand on or, delete the placeholder. There appears to be a bug in mathCAD, as it seems to make no difference what is in the placeholder.

Expand the following examples:

‘Undefining’ Variables

When processing equations symbolically, mathCAD will use the numeric value of any variables that have been already defined. As an example, consider the following binomial expansion:

Here mathCAD has used the value of 1 in the place of x before performing the expansion. Depending on your problem, this may not always be what you want to achieve.

In order to undefined a variable, use an assignment operator to set the variable equal to itself. MathCAD understands this recursive definition to undefine the variable. This is illustrated below:

Solving Equations

To solve an equation using the symbolic processor, enter the equation you wish to solve and then press ‘solve’ on the symbolic toolbar. Fill in the variable you wish to solve for in the placeholder and press <enter> to see the result. You should use the logical (or symbolic) bold equals sign from the evaluation palette in these examples.

Note that MathCAD cannot find a solution to the above case where x is already defined, you must undefine x using the recursive definition introduced above.

Now solve the following equations using the symbolic processor:

What follows next is an example showing how the symbolic processor can be used to solve the sort of maths and algebra problem you will come across in the physics course. It uses many of the symbolic algebra techniques we have just introduced and some others, like symbolic differentiation, that we haven’t covered.

The tin can problem

A manufacturer of tin cans wishes to maximise the volume contained in a can, whilst minimising the amount of metal used to construct the can. Show that, for given amount of metal, the volume of a can is maximised when the radius is half the height.

Start by writing down expressions for the Volume and Area of the can, in terms of the radius, r, and height, h.

Use the bold symbolic equals sign in constructing these two equations, as we will be manipulating them with the symbolic processor.

Copy and paste the expression for area and use the symbolic solver to derive an expression for h in terms of the area and r.

Copy and paste the expression for the volume and use the symbolic engine to substitute h in the expression with the expression you have just derived:

We now have an expression for the volume of the can in terms of the Area and the radius. Given that the area is constant, dVol/dr will be zero at the maximum volume. Copy and paste the expression for volume and use symbolic differentiation to differentiate it with respect to r. You can find the differentiation operator on the calculus palette.

Use the ® symbol from the symbolic palette, or type <ctrl>.

Now we have an expression for dVol/dr, we can find the value of r where this is zero, cut and paste the result of the previous manipulation and solve to find the value of r that makes this zero.

MathCAD has provided two solutions corresponding to positive and negative values of r. Real tin cans made of real metal tend to have positive radii, so we will copy the first solution and substitute back the expression for the area, to arrive at an expression for r at the point of maximum volume.

We now have an expression for r in terms of r and h which maximises the volume. So we can now solve for r in terms of h.

Neglecting the r=0 case, we now have the solution that the maximum volume is attained when r=h/2.

Although this example has introduced several aspects of MathCAD that you haven’t been formally taught yet, it should serve as an example of how developing a fluency in MathCAD can help you with many, many aspects of the degree course.

© DPL 2002,5 1/10