* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Handout 9
Survey
Document related concepts
Transcript
Example Sheet 9 – The if() function, Complex numbers and Symbolic Algebra. 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: if( cond Tval Fval) 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. © DPL 2002,5 1/10 PH15720 MathCAD Example Sheet 9 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 a R 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: B( r ) 0 I 2 r where: 0 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: I r B( r ) o 2 a2 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) © DPL 2002,5 2/10 PH15720 MathCAD Example Sheet 9 Magnetic field inside and outside a conductor I 1 amp a 1 mm 0 Current in conductor Diameter of conductor 7 newton 4 10 2 amp Permeability of vacuum 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. 0 I 2 r Bouts ide( r ) 0 I r Bins ide( r ) 2 a 2 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. 2 10 3 0.002 0.0015 Boutside ( z) 0.001 Binside( z) 5 10 2 10 4 5 0 0.1 mm 0.002 0.004 0.006 z 0.008 10 mm 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: © DPL 2002,5 3/10 PH15720 MathCAD Example Sheet 9 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: 4 10 4 3 10 4 B( r ) 2 10 4 1 10 4 0 0 0 0.002 0 mm 0.004 0.006 0.008 r 10 mm 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. B( r ) 0 I r 0 I if r a 2 2 a 2 r 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. B( r ) 0 I 2 if r a r 1 2 a r 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) © DPL 2002,5 4/10 PH15720 MathCAD Example Sheet 9 Complex Numbers MathCAD understands and can operate on complex and imaginary numbers with the 4 same ease3 10it handles ordinary numbers. As with many aspects of MathCAD, the trick is to understand how to enter complex numbers in the MathCAD environment. B( r ) 2 10 To start with, ask MathCAD to calculate the square root of –1 by typing \-1=. MathCAD will display the result as shown: 4 1 1i 1 10 4 If you move the 0 selection box out of the equation the display will change, to show the 0 0.002 0.004 0.006 0.008 complex number in a more usual form: r z 1 i 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) ( 2 3 j ) (3 2 j ) 43j © DPL 2002,5 5/10 PH15720 MathCAD Example Sheet 9 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: 6 x 2 4 x a 3 x 2 4 a x 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. 6 x 2 4 x a 3 x 2 4 a x simplify 3 x 2 4 x a 4 a x Note how MathCAD uses the right facing arrow as a ‘symbolic equals’ sign. © DPL 2002,5 6/10 PH15720 MathCAD Example Sheet 9 Try and simplify the following expressions: 3 x 7 a 2 6.5 p 2 x 6 a 2 5.7 q 2 6.9 q 2 2 3 x 2 2 x 1.2 p 7 a ( 2 q) 2 2 x 2 3 3 6 3 r s m n 4 3 r m n 7 3 3 7 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. 3 ( g h) expand 3 g 3 h 3 g h 2 2 3 g h Expand the following examples: 2 y ( x 7 y) 3 x ( p x q ) ( r x s) 1.23 a 2 2 2 2 2 4.56 b 7.89 b 6.66 a 2.7 a 2 4.2 b 2 ‘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: x 1 2 ( x y) expand 1 2 y y 2 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. © DPL 2002,5 7/10 PH15720 MathCAD Example Sheet 9 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: x 1 2 ( x y) expand x 1 2 y y 2 x 2 ( x y) expand 2 x 2 2 x y y 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. 2 x x 2 0 s olve x 1 2 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: ( x 1) ( x 1) 0 2 x 1 0 2 x 1 0 ( y 2) ( y 3) 0 3 z 2 2 z 8 0 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. © DPL 2002,5 8/10 PH15720 MathCAD Example Sheet 9 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. 2 Vol r h 2 Area 2 r h 2 r 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. 2 Area 2 rh 2 r solve h 1 Area 2 r 2 ( r) 2 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: 2 Vol r h substituteh 1 Area 2 r 2 ( r) 2 Vol 1 2 r Area 2 r 2 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. d 1 2 r Area 2 r dr 2 1 2 Area 3 r 2 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. © DPL 2002,5 9/10 PH15720 MathCAD Example Sheet 9 1 1 2 Area 3 r 0 s olve r 2 1 2 6 ( Area) (6 ) 1 1 2 6 ( Area) ( 6 ) 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. 1 1 1 2 2 6 ( Area) substituteArea 2 r h 2 r (6 ) 1 2 6 2 r h 2 r (6 ) 2 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. 1 1 2 r 6 2 r h 2 r ( 6 ) 2 0 s olve r 1 h 2 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 10/10