PH36010 MathCAD worksheet 3 Roots, solve blocks and symbolic mathematics The aim of this worksheet is to introduce you to different solution techniques in MathCAD. By the end of the sheet you will know how to: Use the root() function to solve equations of one variable Use the polyroots() function to find multiple roots of a polynomial Use a solve block to solve more complex systems of equations Use the symbolic processor to manipulate and solve equations This worksheet takes the form of a number of examples which illustrate and introduce these principles. Work through as many of them as you are able. At least you should read through the entire sheet to get an overview of the topic. Exercise 1 – Using the root() function In this exercise we will use the root() function to solve some simple equations. Start MathCAD and create a new, blank worksheet The first equation we will try and find a solution for is ex = x2 In order to use the root function, this must be in the form of f(x)=0 so re-write the equation in this form, and plot it. Add grid lines to the graph to help estimate the point where the curve crosses the y=0 line. You should adjust the axes limits of the plot to zoom in on the region where the line crosses the axis. When you have finished, your plot should look like the one below: f( x) x e 2 x 2 1 f( x) 0 1 1 0.5 0 0.5 x © dpl 2002,2005 1 1 PH36010 x MathCAD Example Sheet 3 By examining the graph, you can see that the solution will lie between –1 and – 0.5, so pick –0.75 as the guess and use the root function to get the exact value. 0.75 root( f( x) x) 0.703 Having successfully solved this equation, use the same technique to solve the following equations. In each case, use a plot of the function to find approximate solutions before solving with root(). 1.423 x 3 2 8.114238 x 8.537238 0 sin( x) cos( x) A cubic equation will have 3 roots. This will have many solutions 7 x Complex roots need a complex guess x 2 2 x 6 Exercise 2 – The effect of tolerance on solutions The root() function uses an iterative technique which takes successive guesses to, hopefully, converge on a solution. There is a system variable, TOL, when determines how close the function has to evaluate to 0 before mathCAD assumes that it has reached a solution. A small value of TOL will, in general, produce more accurate solutions, but may take longer to arrive at a result. In this exercise we will see how the value of TOL changes the result of solutions. The following quadratic equation has two co-incident roots: 9 x 2 12 x 4 Plot the quadratic and zoom in on the area of the roots. Use the following segment of worksheet to enable you to see how varying the value of TOL changes the solutions offered by MathCAD. TOL 0.0001 x 1 s oln1 root( f( x) x) s oln1 0.66898 f( s oln1) 4.82364 10 x 0.5 s oln2 root( f( x) x) s oln2 0.66364 f( s oln2) 8.2614 10 5 5 It is possible to change the value of TOL by means of a menu option within mathCAD (Math|Options|Built-In variables|TOL) but the method of explicitly setting the system variable on the worksheet is preferable as it shows what is being changed. Since TOL is already defined by the system, you will have to force the system to put up an assignment operator by using the ‘:’ key or selecting := from the evaluation palette. © dpl 2002,2005 2 PH36010 MathCAD Example Sheet 3 Exercise 3 – Using Polyroots() function to get roots of polynomials MathCAD provides a specialist function, polyroots(), for funding all the roots of a polynomial in one operation. It has the advantage that all roots, both real and complex, are returned at once and that it is not necessary to provide guess values. As an example, to find all the roots of the cubic equation from exercise 1, do the following: 3 x 1.423 x 2 8.114238 x 8.537238 polyroots 8.114238 1.423 1 8.537238 0 Equation to solve 2.718 1 3.141 You will need to generate the argument for the polyroots() function by inserting a matrix with 4 rows and a single column. Now use polyroots() to find solutions to the following polynomials. 2 7 x 2 x 6 2 9 x 12 x 4 4 2 3 9 x 13 x 12 x 12 x 4 4 3 2 x 20 x 87 x 180 x 864 Can you use polyroots to find the 3 cube roots of –1 and the 4 fourth roots of –1 ? © dpl 2002,2005 3 PH36010 MathCAD Example Sheet 3 Exercise 4 – Using a solve block to solve more difficult problems. The solve block is another iterative technique available within mathCAD which will solve up to 50 equations in 50 unknowns. The equations do not have to be polynomials and can include inequalities (<,>,,). Each of the unknowns must be given an initial guess value. We will start with some simple examples and progress to more difficult situations. The first example will use a solve block to find the square root of 3, starting with a guess value of 2. x 2 Given 2 x 3 0 find( x) 1.73205 Initial guess value is defined first Solve block starts with keyword 'given' Use <ctrl>= to get bold equals sign The find() function finishes the solve block and returns solution(s) Note the use of the bold equals sign (=) or ‘equals to’ operator in the solve block. This is a different symbol to either the assignment operator (:=) or the ‘print the value of a variable’ operator (=). The ‘equals to’ operator is used extensively within solve blocks and can also be used in Boolean logic. In the next example, we will use a solve block to find the places where the unit circle intersects with the line x+y=0. Unit circle (x2+y2=12) Solutions Line of x+y=0 As before, we start our solve block with guess values for x- and y- © dpl 2002,2005 4 PH36010 x 1 MathCAD Example Sheet 3 y 1 Guess values given 2 x Equations in solve block 2 y 1 x y 0 find( x y) 0.70711 Find() returns vector of solutions 0.70711 this returns the x,y values for one of the intersection points. In order to return the coordinates of the other point, we can either start with guess values closer to it, or put a condition in the solve block to force the solution. x 1 y 1 given 2 x 2 y 1 Extra inequality added to solve block x y 0 x 0 find( x y) 0.70711 0.70711 MathCAD has restrictions on what may be written in the solve block (between ‘given’ and ‘find()’ ), in particular it is not allowed to put a definition of either a variable or a function. In the above examples, we have used the ‘print value of’ equals sign to print the values of x and y at the solution. By creating a vector to hold the results, we can set variables to hold the solution in one step, as shown below: xval yval find( x y) Multiple assignments in single step xval 0.70711 yval 0.70711 © dpl 2002,2005 5 PH36010 MathCAD Example Sheet 3 Use a solve block to solve the following systems of equations: 3 x 7 y 90 2 x 7 x 2 y 25 x 2 y z 4 2 y 3 3 x 4 y 2 z x y 1 You should have 4 solution points 3 6 x 2 y z 4 For the first and last of these, you should check your result using the matrix inversion technique from a previous worksheet. At the back of this handout is an exercise, taken from a previous PH15010 example sheet, to plot the trajectory of a cannon ball. Create this exercise in a fresh worksheet and use one of the above solution techniques to determine the time at which the cannon ball hits the ground after it has been fired. Exercise 5 – Symbolic Maths and manipulation of equations As well as the techniques for providing numeric solutions to equations and systems of equations we have used so far, MathCAD includes a power symbolic processor, which can manipulate and solve equations using algebraic techniques. 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 enter the following exercises. Simplifying Expressions To see how the symbolic processor can help simplify expressions, enter the following expression in MathCAD. 6x 2 2x a 7x 3a x 2 with the selection box In the expression, press ‘simplify’ from the symbolic palette and see how mathCAD simplifies the expression, collecting common terms and cancelling out where possible. 6 x 2 2 x a 7 x 3 a © dpl 2002,2005 2 x simplify 7 x 2 5 x 4 a 6 PH36010 MathCAD Example Sheet 3 Use the symbolic processor to 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 the 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,2005 7 PH36010 MathCAD Example Sheet 3 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. 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 © dpl 2002,2005 8 PH36010 MathCAD Example Sheet 3 Exercise 6 – The tin can problem This is an exercise taken from a first year example sheet, which shows how we can use the symbolic processor in MathCAD to solve a real world problem. It is presented as a worked example, which you should copy and satisfy yourself that you understand the steps to reach a solution. 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 Area ( 2 r h) 2 r 2 Use the bold symbolic equals sign in constructing these two equations, as we will be manipulating them with the symbolic processor. Copy the expression for area and use the symbolic solver to derive an expression for h in terms of the area and r. Area ( 2 r h) 2 r solve h 2 1 2 Area 2 r ( r) 2 Copy 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 s ubs tituteh Area 2 r ( r) 2 1 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. 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. © dpl 2002,2005 9 PH36010 MathCAD Example Sheet 3 1 1 2 Area 3 r 2 0 solve r 1 2 6 ( Area) ( 6 ) 1 1 2 6 ( Area) (6 ) MathCAD has provided two solutions corresponding to positive and negative values of r. We are interested in positive values of r, 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 6 ( Area) substituteArea ( 2 r h) ( 6 ) 1 6 2rh ( 6 ) 2 r 2 2 r 2 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 6 2 r h 2 r (6 ) 2 0 r s olve r 1 2 h Neglecting the r=0 case, we now have the solution that the maximum volume is attained when r=h/2. Note that it is possible to combine together several symbolic operations in one step, as shown below: 1 1 2 r Area 2 r dr 2 d 1 6 2rh ( 6 ) solve r 0 substituteArea ( 2 r h) 2 r 2 2 r 2 2 1 1 6 2rh ( 6 ) 2 r 2 2 As an exercise, return to the cannon ball problem mentioned earlier, and, using the symbolic processor, derive an expression for the distance the cannon ball will travel before it hits the ground in terms of initial velocity, V0 and angle of the cannon, . Use symbolic differentiation to find the angle of that maximises the range of the cannon. © dpl 2002,2005 10 PH36010 MathCAD Example Sheet 3 Appendix 1- Plotting the path of a cannon ball. This exercise is taken from a previous PH15010 course and shows how MathCAD can predict the simple trajectory of a cannon ball. 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. 30 deg VInitial 50 m s () VXInitial VInitialcos Seperate initial velocity into X- & Y- components () VYInitial VInitialsin 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 xaxis 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. Pos X( t ) VXInitialt Pos Y( t ) VYInitialt 1 2 gt 2 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. © dpl 2002,2005 11 PH36010 t MathCAD Example Sheet 3 0 s 0.1 s 6 s 40 30 20 10 PosY( t ) 0 10 20 30 0 50 100 150 200 250 300 PosX ( t ) 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 which gives the maximum range – is this value what you expected ? © dpl 2002,2005 12