Root Math Cad

7
 Using Mathcad to Find a Root of an Equation Charles Nippert This set of notes describes how to use Mathcad’s “root” function to find the root of an equation. In the first example we will solve the equation x e x  =  (1) This equation is representative of a broad range of equations that can only be solved by successive approximations. A simple approach to dealing with such equations is to rearrange the equation into the form f(x) = 0. A solution of the original equation is also a root of the function f(x) which is oft en called an “objective function”. A simple way to convert equation 1 into a function is to subtract the right hand side from both sides of the equation. This operation converts equation 1 to 0 e x x =   (2) So that x e x ) x ( f  =  (3) The roots of the original equation are also “zeros” of the function f(x). In other words, if X is a solution of equation 1, then f(X) = 0.  Now, you will use Mathcad’s root function to find a solution to equation 1 by finding a zero of the function f(x) given in equation 3. 1. Open Mathcad. Successive approximations require a first guess that must be supplied by the user. Type the name of the variable whose r oot you want to find and set it equal to your first guess . In this example you will use a value of 2 as the fir st guess. Move the red cross cursor to the upper left and corner of the workspace and type “x : 2” and press enter. The “:” key on the keyboard is a shortcut key for the button on the “Evaluation” toolbar that you can open by selecting “View/Toolbars/Evaluation” from the menu. Your workspace should look like Figure 1 Figure 1 After Entering the First Guess 2. Next, enter an equation that finds a root of the function in equation 3. Mathcad’s root function requires two arguments separated by a coma. The first argument is the function

description

how to do root in mathcad

Transcript of Root Math Cad

  • Using Mathcad to Find a Root of an Equation Charles Nippert

    This set of notes describes how to use Mathcads root function to find the root of an equation. In the first example we will solve the equation

    xex = (1) This equation is representative of a broad range of equations that can only be solved by successive approximations. A simple approach to dealing with such equations is to rearrange the equation into the form f(x) = 0. A solution of the original equation is also a root of the function f(x) which is often called an objective function. A simple way to convert equation 1 into a function is to subtract the right hand side from both sides of the equation. This operation converts equation 1 to

    0ex x = (2)

    So that

    xex)x(f = (3)

    The roots of the original equation are also zeros of the function f(x). In other words, if X is a solution of equation 1, then f(X) = 0. Now, you will use Mathcads root function to find a solution to equation 1 by finding a zero of the function f(x) given in equation 3. 1. Open Mathcad. Successive approximations require a first guess that must be supplied by

    the user. Type the name of the variable whose root you want to find and set it equal to your first guess. In this example you will use a value of 2 as the first guess. Move the red cross cursor to the upper left and corner of the workspace and type x : 2 and press

    enter. The : key on the keyboard is a shortcut key for the button on the Evaluation toolbar that you can open by selecting View/Toolbars/Evaluation from the menu. Your workspace should look like Figure 1

    Figure 1

    After Entering the First Guess

    2. Next, enter an equation that finds a root of the function in equation 3. Mathcads root function requires two arguments separated by a coma. The first argument is the function

  • that you want root to force to 0 and the second argument is the name of the variable that the root function will change. The root function returns one (possibly complex) value if it is successful even if the objective function has more than one zero. The use of the root function will be clearer as you proceed in this example. Thype xans: to define a new variable that will contain the value returned by root. Your workspace should look like Figure 2 a. Next type root( to specify the root function then press the keys x-exp(-x),x) then press enter. The completed function should like Figure 2b.

    Figure 2a

    Enter A Variable Name

    Figure 2b The Completed Function

    3. You will complete this example by displaying the answer. Type ans= below the line

    you just entered. The answer will appear as shown in Figure 3

    Figure 3 Displaying the Solution

    Example 2 Manipulating the Equation to Find a Solution. Sometimes the technique of subtracting the right hand side of an equation from the left hand side produces a function that needs to be modified in order for root to find a solution. In this example, you will attempt to find the roots of equation 4

    ( ) 03s1

    2s1

    2 =+ (4) 1. Begin this example by opening a new workspace. Select File/New from the menu. A

    small dialog box should appear. Select Normal from the options and click the button labeled OK. You should see an empty worksheet in Mathcad Window.

    This variable contains a value of the root of the function

    The first argument of root is the function to be driven to 0

    The second argument of root is the name of the variable that root will change to drive the function to 0

  • 2. Now define the variable and its first guess. Press the keys s:1 to enter the formula

    shown in Figure 4 Figure 4

    Enter the First Guess For the Value of s

    3. Now enter the function shown in equation 5 below.

    ( )23s1

    2s1)s(f += (5)

    When writing complicated functions into Mathcad, it is wise to break the function into components using parenthesis. In this step, you will learn how to use parenthesis to help writing long functions. Begin by pressing the keys f(s): to define a function f which takes the single variable, s, as an argument. Next press the () keys. Your screen should now look like figure 5a. Notice that the blue inverted L cursor in inside the parenthesis. Move the cursor outside the parenthesis by pressing the right arrow key once. Your function should look like figure 5b. Next press the +() keys to create the second placeholder. These placeholder will hold the two fractions in the functions. Your screen should now look like Figure 5c. Now press the / key. The second placeholder is replaced by a placeholder for the numerator and one for the denominator of the second fraction as shown in Figure 5d. The inverted L is on the placeholder for the numerator. Type 1 into the numerator and press the down arrow key to0 mover the cursor to the denominator. Type () to create the parenthesis needed for the denominator. Enter the term s-3. Move the cursor outside the parenthesis and press ^2 to create the exponent as shown in Figure 5f. Finally, use the arrow key to create the first fraction. The finished function is shown in Figure 5g

    Figure 5a The Function

    After the First Set of Parenthesis

    Figure 5b Move the Cursor

    Figure 5c Create the Second Set of

    Parenthesis

    Figure 5d Enter the /

    Figure 5e Enter the

    Numerator

    Figure 5f

    Enter the Denominator

    Figure 5g Finished

    4. Finish this example by creating a statement that uses root to drive f(s) to 0 by adjusting s.

    Below the function you just entered press the keys sans:root(f(s),s) and press Enter. Below that press the keys sans=. Your worksheet should look like Figure 6

  • Figure 6

    Finish the Example

    Why Didnt It Work? What happened? The explanation for why root did not find a zero of the function is found by examining a plot of f(s) versus s shown in Figure 7

    Figure 7 Plot of f(s) vs. s

    The limit of f(s) as s approaches +/- infinity is 0 and the absolute value of f(s) decreases as s goes to +/- infinity. The behavior of this function tricks root into looking for the value in the limit rather than searching for the actual solutions of the quadratic equation. At some large value of s, f(s) becomes smaller than the tolerance Mathcad uses to stop roots iterative search function and return an answer.

  • Another reason that this function is ill behaved is the presence of values of s for which f is infinite (these points are often called poles). Generally, it is wise to manipulate the objective function to remove poles if possible. To find a correct root for this equation you must perform some math to make the function well behaved (remove the poles and redefine the function so that the limits as s approaches +/- infinity are not 0). In this example, clear the fractions by multiplying the equation by (s-2)(s-3)2 to obtain equation 6

    ( ) ( ) ( )2s3ssg 2 += (6)

    Example 3: The correct solution to Example 2 1. Now, you will use Mathcads root function to find a root of equation5. Use the same

    worksheet that you used in step 4 and enter equation 6 below the work from the previous example. Use the function name g(s) because you will check that the solution you obtain also gives a zero for the function f(s). Therefore, you cannot override the existing definition of f(s). Your function g(s) should look like Figure 8

    Figure 8

    The Mathcad Form of g(s)

    2. Use the root function to drive g(s) to 0 using the same starting value (s=1) as you used

    previously. The first argument of root is the function root will drive to 0, the second is the name of the variable root will adjust. You will store the value root returns in gans. Enter the equation in Figure 9 below the equation for g(s).

    Figure 9

    Find a Solution for g(s)

    3. Display the value contained in gans. Type gans=. The result is shown in Figure 9.

    Notice that root was able to find a complex value of the solution of the quadratic.

    Figure 10 A Root to the Quadratic

  • 4. Finally, evaluate f(s) and g(s) at the value contained in gans. Type f(gans)= and press

    enter. Then type g(ans)= and press enter to check that the value really is a zero of the functions f(s) and g(s). The entire worksheet is shown in Figure 11.

  • Figure 11 The Complete Solution for Equation 4 and a Check