Top Banner
Byrge Birkeland Calculus and Algebra with Mathcad 2000 Studentlitteratur
190

Studentlitteratur.ab.Calculus.and.Algebra.with.Mathcad.2000.2008.Retail.ebook Distribution

Oct 31, 2015

Download

Documents

Liviu Orlescu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Byrge Birkeland

    Calculus and Algebra with Mathcad 2000

    Studentlitteratur

    Alekseythumbnail.jpg

  • Mathcad is a registrered trademark of Math Soft Inc.

    In Sweden Mathcad is distributed by Ravenholm Computing AB Email: [email protected]

    Copying prohibited

    All rights reserved. No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or any information storage and retrieval system, without permission in writing from the publisher.

    Art. No. 7193 ISBN 91-44-01356-6 eISBN 91-44-03290-0 Byrge Birkeland and Studentlitteratur 2000

    Printed in Sweden Studentlitteratur, Lund Web address: www.studentlitteratur.se Printing/year 1 2 3 4 5 6 7 8 9 10 04 03 02 01 2000

  • Table of contents Preface 9

    1 Fundamentals 11 1.1 Elementary operations 11 1.2 Handling files 15

    1.2.1 Saving your work 15 1.2.2 Starting a new file 15 1.2.3 Opening an existing file 16 1.2.4 Closing a file 16 1.2.5 Printing a file 16

    1.3 Symbolic versus numerical calculations 17 1.3.1 Numerical and symbolic Mathcad 17 1.3.2 Forcing symbolic calculations 17

    1.4 Mathcad's built-in functions 19 1.5 Variables 20

    1.5.1 Defining a variable 20 1.5.2 Variable names 21 1.5.3 Range variables 22 1.5.4 Using dimensions 23

    1.6 Arrays 25 1.6.1 Defining an array with [Ctrl]M 25 1.6.2 Accessing the elements of an array 26 1.6.3 The nth column vector of a matrix 26 1.6.4 Defining an array by means of a formula 27 1.6.5 The first index of an array 28 1.6.6 The number of rows and columns 28 1.6.7 Array operations 28 1.6.8 Nested arrays 30 1.6.9 Defining bigger arrays 30

    1.7 Functions 32 1.7.1 Defining your own functions 32 1.7.2 Drawing the graph of a function 33

    3

  • 1.7.3 Drawing a parameter curve 36 1.7.4 Drawing a polar curve 36 1.7.5 Functions as arguments to a new function 38

    1.8 Using Mathcad's programming language 40 1.8.1 An example: The trapezoidal rule 40 1.8.2 Using Mathcad's programming language 40

    1.9 Numbers 46 1.9.1 Functions for number theory and combinatorics 46 1.9.2 Functions for rounding and truncations 46 1.9.3 Prime numbers 47 1.9.4 Numbers in various bases 49

    1.10 Sums and products 50 1.10.1 The summation symbol 50 1.10.2 The product symbol 51

    1.11 Three dimensional plots 52 1.11.1 The graph of a function of two variables 52 1.11.2 Contour plots and 3D bar charts 53 1.11.3 Parameter surfaces 54 1.11.4 Space curves 57 1.11.5 Drawing several three dimensional objects 58

    1.12 Text 60 1.12.1 Text blocks 60 1.12.2 Text variables 62 1.12.3 Text on two dimensional graphs 63

    1.13 Animation 65

    2 Algebra 67 2.1 Tools for symbolic algebra 67 2.2 Algebraic expressions 68

    2.2.1 Evaluating an expression 68 2.2.2 Simplifying an expression 69 2.2.3 Expanding an expression 70 2.2.4 Factoring an expression 71 2.2.5 Collecting on a subexpression 72 2.2.6 Partial fractions 73

    4

  • 2.2.7 Substituting an expression in another expression 74 2.2.8 Getting a floating point value 76 2.2.9 Polynomial coefficients 76

    2.3 Equations 77 2.3.1 Solving an equation with one unknown numerically 77 2.3.2 Solving an equation with one unknown symbolically 78 2.3.3 Polynomial equations solved numerically 79 2.3.4 Several equations with several unknown solved

    numerically 79 2.3.5 Linear equations 80 2.3.6 Several simultaneous equations solved symbolically 81

    3 Calculus 84 3.1 Limits 83 3.2 Differentiation 83

    3.2.1 The derivative as a limit 83 3.2.2 Mathcad's built-in differentiation operator 84 3.2.3 Higher order derivatives 85 3.2.4 Differential formulae 85 3.2.5 Differentiation of implicit functions 86 3.2.6 Tangent and normal 87 3.2.7 Curvature and evolute 89 3.2.8 Envelope of a family of curves 93 3.2.9 Extrema of functions 94 3.2.10 Partial derivatives 96 3.2.11 Series 96 3.2.12 Extrema of functions of two variables 97 3.2.13 Extrema of functions with three or more variables 99 3.2.14 Extrema with side conditions 101 3.2.15 Linear optimization 103

    3.3 Integration 104 3.3.1 Indefinite integrals 104 3.3.2 Definite integrals 106 3.3.3 Improper integrals 108 3.3.4 Plane areas 109 3.3.5 Curve length 112 3.3.6 Centroids and moments of inertia 114 3.3.7 The volume of a surface of revolution 116

    5

  • 3.3.8 Surface area of a surface of revolution 119 3.3.9 Double integrals 121

    3.4 Vector calculus 122 3.4.1 Vectors in Rn 122 3.4.2 Vector valued functions 123 3.4.3 Differential geometry of space curves 125 3.4.4 Vector fields and scalar fields 127 3.4.5 Line integrals 128 3.4.6 Surface integrals 130 3.4.7 Surface areas 132 3.4.8 Triple integrals 132

    4 Differential equations 135 4.1 Differential equations of order one 135

    4.1.1 Indefinite integrals y'=f(x) 135 4.1.2 Separable equations f(y) dy = g(x) dx 135 4.1.3 Linear equations y' + f(x) y = g(x) 136 4.1.4 Linear equations y' - a y = g(x), where a is a constant 136 4.1.5 Bernoulli's equation 137 4.1.6 Total differentials 137

    4.2 Differential equations or order two or higher 138 4.2.1 Homogeneous equations with constant coefficients 138 4.2.2 Inhomogeneous equations with constant coefficients 139 4.2.3 Solution by power series expansion 142

    4.3 Laplace transforms 144 4.3.1 The built-in Laplace transform 144 4.3.2 Application to initial value problems 144

    4.4 Difference equations 146 4.4.1 The method of undetermined coefficients 146 4.4.2 The z transform 147

    4.5 Fourier series 149 4.5.1 Fourier coefficients and Fourier series 149 4.5.2 Step functions 150 4.5.3 Fourier transforms 151 4.5.4 Fourier transforms and the heat equation 152 4.5.5 Discrete Fourier transforms 153

    6

  • 4.6 Numerical solution of differential equations 155 4.6.1 Drawing a two dimensional vector field 155 4.6.2 Drawing a family of integral curves 158 4.6.3 Runge-Kutta's fourth order method rkfixed 159 4.6.4 Drawing a family of numerically computed integral curves

    161 4.6.5 ODEs of order two or higher 163 4.6.6 Phase diagram for a damped pendulum 165 4.6.7 Other differential equation solvers 165 4.6.8 A boundary value proble: Ballistics 169

    5 Linear Algebra 173 5.1 Elementary row operations and Gauss elimination 173

    5.1.1 Elementary row operations 173 5.1.2 Naiv Gauss elimination 175 5.1.3 Gauss elimination with pivoting 175

    5.2 Rank and subspaces 177 5.2.1 The rank of a matrix 177 5.2.2 The space spanned by the column/row vectors of a matrix

    178 5.2.3 Basis for the null space of a matrix 178 5.2.4 Gram-Schmidt's orthonormalization process 181 5.2.5 Completion to a full base 182

    5.3 Eigenvalues and quadratic forms 182

    Index 187

    7

  • Preface This book is intended to help students and teachers who want to use Mathcad

    as their computer tool in mathematics courses at first year college level. It

    contains the standard topics usually included: Calculus, vector calculus,

    differential equations and linear algebra, and in addition chapters on the

    fundamentals of Mathcad and elementary algebra.

    I have used Mathcad Professional version 2000.

    I have made no attempt to include the mathematical theory in the book. I

    presume that the theory is known from some standard textbook, and my

    emphasis has been to show how the various topics can be implemented with

    Mathcad. I have included a few exercises after most of the sections, but you

    may as well take your mathematics textbook and do the exercises you find

    there with the methods I describe in the book. For many topics, I have written

    my own functions that you may want to apply. You can find them on the WEB

    at the address www.studentlitteratur.se/7193. At this address, you can also

    find suggestions for solutions to some of the exercises. The definitions are in

    files with names of the form Dx_y.mcd for section x.y, and they are placed in

    a collapsed area. The suggested solution for exercise u in subsection x.y.z is in

    the file Xx_y_z_u.mcd. To use the definitions you can either use Insert -

    Reference and refer to the relevant file, or you can open the definition file and

    copy the definitions into your file.

    I would be interested to receive suggestions and remarks from the readers. My

    address is:

    Byrge Birkeland

    Agder College,

    Department of Mathematics and Sciences

    Serviceboks 422

    N-4604 KRISTIANSAND

    NORWAY

    Telephone: +47 38 14 15 08

    e-mail: [email protected]

    9

  • 1 Fundamentals

    1.1 Elementary operations You start Mathcad by double clicking on the Mathcad icon

    Mathcad is primarily a mathematics program that can be used to do calculations and draw diagrams. You can also use Mathcad as a calculator in a natural way. If you want to calculate say 2+3, you just put the cursor where you want the calculation to take place, type 2+3 and press =. Then what you see, is:

    2 + 3 = 5

    So the program has computed the value of 2+3 and inserted the result after the equality sign. Also, the program has inserted some white spaces to make the formula look better. Now, press [Enter] to move the cursor a suitable number of rows downwards, and write 2*3=. You see the following:

    2 3 = 6 So, Mathcad shows the multiplication sign * as the usual dot . This is a general principle of Mathcad: The formula you type, will look the way you are used to in mathematics rather than the way you type them. Try 2-2/3=. This will look like this:

    2 2 = 1.333

    3

    Mathcad performs the arithmetical operations in the same order as you are used to in mathematics:

    1 + 23 = 7 (1 + 2)3 = 9 Multiplication takes priority before addition, so if you want the addition to be performed before multiplication, you have to insert parentheses to change the default order in which the operations take place.

    Of course, Mathcad contains all the standard elementary operations, and they are performed in the order you are used to in mathematics. If you use the same operator twice or two operators of the same priority after one another, then the leftmost operator will be used first.

    11

  • In Mathcad, most operations can be done in several ways. You can use the keyboard or a menu for many operations. You will probably find it best to use the keyboard rather than the mouse, especially for operations that are used often. To get access to the elementary arithmetic operations, look at the Math Toolbar:

    This may have a different format on your screen. If you don't see it at all, you need to choose View - Toolbars - Math from the menu. Click on the first button -the one with the calculator - to get the calculator menu:

    The symbols on the toolbar are more or less self explanatory. However, there are alternative keystrokes for many of the operators on the toolbar, and you may want to use them instead of the palette.

    Instead of Use keystroke: Instead of Use keystroke

    ! '

    1i x^2

    ; \

    [ / | * e^x + x^-1 :

    .

    ^ -

    [Ctrl] | = [Ctrl] p

    To make the keystroke [Ctrl]p work, you need to turn off Standard Windows Shortcut keys: Use View - Preferences - General. If you haven't doen so, the shortcut key combination is [Ctrl] [Shift] p.

    12

  • All the functions ln, log etc. can be typed from the keyboard instead of using the palette. Also, if you need to write a sum over two lines, use the keystroke [Ctrl][Enter] The space bar plays an important role when writing and editing formulas. When you write a mathematical expression, you see a vertical and a horizontal editing line. The vertical line indicates where the next character is going to be placed, while the horizontal line indicates what part of the expression that is going to be an operand for the next operator you type. Pressing the space bar

    will extend this subexpression. For instance, say you want to compute 1 + 2 5

    Then you either use parentheses (1+2)/5, or you can use the space bar 1+2_/5 (where the underscore _ denotes the space bar).

    Example 1

    Type: To get: Type:

    (3*10^5)/7 (3 105) 2^3^2= 7

    3*10^5_ 7 3 105 2^3_^2=

    7 2+3^3=

    3 105 2+3_^3=3*10^5_ _ 7 7

    \3_+\5= 3 + 5 = 3.968 1/1/3+1/5=

    \3+\5= 3 + 5 = 2.288

    5 6 + 6 8 5*6+6*8_ _ _/15*4+12*3=

    To get:

    232

    = 512

    23( )2 = 64 2 + 33 = 29

    (2 + 3)3 = 125

    1 = 3.2

    1 1

    3+ 5

    = 0.813 154 + 123

    11/1/3_+1/5= = 1.875 1 1

    + 3 5

    2+3_*2[Ctrl]/[Enter]3+4_*5= (2 + 3)2 ... = 45 + (3 + 4)5

    2 [Ctrl]/ 3 or 2 [Ctrl] * 3 = 2 3 = 0.667 Let us look at an important detail. Mathcad, by default gives the result of a calculation as a decimal fraction:

    13

  • Type 2/3_ - 1/4= to get:

    After the result 0.417 you see a filled rectangle. This is Mathcad's notation for a placeholder - a place where an input is expected. In this case the input is optional, and if you choose to give any input here (click on the placeholder), the effect is that the result is given as the product of this input and another factor. In the case above, you could choose 1/12, since 12 is the common denominator and get:

    2

    1 5 1 = 3 4 12

    This trick can be used in several connections. Say you have a numerical result, but have a suspicion that it can be expressed as a multiple of some known symbolic constant. Then you just use this symbolic constant as a factor in the way described above. Here are some examples:

    atan 1( ) = 1 pi 8 = 2 2 sin pi = 0.5 4 4 2Alternatively, you can use symbolic evaluation, as described in section 1.3:

    atan 1( ) 1 pi 8 2 2 sin pi 1 4 4 2 2Another application is using dimensions, which we shall discuss briefly in section 1.5.4.

    Exercises. Make Mathcad do the following computations:

    1.21019 2.51020 3

    = 4.5 1018 2 + 5 = 3.65 100

    100 1 1 + = 2.705 22 + 32 + 42 + 52 = 54 100 1 1 1 1 12 + 13.50 10 + 1

    + + + = 0.464 = 1.833 22 32 42 52 2(3 + 1.5)

    1 1 1 1 8 + + + = 0.717 15 = 14.923

    2! 3! 4! 5! 104.5

    2 6 = 0.707 1 + 4.5 = 1.302 2 100 1 1 1 1 1 1

    1 + + + + + + = 127 1

    2 4 8 16 32 64 64

    14

  • 1.2 Handling files 1.2.1 Saving your work At this point you may want to save your work. The mechanism for saving your file is the same as for most windows programs. In fact there are several alternative ways to do it:

    Choose File from the menu. You get a pull-down menu where the first options are the following:

    Choose Save to save your work. If you save for the first time, you'll be asked for a file name. Otherwise, the present version of your file will replace whatever version you had before. If you want to keep both versions, you have to choose Save As instead. Then you'll be asked for a new file name for the new version of your file.

    Alternatively, you can save your file by clicking the icon

    You can also press [Ctrl] S or [F6].

    1.2.2 Starting a new file To open a new file, you also have four possibilities:.

    Choose New from the File menu

    Click on the icon

    Press [Ctrl] N Press [F7]

    15

  • 1.2.3 Opening an existing file Similarly, to open an existing file, you have the following four options:

    Choose Open from the File menu

    Click on the icon

    Press [F5] Press [Ctrl] O

    You'll be asked for a file name, which you can either type or pick from a list of files in the current directory.

    1.2.4 Closing a file To close a file means to save it to disk and then remove it from the machine's internal memory. You can do this by either

    choosing Close from the File menu or pressing [Ctrl][F4]

    1.2.5 Printing a file

    For printing the active file, you have two possibilities:

    Choose Print from the File menu.

    Click on the icon:

    In either case you get a dialogue box where there is a number of choices you can make. Most of the options are self explanatory, and I won't go into further details. Refer to Mathcad's documentation or use the Help function if you need any help.

    One thing, though: If you need to produce a Postscript file (or any other print file) that a publisher can use to produce a book (like this one) without having to open Mathcad, check the Print to File box. Then a file with Postscript code will be produced instead of a printout.

    16

  • 1.3 Symbolic versus numerical calculations

    1.3.1 Numerical and symbolic Mathcad

    Mathcad in its present form is really a combination of several programs. The first versions of Mathcad could only do numerical calculations. Symbolic manipulation of expressions was impossible. From version 3.0 on, however, Mathcad has included a symbolic part as well. The technology comes partly from the mathematics program Maple. Mathcad calls this part of the program "The Symbolic Processor" - the name could perhaps have been chosen better, since most people think of hardware when they see the word processor.

    The Symbolic Processor was a very useful addition to Mathcad. However, we shall see that the two parts of the program - the numerical and the symbolic part - are not one hundred percent integrated. The are some subtle, but important syntactic differences between the two parts, which you have to take careful notice of. For instance, numerical Mathcad can take a function as an argument to another function, whereas the Symbolic Processor can also handle an expression instead of a function as an argument to a new function. This is in agreement with programs like Maple, Derive and Mathematica. Also, Mathcad's programming language, which was a very valuable addition in version 6.0, works somewhat differently for functions that you are going to use numerically and symbolically.

    1.3.2 Forcing symbolic calculation Anyway, Mathcad can do many calculations both numerically and symbolically. You just have to signal to the program which one of the two possibilities you want to use.

    For elementary operations the only thing you need to do to make a result come out symbolically instead of numerically, is to replace the ordinary equal symbol = by a right arrow symbol:

    2 3 2 3 17 + = 1.417 is replaced by +

    3 4 3 4 12

    To get the symbolic calculation symbol you can do one of two things:

    17

  • Either you click on the icon to get the menu:

    Then click on the icon

    Or - more easily - you press [Ctrl][Period]

    Examples

    3 105 = 4.286 104

    3 105 300000

    7 7 7

    3 + 5 1

    10 1 = 2.288 3 + 5 + 2 2

    2

    3 2 + 2 = 2.414 3 2 + 2 2 + 1

    Notice that you can follow a symbolic calculation with a numerical calculation:

    5 6 + 6 8 5 6 + 6 8 13 = 0.813 = 0.813

    154 + 123 154 + 123 16

    sin pi 1 2 = 0.707 4 2 You can get as many digits in the approximation as you want by using the

    button or pressing [Ctrl] [Shift] [Period] and then writing float,40 in the placeholder:

    2asin 1( ) float , 40 3.141592653589793238462643383279502884197 You can also use the menu choice Symbolic - Evaluate - Floating Point and write the number of decimals into the dialogue box:

    ( ) floating point evaluation yields 3.14159265358979323852asin 1Notice that numerical evaluation will give you at most 15 significant digits. The number of significant digits shown in numerical results is determined by choosing Format - Result - Number Format from the menu.Then set the number of decimal places you want after Number of decimal places. If you want to change the displayed format for just one result, double click on it to get the same dialogue box. The change will now concern just the one result. Exercises. Repeat (some of) the exercises after section 1.1, but with symbolic calculation.

    18

  • Dirac

    1.4 Mathcad's built-in functions Mathcad's built-in functions in general have the syntax you know from mathematics. So you can just type the function's name from the keyboard. Alternatively, you can choose from a list of functions: Choose Insert -Function from the menu. Alternatively, press the button

    on the standard toolbar. You get the following dialogue box:

    In the left half of the window, you scroll down to the category of functions that you need. Then, in the right window you'll see a list of Mathcad's built-in functions of that category. Click on the one you want you want. You'll see an explanation of the function in the area below the lists. Press the button Insert to insert the function into your worksheet. Most of the functions are understood both numerically and symbolically, but there are some exceptions, e.g.

    (1 0) = 0 (1 0) (1 0) Kronecker's delta function, , , 1 ( ) 0 Dirac's delta functionDirac( ) = Dirac 1

    I shall not make any attempt to list Mathcad's built-in functions. The best way to learn about them is to go through the dialogue box above, or to consult Mathcad's documentation "Reference manual".

    19

  • a

    1.5 Variables 1.5.1 Defining a variable Defining variables is a fundamental operation in any programming language, and so it is in Mathcad. Say you want to assign the value 2 to a variable named x and the value 3 to the value y. Then type x:2 and y:3. The colon will appear as :=

    x := 2 y := 3

    After having defined x and y you can use them in calculations:

    x = 2 y = 3 x + y = 5 x + y2 = 11 sin(xy) = -0.279 x

    fi

    2 y fi

    3 x + y fi 5 x + y2 fi 11 sin(xy) fi sin 6( ) If you try to use a variable before it has been defined, Mathcad shows the variable in a different color and shows the error message "This variable or function is not defined above".

    a = a := 34 a = 34

    However you can choose to let Mathcad behave differently at this point. Choose View - Preferences from the menu. Choose the General tab, and check Use context sensitive equal sign. Now, when you try to use the = sign on an undefined variable, Mathcad will find that this variable is not defined, and will assume that your intention is to define the new variable instead of trying to evaluate an already defined variable. There is one more operation for assigning a value to a variable, which Mathcad denotes global assignment. Instead of the colon, you use the tilde ~, which is shown as

    X

    5 Y

    7 X + Y = 12 XY = 7.813 104

    However, a global assignment works a little different than a local assignment with :=. Global assignments are calculated before the local assignments, so they appear as working "both ways". So you get the following:

    A = 6 A 6 A = 6 A := 3 A = 3

    You see that even if you get an error message the first time you try to evaluate an undefined variable, the error message will disappear as soon as you define the variable by means of a global assignment, even if this definition comes after the evaluation. The last two blocks above show that a global definition is overridden by a local definition. You cannot use a variable that has been defined as local when you define a global variable, but you can use a global variable when you define a local variable:

    20

  • 3A

    aB

    2

    a (a is shown as an undefined variable) but: D

    3 B

    2

    D D = 3 E := 2D E = 6

    1.5.2 Variable names

    Mathcad has a set of rules concerning variable names, which you can find in Mathcad's documentation. Here are a few things to notice: A variable name can consist of letters, digits, the underscore, the prime ,

    the percent %, and the infinity symbol , which you can get by typing

    [Ctrl] Z. The prime will combine with some vowels like in ,,. To get a prime alone, write, say, t and delete the t.

    A variable name has to start with or a letter, which can be uppercase or lowercase. The symbol

    can only be used as the first letter of a variable name (which you will probably never do). If you violate this rule you'll get the error message that states that the expression cannot be defined.:

    B := 4 is all right but 3A := 3 gives an error message.

    You can use Greek letters in variable names. Type the Roman equivalent (a list of these can be found in Mathcad's documentation), and press [Ctrl]G. Roman and Greek letters can be mixed: a

    g := 3 ag = 3

    You can make part of a name a subscript by pressing period (.). This is just a typographic effect, and has nothing to do with a vector index, which you get by pressing the left square bracket [, as you'll see later.

    aF := 5 aF = 5 b16 := 13 b16 = 13

    You can let the type style be part of a variable name in the following way. Choose Format - Equation from the menu to get the following dialogue box.

    21

  • aClick on the down arrow to the right of the upper input field, and scroll down to the name MyType (or User 2 ...). Then press the button Modify. You get a dialogue box where you can choose the type style. Say you choose Italic type. Then press OK to get out of the dialogue box. Now, to define a new variable with the name A in italic type style, type A and click the down arrow button on the formatting toolbar:

    Click on MyType. Then continue with the variable definition: A := 67 Notice that: A = 3 A = 67

    This means that the style is part of a variable's name. Two variables with their names spelled identically are different if their styles are different.

    1.5.3 Range variables Often you need a sequence of numbers that increase or decrease by a constant amount. For this Mathcad has the concept range variable. It has the syntax

    First value, Second value; Last value or First value; Last value.

    With the second syntax, it is assumed that Second Value = First Value + 1:

    The semicolon ; will be shown as two periods [..]. For example, if you want to assign the numbers 0,0.5,1..2 to a variable with the name a, you type a:0,0.5;2:

    a := 0 , 0.5.. 1.5

    Notice that it is a common mistake to use two periods .. instead of semicolon. Mathcad will mark that as an error (Can't understand this number) Then you can use the variable a in computations. The results will be given in output tables. As you see, the operation is performed for all the values in the range variable a. However, if you try to define a new variable by means of a range variable, you get an error message "Illegal context":

    ( ) = 2 a =a = a2 = sin a 0

    0.5 1

    1.5

    0 0.25

    1 2.25

    0 0.479 0.841 0.997

    0 1 2 3

    b := 2a

    (Illegal context)

    22

  • The main applications for range variables are as array indices and as variables that runs through a function's interval of definition e.g. when you draw the graph. Range variables cannot be used in symbolic calculations.

    1.5.4 Using dimensions Mathcad has several unit systems built-in: SI, MKS, CGS and US. You can choose which one you want to use by choosing Math - Options from the menu, and then clicking the Unit System tag. Let's say we use the SI system. You can define variables that involve units or dimensions. This is very useful in physics. Here is a (physically trivial) example: Say you want to find the force that is necessary to give a mass of 1 kg an acceleration of 2 m/s2. First you define a variable for the mass. Write m := 1

    (Use MyType format) With the cursor on the placeholder after the multiplication dot, choose Insert - Unit from the menu. Alternatively, press [Ctrl] U. You get the following dialogue box:

    Mathcad has a set of units for most of the physical quantities you can think of. To access the units of mass you scroll down the list to the left till you come to Mass. The two lists now look like this: Click on Kilograms [kg] and then on the Insert button (or double click on Kilograms). The result is:

    m := 1 kg

    Now, define a variable a for the acceleration. Using the same procedure as for the mass, you get:

    m a := 2 To compute the force, you define by Newton's second law:

    2 s

    23

  • F := ma Writing F = you get: F = 2 N

    So, Mathcad applies any definition of derived units that it finds. You can also use your own version of the unit abbreviations if they are different from Mathcad's. Try to write an N in the placeholder after the result above, and you get: F = 2 N You can also choose to format the resulting unit differently. Instead of

    F = 2 kg m s-2 you may want to have F = 2 kg m

    2 s

    In that case you do check Format Units instead of Simplify units when possible in the dialogue box mentioned above

    Exercises.

    1. Use Mathcad to compute x+y when

    a. x=2 and y=3 b. x=3.14 and y=1.24

    2. Use Mathcad to compute the value of x/y when

    a. x=3.14 and y=3.4 b. x=4.5 and y=1.26

    3. Define x := 2 and y := 3 and calculate the value of the following expressions. Use numerical and symbolic evaluation.

    x + y (x + y)10 x - y2 y! xy x - y x - y x

    -

    y ln(x

    -

    y) x -

    y atan2(x , y) atan

    y

    x2

    + y2 x

    , 2 3 ( ) for x = 1,2,...,104. Use a range variable to find the values of x x , x , sin x

    5. Use Mathcad to find the resistance of the following electric circuit:

    (This figure has been drawn entirely by means of Mathcad, using some functions that I have developed somewhere else, and which are beyond the scope of this book. Then it has been copied to this file.)

    24

  • 1.6 Arrays

    1.6.1 Defining an array with [Ctrl]M Arrays in Mathcad are basically of dimension one or two, i.e. they are vectors or matrices. But in addition you can form nested arrays, which are arrays whose elements are themselves arrays. This is very useful in many connections, e.g. in graphics.

    Arrays can be defined either by giving the components one by one in an input array or by defining the components by means of a formula.

    Say you want to write the matrix

    1 4 7

    2 5 8

    3 6 9

    Press [Ctrl]M. Alternatively, press the button on the Math toolbar to get the palette The press the button

    You get a dialogue box:

    Choose the number of rows (3) and columns (3) and click

    on Create. Then you get the following array of

    placeholders:

    Then you can just fill in the values. To proceed from one element to the next, press [TAB]. You'll be lead to fill out the array row by row. A vector in Mathcad is by default a matrix with one column (not a row, as most other programs prefer), and can be defined by the same procedure as above.

    Notice that an array has to have at least two elements when you use [Ctrl] M. You can define a one element array, cf. next subsection.

    25

  • You can assign an array to a variable by typing the variable name, pressing : (or ~ for global assignment) and then do the procedure above.

    1 4 7

    2

    1 4 7

    2

    A := 2 5 8 B := 3 A = 2 5 8 B = 3

    3 6 9

    4

    3 6 9

    4

    1.6.2 Accessing the elements of an array If you want to access one particular elements of an array, you have to use the index operator, which you get by typing the left square bracket [. Array indices in Mathcad start with 0 by default (this can be changed), so to get the first component of the vector B, you type B[0= to get B0 = 2

    Alternatively, press the button

    Notice how this looks almost but not quite like the subscript operator (period), which has purely cosmetic function: B0

    on the matrix palette to get

    For matrices you have to use two indices with comma in between:

    A0 0 = 1 A2 2 = 9, ,

    1.6.3 The nth column vector of a matrix

    To access the nth column vector of a matrix, you have the operator , which you can get by pressing [Ctrl]6 on the alphanumeric keyboard, or by clicking on the icon

    in the matrix palette. For the matrices above:

    1

    4

    7

    2

    0 1 2 0A = 2 A = 5 A = 8 B = 3

    3

    6

    9

    4

    There is no similar operator for the nth row, but you can get it by combining the column operator with the transposition operator [Ctrl]1:

    0 1 AT AT( ) T = ( 1 4 7 ) ( ) T = ( 2 5 8 )

    26

  • 1.6.4 Defining an array by means of a formula The method described above to define an array has a restriction: The total number of elements in the matrix has to be between 2 and 100. For arrays bigger or smaller than that you have to use other methods.

    Say want want to define a vector X with 200 elements whose nth element is n2. Then you do the following:

    First, define an index set for the vector as a range variable: n := 0 .. 199

    Then define the value of the nth component, using the [ operator: :=Xn n If you try to evaluate the vector X by pressing X=, you get a long table which eats a great deal of paper. It's better to evaluate the transpose, which will then be a row vector. You get the transpose by pressing [Ctrl]1 after the variable name (1 must be typed on the alphanumeric keyboard).

    XT = 0 1 2 3 4 5 6 7 8 9 0 0 1 4 9 16 25 36 49 64 81 1

    Mathcad will show just the first few elements, but you can click on the table and scroll to see the rest of the components.

    The same method works for matrices. Define index range variables:

    i := 0 .. 2 j := 0 .. 12 and a formula for the element with indices (i,j):

    Yi , j := i + j

    0 1 2 3 4 5 6 7 8 9 10 11 12

    Y = 1 2 3 4 5 6 7 8 9 10 11 12 13

    2 3 4 5 6 7 8 9 10 11 12 13 14

    Sometimes it's necessary to define an array with just one element. If you try to do this with [Ctrl]M, you'll get an error message "Zero or single element matrix not allowed". However, nothing stops you from defining such a matrix by means of a formula:

    1W0 := 1 W = ( ) W0 0 = 1 , Notice that Mathcad writes a one element matrix in parentheses - this is new in version 8. Also, Mathcad has boolean functions that signals whether a variable has a certain type - array, scalar or string:

    IsArray W ( ) = 0 IsScalar W( ) = 1 IsArray W0 ( ) = 0 IsScalar W0 ( ) = 0 IsString("abc" ) = 1( ) = 1 IsString W

    27

    2

  • 1.6.5 The first index of an array By default, Mathcad starts to count indices at zero. This can be changed, however, by assigning a new value to the variable ORIGIN, which is one of Mathcad's built-in variables. Let's change it to 1:

    1 4 7

    A =

    2 5 8

    A1 1 = 5 ORIGIN := 1 A1 1 = 1 , ,

    3 6 9

    In my experience, it's best to leave the value of ORIGIN at 0. Otherwise you have to involve the value of ORIGIN in most of the programs with arrays that you write.

    1.6.6 The number of rows and columns The functions rows and cols return the number of rows and columns of an array. For a vector, the function length returns the number of components, while the function last returns the last index of the array:

    ORIGIN := 0

    1 4 7

    2

    A = 2 5 8 B = 3 rows A ( ) = 3

    ( ) = 3 cols A ( ) = 3 last B 3 6 9 4 length B ( ) = 2

    ( ) -

    1In general, last(B) = ORIGIN + length B

    1.6.7 Array operations Many of the operators in Mathcad are polymorphic in the sense that they will change according to the data types of its operands. This is the case with the elementary operators +,-,*,/, as you can see from the examples below.

    1 4 7

    2

    5 0 -

    2

    A = 2 5 8 B = 3 C := 4 2 2

    3 6 9

    4

    -

    1 0 0

    Multiplication and addition with array operands becomes matrix multiplication:

    42

    14 8 6

    6 4 5

    A B = 51 A C = 22 10 6 A + C fi 6 7 10

    60

    30 12 6

    2 6 9

    Multiplication with vector operands becomes scalar products: B B = 29

    28

  • Absolute value with a vector argument becomes vector length:

    2

    B = 3 B fi B B 29 fi 29 = 5.385

    4

    Absolute value with a square matrix argument becomes determinant:

    2 4 X :=

    X = -2 A = 0 C = -4

    3 5

    Be aware, though, that this doesn't apply to arrays with one element, which are interpreted as vectors:

    a0 := -1 a = ( -1 ) a = 1

    Multiplication with one scalar and one array operand multiplies all the elements of the array with the same scalar, and similarly for addition:

    1 4 7

    2 8 14

    3 6 9

    A = 2 5 8 2 A = 4 10 16 2 + A fi 4 7 10

    3 6 9

    6 12 18

    5 8 11

    A-1 with A a square matrix gives the matrix inverse:

    0 0 -

    1

    5 0 -

    2

    0 0 -

    1

    1 1 9

    C = 4 2 2 C- 1 = 0.5 0.5 4.5 C- 1 fi

    2 2 2

    -

    1 0 0

    -

    0.5 0 -

    2.5

    -

    1 -

    5

    0

    2 2

    [Ctrl] 1 gives the transpose of a matrix. There is also a button on the Matrix toolbar for that operation.

    1 4 7

    1 2 3

    A = 2 5 8 AT = 4 5 6

    3 6 9

    7 8 9

    The function submatrix is of course used to define a submatrix, and has the syntax submatrix(Matrix, FromRow, ToRow, FromCol, ToCol):

    1 4 7

    submatrix(A, 0 , 1 , 0 , 2) =

    2 5 8

    29

  • 1.6.8 Nested arrays You can use at most two indices when defining arrays in Mathcad. If you need matrices of higher dimensions, you have to use nested arrays. A nested array is an array where the elements are themselves arrays. You can nest arrays in several levels. Here is an example:

    1

    2

    3 4

    5 6

    7

    8

    ORIGIN := 0 G0 0, := G0 1, := G = ( {2,2} {2,2} )

    As you see G= gives you only the dimensions of the component arrays, not the values. This can be changed, however. Type G= again, and double click on the result to get the usual Result Format box. Check Expand nested arrays. You get:

    G =

    1

    2

    3 4

    5 6

    7

    8

    If you need to access an element of one of the arrays that are components, you have to use the space bar or parentheses to get the nested indices:

    1

    2

    3 4

    G0( )0 0, = 1 G0 0, = (G0 1)1 1, , = 8

    1.6.9 Defining bigger arrays

    With the [Ctrl] M method you can create matrices with 100 or less elements. Using a formula, the size of the matrix is only limited by the size of your machine's RAM. Some times you need to define matrices with more than 100 elements for which there is no explicit formula, e.g. to include the results of repeated experiments in your file. If you have the data in a spreadsheet, say Excel, you can import from there, but you can also go via an ASCII file. You can use an ASCII editor to create a text file with the same number of numbers in each row. Or you can write them in a text region in Mathcad and copy them to an ASCII file. Let me demonstrate by an example. Below, I have written some numbers in a Mathcad text region. There is a [Return] character at the end of each row, which will correspond to going from one row to the next in the matrix.

    1.0 2.3 3.4 5.6 6.7 3.2 4.5 5.6 7.6 7.8 2.1 3.4 4.6 7.6 4.5 2.3 4.5 5.6 7.6 6.7 3.4 5.4 6.7 7.8 8.8 4.5 3.6 6.7 4.5 5.6

    Then you copy this to the clipboard by selecting it and the choosing Edit -Copy from the menu (or the equivalent copy button). Then you open Notepad and paste the content of the clipboard back to the Notepad window. You may have to do some editing: Press [Enter] at each of the black boxes and delete the black box. Then you save it with some name, say data.txt. Then you go back to Mathcad, and define a text variable say with the name file:

    30

  • file := "data.txt" (Press " immediately after the definition colon.) Remember to include the full path to the file, if it isn't in the current directory.

    Then you can get the data back as a matrix by means of the function READPRN:

    M := READPRN(file)

    1 2.3 3.4 5.6 6.7 3.2 4.5 5.6 7.6 7.8

    M = 2.1 3.4 4.6 7.6 4.5 2.3 4.5 5.6 7.6 6.7

    3.4 5.4 6.7 7.8 8.8 4.5 3.6 6.7 4.5 5.6

    You can also defined matrices with more than a hundred elements by using the functions stack and augment. The function stack works on two matrices with the same number of columns and puts them underneath each other. The function augment works on two matrices with the same number of rows and puts one after the other:

    2 3 4

    1 1 1 1

    C := ( 8 9 0 )A :=

    B :=

    5 6 7

    2 2 2 2

    2 3 4

    augment(A, B) =

    2 3 4 1 1 1 1

    stack(A, C) =

    5 6 7

    5 6 7 2 2 2 2

    8 9 0

    Exercises

    1. Given the system of linear equations:

    x2 - 3 x3 = -5 a. Define the system's augmented coefficientmatrix M, using the [Ctrl]M method.

    2 x1 + 3 x2 - x3 = 7 b. Use the submatrix function to define the system's coefficient matrix A and the vector of

    4 x1 + 5 x2 - 2 x3 = 10 right-hand sides B. c. Solve the system by computing A-1 B

    2. The function rnd(x) returns a random number between 0 and x. Define a random 100 x 100 matrix by means of this function and compute its inverse.

    3. Use symbolic computation 4. Given the matrix 1 3 -2 to find the inverse of the A := 2 5 -3 following matrices:

    -

    3 2 -

    4

    a b

    1 a 2

    c d

    -

    a 3 -

    1

    Find A-1, and compare with the result of

    ( )))

    3 -

    1 2

    rref(augment(A, identity 3

    31

  • f x

    f 1f 0

    f x

    1.7 Functions

    1.7.1 Defining your own functions You have seen that Mathcad contains a library of common mathematical functions. If this library doesn't cover your needs, you can define your own functions. To define a function, you write the function name, the list of arguments separated by commas and surrounded by parentheses. Then you press the colon : or the tilde ~ to get the assignment operator. Then to the right, you write a valid formula, using Mathcad's built-in functions, or a previously defined function of your own. The arguments of the functions cannot be typed (as in Mathematica) - their data type are determined as soon as the function is applied and Mathcad sees what kind of operators you use. The relation between global and local definition is the same as for assigning values to variables. Mathcad doesn't distinguish between variable names and function names, so you have to take care not to use the same name for a variable and a function. Example: ( ) := x2

    Then you can use the evaluation operators = or fi

    to get function values:

    ( ) = 0 ( ) = 1 f(2.5) = 6.25 f(2.5) fi 6.25 f

    5

    fi

    25

    2

    4 Say you have a vector of arguments for the function:

    i := 0 .. 10 xi := 0.5i

    Then you can get the a vector of corresponding function values in one of two ways: Either you define a vector y by: yi := f xi( )

    T y = ( 0 0.25 1 2.25 4 6.25 9 12.25 16 20.25 25 )

    Notice that this procedure also works symbolically (new in version 2000): T

    y fi

    ( 0 .25 1.00 2.25 4.00 6.25 9.00 12.25 16.00 20.25 25.00 ) Or you use the vectorize operator, which you get either by pressing [Ctrl]- or pressing the icon

    from the Matrix toolbar

    Now, type f(x) and get the vectorize symbol in one of the two ways just described. The result is:

    fi)T( ( ) = ( 0 0.25 1 2.25 4 6.25 9 12.25 16 20.25 25 ) 32

  • f x( ) f xNotice that the vectorize operation does not make sense symbolically:fi

    ( ) fi

    . ("Matrix dimensions are incompatible"). Many of Mathcad's built-in functions can be used on arrays without the vectorize operation:

    1

    0.841

    1

    0

    ( ) = -0.909 t2 = 4 ln tt :=

    -

    2

    sin t

    ( ) =

    0.693 + 3.142i

    3

    0.141

    9

    1.099

    0.785 However:

    fi

    1

    atan t( ) =

    -

    1.107

    t = 3.742 t = 2

    1.249

    3

    This is because the expression |t| has a special meaning if t is a vector The square of a matrix is another matrix: But:

    1 2 3

    30 36 42 fi

    1 4 9

    A := 4 5 6 A2 = 66 81 96 A2 = 16 25 36

    7 8 9

    102 126 150

    49 64 81

    If you want the matrix of squares of elements from A, you need to use the vectorize operator:

    1.7.2 Drawing the graph of a function To draw the graph of a function of one variable over an interval [a,b] you need to do the following:

    Define the function as described above, say f x ( )2 -

    0.5( ) := sin x Define a range variable, say with the name x, that runs through the

    interval [a,b], say: x := 0 , 0.1.. 2p (To get p, type [Ctrl] P.) This step may be omitted. Then Mathcad will choose a default range variable in the interval [-10,10].

    Get the Graph toolbar from the Math menu by clicking the icon Choose Insert - Graph - XY-Plot from the menu or

    The graph menu looks like this:

    and you click the icon

    or

    Press @. This is the shortest way to the diagram.

    33

  • In any case, you get the following input scheme:

    There are six placeholders. The middle placeholders on each of the axes are to hold the formulas for the function and the argument. So, write f(x) in the middle placeholder on the y axis and x in the middle placeholder on the x axis. Then press [Enter]. The result is as follows:

    0 4 6

    1

    1

    f x( ) 2

    x

    There is a lot of things you can do with this plot. To change the format, you simply double click on the graph. This gives you a dialogue box, where you can change many aspects of the diagrams format. I won't go into details about this, you are referred to Mathcad's documentation. On of the options is to change the axes to a more familiar format: Choose XY axes Crossed and you get:

    0 4 6 f x( ) 2

    1

    x

    If you choose to use Mathcad's default range variable, use an undefined variable, say u:

    34

  • uxx

    x

    f( ) 10 5 0 5 10

    u

    The four other placeholders above give you the opportunity to determine the endpoints of the intervals manually. You may omit this - Mathcad will then choose values that fit the actual values rounded up to some "nice" number. However, there may be good reasons to choose the interval endpoints manually, as in the following example:

    1 sin( ) x 0

    p

    , .. 2.p64

    g( )

    0 5 10

    1 .1016

    1 .1016

    g x( ) 0 5g( )

    x x

    Interval endpoints set by Mathcad Endpoints inserted manually.

    Notice how Mathcad simply drops the x values where the function has a singularity.

    You can draw the graphs of several function in the same diagram: Just write the expression in the middle placeholder on the y axis with comma between them:

    0 2 4 6

    2

    x 2

    cos x( )

    sin x 2

    x

    35

  • 1.7.3 Drawing a parameter curve.

    You can even use function expressions on both axes to draw the graph of a parameter curve. Here is a circle (you may have to adjust the size of the diagram to make it look like a circle, not an ellipse):

    You can even use a list ofp

    t := 0 , .. 2p expressions on each axis to plot a 64 series of parametric curves.

    sin t( ) sin x( ) 0 sin 2t( )

    ( ) cos(t) , cos 3tcos x ( ) You can draw a family of functions depending on a parameter that is defined in a range variable. Define:

    A := 1 .. 5 x := 0 , 0.05 .. 6.3

    Asin x( ) 0 5

    x

    1.7.4 Drawing a polar curve The method from the previous subsection can be used to draw curves that are given on polar form as r = f(t). You just define Cartesian parameter curves based on the polar function by x(t) = f(t) cos(t), y(t) = f(t) sin(t). You could define the transition from polar to Cartesian coordinates in the following way:

    ( )

    cos t

    f t ( ) P2C(f , t) :=

    or ( )

    sin t

    f t ( )

    ( ) in complex notation.CP2C(f , t) := f(t)exp it

    36

  • Example: Cardioid. f1(t) := 1 - cos t( )

    0 P2C f1 t,( 1 )

    P2C(f1, t)0 Mathcad also has a tool which is intended to take care of polar curves in particular. Choose Insert - Graph - Polar Plot from the menu, or press [Ctrl] 7, where 7 is on the alphanumeric keyboard. There is also a button on the Graph Toolbar for it. Then fill in the placeholders with the polar function and the parameter t:

    90

    150

    210 330

    1.5

    0.5

    120 60 This method will guarantee that 30 circles are drawn as circles, not

    ( ) 180 0 ellipses, and you may draw grid linesf1 tas circles and radii instead of lines parallel to the x and y axes. There is

    240 300 a set of formatting options similar to 270 those for Cartesian plots.

    t

    Exercises.

    1. Draw the graphs of the following functions:

    1 x

    sin x x

    sin x ( ) ( ) 1 ( ) tan x (x -

    1)

    (x -

    3)

    (x -

    4)

    (x -

    6) x 1 + x2

    2. Draw the following parameter curves:

    sin t ( )

    cos t ( )

    ( )

    1

    t

    t -

    sin t ( )3

    9 cos(t) -

    cos 9t

    t

    t

    1

    1 -

    cos t ( )3

    9 sin(t) -

    sin 9t( )

    sin t ( ) 3. Draw the following polar curves:

    1 r = 1 r = sin 2t cos 2t( ) ( ) r = 1 - cos t( ) r = r = t r = + cos t ( )

    2

    37

  • 1 Ux

    2 Ux

    f x f x

    f x

    f x f x f x f x f x

    f x

    1.7.5 Functions as argument to a new function Mathcad allows functions to be used as arguments in new functions in the same way as you can in C++ or modern versions of Pascal. E.g. if you define:

    b Area1(f , a , b) := ( ) dx ( ) := x2 then Area1(f , 0 , 1) fi 1

    3a

    and Area1(f , 0 , 1) = 0.333

    so this works both numerically and symbolically.

    On the other hand many of Mathcad's own built-in operators work on

    expressions, not functions. The integral operator is one of them:

    1

    1 1

    f dx = doesn't work, but ( ) dx fi is OK.

    0 0 3

    Furthermore the output of a Mathcad function is never a function (with one exception: the function Odesolve), but an expression, so it is easier to define the composition of functions whose arguments are expressions than whose arguments are functions. Example:

    D1(f , x) := d2

    ( ) -

    2 d ( ) + ( ) D2(f , x) := d ( ) - ( ) dx2 dx dx

    ( )( ) := (x + x2)ex Then D1(f , x) expand fi 2exp x is an expression, not a function, so you can't use it as a functional argument to a new function. You have to define it as a new function:

    Df x ( ) Then you can compute( ) := D1(f , x) expand fi 2exp xD2(Df , x) expand fi 0 Now, compare this with the following definitions

    D

    ( ) := d2

    U -

    2 d U + U D ( ) := d U - U dx2 dx dx

    Two of the x's are in red to indicate that the variable has not been defined, but you can ignore this. Symbolic Mathcad will be able to use the functions

    D

    1 and

    D

    2 anyway. Then ( ( )) expand

    fi

    2

    exp x ( ( ))) expand fi

    0 or evenD1 f x ( ) D

    2(D

    1 f x

    D

    1 x + x2)ex

    expand fi

    2

    exp x

    (( ( ) D

    2

    D

    1 x + x2)ex

    expand fi

    0

    So, using expressions is sometimes preferable. I'm going to do so consistently

    with symbolic differential equations. But whenever it is possible, you should

    use functions rather than expressions, because they are understodd both by

    numerical and symbolic Mathcad.

    If you have a function f, you can always get an expression by writing f(x).

    But you can also go the other way:

    38

  • xU 2

    H 0

    x 1+x Ulim

    fi

    fi

    x g x U

    g 2

    If you define U := sin( ) and ( ) := U it won't work:x g t ( ) g 2 ( ) g( ) = (Function not defined.)( ) fi sin x ( ) fi sin x

    but if you compute it symbolically:

    h x ( ) it will be OK: h

    p

    = 1 h t ( )( ) := U fi sin x ( ) fi sin t

    2

    Doing the symbolic evaluation gives the function a meaning in numerical Mathcad. Here is a warning: It would be natural to define:

    DD1(U, x) := d2

    U -

    2 d U + U dx2 dx

    and then try to change to another variable name. However, this doesn't work:

    DD1

    (t + t2) , t

    fi

    t + t2 DD1(x + x2 , x) fi -3x + x2 So you should make a decision about the name of the independent variable and stick to it. If you have defined a function, it's easy to get a function value:

    ( ) := sin x ( ) = 0.909 f 2 ( ) f x + 1) fi sin(x + 1)f x ( ) f 2 ( ) fi sin 2 ( How do you do this with an expression? Say you define U := sin( )x Then, of course, you cannot do U( ) = and U x + 1) fi sin(x)(x + 1)( will be interpreted as a product, because U is not a function. But you can use the substitute keyword:

    U substitute , x = 2 fi sin 2( ) U substitute , x = x + 1 fi sin(x + 1) or you can define a function based on U as above: H x ( )( ) := U fi sin x

    ( ) = 0 H 1 ( ) fi sin 1( ) = 0.841 H 1 ( ) You may also need to use this in a program. Then you can use the limit function: lim U

    fi

    0 lim U fi

    sin 1( ) xfi0 xfi1

    However, you cannot substitute x+1 (or any other expression containing x) for x: lim U

    fi

    ("A value is invalid. It may be complex.") xfix 1+

    But then you can go via an intermediate variable:

    Val(U, t) := lim lim U tttfit xfittt

    Val(U, x + 1) fi sin(x)cos 1 ( )( ) + cos(x)sin 1

    Val(x2 + y2 , x + 1) fi x2 + 2 x + 1 + y2 39

  • f x

    f a

    1.8 Using Mathcad's programming language 1.8.1 An example: The trapezoid rule Using the summation symbol that was described in the previous section, you can express the trapezoid rule by the following function:

    n 1- ( ) Trapez(f , a , b , n) b - a

    ( ) + f b +

    f a + i

    b -

    a

    n

    2

    n

    i = 1

    2For example, with f x ( ) you get:( ) := sin x Trapez(f , 0 , 2p , 100) = 0.6412750596 Trapez(f , 0 , 2p , 10000) = 0.642138102

    Mathcad's built-in numerical

    2p integral operator gives:

    ( ) dx = 0.6421382282 0

    To get 10 digits instead of the default three, double click on the result to get a dialogue box for number format. In Number Format, change Number of decimal places 3 to 10.

    1.8.2 Using Mathcad's programming language Although the program above works fine, you may agree that it has some

    weaknesses. First, the quantity b - a will be evaluated once for every value n

    of i, in addition to the first time outside of the parentheses. It would be better to evaluate this quantity once and for all and put it into a variable say h. Here is how this can be done with Mathcad's programming language:

    First click on the icon

    to get the programming palette:

    Notice that Mathcad's programming language consists of only ten commands! But then, of course, you can use the rest of Mathcad in addition.

    40

  • f a

    f a

    Now, let's write a small program that evaluates b - a once and for all: Define n

    a new function Trapez2(f,a,b,n): Type h

    (b-a)/n (

    is assignment within a program). Press [SPACE BAR] some times until the horizontal editing line covers all of this expression. Then press the Add Line button to get a new placeholder in the line below. Type the expression below.

    b -

    a Trapez2(f , a , b , n) h

    n

    -

    h

    ( ) + f b n 1

    (

    2 ( )

    +

    f a + ih)

    i = 1

    You can see that this gives the same result as above:

    Trapez2(f , 0 , 2p , 10000) = 0.642138102030335 Mathcad has no function for taking the time, so it may be difficult to see the time gained by this new program. So, you may just consider it as an illustration of the principles of Mathcad programming.

    We have used two of the programming commands:

    was used to indicate that we are starting to use the program language in a program with more than one statement. It is also used within a program to add new program lines, for instance in a loop.is the assignment operator that is used within programs. Notice that this can be used only to assign a value to a variable. If you need to call another function, this function must have been defined outside of the current program.

    Next, we illustrate the use of the for command. Notice that you have to click the for button. It doesn't work to type it. Let's rewrite the function Trapez2 in the following way:

    b -

    a Trapez3(f , a , b , n) h

    n

    sum

    ( ) + f b( ) 2

    for i 1 .. n - 1 sum

    sum + f(a + ih) sum

    h

    41

  • f a

    f a

    Notice that the function Trapez3 returns the value that is computed in the last program line.

    This also agrees with the former definitions:

    Trapez3(f , 0 , 2p , 10000) = 0.642138102030335 You could also have used a loop instead of a for loop:

    Trapez4(f , a , b , n)

    h

    b -

    a

    sum

    ( ) + f b( )

    (i

    1)

    n

    2

    while i n< sum

    sum + f(a + ih) i

    i + 1 sum

    h

    Notice that you can write several program statements on one line if you enclose each of them in parentheses and multiply (which is the same as logical and). This also agrees with the previous versions:

    Trapez4(f , 0 , 2p , 10000) = 0.642138102030335 Here we have used a while loop instead of a for loop. Notice that you have to press the [Add Line] button when the cursor is on the placeholder after the line with the while command in order to get an extra program line in the loop. You get a vertical line that indicates where the while loop starts and where it ends. These vertical lines make Mathcad programs very easy to understand - you can easily understand the structure of the programs, and you're almost forced to program correctly.

    The button is used to break out of a loop when some condition

    is fulfilled. You can rewrite the Trapez4 in the following way:

    Trapez5(f , a , b , n)

    h

    b -

    a

    sum

    ( ) + f b( )

    (i

    1)

    n

    2

    while 1 sum

    sum + f(a + ih) i

    i + 1 break if i

    n

    sum

    h

    42

  • g x

    Trapez5(f , 0 , 2p , 10000) = 0.642138102030335 Here the 1 after while means "true for ever", and the loop would go on for ever if it wasn't for the break statement, which has the same effect as the condition i

  • 2 4

    5

    5

    g x( ) 3 6 5

    x

    is used if you want a function to return a value and halt before

    the last program line. The next example also demonstrates how you can use the function error to return an error message under a certain condition:

    divideby(y , x) := return error("The denominator is zero" ) if x = 0 y x

    divideby(2 , 1) = 2

    Finally, let's demonstrate the use of

    This button is used e.g. to handle exceptions where you give an input that is of the wrong data type.

    Square(x) error("Must be a real or complex number" ) x 2:= on error

    Here "a" is a string variable, and thus cannot be squared.

    Exercises.

    1. Program a function which, given a natural number N, returns a vector with N components, all of which are zero. Compare with the value of

    identity N( ) 0

    0 2. You have seen before how Mathcad interprets the sum of an array and a scalar: The scalar is added to each of the components of the array. Use this and the program from exercise 1 to define a function that return a vector of given length with all the components equal to a given number.

    44

  • 3. Program a function Diag(x), which for a given vector x gives a square matrix where the components of x are on the main diagonal.

    4. Program a function PositiveComp(x) which for a given vector x returns the vector whose components are the positive components of x.

    5. Program a function GreaterThan(x), which, given a vector x and a number a, returns a vector whose components are the the components of x which are greater than a.

    6. Program a function Max(x) which, given a vector x, returns the largest component of x. Compare with Mathcad's built-in function max.

    7. Program a function which, given a vector x, returns the largest component of x and the index of the first component of x where this is found.

    8. Program a function which, given a vector x, returns the largest component of x and a vector of indices of the components that have this value.

    9. Program a function which for a given matrix M returns a vector where all the column vectors of M are put underneath each other.

    10. Program a function which for a given array M returns the mean of the components of m. Compare with the built-in function mean.

    11. Program a function which for a given matrix returns the sum of its components.

    12. Program a function Det(x) which returns the determinant of a square matrix x even if x is a 1 x 1 matrix.

    13. Program the elementary row operations on matrix: Multiplying a row by a scalar Adding a multiple of one row to another row Interchanging two rows.

    Try the function on some examples, both numerically and symbolically. Use

    the elementary row operations to solve the following system of equations by

    naiv Gauss elimination:

    2x + 4y + 6z = 18

    4x + 5y + 6z = 24

    3x + y - 2z = 4

    45

  • 171!

    1.9 Numbers 1.9.1 Functions for number theory and combinatorics

    Mathcad has the two functions gcd (greatest common divisor) and lcm (least common multiple). They both take arrays as arguments:

    112 7 b :=

    gcd b( ) = 7 lcm(b) = 336

    42 14

    The function mod(x,y) gives the remainder on dividing the real number x by y. mod(7 , 4) = 3 mod(-3 , 4) = -3 mod(-5 , 4) = -1

    If you need a function that is always positive, you can define: Mod(x , y) := mod(x , y) + y(x < 0) Mod(7 , 4) = 3 Mod(-3 , 4) = 1 Mod(-5 , 3.5) = 2

    The function permut(n,k) gives the number of ordered k-tuples using elements from a set with n elements:

    permut(5 , 3) = 60 permut(50 , 25) = 1.961 1039

    5!= 60

    50!= 1.961 1039 (5

    -

    3)! 25! The function combin(n,k) gives the number of subsets with k elements from a set with n elements:

    5!combin(5 , 3) = 10 = 10

    3!(5 - 3)! n!

    combin(n,k) can give results where the expression fails: (n

    -

    k)!k! 171! 171!

    combin(171 , 2) = 14535 = but: fi 14535 169!2! 169!2!

    The functions in this subsection only work numerically.

    1.9.2 Functions for rounding and truncating The function ceil(x) gives the least integer which is

    x:

    ceil(2.78) = 3 ceil 3( ) = 3 ceil(-2.2) = -2 ceil(-2.8) = -2 The function floor(x) gives the greatest integer which is

    x:

    floor(2.78) = 2 floor(-2.78) = -3

    46

  • The function round(x,n) rounds x to the nearest real number with n decimals: round(2.78 , 1) = 2.8 round(e , 5) = 2.71828 round(-2.78) = -3

    If the second argument is missing, it's assumed to be 0: round(2.78) = 3 If the second argument is negative, the number is rounded to that many zeros:

    round(344557, -3) = 345000 trunc(x) gives the integer part of a real number by removing the fractional part:

    trunc(2.78) = 2 trunc(-2.78) = -2 Some times you may need the integer quotient:

    div(x , y) := trunc

    x

    -

    312 = -124.8 div(-312 , 2.5) = -124

    y

    2.5

    1.9.3 Prime numbers Prime number factorization You can factor a number using the symbolic keyword factor. Examples:

    92 factor fi

    22

    23 490 factor fi

    2

    5

    72

    Alternatively, you write the number you want to factor. From the Symbolic menu, choose Derivation Format and then Horizontally to get the factorization to the right of the number. Examples:

    Number Factorization Number Factorization

    123456 26

    3

    643 19283618171 5448889

    3539

    Then, of course, you can use the command factor do decide whether a given number is a prime or not:

    30 is not a prime: 30 factor fi

    2

    3

    5 2371 is a prime: 2371 factor fi

    2371

    Mersenne numbers Mersenne numbers are numbers of the form 2n-1. Their properties with respect to factorization have been studied extensively. You find:

    Number Factorization Number Factorization

    213 -

    1 8191 241 -

    1 164511353

    13367

    217 -

    1 131071 261 -

    1 2305843009213693951

    218 -

    1 33

    7

    19

    73 289 -

    1 618970019642690137449562111

    47

  • Eratosthenes' sieve To list the prime number less than a given number N, you can use Eratosthenes' sieve. Here is a program for this:

    ( )

    for i 2 .. n 1 + So, here is a list of theErathostenes n primes less that 4000:

    nfor k 2 ..

    i Erat := Erathostenes(4000) Sk i 1

    m

    0 for j 2 .. n

    if Sj = 0 Erat =

    Pm j m

    m + 1

    P

    0 0 1 2 3 4

    2 3 5 7

    11

    The prime number function Given the vector x above that contains all the prime numbers less than 4000, we can define prime number function for values up to 4000 as follows:

    fi

    Prime(n) :=

    Erat

    n Prime(3200) = 452 Prime(3800) = 528 i := 1 .. 40 Mi 0 := 100i Mi 1 := Prime(Mi 0), , ,

    MT = 0 1 2 3 4 5 6 7 8 9

    0 1

    0 100 200 300 400 500 600 700 800 900 1 0 25 46 62 78 95 109 125 139 154

    You can scroll the table in order to find the value you want.

    Fibonacci numbers The sequences of Fibonacci numbers can be programmed as follows:

    Fibonacci n( ) := x0 1 x1 1 Then you get the first 60 for i 2 .. n - 1 Fibonacci number by writing

    ( ) and then xi xi 1 + xi 2

    f := Fibonacci 60 - - f=. I omit it here, to save space.

    x

    48

  • 1.9.4 Numbers in various number bases

    Conversion from decimal to other number systems Mathcad can show numerical results in four different base systems: Decimal, octal, hexadecimal and binary. If you want to see what a decimal number looks like in hex, oct or bin, you just write it on the screen and press =. Then you double click on the result to get the dialog box for numerical format. At the option Display Style - Radix you can choose from a list of radices: Decimal, Hexadecimal, Octal and Binary. Examples:

    234.4 = 0ea.666h 3456 = 6600o 256 = 100h

    10 = 0ah 10.1 = 12.063146314631463o 10 = 1010b

    Conversion from hex, oct or bin to decimal Conversion from hex, oct or bin to decimal is even simpler: Just write an h, o or b after the number and press =. Notice, however, that you have to put a zero in front of a hexadecimal number that otherwise would have started with one of the letters a-h. This is in order to distinguish it from a variable name. Examples: 0eah = 234 101b = 5 123.45o = 83.578

    You can also combine the two methods to go directly between hex, oct or bin:

    0ea.ah = 352.5o 6600o = 0d80h 100h = 400o

    Exercises

    1. A pair of prime number twins is a pair of prime numbers whose difference is 2. Write a program that for a given natural number n lists all prime number twin pairs less than n in a matrix with two rows. Build on the function Eratosthenes in the text above. 2. A Pythagorean triple is a a triple (x,y,z) of integers such that x2+y2=z2. Write a function that, for a given n, lists all Pythagorean triples whose members are all at most n: a. Write a function In(x,X) which, for a given number x and a vector X, returns -1 if x is not a component of X, and otherwise returns the first n such that x=Xn.

    b. Write a function that lists the the Pythagorean triples: For every pair (a,b) such that a

  • 1.10 Sums and products The sum and product symbols are built into Mathcad. By definition,

    n n n

    xi =

    xk = x1 + x2 + .. + xn

    xk = x1x2 .. xn

    i = 1 k = 1 k = 1

    If you use Postscript, the summation sign may have a rectangle in its lower part. To get rid of that you have to use another printer driver.

    1.10.1 The summation symbol In Mathcad, the summation symbol comes in three variations:

    Name: Summation Range sum Vector sum

    Screen image:

    Icon:

    Keyboard shortcut

    Typical use:

    Examples:

    =

    [Ctrl][Shift]4 $

    General Sum with predefined index range variable

    10

    i2 = 385 i := 1 .. 10 i = 1

    i2 = 385 i

    n

    i2 expand 1 factor fi 6

    n

    (n + 1)(2 n + 1) i = 1

    [Ctrl]4

    Sum of the componen-ts of a vector.

    1

    3

    v :=

    7

    5

    3

    v = 19

    50

  • 1.10.2 The product symbol Similarly, the product symbol comes in two variations:

    Name: Product Range product

    Screen

    image:

    Icon:

    =

    Keyboard [Ctrl][Shift]3 # shortcut

    Typical General Product with predefined use index range variable

    10

    Examples:

    i = 3.629 106 i := 1 .. 10 i = 1

    i = 3.629 106

    i

    Exercises

    Compute the following sums and product. You get the infinity symbol by pressing [Ctrl] Z. Simplify the expression when needed.

    n 20

    1

    i

    1 -

    1

    a kn i

    i = 1 k = 2 k3 n = 1 i = 1

    -

    1 + n n

    1

    ( )i 1

    k2

    k3 i

    i = 1 k = 1 k = 1 k = 1 k2

    1 -

    1

    ( )k

    ln

    1

    n!

    k = 1 k3

    k = 1 k2

    n = 1

    n

    n = 1 1000n

    51

  • 1.11 Three dimensional plots 1.11.1 The graph of a function of two variables Say you want to draw the graph of the function:

    (F x , y) := cos(2 x 2 y 2 + ) The simplest way to do this is to compute a nested array of coordinate matrices that can be used to draw a quadrangle mesh by means of the function CreateMesh:

    M := CreateMesh F( ) Then you can plot the surface by choosing Insert - Graph - Surface Plot from the menu. Alternatively, press [Ctrl]2. Then write M is the placeholder:

    M

    With just one argument for CreateMesh Mathcad chooses the mesh in its own way: Both the x interval and the y interval is [-5,5], and the number of subdivision points in each direction is 20. However, you can define your own grid by adding more arguments. One possible syntax is CreateMesh(F,a,A,b,B,Nx,Ny), where [a,A] and [b,B] are the x and y intervals, and Nx and Ny are the numbers of subdivision points in each direction. E.g. M2 := CreateMesh(F, -4 , 4 , -5 , 5 , 20 , 50)

    M2

    52

  • There is also a lot of formatting options for surface plots. You can access them by double clicking on the diagram to get a dialogue box. I shall not go into details. You can refer to Mathcad's documentation or try out the possibilities.

    1.11.2 Contour plots and 3D bar charts. You can also draw a contour plot of a function of two variables. You proceed as before to produce a matrix of function values over a grid of points. But then you choose Insert - Graph - Contour Plot (or press [Ctrl]5) from the menu or press [Ctrl] 5. With the example above you get:

    M2

    You can also draw a 3D bar chart by choosing Graphics - Create 3D Bar Chart from the menu. With the example above:

    M2

    53

  • 1.11.3 Parameter surfaces You can use the Surface Plot tool with another type of function argument than a scalar function of two variables. Say you want to draw a sphere. A sphere is defined by means of three parameter

    ( ( ) y u , v) := sin(u)sin v ( ( )x u , v) := sin(u)cos v ( ( ) z u , v) := cos u Equivalently, you can define a 3 D vector function of two variables:

    sin(s) cos t( )

    (

    ( )

    R s, t) := 2 sin(s)sin t

    cos s( )

    Then define:

    , p , 0 , 2p , 10 , 20) or, alternativelyM1 := CreateMesh(R 0 , M2 := CreateMesh(x , y , z , p , 0 , 2p , 10 , 20) 0 , Then use Insert - Graph - Surface Plot. The result is:

    M1 M2 You can draw several such surfaces in the same diagram. Here is a torus:

    ( ))

    cos u

    (4 + cos v ( )

    T u , v) := (4 + cos v ( ) (

    ( ))

    sin u

    sin v( )

    M3 := CreateMesh(T , 0 , 2p , 0 , 2p , 40, 10) Then to draw the sphere and the torus in the same diagram, define either

    M4 := stack(M1, M3) or M50 := M10 M51 := M30

    54

  • M4 M5

    Notice that the variables M1, M2 and M3 are actually nested arrays with one component, which is itself a nested array with three component, each component giving a matrix of coordinates, which is used to draw the quadrangle mesh.

    You don't have to start with a function and draw its graph in order to use the surface Plot tool. Say you want to draw a surface of revolution generated by rotating a polygon P in the plane about the y axis. Then you can write a program to compute the necessary coordinate matrices:

    ( )RevSurf(P, N) := M last P for m 0 .. M

    for n 0 .. N

    vn 2p

    n N

    xm n Re Pm ( ), ( )cos vnym n Re Pm ( ), ( )sin vnzm n Im Pm, ( )

    x

    y

    z

    For example, here is a "circular hut"

    M60 := RevSurf((0 1 1 + 2i )T , 20) M61 := RevSurf([ 3i 1.5(1 + i) ]T , 40)

    55

  • M6

    Exercises.

    1. Draw the graphs of the following functions of two variables:

    -

    4 3 (z x , y) := 2 (

    (1 + x2 + 2y2 z x , y) := (x2 + y2) - 4 x2 + y2)z x , y) := 1 z x , y) := x y(x2 - y2) z x , y) := 1 + cos(x2 + y2)( ( (

    (x2 + y2 - 1 x2 + y2 z x , y) := xy

    z x , y) := e - ycos x( ( ) z x , y) := x - y - x + y(

    (z x , y) := x2 - x4 - y2 2 2

    Try various formatting options.

    2. Draw the surfaces the following parameter function:

    u

    cos v( )

    R u , v) := usin v(

    ( )

    cos 4u( )

    3. If a function f(x,y) depends only on x2 + y2 , it is symmetric with the z axis as a symmetry axis. Write a program to draw the graph of f over a circular region, using cylindrical coordinates. Then draw the graphs of the following functions over a circular region:

    ( (f x , y) := x2 + y2 g x , y) := cos(4 x 2 y 2 + ) 56

  • 1.11.4 Space curves Space curves can be drawn in a manner similar to surfaces, but using Insert -Graph - 3D Scatter Plot. There is a function CreateSpace similar to CreateMesh that can be used to create three coordinate vectors that Mathcad can use to draw a space curve. Say you want to draw a circular helix, which is defined by the following vector function of one variable:

    cos t( )

    f t ( ) Then define ( ) := sin t

    0.5

    t

    M1 := CreateSpace(f , 0 , 8p , 80) and choose Insert - Graph - 3D Scatter Plot from the menu. Write M1 in the placeholder.

    M1

    Exercise. Draw the space curves defined by the following vector functions:

    sec t

    cos t( )

    t + 1

    ( )

    ( )

    t2 -

    1

    tan t sin t

    ( )

    4

    sin 2t( )

    2t

    3t

    2 ln(t + 1)

    t2

    ln(t2 + 1)

    3t

    3 t

    + 1

    atan t( )

    t2

    1 2 + t

    t2

    2

    57

  • 1.11.5 Drawing several three dimensional objects You can draw 3D objects of different kinds in the same diagram. You can mix, surfaces, space curves and Contour plots. Here are a couple of examples:

    Example:

    A sphere with radius 2: A torus: R2 := 4 r2 := 0.5

    cos(u)

    cos v ( ))

    cos v( )

    (R2 + r2cos u ( )

    (

    ( )

    T u , v) :=

    (R2 + r2cos u ( )

    R u , v) := 2 cos(u)sin v ( ( ))sin v ( )

    r2

    sin u

    sin u ( )

    M0 := CreateMeshR ,

    -p

    ,

    p

    , 0 , 2p , 10 , 20

    2 2

    0

    M1 := CreateMesh(T , 0 , 2p , 0 , 2p , 10 , 40)0 ( )

    A circular helix: 2.5cos tH t( ) := 2.5sin t

    ( )

    0.2

    t -

    2

    M2 := CreateSpace(H, 0 , 8p , 80)0

    M

    For the various formatting options, refer to Mathcad's documentation. Or try them by double clicking on the diagram.

    58

  • You can also use the same surface with different formats to get special effects:

    u

    cos v( )

    F u , v) :=

    u

    sin v( ( )

    M20 := CreateMesh(F, -2 , 2 , -2 , 2 , 20 , 20)0

    u2

    M21 := M20

    M2

    The three dimensional graph tool is very fascinating, but it is beyond the scope of this book to explore its possibilities further.

    Exercises.

    1. Draw two toruses. The second copy goes through the center of the first.

    2. Draw a torus which intersects a sphere.

    3. Draw three identical ellipsoids that have their longest axis parallel to the respective coordinate axes.

    4. Draw Archimedes' diagram: A sphere, a cylinder and a cone, all with the same diameter. Make the surfaces transparent, so that all surfaces are visible.

    5. Draw the graph of a function, say f x , y) := x2 + y2 and its tangent plane( in some point. Write a suitable program that automates this job. 6. Draw a space curve, say a circular helix, and a tangent line in one of its point. Write a suitable function that automates this job.

    59

  • 1.12 Text 1.12.1 Text blocks One of Mathcad's strongest points is its flexibility when it comes to mixing mathematical formulae and text. You are not restricted to having just text or just formulae in one row, as you are in most other mathematics packages. All of this book was written entirely with Mathcad, except for the index and the table of contents. Actually, the book is just a collection of Mathcad files. . You start a text region by pressing " ([Shift] 2) or by choosing Insert - Text region from the menu. Then you just start typing. The text will continue till you hit the right margin. If you need to have a narrower block than this, it is recommendable to adjust the size of the block after you have finished the text. You might be tempted to press [Enter], but don't - that would make it more difficult to change your mind if you want the block to have yet another size. You can force a text region to occupy the whole page width from the left to the right margin: While the cursor is in the block, click the right mouse button, choose Properties and click on the tag Text. Then check Occupy Page Width. It is possible to write a formula in the middle of a text region by choosing Insert - Math Region from the menu. Or you can press [Ctrl] a. After having written the formula, you must click outside the formula to continue writing text. If the formula was the last thing you wrote in the text region, you have to click before the formula and then use the [End] key. It is often more convenient to write more text first and then insert the formula afterwards. You can format text regions or part of them in various ways using the following buttons on the formatting toolbar.

    I shall not go into details about this, you should consult Mathcad's Reference Manual or simply test the possibilities. They work as you would expect them to work, based on experiences you may have from other Windows programs. Notice, however, that you have the opportunity to make subscript and superscript in a text region. Select the region that you want to subscript or superscript, click the right mouse button, and choose Subscript or Superscript.

    Special characters in a text block You may often want to use mathematical symbols that are not represented on the keyboard. You have to use special key sequences followed by Ctrl/G, which translates the sequence to the sign you want: The most common special characters are as follows:

    60

  • Logical symbols "

    "[Ctrl]G $

    $ [Ctrl]G

    [Ctrl]G

    [Ctrl]G

    [Ctrl]G \

    \ [Ctrl]G

    [Ctrl]G

    [Ctrl]G

    [Alt/0222] [Ctrl]G

    [Ctrl]G

    [Alt/225 [Ctrl]G or [Alt/0223 [Ctrl]G

    Set Theory

    [Ctrl]G

    [Alt]128 [Ctrl]G or [Alt]0199 [Ctrl]G

    [Ctrl]G

    [Ctrl]G

    [Ctrl]G

    ' [Ctrl]G

    [Ctrl]G

    [Ctrl]G

    [Ctrl]G

    [Ctrl]G

    [Ctrl]G

    Comparison

    [Ctrl]G

    [Alt]0179 [Ctrl]G @

    @ [Ctrl]G

    [Alt]167 [Ctrl]G

    [Alt]0185 [Ctrl]G

    [Alt]0187 [Ctrl]G

    Open intervals

    [Ctrl]G

    [Ctrl]G

    Binary operations

    [Alt]0215 [Ctlr]G

    [Ctrl]G

    [Ctrl]G

    [Alt]0215 Write t and erase the t.

    [Alt]0184 [Ctrl]G

    [Alt]250 [Ctrl]G

    Arrows

    [Alt]0171 [Ctrl]G

    [Alt]0173 [Ctrl]G fi

    [Alt]0174 [Ctrl]G

    [Alt]168 [Ctrl]G

    [Alt]0189 [Ctrl]G

    You can also copy symbols from a Quick Sheet: Choose Help - Resource Center - Quick Sheets and Reference Tables - Extra Math Symbols from th menu. Use the right mouse button to copy a symbol from the Quick Sheet.

    61

  • 1.12.2 Text variables

    Mathcad has a built-in data type for text strings. You can define a variable to have a text string constant as its value. You just start with a quotation sign " right after the assignment operator :=.

    text1 := "This " text2 := "is a " text3 := "text."

    Mathcad has the following functions for manipulating strings:

    concat(S1,S2) joins S1 and S2 to a new string concat(text1, text2) = "This is a "

    strlen(S) gives the number of characters in the string S: strlen(text1) = 5

    search(S1,subS,m) gives the start index for the substring subS in the string S from character number m on: search(text1, "hi" , 0) = 1

    substr(S,m,n) gives the substring of length n starting with character number m in the string S: substr(text1, 1 , 2) = "hi"

    str2num(S) converts the string S to a number. S may contain digits, decimal point, the imaginary unit and numbers of the form 2e4. Spaces are ignored.

    str2num("1+3i" ) = 1 + 3i str2num("1e34" ) = 1 1034

    num2str(t) converts a number to a string. ( ) = "1e+307"num2str(11034) = "1e+034" num2str

    str2vec(S) converts a string to a vector of the corresponding ascii codes. str2vec(" abc" )T = ( 32 97 98 99 230 248 229 )

    vec2str(v) converts a vector of ascii codes to the corresponding string.

    vec2str(( 32 97 98 230 248 )T ) = " ab" Mathcad's documentation mentions assignment of Mathcad variables to filenames as the main application of text variables. You can also use them to write text on a two dimensional graph, as I will discuss briefly in the following subsection.

    62

  • 1.12.3 Text on a two dimensional graph Mathcad has not built-in the possibility to write text on a two dimensional graph. You have to write your own functions to do that. I have done so. The necessary functions are hidden in the areas below

    Font information

    Functions for text on graphs

    These functions are in the file TextFunctions.mcd, and can be copied from there. The main function is the following:

    Write(text, loc, it, sp, wi, he, ha, va, an) Here, text is the text to be written. This is either a single text or an array of texts loc is the location of the anchor point of the text expressed as a complex

    number. This is either a single complex scalar or an array of complex scalars of the same dimension as text.

    it is a factor giving the italicization of the text. sp is a number giving the space between the characters wi is a factor that determines the width of the characters he determines the height of the characters ha is the horizontal alignment of the text relative to the anchor point: 0,1

    and 2 mean left, center and right respectively va is the vertical alignment of the text relative to the anchor point: 0,1 and

    2 mean bottom, center and top respectively. an is the angle between the base line and the x axis.

    Example. Define two write functions write1(text , loc) := Write(text , loc , 0.1, 0.2, 0.3, 0.3, 0 , 0 , 0) write2(text , loc) := Write(text , loc , 0 , 0.2, 0.15 , 0.2, 0 , 0 , 20deg)

    If you need to write subscripts, write them between a pair of underscores. If you need to write superscripts, write them between a pair of ^'s:

    and draw the text: TT := write1("This is a text." , 0) TS := write2("Here is an equation: x_1_^2^+a x_1_+b = 0" , 0.5i)

    0

    1

    2

    Im TT(

    Im TS(

    )

    )

    0 1 2 Re(TT) , Re(TS)

    63

  • Notice that for this to work, you have to use the trace type Draw for the text, and you have to set the right endpoint of the x-axis interval manually (and possible more endpoints, depending on the parameter an), because the functions uses distant points that have to be cut.

    The function Write has been written so that it accepts arrays as the arguments text and loc. This useful when you need to draw a number of texts at various locations.

    Here is an example, where I draw a quadrangle and name vertices:

    :=

    0 2

    2 i+

    i

    0

    :=

    -

    0.3 -

    0.3i 2.2 + 1.1i

    2.2 -

    0.3i

    -

    0.3 + 1.1i

    "A"

    "C" "B"

    "D"

    T := loc

    Q Te := write1(T , loc)

    2

    0

    1Im Q( )

    Im Te( ) 2 4

    1

    ( ) , Re TeRe Q ( ) In version 2000 you can also write the texts outside of the graph and drag them into the graph afterwards. This works fine for a modest number of text whose locations is judged by the eye. If you need to compute the locations of the texts mathematically, the method above is probably preferable. Here is the same quadrangle with the text inserted by this method:

    2

    0

    CD

    A B 2

    Im Q( )

    4

    Re Q( )

    64

  • 1.13 Animation

    Animations are very useful while teaching and learning mathematical concepts. Once you have seen an animation of a cycloid, you never forget how it is defined. In Mathcad, the key to animation lies in the variable FRAME. This is a predefined variable which works like a range variable with integer values. But you can use it as a variable last value for another range variable. Say you want to draw the trajectory of a projectile which is thrown with the initial speed v0 at elevation angle

    a

    .:

    v0 := 100 a := 45deg g := 10

    The the trajectory can be described by a set of parameter functions 1 ( ) := v0cos a ( ) := v0sin ax t ( )t y t ( )t - 2

    g

    t2

    Then you can draw the trajectory and the projectile at a given time T as follows: T := 10 t := 0 , 0.1.. T

    200 y t( ) y T( )

    0 200 400 600 800 1000

    ( ) , x Tx t ( ) Now, define T := FRAME t := 0 , 0.1.. T and write

    y(t),y(T)@x(t),x(T) as before

    200 y t( ) y T( )

    0 200 400 600 800 1000

    ( ) , x Tx t ( ) Notice that you have to fix the interval endpoints manually, since you don't want the axis intervals to change. Now, click on the diagram and choose View - Animate from the menu. You get the following dialogue box:

    65

  • x T

    Say you change the To: field to 15 to get pictures of the first 15 seconds. You have to select the part of the diagram that you want to include in the animation (maybe you have to move dialogue box to do this). Then choose Animate.

    button. This will give an animation of the projectile's trajectory. Mathcad will then create an avi-file, which can be run using the

    When Mathcad has produced its avi-file, you can store it for later use by pressing the Save As button. You can create a hyperlink to this file in the following way: Click on the graph, and choose Insert Hyperlink. Then write the name of the avi-file. After this step,