Byrge Birkeland
Calculus and Algebra with Mathcad 2000
Studentlitteratur
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 1.2.2 1.2.3 1.2.4 1.2.5
1.3
Saving your work 15 Starting a new file 15 Opening an existing file 16 Closing a file 16 Printing a file 16
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 1.5.2 1.5.3 1.5.4
1.6
Arrays 25 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9
1.7
Defining a variable 20 Variable names 21 Range variables 22 Using dimensions 23 Defining an array with [Ctrl]M 25 Accessing the elements of an array 26 The nth column vector of a matrix 26 Defining an array by means of a formula 27 The first index of an array 28 The number of rows and columns 28 Array operations 28 Nested arrays 30 Defining bigger arrays 30
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 1.9.2 1.9.3 1.9.4
Functions for number theory and combinatorics 46 Functions for rounding and truncations 46 Prime numbers 47 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 1.11.2 1.11.3 1.11.4 1.11.5
The graph of a function of two variables 52 Contour plots and 3D bar charts 53 Parameter surfaces 54 Space curves 57 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 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6
Evaluating an expression 68 Simplifying an expression 69 Expanding an expression 70 Factoring an expression 71 Collecting on a subexpression 72 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 Solving an equation with one unknown numerically 77 Solving an equation with one unknown symbolically 78 Polynomial equations solved numerically 79 Several equations with several unknown solved numerically 79 2.3.5 Linear equations 80 2.3.6 Several simultaneous equations solved symbolically 81 2.3.1 2.3.2 2.3.3 2.3.4
3 Calculus 84 3.1
Limits 83
3.2
Differentiation 83 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15
3.3
The derivative as a limit 83 Mathcad's built-in differentiation operator 84 Higher order derivatives 85 Differential formulae 85 Differentiation of implicit functions 86 Tangent and normal 87 Curvature and evolute 89 Envelope of a family of curves 93 Extrema of functions 94 Partial derivatives 96 Series 96 Extrema of functions of two variables 97 Extrema of functions with three or more variables 99 Extrema with side conditions 101 Linear optimization 103
Integration 104 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7
Indefinite integrals 104 Definite integrals 106 Improper integrals 108 Plane areas 109 Curve length 112 Centroids and moments of inertia 114 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 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8
Vectors in Rn 122 Vector valued functions 123 Differential geometry of space curves 125 Vector fields and scalar fields 127 Line integrals 128 Surface integrals 130 Surface areas 132 Triple integrals 132
4 Differential equations 135 4.1
Differential equations of order one 135 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6
4.2
Indefinite integrals y'=f(x) 135 Separable equations f(y) dy = g(x) dx 135 Linear equations y' + f(x) y = g(x) 136 Linear equations y' - a y = g(x), where a is a constant 136 Bernoulli's equation 137 Total differentials 137
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 4.5.2 4.5.3 4.5.4 4.5.5
Fourier coefficients and Fourier series 149 Step functions 150 Fourier transforms 151 Fourier transforms and the heat equation 152 Discrete Fourier transforms 153
6
4.6
Numerical solution of differential equations 155 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8
Drawing a two dimensional vector field 155 Drawing a family of integral curves 158 Runge-Kutta's fourth order method rkfixed 159 Drawing a family of numerically computed integral curves 161 ODEs of order two or higher 163 Phase diagram for a damped pendulum 165 Other differential equation solvers 165 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 + 2⋅3 = 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 1+2 will extend this subexpression. For instance, say you want to compute 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:
To get:
Type:
( 3⋅ 10 ) 5
(3*10^5)/7
2
3*10^5_ 7
3⋅
10 7
5
3*10^5_ _ 7
3 ⋅ 10
= 512
2
2^3_^2=
( 23) 2 = 64
2+3^3=
2 + 3 = 29
2+3_^3=
(2 + 3) = 125
7 5
3
2^3^2=
3
3
7 \3_+\5= \3+\5=
3 + 5 = 3.968
1/1/3+1/5=
3+
5⋅ 6 + 6⋅ 8 15⋅4 + 12⋅3 1
1/1/3_+1/5= 1 3 2+3_*2[Ctrl]/[Enter]3+4_*5=
2 [Ctrl]/ 3 or 2 [Ctrl] * 3 =
= 3.2
1
3 + 5 = 2.288
5*6+6*8_ _ _/15*4+12*3=
1
+
1
1 5
= 0.813
= 1.875
5
(2 + 3)⋅2 ... = 45 + (3 + 4)⋅5 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
−
3
4
=5
1 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 8=2 2 sin = 0.5 2 4 4 Alternatively, you can use symbolic evaluation, as described in section 1.3: atan( 1 ) →
1 4
⋅π
π → 1⋅ 2 4 2
sin
8 → 2⋅ 2
Another application is using dimensions, which we shall discuss briefly in section 1.5.4. Exercises. Make Mathcad do the following computations: 1.2⋅10
19
3
20
− 2.5⋅10 ⋅
18
100
= 4.5 × 10
100 1 + 1 = 2.705 100
1 2
2
1 2! 2 2 1+
+
1 3
+
2
1 3!
+
1 4
+
2
1 4!
1
+
5 +
2
1 5!
2
2
+
1 4
2
2
2
2 + 3 + 4 + 5 = 54 12 + 13.50 − 10 + 1
= 0.464
2⋅(3 + 1.5)
= 0.717
15 −
8 104.5
= 14.923
6 1 + 4.5 = 1.302 100
= 0.707 1
2 + 5 = 3.65
+
1 8
+
1 16
+
1 32
+
1 64
= 127
14
1 64
= 1.833
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
+
3 4
= 1.417 is replaced by
2 3
+
3 4
→
17 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 5
3 ⋅ 10 7
4
= 4.286 × 10
3 ⋅ 10 7
5
→
300000 7 1
3 + 5 = 2.288
3+ 5→
3 + 2 ⋅ 2 = 2.414
3 + 2⋅ 2 → 2 + 1
2
⋅ 10 +
1 2
⋅ 2
Notice that you can follow a symbolic calculation with a numerical calculation: 5⋅ 6 + 6⋅ 8 15⋅4 + 12⋅3
= 0.813
5⋅ 6 + 6⋅ 8 15⋅4 + 12⋅3
→
13 16
= 0.813
π → 1 ⋅ 2 = 0.707 4 2
sin
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: 2⋅asin( 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: 2⋅asin( 1 )
floating point evaluation yields
3.1415926535897932385
Notice 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
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)
Dirac 1 ) = Dirac(
Dirac( 1 ) → 0
Kronecker's delta function Dirac's delta function
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
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 x→2
y=3 y→3
x+y=5
x + y = 11
2
sin(x⋅y) = −0.279
x+y→5
2
sin(x⋅y) → sin( 6 )
x + y → 11
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
Y
X + Y = 12
4
X = 7.813 × 10
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
B ≡ 2⋅aa (a is shown as an undefined variable) but: D≡ 3
B ≡ 2⋅D
D= 3
E := 2⋅D
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: αg := 3 αg = 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. α F := 5
•
αF = 5
β 16 := 13
β 16 = 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
Click 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: Notice that: A := 67 A = 67 A= 3 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
First value; Last value.
or
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": a =
0 0.5 1 1.5
2
a =
0 0.25 1 2.25
sin( a) =
2⋅a =
0 0.479 0.841 0.997
0 1 2 3 22
b := 2⋅aa (Illegal context)
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 To compute the force, you define by Newton's second law: a := 2⋅ 2 s 23
F := m⋅a
Writing F =
you get:
F = 2N
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 kg m -2 you may want to have F = 2 F = 2 kg m s 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 2
x +y
10
x−y
ln(x − y)
x−y
(x + y)
x+y 2
2
y!
x
y
atan2(x , y)
x−y atan
x−y
y
x
4. Use a range variable to find the values of x , x 2 , x 3 , sin( x ) for x = 1,2,...,10 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.
1 4 7 2 5 8 3 6 9
Say you want to write the matrix
Press [Ctrl]M. Alternatively, press the button get the palette
on the Math toolbar to 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 A := 2 5 8 3 6 9
2 B := 3 4
1 4 7 A = 2 5 8 3 6 9
2 B = 3 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
on the matrix palette to get
Notice how this looks almost but not quite like the subscript operator (period), which has purely cosmetic function: B0 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:
A
〈0〉
1 = 2 3
A
〈1〉
4 = 5 6
A
〈2〉
7 = 8 9
B
〈0〉
2 = 3 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:
( AT )
〈0〉
T
= (1 4 7 )
( AT )
〈1〉
26
T
= (2 5 8 )
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 n 2. 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
2
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). T
X =
0
0 1 2 3 4 5 6 7 8 9 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 Yi , j := i + j
j := 0 .. 12
and a formula for the element with indices (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: W0 := 1
W = (1 )
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) = 1
IsArray( W0) = 0
IsScalar( W) = 0
IsScalar( W0) = 1
IsString( W) = 0
IsString("abc" ) = 1
27
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 3 6 9
A1 , 1 = 5
ORIGIN := 1
A1 , 1 = 1
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 A = 2 5 8 3 6 9 In general,
2 B = 3 rows( A) = 3 4 length( B) = 3
cols( A) = 3 last( B) = 2
last(B) = ORIGIN + length( B) − 1
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 A = 2 5 8 3 6 9
2 B = 3 4
5 0 −2 C := 4 2 2 −1 0 0
Multiplication and addition with array operands becomes matrix multiplication:
42 A⋅ B = 51 60
14 8 6 A⋅ C = 22 10 6 30 12 6
6 4 5 A + C → 6 7 10 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⋅ B → 29 = 5.385
B → 29
4
Absolute value with a square matrix argument becomes determinant: X :=
2 4 3 5
X = −2
A =0
C = −4
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 A = 2 5 8 3 6 9
2 8 14 2 ⋅ A = 4 10 16 6 12 18
3 6 9 2 + A → 4 7 10 5 8 11
A-1 with A a square matrix gives the matrix inverse:
C=4 2 2 −1 0 0 5
0 −2
C
−1
−1
= 0.5 0.5 4.5 −0.5 0 −2.5 0
0
0 1 −1 C → 2 −1 2
1 9 2 2 −5 0 2 0
−1
[Ctrl] 1 gives the transpose of a matrix. There is also a button on the Matrix toolbar for that operation.
1 4 7 A = 2 5 8 3 6 9
1 2 3 A = 4 5 6 7 8 9 T
The function submatrix is of course used to define a submatrix, and has the syntax submatrix(Matrix, FromRow, ToRow, FromCol, ToCol): submatrix(A, 0 , 1 , 0 , 2) =
1 4 7 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 3 G := 5 7 G = ( {2,2} {2,2} ) ORIGIN := 0 G0 , 0 := 0, 1 6 8 2 4 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: 1 3 5 7 G = 2 4 6 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:
( G0) 0 , 0 = 1
G0 , 0 =
1 3 2 4
( 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: A :=
2 3 4 B := 1 1 1 1 C := ( 8 9 0 ) 2 2 2 2 5 6 7
augment(A, B) =
2 3 4 1 1 1 1
5 6 7 2 2 2 2
2 3 4 stack(A, C) = 5 6 7 8 9 0
Exercises 1. Given the system of linear equations: x 2 − 3 ⋅ x 3 = −5
a. Define the system's augmented coefficient matrix M, using the [Ctrl]M method. 2 x 1 + 3 x 2 − x 3 = 7 b. Use the submatrix function to define the system's coefficient matrix A and the vector of 4 x 1 + 5 x 2 − 2 x 3 = 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 to find the inverse of the following matrices:
a b c d
1 a 2 −a 3 −1 3 −1 2
4. Given the matrix
1 3 −2 A := 2 5 −3 −3 2 −4
Find A-1, and compare with the result of
rref(augment(A, identity( 3 ))) 31
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:
f ( x ) := x
2
Then you can use the evaluation operators = or → to get function values: f ( 0) = 0
f ( 1) = 1
f(2.5) = 6.25
f(2.5) → 6.25
f
5
25
→ 4 2
Say you have a vector of arguments for the function: i := 0 .. 10
x i := 0.5⋅i
Then you can get the a vector of corresponding function values in one of two ways: Either you define a vector y by: y i := f ( x i) 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 → ( 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:
( f(→x))
T
= ( 0 0.25 1 2.25 4 6.25 9 12.25 16 20.25 25 ) 32
Notice that the vectorize operation does not make sense symbolically: → f ( x ) → . ("Matrix dimensions are incompatible"). Many of Mathcad's built-in functions can be used on arrays without the vectorize operation:
1 t := −2 sin( t ) = 3 0.785 atan( t) = −1.107 1.249
0.841 −0.909 0.141
1 t = 4 ln( t) = 9
However:
1 → t = 2
2
t = 3.742
0 0.693 + 3.142i 1.099
3
This is because the expression |t| has a special meaning if t is a vector The square of a matrix is another matrix: 1 2 3 30 36 A := 4 5 6 7 8 9
42 A = 66 81 96 102 126 150 2
But: → 1 4 9 2 A = 16 25 36
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 ) := sin( x ) 2 − 0.5
•
Define a range variable, say with the name x, that runs through the interval [a,b], say: x := 0 , 0.1.. 2⋅π (To get π, type [Ctrl] P.)
This step may be omitted. Then Mathcad will choose a default range variable in the interval [-10,10]. • Choose Insert - Graph - XY-Plot from the menu or • Get the Graph toolbar from the Math menu by clicking the icon 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: 1
f ( x)
2
0
4
6
1 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
f ( x)
4
2
6
1 x
If you choose to use Mathcad's default range variable, use an undefined variable, say u:
34
f( u )
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 )
g( x )
0,
x
π 64
.. 2 .π
16 1 .10
g( x )
0
5
g( x )
10
0
5
16 1 .10 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: 2
x
2
cos( x ) sin x
2
0
2
4
x
35
6
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 of π expressions on each axis to plot a t := 0 , .. 2⋅π 64 series of parametric curves.
sin ( t) sin ( x)
0
sin ( 2⋅t)
cos(t) , cos( 3⋅t)
cos( x)
You can draw a family of functions depending on a parameter that is defined in a range variable. Define: A := 1 .. 5
A⋅sin ( x)
x := 0 , 0.05.. 6.3
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: P2C(f , t) :=
f ( t)⋅cos( t) f ( t)⋅sin( t)
or
CP2C(f , t) := f(t)⋅exp( i⋅t) in complex notation. 36
Example: Cardioid. f1(t) := 1 − cos( t )
P2C( f1 , t) 1
0
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: 120
60
1.5
150 f1( t)
90
30
0.5 180
0
210
330 240
270 t
300
This method will guarantee that circles are drawn as circles, not ellipses, and you may draw grid lines as circles and radii instead of lines parallel to the x and y axes. There is a set of formatting options similar to those for Cartesian plots.
Exercises. 1. Draw the graphs of the following functions: x⋅sin( x )
1 (x − 1)⋅(x − 3)⋅(x − 4)⋅(x − 6)
tan( x )
x
sin( x )
1
x
1+x
2. Draw the following parameter curves:
sin( t) 1 t t
t t − sin( t) 1 1 − cos( t)
cos( t) 3 9 cos(t) − cos( 9t) sin( t) 3 9 sin(t) − sin( 9t)
3. Draw the following polar curves: r= 1
r = sin( 2t )
r = cos( 2t )
r= t r=
37
1 2
+ cos( t) r = 1 − cos( t )
2
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) := f ( x ) dx ⌡a
2 f ( x ) := x then
Area1(f , 0 , 1) →
1 3
Area1(f , 0 , 1) = 0.333
and
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
⌠ f dx = ⌡0
doesn't work, but
⌠ 1
f ( x ) dx → ⌡0 3
is OK.
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: d2
D1(f , x) :=
dx
(
2
f(x) − 2
)
2
f ( x ) := x + x ⋅e
x
d f ( x) + f ( x) dx
Then
D2(f , x) :=
d f( x) − f( x) dx
D1(f , x) expand → 2⋅exp( 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 ) := D1(f , x) expand → 2⋅exp( x ) Then you can compute D2(Df , x) expand → 0 d2
Now, compare this with the following definitions
d d U+U ∆2 ( U) := U − U x x dx 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 ∆1 and ∆2 anyway. Then ∆1 ( f ( x )) expand → 2⋅exp( x ) ∆2 ( ∆1 ( f ( x )) ) expand → 0 or even ∆1 ( U) :=
(
2
)
U−2
∆1 x + x ⋅e 2
2 x expand → 2⋅exp( x) ∆2 ∆1( x + x ) ⋅e expand → 0
x
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
If you define
U := sin( x )
g ( t ) → sin( x )
g ( 2 ) → sin( x )
g ( x ) := U it won't work:
and
g 2) = g(
(Function not defined.)
but if you compute it symbolically: h ( x ) := U → sin( x )
it will be OK:
π = 1 2
h
h ( t ) → sin( t )
Doing the symbolic evaluation gives the function a meaning in numerical Mathcad. Here is a warning: It would be natural to define: d2
d U+U dx dx and then try to change to another variable name. However, this doesn't work: DD1(U, x) :=
(
2
U−2
)
DD1 t + t , t → t + t 2
(
2
2
)
DD1 x + x , x → −3⋅x + x
2
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: f ( x ) := sin( x ) f ( 2 ) = 0.909
f ( 2 ) → sin( 2 )
f ( x + 1) → sin(x + 1)
How do you do this with an expression? Say you define U := sin( x ) and U( x + 1) → sin(x)(x + 1) Then, of course, you cannot do U( U 2) = will be interpreted as a product, because U is not a function. But you can use the substitute keyword: U substitute, x = 2 → sin( 2 )
U substitute, x = x + 1 → sin(x + 1)
or you can define a function based on U as above: H( x ) := U → sin( x ) H( 0 ) = 0
H( 1 ) = 0.841
H( 1 ) → sin( 1 )
You may also need to use this in a program. Then you can use the limit function: lim U → 0 lim U → sin( 1 ) x→0
x→1
However, you cannot substitute x+1 (or any other expression containing x) for x: lim U → ("A value is invalid. It may be complex.") xx→x →x+ 1
But then you can go via an intermediate variable: Val(U, t) :=
lim ttt→t
lim U x→ttt
Val(U, x + 1) → sin(x)⋅cos( 1 ) + cos(x)⋅sin( 1 )
(
2
2
)
2
Val x + y , x + 1 → x + 2 ⋅ x + 1 + y 39
2
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: Trapez(f , a , b , n) ≡
f ( a ) + f ( b ) n− 1 ⋅ + n 2 i=1
∑
b−a
( )
For example, with f ( x ) := sin x 2
f a + i⋅
b − a
n
you get:
Trapez( f , 0 , 2⋅π , 100 ) = 0.6412750596 Trapez( f , 0 , 2⋅π , 10000 ) = 0.642138102 Mathcad's built-in numerical integral operator gives:
2⋅π
⌠ ⌡0
f ( x ) dx = 0.6421382282
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 b−a weaknesses. First, the quantity 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
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
f ( a ) + f ( b ) n− 1 f ( a + i⋅h) h⋅ + 2 i=1
∑
You can see that this gives the same result as above: Trapez2( f , 0 , 2⋅π , 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: Trapez3(f , a , b , n) ≡
h←
b−a
sum ←
n f ( a) + f ( b ) 2
for i ∈ 1 .. n − 1 sum ← sum + f(a + i⋅h) sum⋅h 41
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 , 2⋅π , 10000 ) = 0.642138102030335 loop instead of a for loop:
You could also have used a Trapez4(f , a , b , n) ≡
h ← b − a ⋅ sum ← f ( a) + f ( b ) ⋅(i ← 1) n 2 while i < n sum ← sum + f(a + i⋅h) 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 , 2⋅π , 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 ( a) + f ( b ) ⋅(i ← 1) n 2 while 1 sum ← sum + f(a + i⋅h) i←i+1 break if i ≥ n sum⋅h 42
Trapez5( f , 0 , 2⋅π , 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
statement, let's write a program that extracts
only the even number out of the first n positive integers: Even( n ) :=
j←0 for i ∈ 0 .. n continue if mod(i , 2) = 1 xj ← i j←j+1 x
0 2 Even( 8 ) = 4 6 8
This example also illustrates the use of the if statement. Here is another example that also involves the [otherwise] button. We define a function by different formulae in different intervals: g( x) ≡
2
x − 3 ⋅ x − 4 if x < 3 x − 4 if 3 ≤ x < 4 (x − 4)
2
if 4 ≤ x < 5
(6 − x)
2
otherwise
Two comments: •
or
Before you press the
button, press the space bar in order include everything to the left of it. This is necessary when you have a statement rather than a value to the left of if. Alternatively, press the [if] or [otherwise] button first. •
The strict inequality signs < and > are on the keyboard. The others are on the Evaluation and Boolean toolbar. Press to get the Evaluation toolbar: Let's draw the graph of the function g: x := 2 , 2.05.. 6 43
5
g( x)
2
3
4
5
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" ) on error x
2
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
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: b :=
112 7 42 14
gcd( b ) = 7
lcm(b) = 336
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: 39
permut(5 , 3) = 60 5! (5 − 3)!
permut(50, 25) = 1.961 × 10 50!
= 60
25!
39
= 1.961 × 10
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! combin(171 , 2) = 14535
171! 169!⋅2!
=
171!
but:
169!⋅2!
→ 14535
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
y
−312 2.5
= −124.8
div(−312 , 2.5) = −124
1.9.3 Prime numbers Prime number factorization You can factor a number using the symbolic keyword factor. Examples: 2
92 factor → 2 ⋅23
490 factor → 2⋅5⋅7
2
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
123456
2 ⋅3⋅643
6
Number 19283618171
Factorization 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 → 2⋅3⋅5 2371 is a prime: 2371 factor → 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 2
13
2
17
−1
131071
2
18
−1
3 ⋅7⋅19⋅73
−1
8191 3
2
41
−1
164511353⋅13367
2
61
−1
2305843009213693951
2
89
−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: Erathostenes ( n ) ≡
for i ∈ 2 .. n + 1 for k ∈ 2 ..
n
So, here is a list of the primes less that 4000: Erat := Erathostenes(4000)
i
Sk⋅ i ← 1
0
m←0 for j ∈ 2 .. n Erat =
if S j = 0 Pm ← j m←m+1
0 1 2 3 4
2 3 5 7 11
P
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: → Prime(n) := Erat ≤ n Prime(3200) = 452 Prime(3800) = 528
∑
T
M = 0
1
i := 1 .. 40
M i , 0 := 100⋅i
M i , 1 := Prime( M i , 0)
0 1 2 3 4 5 6 7 8 9 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 for i ∈ 2 .. n − 1 x i ← x i− 1 + x i− 2 x 48
Then you get the first 60 Fibonacci number by writing f := Fibonacci( 60) and then f=. I omit it here, to save space.
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 10 = 0ah
3456 = 6600o
256 = 100h
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
∑ i
n
xi =
=1
∑
n
xk = x1 + x2 +
..
∏x
+ xn
=1
k
k
k
= x 1⋅x 2⋅ .. ⋅x n
=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
∑
Screen image:
=
Range sum
Vector sum
∑
∑
Icon:
Keyboard shortcut
[Ctrl][Shift]4
Typical use:
General
$
Sum with predefined index range variable
10
Examples:
∑ i
[Ctrl]4
2
i = 385
i := 1 .. 10
=1
∑i i
n
∑ i
=1
i
2
expand 1 → ⋅n⋅(n + 1)⋅(2 ⋅ n + 1) factor 6
50
2
= 385
Sum of the components of a vector.
1 3 v := 7 5 3
∑ v = 19
1.10.2 The product symbol Similarly, the product symbol comes in two variations: Name:
Product
Range product
∏
∏
Screen image:
=
Icon: Keyboard shortcut
[Ctrl][Shift]3
#
Typical use
General
Product with predefined index range variable
10
∏ i = 3.629 × 10
6
Examples:
i
i := 1 .. 10
=1
∏i = 3.629 × 10
6
i
Exercises Compute the following sums and product. You get the infinity symbol by pressing [Ctrl] Z. Simplify the expression when needed. n
∏ i
∑
∏
( −1 )
∑
=1 k
3
∞
∑ n
a⋅ k
∞
∑ k
=1
=1
k
i
∑
2
=1
k
( −1 ) k
∑
n
2
k
k
3
=1
∞
1 ln n n=1
∑
51
1 i
=1
∞
n
∑ k
1
∞
n
i+ 1
i
=1 ∞
k
1 − 1 3 k k=2
=1
∞
i
20
i
∑ k
=1 k
∞
∑ n
1 2
n! n
= 1 1000
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:
(
2
F( x , y) := cos 2⋅ x + 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 x ( u , v) := sin(u)⋅cos( v ) y ( u , v) := sin(u)⋅sin( 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:
M1 := CreateMesh( R , 0 , π , 0 , 2π , 10, 20)
or, alternatively
M2 := CreateMesh( x , y , z, 0 , π , 0 , 2⋅π , 10, 20) 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: (4 + cos( v ))⋅cos( u ) T( u , v) :=
(4 + cos( v ))⋅sin( u ) sin( v )
M3 := CreateMesh( T, 0 , 2π , 0 , 2π , 40, 10) Then to draw the sphere and the torus in the same diagram, define either M4 := stack(M1, M3)
or
M50 := M10
54
M51 := M30
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 v n ← 2⋅
π N
⋅n
x m , n ← Re( Pm) ⋅cos( v n)
y m , n ← Re( Pm) ⋅sin( v n) zm , n ← Im( Pm)
x y z For example, here is a "circular hut"
( M61 := RevSurf ( [ 3i
T
)
M60 := RevSurf ( 0 1 1 + 2i ) , 20
)
T
1.5⋅(1 + i) ] , 40
55
M6
Exercises. 1. Draw the graphs of the following functions of two variables: z( x , y) :=
z( x , y) :=
−4
3
2
1 + x + 2y
2
1 2
2
−y
⋅cos( x )
2
4
x +y −1
z( x , y) := e
z( x , y) := x − x − y
2
(
) 2 − 4 ⋅ ( x 2 + y 2) 2 2 x ⋅ y⋅ ( x − y ) z( x , y) := 1 + cos( x 2 + y 2) z( x , y) := 2
z( x , y) := x + y
2
2
x +y
2
z( x , y) := x⋅y
x − y
z( x , y) :=
−
2
x + y 2
Try various formatting options. 2. Draw the surfaces the following parameter function: u⋅cos( v ) R( u , v) :=
u⋅sin( v ) cos( 4u )
3. If a function f(x,y) depends only on x 2 + y 2 , 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: 2
f ( x , y) := x + y
2
(
2
g ( x , y) := cos 4⋅ x + y 56
2
)
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 ) := sin( t) 0.5⋅t
Then define M1 := CreateSpace( f , 0 , 8π , 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:
cos( t) sin( t) sin( 2t)
t+1 t2 − 1 2t
sec( t) tan( t) 4 3 t
2 ln(t + 1) 2 t 2 t 2
ln( t2 + 1) atan( t) 2 1+t
3t + 1 3t t2
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: R2 := 4
r2 := 0.5
A sphere with radius 2:
A torus:
cos(u)⋅cos( v ) R( u , v) := 2⋅ cos(u)⋅sin( v ) sin( u )
(R2 + r2⋅cos( u ))⋅cos( v ) T( u , v) := (R2 + r2⋅cos( u ))⋅sin( v ) r2⋅sin( u )
M 0 := CreateMesh R ,
−π π , , 0 , 2π , 10, 20 0 2 2
M 1 := CreateMesh( T, 0 , 2π , 0 , 2π , 10, 40) 0 A circular helix:
2.5⋅cos( t) H( t ) := 2.5⋅sin( t) 0.2⋅t − 2
M 2 := CreateSpace( H, 0 , 8π , 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 ) u2
M20 := CreateMesh(F, −2 , 2 , −2 , 2 , 20, 20) 0 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) := x 2 + y 2 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]167 [Ctrl]G ≠ [Alt]0185 [Ctrl]G ≈ [Alt]0187 [Ctrl]G
≥
£ [Ctrl]G
[Alt]0179 [Ctrl]G
Open intervals
〈
á [Ctrl]G
〉
ñ [Ctrl]G
Binary operations
⋅
× ´ [Alt]0215 •
Ä [Ctrl]G
⊕
Write ´t and erase the t.
÷
[Alt]0215 [Ctlr]G
⊗
Å [Ctrl]G
[Alt]0184 [Ctrl]G
[Alt]250 [Ctrl]G
Arrows ↔
[Alt]0171 [Ctrl]G ↑
[Alt]0173 [Ctrl]G
→
[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 × 10
34
num2str(t) converts a number to a string.
(
) = "1e+034"
num2str ( ∞ ) = "1e+307"
34
num2str 1⋅10
str2vec(S) converts a string to a vector of the corresponding ascii codes. T
str2vec(" abcæøå" ) = ( 32 97 98 99 230 248 229 ) vec2str(v) converts a vector of ascii codes to the corresponding string.
(
T
vec2str ( 32 97 98 230 248 )
) = " 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 , 20⋅deg) 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) 2
Im( TT) 1 Im( TS)
0 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 Q := 2 + i 0
i
T :=
"A" "B" "C" "D"
loc :=
−0.3 − 0.3i 2.2 − 0.3i 2.2 + 1.1i −0.3 + 1.1i
Te := write1(T, loc)
2
1
Im( Q) Im( Te )
2
0
4
1 Re( Q) , Re( Te )
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
C
D Im( Q)
A
2
0
4
B
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 α.: v0 := 100
α := 45⋅deg
g := 10
The the trajectory can be described by a set of parameter functions 1 2 x ( t ) := v0⋅cos( α ) ⋅t y ( t) := v0⋅sin( α ) ⋅t − ⋅g⋅t 2 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( t) , x( T)
T := FRAME
Now, define
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( t) , x( 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
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. Mathcad will then create an avi-file, which can be run using the button. This will give an animation of the projectile's trajectory. 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, you can double click on the animation to make it run. Here is another example - the cycloid: A point on the cycloid:
T := FRAME
t := 0 , 0.05 .. T
x ( t) := (t − sin( t)) An approximation to the circle: A radius:
r :=
N := 20
y ( t ) := 1 − cos( t)
n := 0 .. N zn := exp i⋅
⋅n + i + T 10
i+T x ( T ) i⋅y ( T ) +
π
5
y( t) Im( z) Im( r) 0
5
10 x( t) , Re( z) , Re( r)
66
15
2 Algebra
2.1 Tools for symbolic algebra Mathcad has two ways to do symbolic mathematics: Either, you do it from the Symbolics menu. Choose Symbolic to get the following menu:
Or you do it from the Symbolic toolbar. Press the icon to get the following palette:
The two ways to do symbolic mathematics give much the same results, but also have some significant differences in the ways they can be used. The symbolic toolbar is generally the most useful tool, and computations produced this way will be recalculated if they are edited. Using the menu has the advantage that it can be used on a subexpression, but the results will not be recalculated if you edit the original expression. An example: 2
3
2
3
2
2
3
(a + b) + (a + 1) expand , a → 4 ⋅ a + 2 ⋅ a⋅b + b + a + 3 ⋅ a + 1 (a + b) + (a + 1)
expands to
2
(
3
2
(a + b) + a + 3 ⋅ a + 3 ⋅ a + 1
)
Here, in the first version you use the button Alternatively, you can press [Ctrl] [Shift] [Period] and then type expand,a In the second version you have to let the blue frame include the expression (a + 1)
3
before you choose Symbolic - Expand from the menu.
67
2.2 Algebraic expressions 2.2.1 Evaluating an expression. In section 1.3.2 you've already seen how algebraic expression can be evaluated by using the symbolic evaluation operator → instead of the numerical evaluation operator =. Use [Ctrl][Period] or the button marked → on the Symbolic or Evaluation toolbar. 2 3 17 + → 3 4 12 Here is an alternative way: Write the expression, and choose Symbolics Evaluate - Evaluate symbolically from the menu. Alternatively, you can press [Shift] [F9]: 2 3 17 yields + 3 4 12 To get the result in this format, you have to choose Symbolics - Evaluation Style - and then check Horizontally and Show Comments. Notice that the frame consisting of the horizontal and the vertical editing bar (let's call it the editing frame) has to be expanded to include the whole expression on the left for this to work - Mathcad will only evaluate whatever is above the horizontal editing line. This makes it possible to evaluate not only the whole of an expression, but also only part of an expression: 1
1 + 1 3 4 1 1 3
+
1
3
+
1
1
yields 12
yields
12
1
7 12 12 7
+
1
+
when
3
12
1
1
1
+
1
12
3 +
1 12
yields
151
1
is in the frame
4 1
when
4
1 1
+
+
+
is in the frame 1 4
when the whole expression is in the frame.
84
4 1
You should compare this to 1 3
+
1
+
1 12
→
151 84
4
Using the menu gives you the opportunity to evaluate only part of an expression, using the toolbar does not. 68
2.2.2 Simplifying an expression To simplify an expression you can write it, see that the editing frame contains exactly what you want to simplify and then choose Symbolic - Simplify from the menu:
When the whole expression is within the frame,
2
x − 3⋅ x − 4 x−4
+ 2 ⋅ x − 5 simplifies to
3⋅ x − 4
2
When only the expression
x − 3⋅ x − 4 x−4
is in the frame.
2
x − 3⋅ x − 4 x−4
+ 2 ⋅ x − 5 simplifies to
x + 1 + 2⋅ x − 5
If you forget to expand the frame at all, simplification will do nothing: 2
x − 3⋅ x − 4 x−4
2
x − 3⋅ x − 4
+ 2 ⋅ x − 5 simplifies to
x−4
+ 2⋅ x − 5
when the cursor is on the number 5, as it is when you have just written the formula. There is a toolbar analogue of this: Press the button marked [simplify] on the Symbolic toolbar. You get: 2
x − 3⋅ x − 4 x−4
+ 2 ⋅ x − 5 simplify → 3⋅x − 4
With the toolbar can only simplify the whole expression - not just a part of it. This is a general principle: Use the menu when you need to manipulate only a part of an expression, otherwise use the toolbar.. Exercises. Simplify the following expressions: a
2
2
sin( x ) + cos( x )
2
x + 2⋅x + 3 ⋅ x − 5 ⋅ x
tan(atan( x ))
69
atan(tan( x ))
2.2.3 Expanding an expression Expanding an expression means to transform a short but possibly complicated expression into a longer expression with less complicated terms. To do this with Mathcad, write the expression, make the editing frame include the part of the expression that you want to expand, and choose Symbolics - Expand.
Examples: (a + b)
With
2
in the frame.
2 (a + b) ⋅(a − b)⋅(a − b⋅c)
expands to
( a2 + 2⋅ a⋅b + b2) ⋅(a − b)⋅(a − b⋅c) 2
(a + b) ⋅(a − b)
With
in the frame.
(a + b) 2⋅(a − b) ⋅(a − b⋅c) expands to
( a3 + a2⋅b − a⋅ b2 − b3) ⋅(a − b⋅c)
When the whole expression is in the frame. 2
(a + b) ⋅(a − b)⋅(a − b⋅c) 4
3
3
2
expands to 2
2
2
3
3
4
a − a ⋅b⋅c + a ⋅b − a ⋅b ⋅c − a ⋅b + a⋅ b ⋅c − b ⋅a + b ⋅c Just as with simplification, you use the button marked expand on the toolbar to expand an expression. Alternatively, press [Ctrl] [Shift] [Period], and type the word expand in the placeholder. 2
3
2
2
2
2
3
(a + b) ⋅(a − b⋅c) expand → a − a ⋅b⋅c + 2 ⋅ a ⋅b − 2 ⋅ a⋅b ⋅c + b ⋅a − b ⋅c Expand can take an expression as a second argument. This will indicate which factor that is not to be expanded: 2
(a + b) ⋅(a − b⋅c) expand , (a + b)
2
2
2
→ (a + b) ⋅a − (a + b) ⋅b⋅c
Exercises. Expand the following expressions: (x + y)
(
2
5
ln x ⋅2⋅y⋅z
sin(x + y)
)
5
sin(x + 2⋅y)
cos(5⋅x)
70
tan(x + y)
e
x+y
2.2.4 Factoring an expression Expanding an expression is in a sense a trivial thing to do, once you know a few rules. The converse operation is far less trivial, and may not even be possible in principle. Mathcad has a menu choice for it: Symbolics - Factor. Example: x 3 − 4 ⋅ x 2 + x + 6by factoring, yields
(x + 1)⋅(x − 2)⋅(x − 3)
As you might expect, there is an alternative button marked [factor] on the Symbolic toolbar that does the same as the menu choice, but only for the whole of an expression: a⋅ b + a⋅ c factor → a⋅(b + c) The keyword factor also works on integers, where it produces a prime number factorization, as you have seen in section 1.9.3 2
30
2
− 1 factor → 3 ⋅7⋅11⋅31⋅151⋅331
If you use factor on a sum of fractions, you get a single fraction: 1 x−1
+
x x+3
+
2⋅ x x+2
factor → 3⋅
( 2 ⋅ x 2 − x + 2 + x 3)
[ (x − 1)⋅(x + 3)⋅(x + 2) ]
Mathcad tries to factor over the rationals. x 2 − 2 is irreducible over the rationals, so you get just: x 2 − 2 factor → x 2 − 2 . However, you can add a radical as a second argument to factor to get x − 2 factor, 2 → ( x + 2 ) ⋅ ( x − 2 ) . If you use the complex unity i 2
(which is written as 1i) or −1 as the radix, you can get: 2
2
2
x + 1 factor → x + 1
x + 1 factor, −1 → (x + i)⋅(x − i)
Exercises. 1 Factor the following expressions using either method: 2
a + 2 ⋅ a⋅b + b
2
2
x +1
3
2
9
x − 4⋅ x + x + 6 x − 1
2345345
2. Expand (x + 1)⋅(x − 2)⋅(x − 3) . Then factor the result, 3 2 3 2 x − 4 ⋅ x + x + 6 when x − 4 ⋅ x is in the editing frame, and when 3 2 x − 4 ⋅ x + x is in the frame.
3. Factor x 2 + 4 over Q[ −2 ]. 71
2.2.5 Collecting on a subexpression Suppose you have an expression 4
3
3
2
2
2
2
2
3
3
4
a − a ⋅b⋅c + a ⋅b − a ⋅b ⋅c − a ⋅b ⋅c − a ⋅b⋅2 + a⋅ b ⋅c − b ⋅a + b ⋅c and you want to write this as a polynomial in one of the variables, say a. Then you write the expression, put the cursor on any occurrence of the variable a and choose Symbolic - Collect on subexpression. The result is the following:
4
3
3
2
2
2
2
2
3
3
4
a − a ⋅b⋅c + a ⋅b − a ⋅b ⋅c − a ⋅b ⋅c − a ⋅b⋅2 + a⋅ b ⋅c − b ⋅a + b ⋅c by collecting terms, yields 4
(
3
2
)
2
(
3
3
)
4
a + (−b⋅c + b)⋅a + −2⋅b − 2 ⋅ b ⋅c ⋅a + b ⋅c − b ⋅a + b ⋅c You can use the variable b instead of a: 4
3
3
2
2
2
2
2
3
3
4
a − a ⋅b⋅c + a ⋅b − a ⋅b ⋅c − a ⋅b ⋅c − a ⋅b⋅2 + a⋅ b ⋅c − b ⋅a + b ⋅c with b in the frame by collecting terms, yields 4
3
2
(
2
3
3
2
)
b ⋅c + (a⋅ c − a)⋅b − 2 ⋅ a ⋅b ⋅c + −a ⋅c + a − 2 ⋅ a ⋅b + a
4
The button marked collect on the Symbolic Toolbar work in much the same way. Supply the subexpression as a second argument after a comma: 2
3
3
2
4
3
3
5
2
3
3
2
4
3
3
5
( 2 5) 3 2 3 3 5 2 4 3 3 2 → x ⋅y + ( x + x ) ⋅y + x ⋅y 4
3
x ⋅y + x ⋅y + x ⋅y + x ⋅y collect, x → x ⋅y + y + y ⋅x + x ⋅y x ⋅y + x ⋅y + x ⋅y + x ⋅y collect, y
Exercise. Do the collect operation on the following expressions: 2 2 2 2 8 cos( x ) ⋅x + cos( x )⋅x + cos( x ) ⋅x with cos(x) in the frame 2 2 2 2 8 cos( x ) ⋅x + cos( x )⋅x + cos( x ) ⋅x with x in the frame 2
3
3
2
4
3
3
x ⋅y + x ⋅y + x ⋅y + x ⋅y
5
with the cursor on x
Repeat the exercise with the [collect] button on the Symbolic toolbar.
72
2.2.6 Partial fractions One of the examples of factorization above was: 1
+
x−1
x (x + 3)
−
2⋅ x x+2
factor →
(
2
− 2⋅ x − 9⋅ x − 6 + x
3
)
[ (x − 1)⋅(x + 3)⋅(x + 2) ]
Sometimes you need to go in the other direction, e.g. when you integrate a rational function. For this, you have the menu choice Symbolics - Variable Convert to Partial Fractions. You have to place the cursor on the variable that is to be considered as the variable of the expression. Therefore the expression may involve one or more parameters in addition to this variable:
(
2
− 2⋅ x − 9⋅ x − 6 + x
3
)
(x − 1)⋅(x + 3)⋅(x + 2) expands in partial fractions to
−1 +
1 (x − 1)
−
3 (x + 3)
+
4 (x + 2)
Here is an example with two parameters in addition to the variable x: 1 (x − a)⋅(x − b)
with the cursor on the x, expands in partial fractions to −1 [(a − b)⋅(−x + a)]
+
1 [(a − b)⋅(−x + b)]
The function you expand in partial fractions, needs to be a rational function in the variable, but its parameters can contain transcendental functions: 1
expands in partial fractions to
2
x − sin( y )
2
1 [ 2⋅sin(y)⋅(x − sin( y )) ]
−
1 [ 2⋅sin(y)⋅(x + sin( y )) ]
The toolbar analogue of this is the button marked parfrac, and you supply the relevant variable as a second argument after a comma: 1 2
x −a
2
convert, parfrac, x →
−1 [ 2 ⋅ a⋅(a − x) ]
−
1 [ 2 ⋅ a⋅(a + x) ]
Notice that you don't have to perform conversion to partial fractions first, in order to integrate a rational function. Mathcad will do it implicitly.
73
2.2.7 Substituting an expression for a variable in another expression Suppose you have the two expressions 2
z +
2
x+3
and
z
and you want to substitute the second expression for the variable z in the first expression. Then you proceed as follows: Select the second expression and copy or move it to the clipboard by means of one the buttons
Then place the cursor on the variable you want to substitute and choose Symbolics Variable - Substitute from the menu. The result is like this: 2
x+3
z +
2
by substitution, yields
z
2
(x + 3) +
2 (x + 3)
Alternatively, use the [substitute] button on the toolbar: 2
z +
2 z
2
2
substitute, z = x + 3 → (x + 3) +
(x + 3)
Suppose you would transform this expression to a single fraction. Then you could either press the factor button when the cursor is in the last expression to get: 2 2 2 x + 6⋅ x + 1 z+ substitute, z = x + 3 → x + 3 + factor → z (x + 3) (x + 3)
(
If you are not interested in the intermediate result, you can press the factor button just after you have written z=x+3: z+
2 z
(
)
2 substitute, z = x + 3 x + 6 ⋅ x + 11 → factor (x + 3)
You can use a series of toolbar commands in this way. 2
z +
2 z
substitute, z = x + 3 factor
2
→ x + 6⋅ x + 9 +
convert, parfrac, x 74
2 (x + 3)
If you need to substitute an expression for another expression in a function that you are going to use symbolically, you can define the expression as a function. f ( z) := z +
2 z
D2(f , x) :=
You can then define
d2 dx
D2(g , x) → 0
2
f ( x + 3) → x + 3 +
Then
2
(x + 3)
f ( x) + f ( x)
h ( x ) := x + sin( x )
g ( x ) := sin( x )
D2(h , x) → x
This is useful when solving differential equations Similarly, for difference equations, you can define: D1(f , n) := f(n + 2) − 4⋅f ( n ) g ( n ) := 2
n
D1(g , n) simplify → 0
You can also substitute in an expression by means of the limit operator Subs(U, x , u) :=
lim U x→u
This will work fine, except when u contains x:
(
2
)
Subs x , x , t → t
2
(
)
2
Subs x , x , x + 1 →
but
gives an error.
In such cases you can go via an intermediate variable: Subst(U, x , u) :=
lim ttt→u
Then
(
2
(
)
x
Subst 2 , x , x + 2 → 4 ⋅ 2
lim U x→ttt
)
Subst x , x , t → t
2
(
2
)
2
Subst x , x , x + 1 → x + 2 ⋅ x + 1
So, you can define a differential operator: D( U) :=
d U − 2⋅U dxx
and get
or a difference operator: and get
( x) → 0
DD 2
( 2x) → 0
D e
DD(U) := Subst(U, x , x + 2) − 4⋅U DD( −2 ) 75
→ 0
x
x
2.2.8 Getting a floating point value from a symbolic expression. By default, symbolic Mathcad gives exact results, which means that function values that are not rational, are not evaluated as a floating point number. Examples: 1 atan( 1 ) → ⋅π sin(1) → sin( 1 ) 2→ 2 4 Of course, sometimes you'll want to see a floating point approximation to an exact value. This can be done by writing the expression and then choosing Symbolics - Evaluate - Floating from the menu. Remember to have the editing frame around whatever subexpression you want to evaluate. sin( 1 ) π
floating point evaluation yields .84147098480789650665
floating point evaluation yields 3.1415926535897932385
Alternatively, from the symbolic toolbar, click on the [float] button on the Symbolic toolbar, and write the number of decimals as a second argument: π float, 40 → 3.141592653589793238462643383279502884197 sin(1) float , 40 → .8414709848078965066525023216302989996226
2.2.9 Polynomial coefficients Given a polynomial, say x 3 + 2x 2 − 3x + 1 , you may need to operate on the coefficients. For instance, there is a built-in equation solver Polyroots that takes this vector as an input to solve a polynomial equation. Write the equation, place the cursor on the variable, and choose Symbolics Polynomial Coefficients from the menu. The result is: 3 2 x + 2 ⋅ x − 3 ⋅ x + 2 has coefficients
(Cursor at an x)
2 Notice that the first −3 component is the 2 constant term. 1
Alternatively, you can use the button marked coeffs on the Symbolic toolbar. The variable of the polynomial is to be inserted as the second argument: 2 x + 2 2
2
x ⋅y + x + 2 coeffs , x →
2 2 1 x ⋅y + x + 2 coeffs , y → 0 y2 x2 76
2.3 Equations
2.3.1 Solving one equation with one unknown numerically The underlying algorithm for Mathcad's numerical equation solver root is the secant method. Given a function, say: f ( x ) := cos( x ) − x for which you are going to find a zero. The first thing you do, is to find an approximation to a zero by drawing a graph of the function with x := −1 , −0.95 .. 4 5
f ( x)
1
0
2
1
3
4
5 x
The graph indicates that you have a zero somewhere in the interval [0,1]. Then you can choose x=0.5 as a starting point for an iterative search towards a zero. As you may know, the secant method requires two start values, but Mathcad will compute a second value based on the first value and the value of the built-in variable TOL, which is used in the break criteria for some numerical algorithms. The second value is chosen as x+TOL · x if x0≠0. If x=0, it's chosen to be x+TOL. You define the variable x as 0.5 and use the built-in function root to compute the solution: x := 0.5
x0 := root(f ( x ) , x)
x0 = 0.739081777771822
Check the function value: f ( x0) = 5.616 × 10− 6 So, the function value is close to zero. The search loop is broken when the
function value is less than TOL = 1 × 10− 3
Now, define TOL to be less than that TOL := 10− 10 and see what you get:
x := 0.5
x1 := root(f ( x ) , x)
x1 = 0.73908513321516
f ( x1) = 0
Exercises. 1. Solve the following equations by means of the root function:. 2
x
2
3x − e = 0 3x − x − 1 = 0
tan( x ) = 1 − x
2
2. Find the point of intersections between the circle with radius 2 centered at the origin and the graph of the function sin(x)/x. 77
2.3.2 Solving one equation with one unknown symbolically The root function works for any function, even if the solution cannot be expressed in terms of elementary function. Of course, many equation can be solved in terms of elementary functions, and for such equations you often want a symbolic expression for the solution. There is a menu choice for this: Symbolics - Variable - Solve Variable. The cursor must be on the name of the unknown variable .
Example: 2 x + p ⋅ x + q = 0 has solution(s)
−1 2 −1 2
⋅p +
1
⋅p −
1
2 2
⋅
( p2 − 4⋅ q)
⋅
( p2 − 4⋅ q)
The = sign here looks a little different from the usual evaluation sign. It is typed using [Ctrl]+. Alternatively, click on the button to get the Boolean toolbar The comparison operator is the first of these. This symbol is shown as a bold equality sign to distinguish it from the numerical evaluation operator =. It is used in equations and in logical expressions to test equality. It also works with arrays. You can consider = to be a function that returns 1 if the two operands are equal and 0 if they are not: 1= 2= 0
1= 1= 1
1 3 = 1 3 = 1 1 3 = 2 4 = 0 2 4 2 4 2 4 3 5
In these computations, the first = is the bold comparison function, while the second is the numerical evaluation operator, which returns either 0 or 1 for a Boolean expression.. Exercises. Solve the following equations symbolically: 3
x − 1= 0
sin( x ) = 1 23⋅ x −
5
3
5
3
2
2
x + 2x − x − 2 = 0 3x − x − 1 = 0
53
3 2 2x − 23x + 86⋅x − 1005 = 0 = 2 12 4
3
2
2500x − 15000x + 35875x − 42750x + 25381x − 6006 = 0 4
3
2
4
3
2
x − 56x + 251x − 7x − 6 = 0 x − 56x + 251x − 7x − 6.0 = 0 78
2.3.3 Polynomial equations solved numerically For polynomial equations of degree five or higher, we know that the solution cannot in general be expressed by means of radicals (roots). For such cases, Mathcad has an equation solver named polyroots, which returns numerical approximations to the solutions of a polynomial equation of any degree. The argument to the function is the vector of coefficients of the polynomial, starting with the constant term: For the example above:
−2 0 −0.5 − 0.866i −0.5 + 0.866i −1 polyroots −1.414i = 2 0 1.414i 1 1 Notice that you have to write zero components for the terms that are missing (i.e. have zero coefficient) in the polynomial. Exercises. Solve the polynomial equations from the previous subsection by means of the function polyroots.
2.3.4 Several equations with several unknown solved numerically Mathcad has built-in a generalization of the secant method called the Levenberg-Marquardt method. It works like this: Say the problem is to find the intersection between the circle x 2 + y 2 = 1 and the straight line y=x + 1/2 in the first quadrant. First you find approximate values for a solution e.g. by drawing a diagram or simply guessing. In this case, let's start with. x := 1 y := 1 Then you write the code word Given in a math region by itself: Given and then the equations and conditions that x and y has to satisfy: 2
2
x +y = 1 y − x = 0.5 Then you get the result by using the function Find: 79
z := Find(x , y)
z=
You may check that
0.411 0.911
( z0) 2 + ( z1) 2 = 1
z1 − z0 = 0.5
To find the second solution, start with other guess values: x := −1 Given
y := −1 2
2
x + y = 1 y − x = 0.5
You may check that
z := Find(x , y)
( z0) 2 + ( z1) 2 = 1
z=
−0.911 −0.411
z1 − z0 = 0.5
In particular, this method can be used to solve a set of linear equations: x := 0
y := 0
z := 0
Given
x − y − 2⋅ z = 5 2⋅ x − y − z = 2 x − 2⋅y + z = −1
z := Find(x , y , z)
−0.667 z = −1 −2.333
2.3.5 Linear equations You just saw how a system of linear equation can be solved numerically by means of a Given - Find block. Mathcad also has other ways to solve such systems of equations. Look at the system above again, and define the matrix of coefficients A and the vector of right-hand sides B:
1 −1 −2 5 A := 1 −2 1 B := −1 2 2 −1 −1 Then use the function lsolve ( which works only numerically. This is equivalent to computing the matrix inverse of A and multiply by B: X := lsolve(A, B)
−0.667 X = −1 −2.333
−0.667 A ⋅B = −1 −2.333 −1
80
2.3.6 Several simultaneous equations solved symbolically −2 3 −1 A ⋅B → −1 −7 3
The matrix inversion method from the example above can be used symbolically:
You may even use matrices with undetermined parameters in the coefficient t 2 matrix: To solve the system t·x+2·y=2, -t·x+3·y=-2 define: D( t ) := −t 3 and use the menu choice Symbolic - Matrix Operations - Invert Matrix to get the inverse of a matrix with a parameter. D( t) :=
t 2 −t 3
by matrix inversion, yields
Then you can multiply:
E( t) :=
3 −2 (5 ⋅ t) t t 1
⋅
2 2 2 2 E( t )⋅ → t Check: D( t)⋅ t → −2 −2 0 0
However, it's more efficient to use a symbolic version of the Given- Find block: Notice that with the symbolic Given-Find block you Given don't give start values for the unknown. On the t⋅ x + 2 ⋅ y = 2 contrary: You have to cancel any previously assigned value of the unknown by defining it recursively to be −t⋅x + 3 ⋅ y = −2 equal to itself.. This method will try to solve your 2 equations symbolically much the same way as you would do it manually yourself, and is not limited to Find(x , y) → t linear equations. Let's use it on the nonlinear example 0 above: Given
2
2
x + y = 1 y− x=
−1 + 1 ⋅ 7 4 4 Find(x , y) → 1 + 1⋅ 7 4 4
1 2
−1
−
1
⋅ 7
0.411 −0.911 = 1 1 0.911 −0.411 − ⋅ 7 4 4 4
4
So you got the same solutions. 81
Exercises 1. Solve the following equations by means of the root function: x= e
x
3
2
x − x − x− 1= 0x
72
3
2
− 2 = 0 x + 2x + 10x − 20 = 0
2. Solve second and the fourth of the equations above using polyroots. 3. Find the points of intersection between the two circles x 2 + y 2 = 1 and
x −
2
+ y − 2 1
1
2
= 1
2
4. Solve the following systems of linear equations numerically and symbolically: a. 2x + 4y + 6z = 18 b. 2x + 4y + 6z = 18 4x + 5y + 6z = 24
4x + 5y + 6z = 24
3x + y − 2z = 4
2x + 7y + 12z = 40
c. x + 3y − 5z + u = 4
d.
2x + 5y − 2z + 4u = 6
x − 2y + 3z = 11 4x + y − z = 4 2x − y + 3z = 10
e. −2x + y + 6z = 18
g.
i.
f.
3x + 6y − 6z = 9
5x + 8z = −16
2x − 5y + 4z = 6
3x + 2y − 10z = −3
−x + 16y − 14z = −3
x + y − z= 7
h.
x − y − z= 7
4x − y + 5z = 4
4x − y + 5z = 4
2x + 2y − 3z = 0
6x + y + 3z = 4
x + y − z= 0
j.
3x + 6y − 6z = 9
4x − y + 5z = 0
2x − 5y + 4z = 6
6x + y + 3z = 0
5x + 28y − 26z = −8
82
3 Calculus 3.1 Limits Mathcad has three built-in limit operators. You access them by clicking on the calculus menu button: and then one of the buttons Alternative keystrokes:
[Ctrl]+L
[Ctrl]+A
[Ctrl]+B
The buttons are self explanatory. These operators will help you find most of the limits you need for functions of one variable. To find a limit, type the expression and press [Ctrl] [Period] to get symbolic evaluation. Alternatively, make the editing frame contain the whole expression and choose Symbolic Evaluate - Evaluate Symbolically from the menu or press [Shift] F9 . Notice that you cannot use numerical evaluation. Here are some examples (press [Ctrl]Z to get ∞). Example
lim n→∞
1 +
1
n
→ exp( 1 )
n
Exercises. Find the following limits: lim n→∞
1 +
lim x →0 +
x x
x
n
n
lim
sin(a⋅x)
x→0
lim x →0 −
x
x
lim
a −1
x→0
x
x x
3.2 Differentiation 3.2.1 The derivative as a limit You could use the limit operator to find the derivative of a function by means of the definition of the derivative: D( f , x) :=
lim h→0
f ( x ) := sin( x )
f ( x + h) − f ( x ) h u D( f , x) → cos( x ) f ( x ) := sin(u⋅x)
83
D( f , x) → cos(u⋅x)⋅u
3.2.2 The built-in differentiation operator However, differentiation is a built-in operator in Mathcad. Like so many other operators in Mathcad, the differentiation operator occurs in two variations in Mathcad: A numerical and a symbolic version. Both versions are accessed the same way: Press the icon for the calculus menu: and then press the button: Or: Press the question mark ? In both cases you get the symbol d d The two placeholders are of course for the expression and the variable name. To force a numerical method to be used, you first define the point where the differentiation is going to take place and then use the operator: d f ( s) = 0.540302305868126 ds You can also define the derivative as a function: g ( x ) := d f ( x ) dx f ( x ) := sin( x )
s := 1
g ( 1 ) = 0.540302305868126 The function g doesn't yet exist as cos(x): cos(1) = 0.54030230586814 which is a little different from the value above. However, as soon as you evaluate it symbolically, it becomes cos(x): d f ( x ) → cos( x ) g ( 1 ) = 0.54030230586814 dx Differentiation with respect to a single variable and partial differentiation is really the same operation from Mathcad's point of view. However, it is possible to make a differentiation look partial if you want to. You just click on the expression with the right mouse button. The first line of the menu you get, is "View Derivative As" which gives you the opportunity to change to partial derivative. E.g.: g ( x ) :=
2
2
f ( x , y) := x ⋅y + y + x ⋅ y
∂
f ( x , y) → 2 ⋅ x⋅y + y
∂
2
f ( x , y) → x + 2 ⋅ y +
∂x ∂y You can also you the Symbolic menu. Write the function you want to differentiate, place the cursor on an x and choose Symbolics - Variable Differentiate.
( 2)
cos x
by differentiation, yields
( 2)
−2⋅sin x ⋅x
However, this won't enable you to directly define the derivative as a new function, so it is normally better to use the Symbolic toolbar. 84
Exercises. Find the derivative of the following functions: sin
x
cos( x )
2
3
x ⋅cos 2
cos(sin( x ))
1
x
1 + cos( 2x )
3.2.3 Higher order derivatives For the higher derivatives, you have similar options. Press the button, and then Alternatively, press [Ctrl] [Shift]? ([Ctrl] [Shift]* on some national keyboards). The symbolic version can be used for any order: f ( x ) := sin( 3x )
h ( x ) :=
d 10 dx
10
f ( x ) → −59049⋅sin(3⋅x) 3
h ( 1 ) = −8.333 × 10
In the numerical version, this can be used only for n up to 5: h5( x ) :=
d5 dx
5
h5(1) = −240.568
f(x)
h6( x ) :=
is OK, but
d6 dx
6
f( x)
h6( 1 ) =
gives an error message. . For higher n's you have to compose more operators. h6_3( x ) :=
d3 d3
f(x) h6_3(1) = −102.876476656908 3 3 dx dx Notice that this has only 4 correct digits, so you should use symbolic derivatives whenever possible. Exercise. Find the derivatives of order two and three of the functions in the exercises after the previous subsection.
3.2.4 Differential formulae Mathcad knows the standard rules of differentiation: Example. d (u ( x ) + v ( x )) → d u ( x ) + d v ( x ) dx dx dx Exercises. Obtain similar results for the derivatives of: u ( x )⋅v ( x ) u( x)
v( x )
u( x)
n
u ( x ) ⋅v ( x )
v( x)
(
a
b
ln u ( x ) ⋅v ( x ) ⋅w( x )
m
)
c
85
u( x)
n
m
ln( u ( x )
v(x)
cos(sin( x ))
)
3.2.5 Differentiation of implicit functions If y is defined as function of x implicitly by some equation U(x,y)=0, then the −Ux derivative of y with respect to x by implicit differentiation is given as . Uy You can define the following function for this:
∂ − U ∂x DImp(U) := simplify ∂ U ∂y
Notice that I've used a non-standard syntax
for the function simplify. This version comes from Maple, which Mathcad uses for symbolic computations.
(
)
−x
Example: f ( x , y) := DImp x 2 + y 2 − 1 →
y
(
2
f 0.5, 1 − 0.5
)
This should be compared with: g ( x ) := 1 − x
2
g1( x ) :=
d g( x) → dx
−1
( 1 − x 2)
1 2
⋅x
For the second derivative you get by differentiating implicitly once more: 2 2 2 2 ∂ U⋅ ∂ U + ∂ U ⋅ ∂ U ... ∂x 2 ∂y ∂y 2 ∂x ∂ ∂ ∂ ∂ U ⋅ U ⋅ U + −2⋅ ∂x ∂y ∂x ∂y D2Imp(U) := simplify − 3 ∂ U ∂y
(
)
Example: f ( x , y) := D2Imp x 2 + y 2 − 1 →
(
2
− y +x y
)
2
3
This should be compared with: g2( x ) :=
d2 dx
g ( x ) simplify →
2
1 1 2 2 2 ( −1 + x ) ⋅ ( 1 − x )
Exercise. Find y' and y", given that a) y 2=x, b) x2-xy+y2=7 by implicit differentiation. 86
3.2.6 Tangent and normal A curve in the plane can be given either as the graph of a function f, or as the graph of a pair of parameter functions, which is equivalent to a two dimensional vector function.
Tangent and normal to a curve In the functions below, R is either a function of one variable, or a two dimensional vector function giving a parameter curve.
Tan(R , t , s) := R( t ) + Nor(R , t , s) := R( t ) +
d R( t ) 0 1 t dt s⋅ on error + s⋅ d R( t) d R ( t ) dt dt R( t) 1 −d R( t) 1 −d R( t) dt t s⋅ on error + s⋅ dt d R( t) 1 dt R( t) 0
Here are programs that can be used to draw families of tangents and normals. The parameter F is Tan for tangents and Nor for normals. Family(R , a , A, N, F) :=
dt ← A − a ⋅(t ← a) N for n ∈ 0 .. N M ← F(R , t , −10) X0 , n ← M 0 Y0 , n ← M 1 M ← F(R , t , 10) X1 , n ← M 0 Y1 , n ← M 1 t ← t + dt
X Y
87
Example 1. Tangent and normal to the parabola R( x ) := x 2 Tan(R , t , s) → f ( x ) := x
2
t+s 2 t + 2 ⋅ t⋅s
Nor(R , t , s) →
T( t) := Tan(f , 0.5, t)
Range variables: t := −5 .. 5
t − 2⋅t⋅s 2 t +s
N( t) := Nor(f , 0.5, t)
x := −2 , −1.9.. 2
1
T( t) 1 N( t) 1 f ( x) 0
1
T( t) 0 , N( t) 0 , x
Families of tangents and normals:
X := Family(R , −4 , 4 , 40, Tan)
Y
U := Family(R , −4 , 4 , 80, Nor)
V
4
4
2
2
Y
V
f ( x)
f ( x) 0
2
0
2
0
2
2
X, x
2
0 U, x
88
2
Example 2
An ellipse:
R( t ) :=
cos( t) 2⋅sin( t)
t := 0 ,
π 32
.. 2⋅π
Tan(R , t , s) →
cos(t) − sin( t) ⋅ s 2⋅sin( t) + 2⋅cos( t)⋅s
X := Family( R , 0 , 2π , 40, Tan) Y
Nor(R , t , s) →
cos(t) − 2⋅cos( t)⋅s 2⋅sin( t) − sin( t) ⋅ s
U := Family( R , 0 , 2π , 40, Nor) V
2
2
Y R ( t) 1
V 0
R ( t) 1
2
0
2 2
0
2
2
X , R ( t) 0
0
2
U , R ( t) 0
Exercises. Draw families of tangents and normals to the curves given by the following parameter functions:
sin( t) t
t − sin( t) 1 − cos( t)
cos( t) 3 sin( t) 3
9⋅cos( t) − cos( 9t) 9 sin(t) − sin( 9t)
3.2.7 Curvature and evolute The evolute of a curve given by two scalar functions can be written in the following way: Evolute(x , y , t) :=
D1 ← d x ( t) ⋅ D2 ← d y ( t) dt dt d2 d2 D3 ← 2 x ( t) ⋅ D4 ← 2 y ( t) dt dt 2
DD ←
D1 + D2
2
D1⋅D4 − D3⋅D2
x ( t) − DD⋅D2 y ( t) + DD⋅D1 89
Curvature(x , y , t) :=
d 2 y ( t) + d y ( t) ⋅ d 2 x ( t) d − x ( t ) ⋅ dt dt2 dt dt2 3 2 2 d x ( t) + d y ( t) dt dt
2
Example 1. The evolute of a cycloid is another cycloid x ( t ) := t − sin( t)
y ( t) := 1 − cos( t )
Curvature(x , y , t) simplify →
1 1 2⋅(2 − 2⋅cos( t )) 2
Ev(t) := Evolute(x , y , t) simplify →
t + sin( t) −1 + cos( t)
t := 0 ,
π 16
.. 6⋅π
2
y( t ) Ev( t) 1
0
5
10
15
2 x( t) , Ev( t) 0
Exercise. Find evolute and curvature of the curves from the previous subsection.
90
20
Curvature and evolute for the graph of a function u=f(x) d2 2 u( x) ÷ 1 + dx
CurvatureF(u , x) :=
(I use
÷ instead of
2 d u ( x ) dx
3 2
to save space. Press [Ctrl]/ or [Ctrl]*.)
2 d2 −d u ( x ) x d EvoluteF(u , x) := + 1 + u ( x ) ÷ 2 u ( x) ⋅ dx u ( x ) dx dx 1
Example 1
f ( x ) := 5⋅cos( x )
CurvatureF(f , x) → −5⋅
cos( x )
( 1 + 25⋅sin(x) 2)
3 2
( 1 + 25⋅sin(x) 2) x − sin( x )⋅ cos( x ) Ev(x) := EvoluteF(f , x) → 2 1 ( 1 + 25⋅sin( x ) ) 5⋅cos( x ) − ⋅ 5 cos( x )
Exercise. Find curvature and evolute for the function y=sin(x) and y=ex
Curvature and evolute for a polar curve CurvatureP( r, t) :=
X1 ← d r( t) X2 ← d 2 r( t) 2 dt dt 1
( r( t) 2 + 2⋅ X12 − r( t) ⋅ X2) ÷ ( r( t) 2 + X12) 2 EvoluteP( r, t) :=
X1 ← d r( t) ⋅ X2 ← d 2 r( t) X4 ← r( t)⋅ cos( t) 2 dt sin( t) dt 2 2 r( t) + 2 ⋅ X1 − r( t ) ⋅ X2 2
X4 −
r( t) + X1
91
2
sin( t) −cos( t)
⋅ X4 + X1⋅
Example 2
Cardioid with Evolute
CurvatureP(r1 , t) simplify → −6⋅
r1(t) := 2⋅(1 − cos( t ))
(−1 + cos( t )) 1 2 (2 − 2⋅cos( t))
2 ⋅cos( t) 2 + 2 ⋅cos( t) − 3 3 Ev(t) := EvoluteP(r1, t) simplify → 2 ⋅(cos( t) + 1)⋅sin( t) 3
4
3
Here is the cardioid with its evolute, which is also a cardioid: t := 0 ,
π 64
.. 2⋅π X( t) := Ev( t) 0
Y( t) := Ev( t) 1
90 4
120
60
3 150
30
2 1
r1( t) 2
X( t) + Y( t)
2
0
180
0
210
330
240
300 270 t , atan2(X( t) , Y( t))
Exercises. Find the evolute and curvature of the following curves. Draw the curve and its evolute in the same diagram Four-leafed rose: r2(t) := sin(2⋅t) Limacon: r3( t ) :=
1 2
Hyberbolic spiral: r4( t ) :=
+ cos( t)
1 t
Pascal's snail: r5(t) := a⋅cos( v ) + b
92
3.2.8 Envelope of a family of curves Given two differentiable functions f and h, then the function F(f,h,x,t) defined by: F( f , h , x , t) := f ( t) ⋅ x + h ( t) defines a family of straight lines: For a given t, the function F with varying x, gives a line through (t,h(t)) with slope f(t). The envelope of the family of lines is a curve whose tangent at every point belongs to the family of curves. It can be shown that the equation of the evolute is: T
−d h ( t) d h ( t) dt dt Envelope(f , h , t) := − ⋅f ( t ) + h ( t) d d f ( t) dt f ( t) dt Example 1. f ( t) := tan( t ) h ( t ) := 2⋅sin( t) F( f , h , x , t) := f ( t ) ⋅ x + h ( t)
G( t , x) := f ( t ) ⋅ x + h ( t) → tan( t) ⋅ x + 2⋅sin( t)
Then you plot the family of straight lines: and the envelope: u := 0 , 0.1.. 6.3 t := −1.5, −1.45 .. 4.5 x := −10, −9.9.. 10 Env(t) := Envelope(f , h , t) simplify →
3 −2⋅cos( t) 2⋅sin( t) − 2⋅sin( t)⋅cos( t) 2 2
2
G( t , x)
0
2
Env( u) 1
2
0
0
2
2
2
x
Env( u) 0
Exercise. Draw the family of curves defined by f(t)=t2 and g(t)=t3. Find the enevelope and draw it. Do the same for the functions -t and 2 t
93
3.2.9 Extrema of functions Say you want to find zeros, extrema and points of inflection of the function f ( x ) := sin( x ) +
x 2
−
3
over the interval [0,5]
2
Start by finding the derivatives: g ( x ) :=
1 d f ( x ) → cos( x ) + 2 dx
h ( x ) :=
d2 dx
2
f ( x ) → −sin( x )
Then draw the graphs of the function and its derivatives: t := 0 , 0.1 .. 5 2
f ( t) g( t ) h( t )
0
1
2
3
4
5
t
From the graph of f (or the derivative g) you see that f must have a local maximum at about 2.1, which you can find by means of the function root: TOL := 10
− 10
t := 2.1
xmax := root( g ( t) , t )
xmax = 2.0944
Likewise, f must have a local minimum at about 4.2, so you find it by means of root: t := 4.2
xmin := root( g ( t ) , t)
xmin = 4.189
Notice that you can try to use the symbolic solve-function: g ( x ) = 0 solve , x →
2 3
⋅ π float , 5 → 2.0944
but this will only give one of the two solutions. 94
It remains to see if these point are global or just local extrema. You have to compare with the values at the endpoints of the interval: f ( 0 ) = −1.5
f(xmax) = 0.413
f ( 5 ) = 0.041
f(xmin) = −0.272
The conclusion is that (0,-1.5) is a global minimum, while (xmax,f(xmax)) is a global maximum point. There is another way to find local extremes without differentiating, or at least you leave it to Mathcad to differentiate. Give the independent variable start value near a local extremum, and use the pair Given - Maximize/ Minimize. The global maximum: t := 5
Given t ≥ 0
t≤5
tH := Maximize(f , t) tH = 2.0944
f ( tH) = 0.413
The global minimum: tL := Minimize(f , t)
tL = 4.189
f ( tL) = −0.272
Points of Inflection If you are asked to find the points of inflection, then you find the zeros of the second derivative either by looking at the graph of f above or by looking at the graph of the second derivative. You see that there are two zeros at about 1 and 4, which you can find by means of the root function: − 13
t := 1
x1 := root(h ( t ) , t)
x1 = −5.07 × 10
t := 4
x2 := root(h ( t ) , t)
x2 = 3.142
Exercises. 1. Find zeros (use the function polyroots), local extrema, global extrema and points of inflection for the function F( x ) := x 3 − 6 ⋅ x 2 + 10⋅x + 5 over the interval [-5,5]. 1 2 2. Same exercise for the function G( x ) := sin( x ) + x − 1 over [0,4]. 10
95
3.2.10 Partial derivatives Partial differentiation is done the same way as ordinary differentiation in Mathcad. In version 2000, you can make partial derivatives look different from ordinary derivatives, cf. section 3.2.2. So, everything that was said about ordinary differentiation in sections 3.2.2 and 3.2.3, applies to partial differentiation as well. Example fx(x , y) :=
3
2
f ( x , y) := x − 2 ⋅ x⋅y + 3 ⋅ y
∂
2
∂x
fx6(x , y) :=
fx6(x , y) :=
f ( x , y) → 3 ⋅ x − 2 ⋅ y
∂6 6 ∂x
∂6 6 ∂x
f ( x , y)
2
f ( 0 , 2) = 48
fx6(0 ( 0 , 2) 2) =
f ( x , y) → 0
4
f ( 0 , 2) → 48
gives error message, but:
fx6(0 , 2) = 0
is allright.
3.2.11 Series The keyword series can be used to find the Taylor series of a function of one or several variables. You have to supply information about what variables that are free, the highest order of the terms and the point of development. Examples ln(x⋅y + z) series , x = 0 , y = 0 , z = 1 , 3 → z − 1 + x ⋅ y −
1 2
⋅(z − 1)
2
Notice that x=0,y=0,z=1 means that the Taylor series is around the point (0,0,1). The final 4 means that the highest order is < 4, i.e. only terms whose sum of exponents is less than 4 are included. ln(x⋅y + z) series , x = 0 , 3 → ln( z) +
y z
2
⋅x −
1 y 2 ⋅ ⋅x 2 z2
Notice that y and z are considered to be constants in this example, so e.g. the term x2·y2 is actually of the second degree in the free variable x. Exercise.
Find the Taylor series with terms of degree less than 5 of the function
sin(x⋅y)⋅z about the point x=0,y=1 with x and y as free variables.
96
3.2.12 Extrema of functions of two variables You can use SmartMath to decide what kind of an extremal point a given stationary point is. Say you have a function (which is simple enough for the symbolic Given - Find): 3
2
f ( x , y) := x − 2 ⋅ x⋅y + 3 ⋅ y
4
Find the partial derivative of the first and second order: fx(x , y) :=
∂
2
∂x
∂2
fxx(x , y) := fxy(x , y) :=
2
f ( x , y) → 3 ⋅ x − 2 ⋅ y fy(x , y) :=
∂x
2
∂ ∂ ∂x ∂y
f ( x , y) → 6 ⋅ x
fyy(x , y) :=
∂ ∂y
f ( x , y) → −4⋅x⋅y + 12⋅y
∂2 ∂y
2
f ( x , y) → −4⋅x + 36⋅y
3
2
f ( x , y) → −4⋅y
Also, define the quantity fxx⋅fyy − fxy2 D( u , x , y) :=
2 2 ∂2 2 u ( x , y) ⋅ ∂ 2 u ( x , y) − ∂ ∂ u ( x , y) ∂y ∂x ∂y ∂x
(
Di(x , y) := D(f , x , y) → 6⋅x⋅ −4⋅x + 36⋅y
2
) − 16⋅y2
Then find the stationary points:
3 ⋅ x 2 − 2 ⋅ y 2 = 0 M := −4⋅x⋅y + 12⋅y 3 = 0
0 0 0 0 2 1 solve , x , y → ⋅ 6 9 9 2 −1 ⋅ 6 9 9
To determine what kind of stationary points you have, compute: i := 0 .. 3 Di := Di( M i , 0 , M i , 1) fxxi := fxx( M i , 0 , M i , 1) ff i := f ( M i , 0 , M i , 1) T
D = ( 0 0 1.185 1.185 ) ff → 0 0 T
T
fxx = ( 0 0 1.333 1.333 )
= ( 0 0 −5.487 × 10− 3 −5.487 × 10− 3 ) 729 729 −4
−4
Therefore, this test gives no decision for the first stationary points while the two other points are both minimum points. We find the function values: 97
However, the point (0,0) must be a saddle point, since f ( x , 0) → x
3
f ( 0 , y) → 3⋅y
4
f ( M 2 , 0 , M 2 , 1) = −5.487 × 10
−3
From this you see that the function is both positive and negative in every neighborhood of (0,0). You can check that the results are reasonable by drawing a contour diagram: F := CreateMesh( f , −0.5 , 0.5 , −0.5 , 0.5 , 20 , 20)
To find global extrema, you can use a numerical solve-block with the functions Maximize and Minimize: x := 1 y := 1 Given x ≤ 1 x ≥ −1 y ≤ 1 y ≥ −1
x1 := Maximize(f , x , y) y1 x := 1
y := 1
Given
x1 = −0.816 y1 1 x≤1
f ( x1, y1) = 4.089
x ≥ −1 y ≤ 1 y ≥ −1
x2 := Minimize(f , x , y) x2 = 0.222 y2 y2 0.272
−3
f ( x2, y2) = −5.487 × 10
The points you got, were correct, but there are more symmetric points. Change the guess values to x:=-1, y:=-1 to find them. Exercise. Find the local and global extrema of the function 2 2 f ( x , y) := 2 + 2x + 2y − x − y inside the triangular plate defined by
x ≥ 0,y ≥ 0,x + y ≤ 9
98
3.2.13 Extrema of functions with three or more variables Suppose you have a function of more than two (say three) variables: 3
2
4
f ( x , y , z) := x − 2 ⋅ x⋅y − 3 ⋅ y − z⋅ y + 6 ⋅ z⋅x To find the stationary points, find the partial derivatives of the first order: fx(x , y , z) := fy(x , y , z) :=
∂ ∂x ∂
2
2
f ( x , y , z) → 3 ⋅ x − 2 ⋅ y + 6 ⋅ z 3
f ( x , y , z) → −4⋅x⋅y − 12⋅y − z ∂y ∂ fz(x , y , z) := f ( x , y , z) → −y + 6 ⋅ x ∂z Then find the stationary points:: 0 0 0 fx(x , y , z) = 0 0 0 0 M := fy(x , y , z) = 0 solve , x , y , z → −71 −71 115943 f (x , y , z) = 0 5184 864 53747712 z
Hessian matrix To determine what kind of extremal points these are, define the Hessian matrix: ∂ ∂ ∂ ∂ ∂ ∂ f ( x , y , z) f ( x , y , z) f ( x , y , z) ∂z ∂x ∂y ∂x ∂x ∂x
∂ ∂ ∂ ∂ ∂ ∂ Hessian(f , x , y , z) := f ( x , y , z) f ( x , y , z) f ( x , y , z) ∂z ∂y ∂y ∂y ∂x ∂y ∂ ∂ ∂ ∂ ∂ ∂ f ( x , y , z) f ( x , y , z) f ( x , y , z) ∂z ∂z ∂y ∂z ∂x ∂z
For the example above you find: 6 −4⋅y 6 ⋅ x H( x , y , z) := Hessian(f , x , y , z) → −4⋅y −4⋅x − 36⋅y 2 −1 6 0 −1
99
0 0 eigenvals(H(0 , 0 , 0)) → 37 = 6.083 − 37 −6.083
−0.079 eigenvals( H( M 2 , 0 , M 2 , 1 , M 2 , 2)) = 5.996 −6.188 Both H(0,0,0) and H(M0,2,M1,2,M2,2) are indefinite, so both are saddle points. To find the global extrema within a volume given by a number of inequalities, you can use a solve block. E.g. here is how to find the global extrema inside the unit sphere: Maximum: x := 0 y := 0 z := 1
x1 Given x + y + z ≤ 1 y1 := Maximize(f , x , y , z) f ( x1, y1, z1) = 3.424 z1 2
2
2
x1 0.767 y1 = −0.062 z1 0.639
Minimum: x := 0 y := 0 z := 1
x2 Given x + y + z ≤ 1 y2 := Minimize(f , x , y , z) f ( x2, y2, z2) = −3.424 z2 2
2
2
x2 −0.767 y2 = 0.062 z2 0.639
Both extrema are on the bounding sphere: 2
2
2
x1 + y1 + z1 = 1
2
2
2
x2 + y2 + z2 = 1
Exercises. Find local and global extrema of the following functions inside the unit ball: f ( x , y , z) := x⋅y + y ⋅ z + x ⋅ z 2
h ( x , y , z) := x + x ⋅ y + y ⋅ z +
1 4
z
2
g ( x , y , z) := x −
1
2
2 2 +y +z 2
Also, find the same extrema, but this time inside the cube centered at the origin with side lengths 2.
100
3.2.14 Extrema with side conditions Method of substitution Example 1: Find the point on the plane 2x+y-z-5=0 that lies closest to the point (1,1,2). Solution: The function to minimize is f ( x , y , z) := ( x − 1 ) 2 + ( y − 1) 2 + ( z − 2) 2 or you may as well take the square: u ( x , y , z) := (x − 1) 2 + (y − 1) 2 + (z − 2) 2 2⋅x + y − z − 5 = 0
From
you find
z = 2⋅x + y − 5
Substituting this into u you find 2
2
v ( x , y) := u(x , y , 2x + y − 5) → (x − 1) + (y − 1) + (2 ⋅ x + y − 7)
2
Then find the partial derivatives of first and second order: v x(x , y) := v y(x , y) :=
∂ ∂x ∂ ∂y
v xy(x , y) :=
v ( x , y) → 10⋅x − 30 + 4 ⋅ y v xx(x , y) := v ( x , y) → 4 ⋅ y − 16 + 4 ⋅ x v yy(x , y) :=
∂ ∂y
v x(x , y) → 4
∂ ∂x ∂ ∂y
v x(x , y) → 10 v y(x , y) → 4
2
v xx(x , y)⋅v yy(x , y) − v xy(x , y) → 24
Then you can find the stationary points: v x(x , y) = 0 7 5 solve , x , y → 3 3 v y(x , y) = 0 This must be a minimum because of the results above for the second order derivatives. The minimum value is:
8 7 5 v , → 3 3 3
Method of Lagrange Multipliers Example 2. Find the greatest and the smallest value of the function f ( x , y) := x⋅y on the ellipse: Solution:
Define
g ( x , y) :=
x
2
8
+
y
2
2
= 1
x2 y2 + − 1 F( x , y , λ ) := f ( x , y) − λ⋅g ( x , y) 2 8 101
Fx( x , y , λ ) :=
∂
Fy( x , y , λ ) :=
∂
∂x ∂y
F( x , y , λ ) → y −
1 4
⋅ x⋅ λ
F( x , y , λ ) → x − λ⋅y Then find the stationary points:
2 Fx( x , y , λ ) = 0 −2 Fy( x , y , λ ) = 0 solve , x , y , λ → −2 g ( x , y) = 0 2
−1 2 1 −2 −1 −2 You find 1
2
f ( 2 , 1) = 2
f (−2 , −1) = 2
corresponding to positive λ=2
f (−2 , 1) = −2
f ( 2 , −1) = −2
corresponding to negative λ=-2
(2,1) and (-2,-1) are maxima, while (-2,1) and (2,-1) are minima.
Mathcad's built-in optimization functions You can also use the Maximize and Minimize functions: x := 0 y := 0 T
Given
g ( x , y) = 0 x ≥ 1
Maximize(f , x , y) = ( 2 1 )
Given
g ( x , y) = 0 x ≥ 1
Minimize(f , x , y) = ( 2 −1 )
Given
g ( x , y) = 0 x ≤ −1 Maximize(f , x , y) = ( −2 −1 )
Given
g ( x , y) = 0 x ≤ −1 Minimize(f , x , y) = ( −2 1 )
T
T
T
You can draw level curves of the two functions to illustrate the problem: X0 := CreateMesh(f , −4 , 4 , −4 , 4 , 40, 40) 0 X1 := CreateMesh(g , −4 , 4 , −4 , 4 , 40, 40) 0
102
3.2.15 Linear optimization The functions Maximize and Minimize can be used very effectively to solve
linear optimization problems
Example 1.
Find the maximum of the function f ( x , y) := 2⋅x + 3y subject to the
constraints: 5x + 3y ≤ 15, 3x + 6y ≤ 18, x ≥ 0 , y ≥ 0 Solution: Define guess values: x := 0 Given
y := 0
and use a solve block:
5x + 3y ≤ 15
3x + 6y ≤ 18
x≥0 y≥0
Then you get the solution by simply using the Maximize function:
x1 := Maximize(f , x , y) x1 = 1.714 y1
y1 = 2.143
f ( x1, y1) = 9.857 Example 2.
Maximize the function f ( x , y , z , u) := 29x + 45y subject to the constraints
2x+8y+z=60, 4x+4y+u=60, x,y,z,u≥0 Solution: Define guess values: x := 20 y := 20 z := 20 u := 20 and use a solve block: Given
2x + 8y + z = 60
4x + 4y + u = 60 x ≥ 0 y ≥ 0 z ≥ 0 u ≥ 0
x2 10 x2 y2 := Maximize(f , x , y , z, u) y2 = 5 z2 z2 0 u2 u2 0
f ( x2, y2, z2, u2) = 515
Exercises. 1. Maximize f ( x , y , z) := 4x + y + 2z subject to x ≥ 0 , y ≥ 0 , z ≥ 0 , x + y + z ≤ 1 , x + y − z ≤ 0. 2. Maximize g ( x , y , z, u) := 4x + y + 3z + 2u subject to x , y , z, u ≥ 0 , 10x + 15y + 30z + 6u ≥ 30, 8x + 10y + 20z + 5u ≤ 40
103
3.3 Integration 3.3.1 Indefinite integrals Mathcad has a built-in operator that returns the indefinite integral (or antiderivative) of a function, which can be accessed both from SmartMath and from the menu system. Using the menu system, you start by writing just the expression to integrate. Then you place the cursor on the x (or whatever variable you have), and choose Symbolics - Variable- Integrate from the menu. Then you get the indefinite integral below or to the right of it. You choose between these two possibilities using the menu choice Symbolics Evaluation Style. Example 1 1
1 asin ⋅x 2
by integration, yields
4−x
2
There is also a button for indefinite integrals on the symbolic toolbar. Press the button to get the calculus menu, and then press the button Alternatively, type [Ctrl]I. In both cases, you get the symbol
⌠ ⌡
d
where you just fill in the placeholders. Then you either press [Ctrl][Period] to get symbolic evaluation in SmartMath, or you choose Symbolics - Evaluate Symbolically from the menu system. Remember to have the whole integral inside the editing frame. The two methods give the same result, but look a little different: ⌠ cos( x ) dx → sin( x ) ⌡
⌠ cos( x ) dx ⌡
yields
sin( x )
The first method method has the advantage that you can define a define a function and then integrate: 1 ⌠ ⌠ f ( x ) := yields f ( x ) dx f ( x ) dx 2 9−x ⌡ ⌡ ⌠ 1 which is not useful ,but f ( x ) dx → asin ⋅x does work. ⌡ 3 104
Sometimes Mathcad gives the result in terms of a functions that is defined in
the symbolic Mathcad, but which is unknown to numerical Mathcad:
⌠ ⌡
sin( x ) x
Si Si( 1 ) =
dx → Si( x )
(Function not defined above) but
Si(1) float, 20 → .94608307036718301494
Some non-elementary integrals are known both numerically and symbolically:
⌠ ⌡
2 π
( 2) dt → erf( t )
erf(1) = 0.842700792949715
⋅exp −t
erf(1) float , 20 → .84270079294971486934
Exercises Integrate the following functions using one of the methods mentioned above: Basic primitive functions: x
1
2
x
1 sin( x )
1 cos( x )
2
a +x
1 2
1
exp( x )
2
1
2
x +a
2
4−x
2
cosh( x )
cos( x )
sin( x )
sinh( x )
2
Rational functions: 3
2
5x + 3
2x − 4x − x − 3
x + 2x − 3
x − 2x − 3
2
2
−2x + 4
( x2 + 1) ⋅(x − 1)2
Notice that you don't have to worry about the conversion to partial fraction. Mathcad knows that that's what has to be done. Algebraic functions: 1
1
x 2
2
2 −x + 3 ⋅ x + 2 x ⋅ x − 9
2
x + 3⋅ x − 5
4−x
2
Exponential and logarithmic functions
( x2 + 5x) ⋅exp( 2x)
exp(2⋅x) 2 + exp(2⋅x) 4
x ⋅ln( x )
3
4
x ⋅ln( x )
3
Try to use the code word collect,exp(2 x) on this one. 1 Use collect,x on this one ⋅cos(ln( x )) x 105
Trigonometric and inverse trigonometric functions
( 2⋅sin(x)2 − sin(x) + 1) ⋅cos( x)
2
2
tan( x ) + 3⋅tan( x ) + 2
x ⋅asin( x )
Miscellaneous cos( x )
asin( x )
1 + sin( x ) cos( x ) x
1−x
1
2
1 + sin( x )
2 x
x e
2
x + 2x + 2 cos( 2x ) − 1
1 1+ x
cos( 1 − x )
cos( 2x ) + 1
x 4
x +1 1
(
3
x ⋅ 2x + 1
)2
3.3.2 Definite Integrals
Mathcad's operators for definite integration
There are several ways to get a definite integral with symbolic Mathcad. Press the calculus palette button
and then
The keyboard shortcut is & (or [Shift][6] on the alphanumeric keyboard) Press = if you want a numeric result, and [Ctrl]+[Period] if you want a symbolic result.If you want a numeric result from the symbolic version, use the keyword float n, where n is the number of decimal digits you want to have. a :=
π
Numerical value:
4
Symbolic value:
a
⌠ x⋅sin( x ) dx = 0.151746413916752 ⌡0
a
⌠ 1 1 x⋅sin( x ) dx → ⋅ 2 − ⋅π⋅ 2 ⌡0 2 8 You can also write the integral and then choose Symbolics - Evaluate Symbolically or press Shift/F9: π
yields
⌠4 x⋅sin( x ) dx ⌡0
1 2
⋅ 2−
1 8
⋅π⋅ 2
a
but you cannot evaluate
⌠ x⋅sin( x ) dx ⌡0
a
yields
⌠ x⋅sin( x ) dx ⌡0
because the definition of a will be invisible from the Symbolics menu. 106
Symbolic evaluation with numerical approximation: a
⌠ 1 1 x⋅sin( x ) dx → ⋅ 2 − ⋅π⋅ 2 = 0.151746413916752 ⌡0 2 8
or
a
⌠ x⋅sin( x ) dx float, 30 → .151746413916751743523859238347 ⌡0
or
a
⌠ (The factor 1.0 will 1.0⋅x⋅sin( x ) dx → .15174641391675174351 force a numerical result.) ⌡0 The precision of a numeric result can be improved by changing the value of the variable TOL to something other than 10-3, which is the default value: J(Tol) :=
( − 3) = 5.00000000006814 −7 J( 10 ) = 5.00000000000001 −1 J( 10 ) = 5.00000005437786
TOL ← Tol
Compare with:
J 10
5
⌠e 1 dt t ⌡1
5
⌠ e 1 dt → 5
t ⌡1
Exercises. Compute the following integrals, numerically and/or symbolically: 9
⌠ ln( e ⌡0
3
⌠ ⌡0
5
⌠ (3x − 7) dx (x − 1)⋅(x − 2)⋅(x − 3) ⌡4
) dx
x
( 16 + x )
π
3
⌠ ⌡2
π
64
1 5
dx
( x2 − 1) 2
⌠2 x 3⋅cos( 2x ) dx ⌡0
⌠ ⌡0
1 1+ x
dx
1 2
( 2) dx
2x⋅asin x
0
107
−3 2
⌠2 cot( x ) ln(e⋅sin( x ) dx π ⌡ 6
⌠ ⌡
2
1
⌠ x ⋅ 1 − x dx ⌡0
dx
3.3.3 Improper Integrals b
⌠ There are two kinds of improper integrals f ( x ) dx ⌡a
Either
1. a=-∞ or b=∞ or both or 2. f(x) has a discontinuity and is unlimited in one or more points of the interval [a,b] As a general principle: Try to use symbolic evaluation for improper integrals. Examples: ∞
Type 1):
∞
⌠ sin( x ) dx → 1 ⋅π x 2 ⌡0
⌠ 1 − cos( x ) 1 dx → ⋅π 2 2 x ⌡0
1
Type 2):
⌠ 1 dx → 1 (x + 1)⋅ 1 − x 2 ⌡0
1
⌠ cos( x ) dx → ∞ 2 x ⌡0
It may happen, of course, that SmartMath cannot find any antiderivative: ⌠ −x ⌠ (− x) dx x dx → x ⌡ ⌡
and therefore no definite integral:
In version 8, you can compute such integrals numerically: ∞
⌠ −x x dx = 1.99545634417723 ⌡0 You have the possibility to choose between a certain number of integration algorithms by clicking on the integral sign with the right mouse button. The default is Autoselect, but then there is also Romberg, Adaptive, Infinite Limit and Singular endpoint. Exercises. Compute the following improper integrals: ∞
⌠ 1 dx x2 + 1 ⌡0
1
⌠ 1 dx x ⌡0
1
⌠ x ⋅ ln( x ) dx ⌡0
108
∞
⌠ 2 2x⋅e− x dx ⌡− ∞
3.3.4 Plane areas The area between the graph of a function u=f(x) and the x axis If the areas below the x-axis are counted as negative, then the area between the curve and the x axis is simply the definite integral: Otherwise, you have to integrate the absolute value of the function: Example 1. Find the area bounded bounded by the x-axis and the graph of the function f from x=0 to x=7: f ( x ) := 8⋅cos( x ) −
x
Here is the graph for
5
x := 0 , 0.1.. 7
10
f ( x) 0 f ( x)
10
0
2
4
6
x
I have used the trace type stem on the last trace to get the shading. If the areas below the x-axis is counted as negative, the area from x=0 to x=7 is: 7
⌠ 49 float, 20 → .3558927897503127232 f ( x ) dx → 8⋅sin( 7 ) − ⌡0 10 or numerically: 7
⌠ f ( x ) dx = 0.355892789750311 ⌡0 If the area below the x-axis is counted as positiv, the area from 0 to 7 is: 7
⌠ ⌡0
f ( x ) dx = 36.4321069196977
The symbolic processor will have difficulties with the absolute value, however (try!). 109
The area between the graphs of two functions To find the area between two curves, you may have to combine a root finding function with integration. Also, the shading is little more difficult in this case. You may use the following function: (x ← a)⋅ dx ←
Shade(f , g , a, A, N) :=
A − a N
for n ∈ 0 .. N
( X0 , n ← x) ⋅ ( X1 , n ← x) ( Y0 , n ← f ( x) ) ⋅ ( Y1 , n ← g( x) ) x ← x + dx
X Y Example 2. Find the area bounded by the graphs of the functions f and g: f ( x ) := 5⋅cos( x )
g ( x ) := 3 −
1 5
⋅x
2
x := 0 , 0.1.. 5
t := 0 , 0.1.. 5
First, find the points of intersection: TOL := 10− 13 x := 1
r1 := root(f ( x ) − g ( x ) , x) r1 = 0.973923271295714
x := 4.5
r2 := root(f ( x ) − g ( x ) , x) r2 = 4.50059493837989 r2
⌠ Then the area is: g ( x ) − f ( x ) dx = 13.5879540097218 ⌡r1
X := Shade(f , g , r1 , r2, 40) Y
x := 0 , 0.1.. 5
5
Y f ( x) 0
1
2
3
g( x)
5 X, x, x
110
4
5
Exercises. Find the area between the graphs of the following pair of curves:: 1. f ( x ) := x
g ( x ) :=
1 2
⋅x
2
f ( x ) := sin( x )
2.
g ( x ) :=
x 2
+
The area bounded by a parameter curve (x(t),y(t)) b
⌠ d A( x , y , a , b) := y ( t)⋅ x ( t ) dt dt ⌡a Example x1(t) := a⋅cos a ( t)
b ( t) y1(t) := b⋅sin
A( x1, y1, 2⋅π , 0 ) → π⋅b⋅a
by Exercises. Find the areas( bounded ) ( )the following y1( ) curves:( ) Cycloid
x2(t) := t − sin( t ) y2(t) := 1 − cos( t)
Asteroid
x3(t) := cos( t)
3
y3(t) := sin( t)
3
The area bounded by a polar curve For curves given on polar form r=r(t), the area bounded by the curve and the two radii t=a and t=b is: b
1 ⌠ 2 AP(r, a , b) := ⋅ r( t ) dt 2 ⌡a Example 4 Cardioid r1(t) := 2⋅(1 − cos( t))
The area is:
AP ( r1 , 0 , 2⋅π ) → 6⋅π
Exercises. Find the area bounded by the following curves: Four leafed rose: r2(t) := sin( 2t ) Limacon.
r3( t) :=
1 2
+ cos( t)
111
1 10
3.3.5 Curve length Curves given as the graph of a function y=f(x) b
⌠ CurveLengthF(f , a , b) := ⌡a
2
1+
d f ( x ) dx dx
Example 1. The length of the graph of the cosine function f ( x ) := cos( x ) from 0 to 1 can be computed as: CurveLengthF(f , 0 , 1) = 1.12388772294552 or: CurveLengthF(cos, 0 , 1) float, 20 → 1.1238877229455250047 Exercise. Find the length of the parabola y=x2 from 0 to b and from 0 to 1.
Curves given by parametric equations For a curve given by a pair of parameter equation the arc length can be evaluated by means of one of the following formulae: b
⌠ CurveLengthP(x , y , a , b) ≡ ⌡a
2
2
d x ( t) + d y ( t) dt
dt dt
In vector formulation, where X is a vector function: b
⌠ CurveLengthV(X, a , b) := ⌡a
2
2
d X( t) + d X( t) dt 0 1 dt dt
Example 2. The length of the cycloid x ( t ) := t − sin( t) or
X( t ) :=
y ( t) := 1 − cos( t )
t − sin( t) whose graph looks like this: t := 0 , π .. 2π 32 1 − cos( t)
2 1− cos( t)
0
2
4 t−sin ( t)
112
6
8
can be computed in one of the following ways: CurveLengthP( x , y , 0 , 2π ) = 8
CurveLengthP( x , y , 0 , 2π ) simplify → 8
CurveLengthV( X, 0 , 2π ) = 8
CurveLengthV( X, 0 , 2π ) → 4 ⋅ 4 simplify → 8
Exercises. Find the lengths of the following curves: Asteroid:
Circle:
cos( t) sin( t) 3 3
X( t) :=
X( t) :=
Ellipse:
a⋅cos( t) a ( t) a⋅sin
X( t ) :=
3 cos( t) 2 sin( t)
Notice that you can have undefined parameters in the functions and still find the curve length symbolically, not numerically.
Curves given on polar form For a curve given on polar form r=r(t), the curve length is given by: b
⌠ CurveLengthPol(r, a, b) := ⌡a
2
2
r( t) +
d r( t) dt dt
Example: The length of the leminscate: r( t ) := 2a⋅(1 a + cos( t)) t := 0 ,
π 32
.. 2π can be computed as
CurveLengthPol( r, 0 , 2π ) simplify, assume, a > 0 → 16⋅a 90 120 150 2⋅(1+cos( t))
60 3
30
1 180
0
210
330 240
300 270 t
Exercises. Find the curve lengths of the following curves given in polar form: Circle:
r( t ) := a
Logarithmic spiral: r( t) := exp( aa⋅t) 113
3.3.6 Centroids and moments of inertia Centroid of plane area bounded by u=f(x) and v=g(x) ⌠ b x⋅ ( u( x) − v( x) ) dx ⌡a 1 Centroid ( u , v , a , b ) := ⋅ b b ⌠ 1 ⌠ 2 2 u( x) − v( x) dx ⋅ ( u( x) − v( x) ) dx 2 ⌡a ⌡a Example 1. Find the centroid of the plane area bounded by the graphs of the following functions: 1 2 u ( x ) := 3 − ⋅ x v ( x ) := 5 ⋅ cos( x ) x := 0 , 0.1 .. 5 5 First, find the points of intersection: t := 1 x1 := root( u ( t ) − v ( t) , t ) x1 = 0.974 t := 4.5 x2 := root( u ( t ) − v ( t) , t )
x2 = 4.501
Then compute the centroid:
c1 := Centroid( u , v , x1 , x2) c1 = 2.825 c2 c2 −1.12 Shade from section 3.3.4
X := Shade( u , v , x1 , x2 , 40) Y 5
u( x) v( x) Y
0
1
2
3
4
5
c2
x , x , X , c1
Exercises. Find the centroid of the areas bounded by the graphs of the following pairs of functions: 1 x 1 1. f1( x ) := x g1( x ) := ⋅ x 2 2. f2( x ) := sin( x ) g2( x ) := + 2 2 10 114
Moments of inertia of graph of u=f(x) with density ρ(x) b
⌠ 2 About the x-axis: 2 d ( ) I1Sx u , ρ , a , b := x ⋅ρ ( x )⋅ 1 + u ( x ) dx dx ⌡a b
⌠ 2 2 d u ( x) dx About the y-axis: I1Sy( u , ρ , a , b ) := u x ( )⋅ 1 ( ) x ⋅ρ + dx ⌡a Example 2:
ρ ( x ) := x
2
f ( x ) := 5⋅cos( x )
I1Sx( f , ρ , 0 , 4 ) = 526.227
I1Sy( f , ρ , 0 , 4 ) = 806.203
Exercise. Find the moment of inertia of the curve u ( x ) := 3 −
1 5
2 ⋅x from 0 to
5.
Moments of inertia of a plane region bounded by the graphs
of u=f(x) and v=g(x) with constant density ρ.
b
⌠ 2 I2Sx( u , v , ρ , a , b ) := ρ⋅ x ⋅(u ( x ) − v ( x )) dx ⌡a I2Sy( u , v , ρ , a , b ) :=
Example 3
b
( u( x) 3 − v( x) 3) dx
1
2 ⋅x v ( x ) := 5⋅cos( x ) cf. example. 1
ρ ⌠ ⋅ 3 ⌡a
u ( x ) := 3 −
5
I2Sx(u , v , 1 , x1, x2) = 116.236
I2Sy(u , v , 1 , x1, x2) = 62.006
Exercises. Find the moments of inertia of the plane regions bounded by the following pairs of functions. Assume that the density is 1. 1.
f1( x ) := x g1( x ) :=
1 2
⋅x
2
2.
115
f2(x) := sin( x ) g2( x ) :=
x 2
+
1 10
3.3.7 The volume of a surface of revolution Drawing a surface of revolution First, we show how to draw a surface of revolution. Suppose the axis of revolution is the x axis, and that you're given a function f. Then the surface of revolution can be drawn by means of the following function: A − a B − b XRevSurf (f , a , A, M , b , B , N) := dx ← ⋅ dt ← M N for m ∈ 0 .. M x m ← a + m⋅dx for n ∈ 0 .. N tn ← b + n⋅dt Xm , n ← x m
Ym , n ← f ( x m) ⋅cos( tn)
Zm , n ← f ( x m) ⋅sin( tn)
X Y Z Example 1. f ( x ) := x 2
M 0 := XRevSurf ( f , 0 , 1 , 10, 0 , 2⋅π , 40)
you get the following graph, using Insert - Graph - Surface Plot or [Ctrl]2
116
If the axis of revolution is the y axis, you define similarly: YRevSurf (f , a , A, M , b , B , N) :=
dx ← A − a ⋅ dt ← B − b M N for m ∈ 0 .. M x m ← a + m⋅dx for n ∈ 0 .. N tn ← b + n⋅dt Xm , n ← x m⋅cos( tn) Ym , n ← x m⋅sin( tn) Zm , n ← f ( x m )
X Y Z Example 2. Revolve the parabola from example 1 about the y axis. M20 := YRevSurf ( f , 0 , 1 , 10, 0 , 2⋅π , 40)
The volume of a surface of revolution Now, the volume of a solid of revolution about the x axis can be evaluated by the following definition : b
b ⌠ ⌠ 2 2d VolXR(f , a , b) := π⋅ f ( x ) dx VolXRP(x , y , a , b) := π⋅ y ( t) ⋅ x ( t) dt ⌡a dt ⌡a
117
Here is a variation where f is a vector function: b
⌠ 2d VolXRevSolidPV(f , a, b) ≡ π⋅ ( f ( t) 1) ⋅ f ( t ) 0 dt dt ⌡a Example 3 f ( x ) := a⋅x a
2
VolXR(f , 0 , 1) →
1 5
⋅π⋅a
2
For a surface of revolution about the y axis, the volume can be evaluated by means of the following functions: b
⌠ 2 d VolYR(U, a , b) := π⋅x ⋅ U( x ) dx dx ⌡a b
⌠ 2d VolYRP(x , y , a , b) := π⋅x ( t ) ⋅ y ( t) dt dt ⌡a b
⌠ In vector notation: VolYRV(U, a, b) := π⋅ ( U( t) 0) 2⋅ d U( t) 1 dt dt ⌡a Example 4. Half sphere 2
2
f1( x ) := r − x VolYR(f1 , r, 0)
simplify
2 3 → ⋅r ⋅π assume, r > 0 3
Exercises. Find the volumes of the following surfaces of revolution about the x axis: 1. A sphere generated by f1( x ) := r2 − x 2 or 2. A cylinder generated by 3. A cone generated by
U1( t ) :=
f2( x ) := r f3( x ) :=
r h
r⋅cos( t) rr⋅sin( t)
⋅x
Also, find the volumes of the following surfaces of revolution about the y axis: 4. Paraboloid: f4( x ) := x 5. Cone: 6. Half sphere: f6( t) :=
r⋅cos( t)
r ( t) r⋅sin 118
f5( x ) :=
h r
⋅x
3.3.8 Surface area of a surface of revolution Surfaces of revolution about the x axis For a surface of revolution about the x axis, the formula for the surface area can be written: b
⌠ 2 d AreaXR(U, a , b) := 2⋅π⋅ U( x )⋅ 1 + U( x ) dx dx ⌡a Parametric versions: b
⌠ AreaXRP(x , y , a , b) := 2⋅π⋅ y ( t )⋅ ⌡a
2
2
d x ( t) + d y ( t) dt dt dt
Vector formulation: b
⌠ AreaXRV(X, a , b) := 2π⋅X1⋅ ⌡a Examples: f ( x ) := x 2 AreaXR(f , 0 , 1) →
9 16
AreaXRV( g ( t) , 0 , π )
2
2
d X + d X dt 0 1 dt dt
(X is an expression in t.)
cos( t) sin( t)
g ( t) := r⋅ r ⋅π⋅ 5 +
1 32
⋅π⋅ln( −2 + 5 ) = 3.81
assume, r > 0 2 → 4⋅π⋅r simplify
Exercises. Find the surface area of the surface generated by the following functions: Cone:
f2( x ) :=
h r
⋅x
Sphere:
2
f3( x ) := r − x
119
2
Surfaces of revolution about the y axis. Analogous to the formulae above we can define the following functions for the surface area for a surface of revolution about the y axis: b
⌠ 2 d AreaYR(U, a , b) := 2π⋅x⋅ 1 + U( x ) dx dx ⌡a b
⌠ AreaYRP(x , y , a , b) := 2⋅π⋅x ( t )⋅ ⌡a
2
2
d x ( t) + d y ( t) dt dt dt
b
⌠ AreaYRV(X, a , b) := 2π⋅X0⋅ ⌡a f ( x ) := x
Examples:
AreaYR(f , a , b) → x ( t ) := r⋅cos ( t) r
AreaYRP x , y , R( t) :=
1 6
2
d X + d X 0 1 dt dt
2
Notice that X is an expression, dt not a function here.
2
(
⋅ 1 + 4⋅b
)
2
3 2
⋅π −
1 6
(
⋅ 1 + 4⋅a
)
2
3 2
⋅π
y ( t ) := r⋅sin r ( t) −π π , 2 2
assume, r > 0 2 → 4⋅π⋅r simplify
r⋅cos( t) AreaYRV R( t) , −π , π assume, r > 0→ 4⋅π⋅r2 2 2 simplify r ( t) r⋅sin
Exercises. Find the surface area generated by the following functions: 2
f2( x ) := r − x
2
f5( t ) :=
cos( t) 3 sin( t) 3
120
f6( t ) :=
cos( t) 2⋅sin( t)
3.3.9 Double integrals Double integrals work both numerically and symbolically:.: Example 1. The volume of a prism whose base is the rectangle [0,2] x [0,1] and whose top lies in the plane z=4-x-y 2
1
2
⌠ ⌠ 4 − x − y dy dx = 5 ⌡0 ⌡0
1
⌠ ⌠ 4 − x − y dy dx → 5 ⌡0 ⌡0
Example 2. The volume of a prism whose base in the xy-plane is bounded by the lines y=0, y=x and x=1 can be computed as: 1
x
1
⌠ ⌠ (3 − x − y) dy dx → 1 ⌡0 ⌡0
or
1
⌠ ⌠ (3 − x − y) dx dy → 1 ⌡0 ⌡y
Change of coordinates Suppose you have a coordinate transformation given as a two dimensional vector expression U in the new coordinates u,v. Then the Jacobian matrix and determinant can be defined as:
∂ (U( u , v)) 0 ∂u Jac(U, u , v) := ∂ (U( u , v)) 1 ∂u
∂v ∂ (U( u , v)) 1 ∂v ∂
(U( u , v)) 0
JacDet(U, u , v) := Jac(U, u , v) For instance, for polar coordinates: U( u , v) :=
u⋅cos( v ) u⋅sin( v )
JacDet(U, u , v)
factor
→u simplify
So, for instance, to find the area of the lemniscate r 2 = 4 cos 2v, you integrate π
⌠4 ⌠ 4⋅ ⌡0 ⌡0
4⋅ cos( 2⋅ v)
u du dv → 4
Exercises. Compute the following double integrals: π
x
⌠ ⌠ x⋅sin( y ) dy dx ⌡0 ⌡0
2
2x
⌠ ⌠ (4x + 2) dy dx ⌡0 ⌡x2 121
3.4 Vector calculus 3.4.1 Vectors in Rn Vectors can be of any finite dimension with Mathcad. Mathcad knows the scalar product of two n dimensional vectors and the norm of an dimensional vector. Hence you can define the angle between n dimensional vectors: It also knows the vector (cross) product of two three dimensional vectors. All the vector operations are on the Matrix toolbar, which access by pressing the matrix icon on the Math toolbar. The matrix toolbar looks like this: The angle between vectors is not built-in , but can be defined as follows: ang(x , y) := acos
Example
1 2 x := −1 4 5
x = 6.856
0 1 y := 3 −1 2
x⋅ y
x ⋅ y
3 z := 2 −1
0 u := −2 1
0 z × u = −3 ang(x , y) = 79.146 deg −6
y = 3.873
x − y = 7.211
x⋅ y = 5
You can also use the symbolic processor: x → 47
x − y → 2 ⋅ 13 ang(x , y) → acos
1
141 T
Notice however that if V := ( a b c )
V →
0 z × u → −3 −6
y → 15 x ⋅ y → 5
( a ) 2 + ( b ) 2 + ( c ) 2
This means that x ⋅ x
1 2
⋅ 47⋅ 15
then
while
(
2
2
V⋅ V → a + b + c
)
2
1 2
should be used instead of |x| in symbolic calculations.
122
3.4.2 Vector valued functions R → R3 You might want define a vector valued function by describing the individual component functions: m := 3
i := 0 .. m − 1
f ( t) i := t
i
(Not defined.)
As you see, Mathcad doesn't accept indexed functions in definitions. You have to define a vector valued function in a vector:
sin( t) 2 P( t ) := t 3 t Once you have defined the function, however, Mathcad does understand the nth component of it: P( t) 0 → sin( t) P( t) 1 → t
2
P( t) 2 → t
3
To differentiate a vector function, you would probably like to make a definition like this: T
D1(f , t) :=
d f ( t) d f ( t) d f ( t) 0 1 2 dt dt dt
(As before, I use transposition to save space.)
This will work fine numerically and symbolically:
−0.416 D1(P, 2) = 4 12
0.995 D1(P, 0.1) = 0.2 0.03
cos( t ) D1(P, t) → 2 ⋅ t 3 ⋅ t2
However, D1(P,t) is an expression, not a function, so it won't work to compute D1 ( D1( P , tt)) , tt)) → D1(D1(P,
(Error: No symbolic result was found.)
There is a way around this: Symbolic Mathcad - as Maple - understands expressions as arguments as well as functions, as you have seen before. So if you define: T cos( t) d d d D1(f , t) := f 0 f1 f2 then D1(P( t ) , t) → 2 ⋅ t and dt dt dt
3 ⋅ t2 −sin( t) D1(D1(P( t ) , t) , t) → 2 6 ⋅ t You may even define: 123
T
D( f ) :=
d f d f d f where it is understood that f is a vector 1 2 0 dt dt dtt whose components are expressions in t.
cos( t) D( P( t )) → 2 ⋅ t 3 ⋅ t2
−sin( t) D( D( P( t))) → 2 6⋅ t
But then you have to use t as independent variable in all your functions, which seems to be a mild restriction. To be able to use D(D(P(t))) as a function and find function values, you define, say:
−sin( t) −sin( 1 ) 2 DP ( 1 ) → 2 DP ( t) := D( D( P( t ))) → 6⋅ t 6 For the second derivative you define likewise:
For the example:
d2 d2 d2 D2( u ) := u0 u1 u 2 2 2 2 dt dtt dt
−sin( t) P2( t ) := D2(P( t )) → 2 6⋅ t
More generally, you can define:
P2( 2 ) = ( −0.909 2 12 )
T
T
T
dn dn dn Dn(u , n) := u0 u1 u n n n 2 dt dtt dt
cos( t) P5( t ) := Dn(P( t ) , 5) → 0 0 T
P5( 2 ) = ( −0.416 0 0 ) Exercise. Find the first three derivatives of the following vector functions:
sin( t) cos( t) sin( 2t) −t e 2 cos(3t 2 sin( 3t)
t + 1 t2 − 1 2t 3t + 1 3t t2
1 + t t2 2 t3 3
sec( t) tan( t) 4 3 t
2 ln(t + 1) 2 t 2 t 2 T
3 3 4 ⋅(1 + t) 2 4 ⋅(1 − t) 2 1 t 9 3 9
124
3.4.3 Differential geometry of space curves You can define the elementary functions from the differential geometry of space curves in the following way: The first three derivatives: T
D1( U) :=
d U d U d U 1 2 0 dt dtt dt
T
d2 d2 d2 D2( U) := U0 U1 U2 2 2 2 dt dtt dt
T
d3 d3 d3 D3( U) := U0 U1 U2 3 3 3 dt dtt dt
Length of tangent vector, speed: V( U) := D1( U) ⋅ D1( U) The derivative of the speed V:
DV( U) :=
The unit tangent vector:
UT( U) :=
The principal normal vector:
d V( U) dtt 1 V( U)
D1 U) ⋅D1(
DV U) N( U) := D2( U) − UT( U)⋅DV(
The unit principal normal vector: UN( U) :=
1 N( U) ⋅ N( U)
⋅ N( U)
The unit binormal vector:
UN U) UB( U) := UT( U) × UN(
Curvature:
κ ( U) :=
W ← D1(U) × D2( U) W⋅ W V U) V(
Torsion:
τ ( U) :=
3
W ← D1(U) × D2( U) D1( U) ⋅( D2(U) × D3 D3( U)) W⋅ W
125
Radius of curvature:
ρ κ ( U) :=
Radius of torsion:
ρ τ ( U) :=
1 κ U) κ( 1 τ ( U)
Example. A circular helix:
3 cos( t) U := 3 sin( t ) 4⋅ t Length of tangent vector, speed: V( U) simplify → 5 The derivative of the speed V: DV( U) → 0 The unit tangent vector:
T
UT( U) simplify →
−3 ⋅sin( t) 3 ⋅cos( t) 4 5 5 5
T
The principal normal vector: N( U) simplify → ( −3⋅cos( t) −3⋅sin( t ) 0 ) T
The unit principal normal vector: UN( U) simplify → ( −cos( t ) −sin( t) 0 ) T
The unit binormal vector: UB( U) simplify → Curvature:
4 ⋅sin( t) −4 ⋅cos( t) 3 5 5 5
κ(U) simplify →
3 25 4
Torsion:
τ (U) simplify →
Radius of curvature:
ρ κ (U) simplify →
25
ρ τ (U) simplify →
25
Radius of torsion:
Exercise. Repeat the example on the conic helix
t⋅cos( t) R( t) := t⋅sin( t ) t 126
25 3
4
3.4.4 Vector fields and scalar fields A vector field is a function R3 → R3. A scalar field is a function R3 →R. To define differential operators on vectors and scalar fields, you have the same options as for space curves: You can use functions as arguments for numerical definitions or expressions as arguments for symbolic definitions. You can include or not include the names of the independent variables. I choose the following convention: Functional arguments are expressions. The independent variables are assumed to be x,y and z, and they are not included in the list of arguments to the differential operators. With this convention, you can define the standard differential operators for vector and scalar fields as follows: Laplacian: Divergence: div( u ) :=
d u + d u + d u ∆ ( u) := d 2 u + d 2 u + d 2 u 2 1 2 0 2 2 dy dzz dy dzz dx dx
Gradient:
T
d d d grad( u ) := u u u dx dy dzz
Curl:
T
curl( u ) := Examples:
(
d u − d u d u − d u d u − d u 1 0 2 1 0 2 dz dz dx dx dyy dy
x 2⋅y y⋅ z grad(x⋅y⋅z) → x ⋅ z div x ⋅ z → 2 ⋅ x⋅y + y x⋅ y y ⋅ z U := xx⋅y⋅z
)
2
2
∆ x ⋅sin(x + y)⋅z → 2⋅sin(x + y)⋅z + 4 ⋅ x⋅cos(x + y)⋅z − 2 ⋅ x ⋅sin(x + y)⋅ Exercises. 1. Find the gradient and Laplacian of the following scalar fields: 2
2
2
x + y − 2z + z⋅ ln( x )
(
3
2
)
2
2z − 3 ⋅ x + y ⋅z + atan(x⋅z)
2. Find the divergence and curl of the following vector fields:
y − x z − y y − x
x 2 −2x ⋅ y 3x⋅z
6x 2 + 2x⋅y 2y + x 2⋅z 2 3 4x y 127
5x 3 + 12x⋅y 2 y 3 + ey⋅sin( z) 3 y 5z + e ⋅cos( z)
y x⋅ y −z
3.4.5 Line integrals We repeat the definition of the derivative of a vector function: T
D( r) :=
d r d r d r 1 2 0 dt dt dtt
If F is a vector expression in the variable t, then you can integrate F from a to b by the definition: T
b b ⌠b ⌠ ⌠ VecInt(F, a , b) := F0 dt F1 dt F2 dt ⌡a ⌡a ⌡a
Example:
T
T 1 1 1 2 3 VecInt ( t t t ) , 0 , 1 → 2 3 4
If F is a scalar expression in the independent variables x,y and z and r is a three dimensional vector expression in t, then the value of F at the point r can be evaluated using the function Val below. This definition can be extended to vector expressions. Val(F, r) ≡
lim
lim
z→r2
y→r1
lim F x→r0
ValV(F, r) := ( Val( F0 , r) Val( F1 , r) Val( F2 , r) )
T
Example:
9⋅ t2 3 ⋅xx 2 + 6⋅ y t cos( t ) 2 A := −14⋅y⋅z r1 := t r2 := sin( t ) ValV(A, r1) → −14⋅t5 3 2 t 7 20⋅x⋅z t 20⋅t Integrals of the kind ∫F(r) dr, where F is a vector field. b
⌠ Int1(F, r, a, b) := ValV(F, r)⋅D( D r) dt ⌡a With the example above: 3
7
Int1(A, r1, a, b) → 3⋅b − 4 ⋅ b + 6 ⋅ b
10
128
3
7
− 3⋅ a + 4⋅ a − 6⋅ a
10
Integrals of the kind ∫φ(r)|dr|, where φ is a scalar field ⌠
Int2( φ , r, a , b ) :=
b
Val( φ , r) ⋅ D( r) ⋅ D( r) dt
⌡a
Int2 x ⋅y⋅z , r2, 0 ,
Example
2
π
2
→ ⋅ 2 9 2
Integrals of the kind ∫ F × dr Int3(F, r, a, b) := VecInt(ValV(F, r) × D D( r) , a , b) T
Int3(A, r1, 0 , 1) →
Example
−349 −29 41 36 10 6
Integrals of the kind ∫ φ dr with φ a scalar field Int4( φ , r, a , b ) := VecInt( Val( φ , r) ⋅D D( r) , a , b )
(
2
Int4 x ⋅y⋅z, r1 , 0 , 1
Example
)
T
→
1 2 3 8 9 10
Example to illustrate Green's theorem in the plane:
3 ⋅xx + 4 ⋅ y 2⋅cos( t) F := 2 ⋅ x − 3 ⋅ y r := 2⋅sin( t ) 0 0
u := 4 − x 2
⌠ ⌡− 2
Int1( F, r, 0 , 2⋅π ) → −8⋅ π
2
u
⌠ d F − d F dy dx → −8⋅π 1 0 dx dy ⌡− u
Exercises.
( y − x 2) ( z − y 2) ( x − z2) over T
1. Find the integral of the vector field
(
the curve r= t t 2 t3
)
T
, 0≤t≤1 using Int1.
2. Integrate f(x,y,z) = x - 3y2 + z over the line segment joining the origin and the point (1,1,1) using Int2. Also integrate over the straight line from (0,0,0) to (1,1,0) and then from (1,1,0) to (1,1,1). 3. Compute ∫ F × dr where F is the vector field from exercise 1 over the to curves described in exercise 2.
129
3.4.6 Surface integrals We repeat some definitions from the previous subsection: D, Val, ValV, len, div
If S is a vector of expressions containing the variables u and v we define the partial derivatives: T
T
∂ ∂ ∂ Du( S) := S0 S1 S2 ∂u ∂u ∂u
∂ ∂ ∂ Dv( S) := S0 S1 S2 ∂v ∂v ∂v
In the function below, U is a scalar expression in u,v. The double integrals are performed either with u. first and then v (σ=0) or with v first and the u(σ=1). A, a, B, b are scalar expressions possibly involving one of the variables v,u. B
A
A
B
⌠ ⌠ return U du dv if σ = 0 ⌡b ⌡a
DInt( U, a , A, b , B , σ ) :=
⌠ ⌠ return U dv du if σ = 1 ⌡a ⌡b Example 1 DInt(u + v , 0 , 1 − v , 0 , 1 , 0) →
1 3
DInt(u + v , 0 , 1 , 0 , 1 − u , 1) →
1 3
After this you can double integrate a vector function in x,y or u,v component wise: DInt( U0 , a , A, b , B , σ )
VDInt( U, a, A, b , B , σ ) := DInt( U1 , a , A, b , B , σ )
DInt( U , a , A, b , B , σ ) 2
Example 2
a2⋅u⋅v U := a⋅ u⋅v 2 3 u ⋅v
1 ⋅a2 24 1 2 T VDInt(U, 0 , 1 , 0 , 1 − u , 1) → ⋅a 24 T
VDInt(U, 0 , 1 − v , 0 , 1 , 0) →
1 1 ⋅a 60 120 1
60
⋅a
1
120
The normal to a surface. A surface can be given either as the graph of a scalar function of two variables x,y or as the graph of a three dimensional vector function of two variables u,v. In the function below, U is either a scalar expression in the variables x,y or a vector expression in the variables u,v: N( U) := Du(U) × Dv Dv( U) 130
Example 3. cos( v)
−cos( v ) N( U) simplify → −sin( v ) U := sin( v ) u 0 Integrals of the type ∫∫SF·dS
SInt1( F, S, a , A, b , B , σ ) := DInt( ValV(F, S) ⋅N( N S) , a , A, b , B , σ ) T
Example 4. The integral of F := ( x y z ) over the cylinder x2+y2≤1, 0≤z≤1. On the curved part of the cylinder, you have: cos( v ) −cos( v ) S :=
sin( v ) Here u
N( S) →
−sin( v ) 0
This points inwards, to you have take the negative of SInt1: −SInt1( F, S, 0 , 2π , 0 , 1 , 0 ) → 2⋅π T
On the bottom base surface you have z=0: SB := ( u⋅cos ( v ) u⋅sin( v ) 0 ) u SInt1( F, SB, 0 , 1 , 0 , 2π , 0 ) → 0 T
On the top surface you have z=1: ST := ( u⋅cos ( v ) u⋅sin( v ) 1 ) u SInt1( F, ST, 0 , 1 , 0 , 2π , 0 ) → π
Hence the surface integral is 3·π. This could also have been done using Gauss' theorem. You find div( F) → 3 The integral of this constant over the cylinder is the constant times the volume π of the cylinder, i.e. 3π as before.
Integrals of the type ∫∫Sf dS, where f is a scalar field
SInt2( f , S, a, A, b , B , σ ) := DInt( Val(f , S)⋅len(N( N S)) , a , A, b , B , σ )
Example 4, continued Suppose: Then the integral of f over the curved surface of the cylinder is
On the bottom base surface you have On the top surface you have: In total, the integral is 0.
2
φ := x ⋅y⋅z
SInt2( φ , S, 0 , 1 , 0 , 2π , 0 ) → 0
SInt2( φ , SB, 0 , 1 , 0 , 2π , 0 ) → 0
SInt2( φ , SR, 0 , 1 , 0 , 2π , 0 ) → 0
Integrals of the type ∫∫SF×dS, where F is a vector field
SInt3( U, S, a , A, b , B , σ ) := VDInt( ValV(U, S) × N( N S) , a, A, b , B , σ )
Example 4 continued. For the curved part of the cylinder you find: 131
SInt3( U, S, 0 , 1 , 0 , 2π , 0 ) → ( 0 0 0 ) At the bottom:
T
SInt3( U, SB, 0 , 1 , 0 , 2π , 0 ) → ( 0 0 0 ) At the top:
T
SInt3( U, ST, 0 , 1 , 0 , 2π , 0 ) → ( 0 0 0 ) So, the total integral is 0.
T
Integrals of the type ∫∫S φ dS, where φ is a scalar field SInt4( φ , S, a, A, b , B , σ ) := VDInt( Val( φ , S) ⋅N( N S) , a , A, b , B , σ )
Example 4 continued. On the curved part of the surface:
(
2
SInt4 x ⋅y⋅z, S, 0 , 1 , 0 , 2π , 0
)
T
−1
→ 0
8
⋅π 0
Since the normal points inwards, you have to take the negative of this. On the bottom surface On the top surface:
(2 ) → (0 T 2 SInt4( x ⋅y⋅z , ST, 0 , 1 , 0 , 2π , 0 ) → ( 0 SInt4 x ⋅y⋅z , SB, 0 , 1 , 0 , 2π , 0
T
0 0) 0 0)
3.4.7 Surface areas For a surface gives by a parameter function S in the independent variables u and v, the surface element is: D( S) := len(N( N S)) So the area is found by integrating D(S). SArea( S, a , A, b , B , σ ) := DInt( len(N( N S)) , a , A, b , B , σ ) Example 1. The surface area of the sphere R⋅cos( v )⋅cos( u ) S :=
R⋅cos( v )⋅sin( u ) R⋅sin( v )
SArea S, 0 , 2π ,
−π π , , 1 2 2
assume, R > 0 2 → 4⋅π⋅R simplify
3.4.8 Triple Integrals Triple integrals are treated much the same way as double integrals. Example 1 1
1
1
⌠ ⌠ ⌠ 1 z dx dy dz → ⌡0 ⌡0 ⌡0 2
⌠ ⌠ ⌠ ⌡0 ⌡− z ⌡− 1
z
132
2
z −y
2
z dx dy dz → 2
2
z −y
1 4
⋅π
Substitutions Suppose you have a vector expression U representing a coordinate change from (u,v,w) to (x,y,z). Then you can define the Jacobian matrix:
∂ U0 ∂u ∂ J( U) := U1 ∂u ∂ U2 ∂u
∂ ∂v
U0
∂ ∂w
U0
∂ ∂ U1 U1 ∂v ∂w ∂ ∂ U2 U2 ∂v ∂w
and the Jacobian determinant: JD( U) := JJ( U)
Then a triple integral
will be transformed into the integral:
⌠ ⌠ ⌠ f dx dy dz ⌡ ⌡ ⌡
⌠ ⌠ ⌠ Val(f , U)⋅JD( U) du dv dw ⌡ ⌡ ⌡
Example 2. Spherical coordinates u⋅sin( v )⋅cos( w) U( u , v , w) :=
u⋅sin( v )⋅sin( w) u⋅cos( v )
JD(U(u , v , w)) simplify → sin( v )⋅u
2
Therefore, the volume of the sphere, which is the triple integral of 1 over the sphere, can be written R
π
2⋅π
⌠ ⌠ ⌠ ⌡0 ⌡0 ⌡0
2
sin( v )⋅u dw dv du →
4
3
⋅R ⋅π 3
The mass of a ball with radius R and density proportional to the distance from the center is: R
π
2⋅π
⌠ ⌠ ⌠ ⌡0 ⌡0 ⌡0
2
4
u⋅sin( v )⋅u dw dv du → R ⋅π
Def. of div, grad, curl T
Example 8. Verify Stokes' theorem for the field F := ( y z x ) over the paraboloid z=1-(x2+y2), z≥0.
(
2 The paraboloid has the parameter function S := u⋅cos u ( v ) u⋅sin( v ) 1 − u
133
)
T
The surface integral of curl F over the curved part of the paraboloid is therefore: SInt1( curl( F) , S, 0 , 1 , 0 , 2π , 0 ) → −π The limiting circle of the boundary has the parameter function T
r := ( cos(t) t sin( t) 0 )
Int1( F, r, 0 , 2π ) → −π
Exercises.
y 1. Find the integral of 2 over the parabolic cylinder y=x2, 0≤x≤2, 0≤z≤3 x⋅ z x2 2. Find the integral of 0 over the portion of the plane x+y+z=1 in the first 2 3y octant. 3. Find the integral of x+y+z over the surface z=x+y, 0≤y≤x, 0≤x≤1
(a + b⋅cos( v))⋅cos( u ) 4. Find the area of the torus (a + b⋅cos( v ))⋅sin( u ) b⋅sin( v ) ex 5. Verify the divergence theorem for the vector field −y⋅ex over the surface 3z x 2+y2≤c2, 0≤z≤h. 2 z 6. Verify Stokes' theorem for the field 0 for the square 0≤x,y≤1, z=1 4x
134
4 Differential equations 4.1 Differential equations of order one Mathcad 6.0 Plus doesn't have a general built-in tool for solving differential equations symbolically. However, since it has built-in tools for symbolic integration and differentiation, you can still use Mathcad as a computing aid when solving ODEs. And Mathcad is well equipped to solve ODEs numerically. Let's look at some of the most important types of ODEs.
4.1.1 Indefinite integrals y'=f(x) This is equivalent to finding an antiderivative of f, which can be done by means of the symbolic integration operator, which you can find on the third vertical tool bar. You can define a SmartMath function, where U is the expression to integrate: ⌠ AntiDerive(U, C) := U dx + C ⌡ Example: f ( x , C) := AntiDerive(cos( x ) , C) → sin( x ) + C Exercise: Find the antiderivative of x 2, tan(x), ln(x)
4.1.2 Separable equations f(y) dy = g(x) dx Here you can integrate both sides of the equation, and then try to solve the result with respect to y. dy can be written Example: y´ = 2⋅x⋅y = 2 ⋅ x⋅dx y ⌠ ⌠ 1 2 Now dy → ln( y ) and 2 ⋅ x dx → x y ⌡ ⌡ So the equation has the solution: ln( y ) = x 2 + C This can be solved with respect to y using Symbolic - Solve for Variable to
(
get the general solution: ln( y ) = x 2 + C solve , y → exp x 2 + C Exercise. Solve the following separable differential equations:
(
)
2
x
y´ = 1 + y ⋅e y´ =
x
2
y
y´ =
(
x
2
y⋅ 1 + x 135
2
)
3
x d y= 2 dx 1+y
)
y´ + f ( x )⋅y = g ( x )
4.1.3 Linear equations This has the general solution
⌠ ⌠ ⌠ Lin1(f , g , C) ≡ exp − f dx ⋅ exp f dx ⋅g dx + ⌡ ⌡ ⌡ y´ +
3
C
⋅y = x (x > 0)
Here you find the general solution: 3 1 1 2 F( x , C) := Lin1 , x , C expand → ⋅x + ⋅C 3 x 5 x Example:
x
Exercise. Solve the following linear equations: y´ − 2x⋅y = x y´ +
y x
= 3 cos( 2x ) x ⋅ y´ + 2y = sin( x ) y´ + 2x⋅y = 2x⋅e
−x
4.1.4 Linear equations y' - a y = g(x), where a is a constant. In this case the general solution can be written as
⌠ Lin1c(g , a , C) := exp(a⋅x)⋅ x exp(−a⋅x)⋅g dx + C ⌡ In particular, if g(x) is a polynomial, you can always get a result. : Example: y´ − 2⋅y = 1 + 2⋅x + 5 ⋅ x 2 + 6 ⋅ x 3 2 3 Define P := 1 + 2⋅x x + 5⋅ x + 6⋅ x
The solution is: h ( x , C) := Lin1c(P, 2 , C) expand →
−9 2
2
Exercises. Find the solutions of the following equations: y´ − a⋅y = A⋅ e
kx
for k=a and for k≠a
y´ − a⋅y = A⋅cos( ω⋅x ) + B⋅sin( ω⋅x ) + 3y = x + e y´ − y = 2e
x
y´ − y = 2x⋅e
2x
2
y´ − 2y = x ⋅e 136
3
2
− 8 ⋅ x − 7 ⋅ x − 3 ⋅ x + exp( x ) ⋅C
2x
− 2x
2
4.1.5 Bernoulli's equation
n
y´ + y⋅f ( x ) = y ⋅ g ( x )
The substitution z=y1-n will lead to the equation z'+(1-n)z f(x) = (1-n) g(x), which is a linear equation. Its solution can be expressed as: 1
− (1− n)⋅ ⌠ ⌡ Ber(f , g , n , C) ≡ e Example:
y´ + y = x ⋅ y
3
⌠ ⌠ f dx ⌡ ⋅ e ⌡
(1− n)⋅f dx ⋅(1 − n)⋅g dx + C
1− n
Solution: 1
F( x , C) := Ber(1 , x , 3 , C) expand →
x + 1 + exp( x ) 2⋅C 2
1 2
Exercises. Solve the following equations: y´ − y = x ⋅ y 5 : 4
y´ + 2x⋅y + x ⋅ y = 0
4.1.6 Total differentials If the equation can be written in the form P dx + Q dy = 0, where P y = Qx, i.e. the left hand side is a total differential, then S(x,y)=C will be a solution, where S(x,y) is given in the following formula: ⌠ ⌠ TotDiff (P, Q) ≡ P dx + ⌡ ⌡ Example:
d ⌠ Q− P dx dy dy ⌡
3
x + 2 ⋅ x⋅y + y P := 4⋅x
2
2
Q := x + 2 ⋅ x⋅y − 4 ⋅ y
d P → 2⋅ x + 2⋅ y dy and the solution is S(x,y)=C, where:
d Q → 2⋅ x + 2⋅ y dx
You find:
4
3
2
2
S( x , y) := TotDiff (P, Q) → x + x ⋅y + y ⋅x − y
4
You can use Insert - Graph - Contour Plot from the menu to draw a family of integral curves. Exercises. Solve the following equations:
( 4x3⋅y3 − 2⋅ x⋅y) ⋅dx + ( 3⋅ x4⋅y2 − x2) ⋅dy = 0 3e3x⋅y − 2x) ⋅dx + e3x⋅dy = 0 137
4.2 Differential equations of order two or higher 4.2.1 Homogeneous equations with constant coefficients Mathcad can be used effectively to solve equations with constant coefficients of the form y''+a y' + b y = R(x). Let's first look at the homogeneous equation y''+ay'+by=0. The solution depends on the root of the characteristic equation r2+ar+b=0, which you can solve numerically or symbolically. y''+5y'+6y=0.
Example 1.
The differential operator corresponding to the left hand side is: D( y ) :=
d2 dx
2
y + 5⋅
d y + 6⋅ y dxx
The characteristic equation is R2 + 5 ⋅ R + 6 = 0 You find the solutions: 2
r := R + 5R + 6 solve , R →
−3 −2
Actually you just get the vector, but then you can insert a definition in front of it. The solution is then: F( x , A, B) := ( A⋅exp( r0⋅x ) + B⋅exp( r1⋅x )) → A⋅exp(−3⋅x) + B⋅exp(−2⋅x) This can be verified using the differential operator above: D( F(x , A, B)) → 0 Mathcad's contribution in this case was to find the roots of the characteristic equation, and to verify the solution. This is not very significant for 2. order equations, but it will be more important for higher order equations. Cases where the characteristic equation has two equal roots or complex roots will have to be done in a similar way. If you have an ODE whose characteristic equation has multiple roots, it's better to use factor than solve: Example 2. y'''-3y''+3y'-y=0 has characteristic polynomial: 3
2
R − 3 ⋅ R + 3 ⋅ R − 1 factor → (R − 1)
3
Exercises. Solve and verify the solutions of the following differential equations: y'' + y' -6y = 0 y'''-y''-12 y' = 0 y''''+6y'''+5y''-24y'-36y=0 y''-2y'+10y=0
y'''+4y'=0
y''''+y'''+2y''-y'+3y=0 138
4.2.2 Inhomogeneous equations with constant coefficients The method of undetermined coefficients The contribution from Mathcad will be more significant when the calculations become heavier, as they do with inhomogeneous equations. You still have to know the theory about ODEs, so you know what kind of solutions you must look. But Mathcad can help you find the unknown coefficients. Example 1.
y''-2·y'+y=(x+1)ex
2 The differential operator is here D1( y ) := d y − 2⋅ d y + y 2 dxx dx
The characteristic equation r 2-2r+1=0 has the double root 1, and so the homogeneous equation has the general solution H1(x , C1, C2) := (C1 + C2⋅x)⋅exp( x ) You can check that this is indeed a solution of the homogeneous equation: D1(H1(x , C1, C2)) → 0 Then according to the theory you must look for a particular solution of the form:
(
)
2
3
P( x , A, B) := A⋅x + B⋅ x ⋅exp( x ) You find D1(P(x , A, B)) factor → 2⋅(A + 3⋅B⋅x)⋅exp( x ) : Therefore,
A=
1
and
B=
2
1 6
The general solution is therefore GS(x , C1, C2) := C1 + C2⋅x +
1 2
2
⋅x +
1 6
⋅x ⋅exp( x ) 3
Check the result: D1(GS(x , C1, C2)) factor → (1 + x)⋅exp( x ) Example 2. y''+4y = xex+x sin 2x 2 Differential operator: D2( y ) := d y + 4 ⋅ y 2 dxx
The homogeneous equation has the general solution C1 cos 2x + C2 sin 2x . You have to try a particular solution of the following form: 139
(
2
)
P2(x , a , b , c , d , f , g) := (a⋅x + b)⋅exp( x ) + c⋅ x + d ⋅ x ⋅cos(2⋅x) ...
(
)
2
+ f ⋅ x + g ⋅ x ⋅sin(2⋅x) To find the undetermined coefficients, evaluate D2(P2(x,a,b,c,d,f,g)) using collect: collect, sin( 2x ) D2(P2(x , a , b , c , d , f , g))
collect, cos( 2x ) collect, exp( x )
The result is too long for the page width. Written on three lines, it looks like this: (2 ⋅ a + 5 ⋅ a⋅x + 5 ⋅ b)⋅exp( x ) ... ... + (2 ⋅ c + 8 ⋅ f⋅x + 4⋅ g)⋅cos(2⋅x) + (−8⋅c⋅x − 4 ⋅ d + 2 ⋅ f)⋅sin(2⋅x) Then you can find the undetermined coefficients by means of one of Mathcad's equation solvers: Given
5a = 1
2a + 5⋅b = 0 8f = 0
−8c = 1
−4d + 2f = 0 T
A := Find(a , b , c , d , f , g) →
2c + 4g = 0
1 −2 −1 0 0 1 16 5 25 8
So, the general solution of the equation is: GS(x , C1, C2) :=
1 ⋅x − 2 ⋅ex + − 1 ⋅x 2 + C1 ⋅cos(2⋅x) ... 8 5 25 1 + ⋅x + C2 ⋅sin(2⋅x) 16
Check the solution: D2(GS(x , C1, C2)) simplify → exp( x )⋅x + x⋅sin(2⋅x) Exercises. Find the general solution of the following diff. equations:
( D2 − 3D + 2) y = ex ( D2 + 9) y = x⋅cos( x) ( D2 − 9D + 18) y = ee ( D2 − 6D + 9) y = e2x ( D2 − 1) y = 4x⋅ex
( D2 − 3D + 2) y = e5x ( D2 + 5D + 4) y = 3 − 2x ( D2 + 4) y = 2 cos(x)⋅cos( 3x) ( D2 − 4D + 3) y = 1 ( D2 − 4D) y = 5 ( D2 + D − 2) y = 2⋅ ( 1 + x − x2) ( D2 − 1) y = ( 1 + e− x) − 2
− 3x
140
Equations of order 3 or higher The help you can get from Mathcad is similar for higher order equations as for second order equations: You have to know the theory, but once you know what form of solution to look for, Mathcad can help you determine it. We show the method by an example. Example 1. y''' - 4 y' = x + 3 cos x + e - 2x D1(u , x) :=
Differential operator:
d3 dx
3
u − 4⋅
d u dx
The solutions of the homogeneous equation are obviously r=0,-2,2. So the general solution of the homogeneous equation is c1 x + c2 e-2x + c3 e2x. According to the theory you must look for a particular solution of the form: 2
PS(x , A1, B , C , D, E) := A1⋅x + B⋅ x + C⋅cos( x ) + D⋅sin( x ) + E⋅ x⋅exp(−2⋅x By symbolic evaluation of D1(PS(x,A,B,C,D,E),x) using [Ctrl][Period] you find: 5 ⋅ C⋅sin( x ) − 5 ⋅ D⋅cos( x ) + 8 ⋅ E⋅exp(−2⋅x) − 4 ⋅ A1 − 8 ⋅ B⋅x Comparing coefficients: Given 5 ⋅ C = 0
−5⋅D1 = 3 8 ⋅ E = 1
Find(A1, B , C , D1, E) → 0 T
−1 8
0
−4⋅A1 = 0 −8⋅B = 1 −3 5
8
1
(The use of D and A as a variable name here gives an error.) So, the particular solution is
1 2 3 1 P( x ) := − ⋅x − ⋅sin( x ) + ⋅x⋅exp(−2⋅ 8 5 8
Hence the general solution is: 1 2 3 1 G( x , C1, C2, C3) := − ⋅x − ⋅sin( x ) + ⋅x⋅exp(−2⋅x) ... 8 5 8 + C1⋅x + C2⋅exp(2⋅x) + C3⋅exp(−2⋅x) Exercises. Find the general solution of the following diff. equations:
( D3 + 3D2 − 4) y = x⋅e− 2x ( D3 − 5D2 + 8D − 4) y = e2x ( D3 − 4D2) y = 5 ( D5 − 4D3) y = 0 ( D3 − 4D) y = x 141
4.2.3 Solution by power series expansion Mathcad can be used to help finding solutions of ODEs by power series expansion, but you must limit yourself to a finite number of terms. Example. Airy's Equation y''- x·y=0 Say you want to find the first six terms of the Taylor expansion of the solution of Airy's equation y''-xy=0 with the initial conditions y(0)=1 and y'(0)=0. 2 Differential operator: D( y ) := d y − x⋅ xy 2 dx
Let's try a polynomial of degree six: 2
3
4
5
A0 + A1⋅x + A2⋅x + A3⋅x + A4⋅x + A5⋅x + A6⋅x U := A0
6
2 ⋅ A2 6 ⋅ A3 − A0 lim U → A0
x→0
12⋅A4 − A1 20⋅A5 − A2 d Then D( U) coeffs , x → lim U → A1 30⋅A6 − A3 x→0 dx −A4 So, the initial conditions give: −A5 A0 = 1 A1 = 0 −A6 To find the coefficients so that all the terms up to degree 6 are zero, you can use (say) the solve keyword:
A0 = 1 A0 A1 = 0 A1 2A2 = 0 A2 6A3 − A0 = 0 1 1 0 0 solve , A3 → 1 0 0 12A4 − A1 = 0 6 180 A4 20A5 − A2 = 0 A5 30A6 − A3 = 0 A6 −A4 = 0
142
So, the first terms of the power series of the solution is f ( x ) := 1 +
1 6
3
⋅x +
1 180
⋅x
6
As a check,
D( f ( x )) expand →
−1 180
⋅x
7
So, f(x) is indeed a correct sixth degree approximation to the solution. Mathcad has the two linearly independent solutions of Airy's equation as built-in functions with the names Ai(x) and Bi(x). Let's use them to solve the above initial value problem: G( x , C1, C2) := C1⋅Ai( x ) + C2⋅Bi( x ) We find the values of the derivatives at 0: d d Ai( x ) z1 = −0.259 z2 := Bi( x ) z2 = 0.448 dx dx Then you can find the coefficients C1 and C2 by means of the initial conditions: x := 0
C1 := 0 Given
z1 :=
C2 := 0
TOL := 10
− 10
C1⋅Ai( 0 ) + C2⋅Bi( 0 ) = 1 C1⋅z1 + C2⋅z2 = 0
c1 := Find(C1, C2) c2
c1 1.4083394102 = c2 0.8131051376
Then you define F( x ) := c1⋅Ai( x ) + c2⋅Bi( x ) and draw the graphs near 0: x := −3 , −2.9.. 3 10
f ( x) 5
F ( x)
2
0
2
x
Exercises. Find sixth degree approximations to the solutions of the following equations: y'' - y = 0 y'' - x y' - y = 0 y'' - x y' - y = 0 (1 - x) y'' + y = 0 y'' + x y' + 2 y = 0 Choose your own initial conditions. 143
4.3 Laplace transforms 4.3.1 The built-in Laplace transform Mathcad has the Laplace transform and its inverse Use can use Symbolics Transform - Laplace from the menu, or you can use the [laplace] button on the Symbolic Toolbar. With the menu and the cursor on t: sin( a⋅t)
has Laplace transform
a
( s2 + a2)
With the Symbolic Toolbar: a
sin(a⋅t) laplace, t →
a
invlaplace , s → sin( a⋅t)
( s2 + a2) ( s2 + a2)
The Laplace transforms have also an undocumented (Maple) syntax: laplace(sin( a⋅t) , t , s) →
(s
2
invlaplace
a
+a )
a
s + a
2
2
2
, s, t → sin( a⋅t)
4.3.2 Application to initial value problems The main application of Laplace transforms is solving initial value problems. So, suppose we have an initial value problem: p n⋅y
( n)
+ p n− 1⋅y
(n− 1)
+
+ p 1⋅y´ + p 0⋅y = f ( x )
..
y ( 0 ) = y 0 y´( 0 ) = y 1
... y (n− 1) ( 0 ) = y n− 1
It can be shown easily (cf. any standard textbook) that the Laplace transform of the ith derivative of a function can be expressed in terms of the Laplace transform of the function itself in the following way: ( i) i laplacey = s ⋅laplace( y ) −
n
∑ k
i− k
s
⋅y k− 1
=1
Hence the Laplace transform of the left hand side of the differential equation above can be written as: rows( p)−1
L( p , y , u , Ly) :=
∑ i
=0
p i⋅ u ⋅Ly −
rows( p)−1
i
∑ k
u
i− k
=1
Then we can write a function that solves the differential equation: 144
⋅y k− 1⋅(k ≤ i)
DiffSolve(p , y , f) :=
A ← L(p , y , s, Ly) B ← solve(A − laplace(f , t , s) , Ly) invlaplace(B , s, t) t
Example. In the initial value problem y ´´´- 3 y´´ + 2 y' = e-t sin t + t2, y 0=5, y1=6, y 2=7 T
(
T
−t
Y := DiffSolve p , y , e ⋅sin( t) + t
p := ( 0 2 −3 1 ) y := ( 5 6 7 )
2
)
Symbolic evaluation by [Ctrl]Period gives 1 6
3
3
⋅t +
4
2
⋅t +
7 4
⋅t +
13 8
+
14 5
⋅exp( t ) +
27 40
⋅exp(2⋅t) −
1 10
⋅exp( −t)⋅cos( t )
You can check the solution in the following way: D( U) :=
d3 dt
3
U − 3⋅
d2
d U + 2⋅ U dtt dt
D( Y) → exp(−t)⋅sin( t ) + t
2
2
The initial conditions: d d2 Y →6 lim Y →7 2 t→0 t→0 dt t→0 dt You can also use Laplace transforms on a set of simultaneous equations. lim Y → 5
lim
Example x´ = 6x + y y´ = 4x + 3y
First, apply Laplace transform and use the result above aboutb the laplace
transform of a derivative. Then use a symbolic solve block to find the Laplace
transforms:
(2 ⋅ s + 1) Given s⋅ LX − 2 = 6 ⋅ LX + LY 2 s − 9 ⋅ s + 14 s⋅ LY − 7 = 4LX + 3LY Find(LX, LY) → (7 ⋅ s − 34)
(
)
2 ( s − 9 ⋅ s + 14)
Then you find the inverse Laplace transform of each component: x ( t ) := y ( t ) :=
( 2 ⋅ s + 1)
( s2 − 9⋅ s + 14) (7 ⋅ s − 34)
( s2 − 9⋅ s + 14)
invlaplace , s → −exp(2⋅t) + 3⋅exp(7⋅t) invlaplace , s → 4⋅exp(2⋅t) + 3⋅exp(7⋅t)
Exercise. Find the "rest solution" (i.e. the particular solution where the function and its derivatives are 0 at 0) of the diff. equation y'''' + 3y''' + 2y'' + 6y' = t2. 145
4.4 Difference equations 4.4.1 Method of undetermined coefficients For difference equations, the situation is much the same as for differential equations. You have to know from the theory what solutions to look for, but then Mathcad can help you do the calculations. The following operator will replace n by n+i in an expression y: D( y , i) :=
lim
lim y
ttt→nn+ i n→ttt
Below, we are going to assume that all indices are named n Example. Find the solution of the difference equation yn+2 + 3 yn+1 + 2 yn = n The characteristic equation and its solutions: 2
r + 3r + 2 solve , r →
−2 −1
Therefore, (-2)n and (-1)n are the two linearly independent solutions to the equations. Let's check this. The difference operator is D1( y ) := D(y D , 2) + 3 D(y , 1) + 2 D(y , 0) D1( −2 )
You find
→ 0
n
D1( −1 )
→ 0
n
According to the theory, the inhomogeneous equation must have a solution of the form Y := A + B⋅n You find: D1( Y) → 6⋅A + 6 ⋅ B⋅n + 5 ⋅ B Comparing coefficients and solving, you find Given
6A + 5B = 0
6B = 1
T
Find(A, B) →
−5 1 36 6
So, the general solution of the inhomogeneous equation is: 1 5 n n Y := n − + C1⋅( −1 ) + C2⋅( −2 ) Control: D1( Y) → n 6 36 Exercises. Solve the following difference equations: y n+ 2 − y n+ 1 − y n = 0
with initial conditions y 0 = 1
y n+ 2 − 4 y n+ 1 + 4 y n = n ⋅ 2 The system
n
x n+ 1 − x n − y n = n y n+ 1 − x n + y n = 3
2
y n+ 2 + ω ⋅y n = 2
n
with initial conditions 146
y 1 = 1 (Fibonacci)
x0 = 0
y 0 = −10
4.4.2 The z transform If x(n) is a function that defines a number sequence, then the z-transform of x is given in the formula: ∞
Z( x , z) :=
∑ n
x ( n )⋅z
For instance, if x ( n ) := 2 n then
−n
z
Z( x , z) →
=0
(−2 + z)
Mathcad has the z-transform and its inverse as built-in functions. You can use the menu choice Symbolics - Transform - Z (or Symbolics - Transform InverseZ) or you can use the analogous buttons on the Symbolic Toolbar. There is also an undocumented Maple syntax ztrans(expression,variable,name) and similarly invztrans(expression,variable,name) z
n
2 ztrans, n → z −2 + z
(
invztrans , z → 2 n
)
2
(−2 + z)
ztrans 2 , n , z →
n
z (−2 + z)
n
z
has z transform z
−2 + z
(−2 + z)
has inverse z transform
invztrans
z
−2 + z
, z , n → 2
2
n
n
Applications to difference equations The main application of the z-transform is to solve difference equations with initial conditions: aN⋅y ( n + N) + aN− 1⋅y ( n + N − 1) + aN− 2⋅y ( n + N − 2) +
..
+ a0⋅ y ( n ) = f(
with y(0),...,y(N-1) given in a vector y. The most important property of the z transform is the following: If Y(z) is the z-transform of y(n), then z Y(z) - z y(0) is the z-transform of y(n+1) and in general: If Y(z) is the z transform of y(n), then zk Y(z) - z k Y(0) -zk-1 Y(1) - ...z Y(k-1) is the z-transform of Y(n+k) ZZ(a , y , z, Z) :=
N ← rows( a) − 1 N− 1 N− i N− i− j aN− i⋅ z ⋅Z − ( j < N − i)⋅z ⋅y j i=0 j=0 N
∑
∑
147
Then you can write a symbolic program to solve the difference equations by means of the z transform: DS(a , y , f) :=
A ← ZZ(a , y , z , ZT) B ← solve(A − ztrans(f , n , z) , ZT) invztrans(B , z, nn)
Example Solve the initial value problem yn+2 + yn+1 -6yn = 2n+2 -1, y0=1, y1=1 With the notations above: −6 1 a := 1 y := 1 1
(
DS a , y , 2 Y := DS
n+ 2
−1
f := 2
)
Y→
nn+ 2
−1
31
n
100
⋅( −3 ) +
1 4
+
2 5
n
⋅2 ⋅n +
11 25
⋅2
n
Let's check the solution: D( y , i) :=
lim
lim y
D1( y ) := D(y D , 2) + D(y , 1) − 6 D(y , 0)
ttt→n n+ i n→ttt
Y :=
31 100
n
⋅( −3 ) +
1 4
+
11 25
n
⋅2 +
2 5
n
⋅2 ⋅n
n
D1( Y) → 4⋅2 − 1 Y substitute, n = 0 → 1 Y substitute, n = 1 → 1 Exercise. Solve the following difference equations by means of the z transform: y n+ 1 − y n = 2
y0 = 1
y n+ 2 − y n+ 1 − y n = 0
148
y0 = 1
y1 = 1
4.5 Fourier series 4.5.1 Fourier coefficients and Fourier series You can define the Fourier coefficients of a function f by the following symbolic functions: L
1 ⌠ n⋅π⋅x dx FourierA(f , L, n) := ⋅ f ( x )⋅cos L L ⌡− L L
1 ⌠ n⋅π⋅x dx FourierB(f , L, n) := ⋅ f ( x )⋅sin L L ⌡− L The the Fourier series with N terms can be defined as: L
FourierSum(f , x , L, N) :=
1 ⌠ ⋅ f ( x ) dx ... 2 ⋅ L ⌡− L N
+
FourierA(f , L, n)⋅cos n⋅π⋅x ... L n=1 n⋅π⋅x + FourierB(f , L, n)⋅sin L
∑
Given a function f defined on some interval [-L,L], you can define the periodic extension of f defined for all x by the following: x + L Periodic(f , L, x) := f x − 2⋅L⋅floor 2 ⋅ L Example 1. f ( x ) := x
h ( x ) := Periodic( f , π , x )
x := −5 , −4.9.. 5
g ( x ) := FourierSum( f , x , π , 3 ) → 2⋅sin( x ) − sin(2⋅x) +
2 3
⋅sin(3⋅x)
5
h( x) g( x)
6
4
2
0
2
5 x
Exercise. Repeat example 1 with the function f(x) = x 2. 149
4
6
4.5.2 Step functions Fourier coefficients also work with discontinuous functions, as long as they are piecewise continuous. The question is how discontinuous functions can be defined in a way that symbolic Mathcad can understand. The solution lies in Heaviside's step function Φ, which both symbolic and numerical Mathcad understand. It is equivalent to if(x<0,0,1). 1
⌠ Φ ( x ) dx = 1 ⌡− 1
1
⌠ Φ ( x ) dx → 1 ⌡− 1
You can use Heaviside's step function to define a function by different formulae on different intervals. Φ(x-a)-Φ(x-b) is a function that is 1 on [a,b〉 and 0 otherwise. Here are two examples: Example 1 f ( x ) := ( Φ ( x + π ) − Φ ( x ) ) ⋅( −1 ) + ( Φ ( x ) − Φ ( x − π )) π
⌠ f ( x )⋅cos( x ) dx expand → 0
⌡− π So the Fourier series of f has to be evaluated by expanding: Fof(x) := FourierSum( f , x , π , 3 ) → here are the graphs:
4 π
⋅sin( x ) +
4
( 3⋅π )
⋅sin(3⋅x)
f2(x) := Periodic( f , π , x ) x := −10, −9.95 .. 10
f2( x) Fof ( x)
10
5
0
5
10
x
Exercises. Repeat the example with the following functions: g ( x ) := ( Φ ( x + π ) − Φ ( x ) ) ⋅ ( x + π ) + ( Φ ( x ) − Φ ( x − π )) ⋅ ( π − x )
(
g ( x ) := ( Φ ( x + π ) − Φ ( x ) ) + ( Φ ( x ) − Φ ( x − π )) ⋅ 1 + x
150
)
2
4.5.3 Fourier transforms Mathcad has the Fourier transform and its inverse built-in. They are defined as follows: ∞
⌠ Fourier( f , ω ) := f ( t)⋅exp( −i⋅ ω⋅t ) dt ⌡− ∞ ∞
InvFourier(f , t) :=
1 ⌠ ⋅ f ( ω ) ⋅exp( i⋅ω⋅t ) dω 2π ⌡− ∞
You can get them in three ways:
( )
Write the expression, say exp −x 2 Then put the cursor on an x and choose Symbolics - Transform - Fourier from the menu. The result is 1 −1 2 2 ⋅exp ⋅ω .You can compare this with π 4 1 ∞ ⌠ −1 2 2 2 exp −t ⋅exp( −i⋅ ω⋅t) dt → exp ⋅ω ⋅π ⌡− ∞ 4
( )
Alternatively write the expression and click on the [fourier] button the symbolic toolbar, and write x or whatever name you have used for the independent variable in the placeholder: 1 −1 2 2 2 . exp −x fourier, x → exp ⋅ω ⋅π 4
( )
Finally, you can use a Maple syntax:
( ( ) 2
)
fourier exp −x , x , t → π
1 2
⋅exp
−1 4
⋅t
2
Here it is possible to choose the names of the independent variable both of the input and the output. You have similar choices for the inverse Fourier transform:
−ω 2 1 2 has inverse Fourier transform ⋅ 4⋅exp( −t ) 2 4 2 −ω invfourier, ω 2 π ⋅ exp → exp( −t ) simplify 4 π ⋅ exp
151
−w2 , w, x simplify → exp( −x2) 4
invfourier π ⋅ exp
The Fourier transform is linear, and has the property that the Fourier transform of f'(x) is i ω times the Fourier transform of f(x). Also, for a function of two variables u(x,t), the Fourier transform of ∂ u ( x , t) ∂t is the partial derivative of the Fourier transform of u.
4.5.4 Fourier transforms and the heat equation The main application for Fourier transforms is partial differential equations. Suppose for example that you have a heat conduction problem in an infinite slab:
∂ ∂t
u −
∂2 2 ∂x
u ( x , 0) = f ( x )
u= 0
u(x,t) bounded
Taking Fourier transforms of the differential equation you get
∂ ∂t
2
fourier(u(x , t)) + ω ⋅fourier(u(x , t)) = 0 fourier(u(x , t) , 0) = fourier(f ( x ))
(
2
This problem has the solution fourier(u(x , t)) = fourier(f ( x ))⋅exp −ω ⋅t For instance, if f ( x ) := e− x
)
2
then 1
1 2 2 2 fourier( f ( x ) , x , ω ) ⋅exp( −ω ) ⋅t simplify → π ⋅exp ⋅ω ⋅(−1 + 4 ⋅ t) 4 So the solution is:
ω
4
u ( x , t) := invfourier π ⋅ exp
2
⋅(−1 + 4 ⋅ t) , ω , x →
−x 2 (4 ⋅ t + 1)
exp
1 2 (4 ⋅ t + 1)
You can check the solution:
∂ ∂t
u ( x , t) −
∂2 ∂x
2
u ( x , t)
simplify
→0 assume, t > 0
152
( 2)
u ( x , 0) simplify → exp −x
4.5.5 Discrete Fourier Transforms Mathcad has built-in functions for the Fast Fourier Transform. To motivate this, notice that the Fourier series of a function f can be written in complex formulation as L
1 ⌠ n⋅π⋅x dx CFourier(f , L, n) := ⋅ f ( x )⋅exp −i⋅ 2⋅ L L ⌡− L L
1 ⌠ CFS(f , x , L, N) := ⋅ f ( x ) dx ... L ⌡− L N
+
∞
∑
or as
c j⋅e
i⋅
CFourier(f , x , L, n)⋅exp i⋅ n⋅ π⋅x ... L n=1 −i⋅ n⋅ π⋅x + CFourier(f , x , L, −n)⋅exp L
∑
L
⌠ j⋅π ⋅x − i⋅ 1 L with c j = ⋅ f ( x )⋅e dx L ⌡− L
j⋅π ⋅x L
j= −∞ To get back the real Fourier coefficients, you compute: a0 = 2 ⋅ c0 a j = 2 ⋅ Re( c j) b j = −2⋅Im( c j)
Now to compute the integral cj, you can approximate it by a Riemann sum. Suppose you subdivide the interval [-L,L〉 into n=2q subintervals of equal lengths h=2L/n=L/q. The subdivision points then become k = 0 , 1 , .. , n tk = −L + k ⋅ h The upper Riemann approximation to the integral then becomes: Cj =
1 L
n
∑
⋅
k
j⋅π⋅tk But ⋅h L
f ( tk) ⋅exp−i⋅
=1
−L + k ⋅ h j⋅π⋅tk = f ( tk) ⋅exp−i⋅ j⋅π⋅ L L 2⋅π⋅k⋅ j f ( tk) ⋅exp( j⋅π⋅i ) ⋅exp −i⋅ n Therefore
f ( tk) ⋅exp−i⋅ =
Cj =
1 L
⋅exp( i ⋅ j⋅ π ) ⋅h⋅
n
∑ k
=1
f ( t k) ⋅exp −i⋅
153
2⋅π ⋅k⋅ j n
or
n 2⋅π⋅k⋅ j C j = ⋅exp( i⋅ j⋅ π ) ⋅h⋅ f ( tk) ⋅exp−i⋅ n n k = 1
∑
2
Now this formula is closely related to the definition of Mathcad's function FFT: Given a sequence of n=2m pairs (tk,vk), k=0,...,n-1 where the t's are evenly spaced. You may interpret the v's as the displacement of a wave at time t. Then the d=FFT(v) is a vector with n/2 + 1 = 2m-1+1 components such that: dj =
1 n
n
⋅
∑
v k⋅exp −i⋅
2⋅π⋅ j⋅k
n
k=1 Let's illustrate with an example where the Fourier coefficients are found by means of the definitions and by means of the FFT to compare:
With the definition: f ( x ) := x
L := π
L
1 ⌠ j⋅π ⋅x dx → −2 ⋅ ( −sin( j⋅π ) + j⋅π⋅cos( j⋅π )) b ( j) := ⋅ f ( x )⋅sin 2 L π L j ⌡− L With FFT: m := 13
Number of points:
n := 2
x k := −L + k⋅
Subdivision points:
m
2⋅π
k := 0 .. n − 1 Observations:
n
v k := f ( x k)
cc := FFT( v )
Using the FFT: Fourier coefficients:
k := 0 ..
ck := 2⋅exp( −k⋅ π⋅i ) ⋅cc k
n 2
Mathcad also has the inverse of FFT, with the name IFFT: If
ww := IFFT( cc )
Results for
then you can check that
ww = v
See below.
j := 1 .. 25 Most of the coefficients agree up to 5 digits.
Define C j , 0 := j C j , 1 := v j C j , 2 := b ( j ) C j , 3 := c j C j , 4 := Im( c j) C j , 5 := ww j and write C= to see that. Applications of FFT are of course not limited to finding the Fourier coefficients of a known function. A more meaningful application is when the function is not given explicitly, but only in terms of a certain number of values of the displacements for the wave in a period. Then FFT gives information about the frequency spectrum of the wave. 154
4.6 Numerical solution of differential equations Mathcad has a variety of numerical differential equation solvers built-in. We shall look at one of them namely the classical fourth order Runge-Kutta method. Before we look at that, however, we shall look at some graphic tools that can used to visualize differential equations.
4.6.1 Drawing a two dimensional vector field Mathcad has a built-in function for drawing a two dimensional vector field. Suppose F is a two dimensional vector field F: R 2 →R2 that you want to illustrate by drawing arrows of the right length and direction at the points of a rectangular grid in the rectangle [a,A] × [b,Β] with Μ+1 subdivision points in the x direction and N+1 subdivision points in the y direction. The following function gives a pair of matrices which you can use in Mathcad's vector field plot tool, which you get access to by choosing Insert - Graph - Vector Field Plot from the menu. A− a VectorGrid(F, a , A, M , b , B , N) := h ← M k←
B−b N
for m ∈ 0 .. M x m ← a + m⋅h for n ∈ 0 .. N y n ← b + n⋅k
x m y n
M ← F
MXm , n ← M 0 MYm , n ← M 1
MX MY Example 1. F( x ) :=
x0 M0 := VectorGrid(F, −2 , 2 , 10, −2 , 2 , 10) x 0⋅x 1 155
However, later on I want to draw a combined diagram with directions and integral curves. For this I need to draw a vector field using the two dimensional XY plot. Here is a program that does that. P is a complex polygon - an "arrow" - to be drawn to indicate the directions: A − a B − b DirField(F, a , A, M , b , B , N, P) := dx ← ⋅ dy ← M N P ← P⋅
0.48⋅if(dx < dy, dx, dy) max(P) − min( P)
k←0 for m ∈ 0 .. M x m ← a + m⋅dx for n ∈ 0 .. N y n ← b + n⋅dy
x m y n
ff ← F
f ← ff 0 + i ⋅ ff 1 S
〈k〉
← P⋅ f + ( x m + i⋅ y n)
k←k+1 S
156
Example 2.
x0 −1 F( x ) := P := 1 x 0⋅ x 1
M := DirField( F , −2 , 2 , 11 , −2 , 2 , 10 , P)
−1 P := 1 0.5 + 0.5i
Im( M)
Re( M)
If you are only interested in the direction, you can define: G( x ) :=
F( x ) F( x )
A := DirField( G , −2 , 2 , 11 , −2 , 2 , 10 , P)
Im( A)
Re( A)
157
4.6.2 Drawing a family of integral curves A differential equation y'=f(x,y) may have a solution that can be written on the form G(x,X,Y), where (X,Y) is a point in the plane which the integral curve is required to pass through. We choose a grid dividing [a,A] × [b,B] in (M+1) × (Ν+1) points and using P+1 points on each integral curve. Curves(G, a , A, M , b , B , N, P) :=
dx ← A − a ⋅ dy ← B − b ⋅(k ← 0) M N ddx ←
A− a P
for m ∈ 0 .. M Xm ← a + m⋅dx for n ∈ 0 .. N Yn ← b + n⋅dx for p ∈ 0 .. P Tp ← a + p⋅ddx Zp , k ← G( Tp , Xm , Yn) k←k+1
T Z Example 3. The differential equation y' = x2 - y has the exact solution through (a,b) given by 2
(
)
2
G( x , a, b) := x − 2x + 2 + −a + b + 2a − 2 ⋅exp(a − x) We plot the integral curves:
M := Curves(G, −2 , 2 , 5 , −2 , 2 , 5 , 50)
together with a plot of the directions: F( x ) :=
1 F( x ) H( x) := 2 F( x ) ( x 0) − x 1
A := DirField(H, −2 , 2 , 10, −2 , 2 , 10, P)
158
2
1
M1 Im( A)
0
1
2
2
1
0
1
2
M0 , Re( A)
4.6.3 Runge-Kutta's fourth order method rkfixed Let's demonstrate the use of Mathcad's Runge-Kutta function rkfixed for differential equations x' = f(t,x) on the initial value problem x'=F(t,x), x(0)=0, where F( t , x) := t + x . It can be shown that the exact solution of this problem is g ( t) := −t − 1 + exp( t ) The rkfixed function needs five arguments: • x0 is a vector giving the initial conditions. In the one dimensional case you have to give a vector with one component. • a and b are the endpoints of the interval for the independent variable t • N is the number of subdivision points in the interval [a,b] • F is the vector function giving the right-hand side of the differential equation. Let's choose
a := 0
b := 1
x00 := 0
X1 := rkfixed(x0, a, b , N, F)
0 0.2 0.4 X1 = 0.6 0.8 1
N := 5
For comparison:
0.0214 0.09181796 0.2221064563 0.4255208258 0.7182511366 0
(
g X1 〈0〉
159
)
0 0.0214027582 0.0918246976 = 0.2221188004 0.4255409285 0.7182818285
The graphs look like this:
0.5
g( t ) X1
t := 0 , 0.05 .. 1
〈1〉
0
0.5 t , X1
1 〈0〉
Exact solution Runge-Kutta rkfixed is intended to work for differential equations equations in any dimensions, so here is a two dimensional example: Example 4. x'(t) = F(t,x), where: F( t , x) :=
x0 − 4⋅ x1 1 Initial condition: x0 := 0 −x 0 + x 1
This is equivalent to the system
x´ = x − 4y y´ = −x + y
It can be shown that the exact 0.5⋅(exp(−t) + exp( 3t)) X( t) := solution of the initial value problem 0.25⋅(exp(−t) − exp( 3t)) is With rkfixed: R := rkfixed(x0, 0 , 2 , 10, F) t := 0 , 0.2.. 2
0 0.2 0.4 0.6 0.8 R= 1 1.2 1.4 1.6 1.8 2
1 1.32 1.994 3.296 5.728 10.207 18.406 33.375 60.665 110.393 200.988
−0.251 −0.662 −1.373 −2.639 −4.92 −9.053 −16.564 −30.231 −55.114 −100.426 0
(1 0 ) ( 1.32 −0.251 ) ( 1.995 −0.662 ) ( 3.299 −1.375 ) ( 5.736 −2.643 ) T X( t) = ( 10.227 −4.929 ) ( 18.45 −9.074 ) ( 33.466 −16.61 ) ( 60.856 −30.327 ) ( 110.786 −55.31 ) ( 201.782 −100.823 ) 160
The graphs look like this: t := 0 , 0.05 .. 2 0
R
2
100
〈2〉
R
200
〈2〉 50
50 X( t) 1
X( t) 1
100
100 R
〈0〉
,t
R
〈1〉
, X( t) 0
4.6.4 Drawing a family of numerically computed integral curves. If S is rkfixed or any other numerical differential equation solver, here is a function that draws a family of integral curves for a one dimensional differential equation x'=D(t,x) through a M × Ν grid of points in the rectangular area [a,A] × [b,B]. IC(a , A, M , b , B , N, c , P, D, S) :=
dt ← A − a ⋅ dx ← B − b ⋅(k ← 0) M N for m ∈ 0 .. M tm ← a + m⋅dt for n ∈ 0 .. N x n ← b + n⋅dx z0 ← x n Z ← S( z, tm , t m + c , P, D) Y
〈k〉
←Z
〈1〉
for p ∈ 0 .. P Up , k ← t m + p⋅ k←k+1
U Y 161
c P
Example 5, cf. example 3 2
F( t , x) := t − x 0
M := IC(−2 , 2 , 5 , −2 , 2 , 5 , 5 , 50, F, rkfixed)
2
M1
0
2
2
0
2
M0
The function IC can easily be extended to the two dimensional case: IC2(a , A, M , b , B , N, c , P, D, S) :=
dt ← A − a ⋅ dx ← B − b ⋅(k ← 0 N M for m ∈ 0 .. M t m ← a + m⋅dt for n ∈ 0 .. N x n ← b + n⋅dx z0 ←
tm xn
Z ← S( z0, tm , t m + c , P, D) X
〈k〉
Y
〈k〉
←Z ←Z
for p ∈ 0 .. P Tp , k ← p⋅ k←k+1
T X Y 162
c P
〈1〉 〈2〉
4.6.5 ODEs of order two or higher Second order ODEs can be transformed into a system of two first order equations. Let's demonstrate the method on an example, say the ODE y''-3y'+2y=t with the initial conditions y(0)=0.75, y'(0)=1. Substitute x0=y, x1=y'. Then the ODE is equivalent to the system: x 0'(t) = x1 x 1(t) = 3 x1 + 2 x0 + t This system can be written on the form x'(t) = D(t,x), where: D( t , x) :=
x1 The initial conditions 0.75 give the initial vector x0 := 1 3 x1 − 2 x0 + t
It can be shown that the exact solution is:
1 t + 3 − 1 exp( t) + 1 exp( 2t) 2 2 4 2 F( t ) := 1 1 − exp( t ) + exp( 2t) 2 2 By rkfixed you get:
t := 0 , 0.05 .. 0.5
X := rkfixed(x0, 0 , 0.5, 10, D)
0 0.05 0.1 0.15 0.2 X = 0.25 0.3 0.35 0.4 0.45 0.5
0.75 0.80195 0.85812 0.91901 0.98521 1.05735 1.13613 1.22234 1.31686 1.42064 1.53478
1.07954 1.16882 1.26894 1.38112 1.50671 1.64719 1.80422 1.97963 2.17545 2.39392 1
( 0.75 1 ) ( 0.80195 1.07954 ) ( 0.85812 1.16882 ) ( 0.91901 1.26894 ) ( 0.98521 1.38112 ) T F( t ) = ( 1.05735 1.50671 ) ( 1.13613 1.64719 ) ( 1.22234 1.80422 ) ( 1.31686 1.97963 ) ( 1.42065 2.17545 ) ( 1.53478 2.39392 )
So they agree up to five decimals.
163
ODESolve From version 2000, Mathcad has an differential equation solver ODEsolve that can take care of intial value and boundary value problems involving one unknown function, whose highest order derivative term must be linear. ODEsolve does the steps described above and then uses rkfixed. Using the right hand mouse button, you can choose an adaptive method instead. To get a function defined for all values of the parameter, it uses spline interpolation .
You start with the code word Given. Then write the differential equation and the initial condition. The prime in y' is written by means of the è accent (accent grave). For the example above it looks like this: Given y''( x ) − 3⋅y'( x ) + 2 ⋅ y ( x ) = x (Use Ctrl + to get the = symbol.) y ( 0 ) = 0.75 y'( 0 ) = 1 f := ODESolve(x , 0.5, 10) (ODESolve returns a function.) x := 0 , 0.05.. 0.5 Write f(x)= and you get exactly the same values as above.
Example of a boundary value problem: Given
4⋅y''( x ) + y ( x ) = 1
y( π ) = 0
y( 0) = 0
g := Odesolve( x , π , 10) It can be shown that the exact solution of the boundary value problem is x x h ( x ) := 1 − cos − sin The values at π/2 are 2 2
g
π = −0.414212827001793 2
Here are the graphs:
x := 0 ,
π = −0.414213562373095 2
h
π .. π 64
which apparently coincide:
0
h( x) 0.2 g( x)
0.4 0
1
2 x
164
3
4.6.6 Phase diagram for a damped pendulum. As an example of this we shall draw the phase diagram for a damped pendulum. The differential equation of a damped pendulum can be written in the form y''+Ay'+Bsin(y)=0. If you substitute x0=t, x1=y', then for a suitable choice of the constants A and B the equation can be written in the form x'(t) = D(t,x), where: x1 −0.4⋅x 1 − sin( x 0)
D( t , x) :=
M1 := IC2(−20, 20, 10, −10, 10, 10, 20, 50, D, rkfixed) M2 := IC2(−20, 20, 10, −10, 10, 10, −20, 50, D, rkfixed) 10
M12 0
M22
10
20
10
0
10
20
M11 , M21
4.6.7 Other differential equation solvers. Mathcad has a number of other differential equation solvers built-in. They are useful in some situations where rkfixed fails to give good result. One of them is Rkadapt, which works similar to rkfixed, the difference being that the step lengths are chosen adaptively instead of being constant. However, the steps in the returned value are of constant length, even if the steps underlying the computation are not. Example 1.
9 x + 24 x + 5 cos( t) − sin( t) 4 1 0 3 3 D( t , x) := y0 := −24 x 0 − 51 x 1 − 9 cos( t) + sin( t) 2 3 3 165
It can be shown that the exact solution of this system is: 2 exp(−3t) − exp(−39t) + 1 cos( t) 3 F( t ) := −exp(−3t) + 2 exp(−39t) − cos( t) 3 Here is a comparison between the values returned by rkfixed, rkadapt and the exact solution: M1 := rkfixed(y0, 0 , 1 , 5 , D) M2 := Rkadapt(y0, 0 , 1 , 5 , D) 〈〉 〈 〉 〈1〉 〈〉 〈〉 〈 〉 〈2〉 〈〉 M 0 := M1 0 := M1 1 := M1 2 M 3 := M2 1 〈〉 〈〉 0 .. 5 M i , 5 := F(0.2⋅i) 0 M i , 6 := F(0.2⋅i) 1 M 4 := M2 2 t
0.2 0.4 M= 0.6 0.8 1 0
rkfixed -----------------1.333 0.667 −97.354
Rkadapt ----- Exact --------1.333 0.667 1.333 0.667
3
−9.754 × 10
1.951 × 10
1.927 × 10
7
1.903 × 10
9
1.879 × 10
−9.514 × 10 −9.396 × 10
0.909 −0.608 0.909 −0.608
4
5
−9.634 × 10
1.424 −0.875 1.424 −0.875
196.683
0.606 −0.44 0.606 −0.44 0.414 −0.323 0.414 −0.323 0.28 −0.23 0.28 −0.23
6 8
10
As you can see, Rkadapt agrees very well with the exact solution, while rkfixed does not. Mathcad also has other ODE solvers Stiffb and Stiffr, but I shall not go into details about them.
First and second order methods You may want to use first or second order methods, e.g. if you are teaching numerical mathematics. Here are the classical first and second order methods. The meaning of the parameters are the same as for rkfixed. Here is Euler's first order method: b − a Euler(x0, a, b , N, F) := h ← ⋅ ( t 0 ← a) N 〈〉 x 0 ← x0 for n ∈ 1 .. N 〈〉 〈 〉 x n ← x n− 1 t n ← t n− 1 + h
(
T
augment t , x
)
166
(
+ h ⋅ F tn− 1 , x
〈n− 1〉
)
deHeuns second order Method: deHeun(x0, a , b , N, F) :=
h ← b − a ⋅ ( t ← a) 0 N x
〈0〉
← x0
for n ∈ 1 .. N
〈n− 1〉 ( ) 〈 〉 F2 ← F( t n− 1 + h , x n− 1 F1 ← F t n− 1 , x
x
〈n〉
←x
〈n− 1〉
+
h 2
+ h ⋅ F1
)
⋅(F1 + F2)
tn ← t n− 1 + h
( T)
augment t , x The second order midpoint method: Midpoint(x0, a , b , N, F) :=
h ← b − a ⋅ ( t ← a) 0 N x
〈0〉
← x0
for n ∈ 1 .. N
(
F1 ← F t n− 1 , x F2 ← F tn− 1 + x
〈n〉
←x
tn ← tn− 1 + h
( T)
augment t , x
167
〈n− 1〉 h
,x
2 〈n− 1〉
) 〈n− 1〉 + h ⋅ F2
+
h 2
⋅F1
Example F( t , x) := identity( 1 )⋅ ( t + x 0) xx0 0 := 0 (This is how you can express a scalar problem as a vector problem.) X1 := Euler(xx0 , 0 , 1 , 5 , F)
Exact solution:
X2 := deHeun(xx0 , 0 , 1 , 5 , F)
g ( x ) := −x − 1 + exp( x )
X3 := Midpoint(xx0 , 0 , 1 , 5 , F) i := 0 .. 5 t i := 0.2⋅i
x := 0 , 0.05 .. 1
Values: M 〈0〉
M := 0 (annihilates any previous value of M) 〈〉 M 〈1〉 := X1 〈1〉 := X1 0
M 〈2〉
〈〉 := X2 1
M 〈3〉
〈〉 := X3 1
M i , 4 := g(i⋅0.2) t
Euler
deHeun
Midpoint
exact
0 0 0 0 0.2 0 0.02 0.02 0.4 0.04 0.0884 0.0884 M= 0.6 0.128 0.215848 0.215848 0.8 0.2736 0.41533456 0.41533456 1 0.48832 0.7027081632 0.7027081632
0
0.0214027582
0.0918246976
0.2221188004 0.4255409285
0.7182818285
0.6 M M M
〈1〉 〈2〉
0.4
〈3〉
g ( x)
0.2
0
0.2
0.4
0.6 t,t,t,x
168
0.8
1
4.6.8 A boundary value problem: Ballistics Instead of giving initial conditions you can give boundary conditions. For instance, you can specify that an integral curve should go through two given points. We illustrate the shooting method on an example - the motion of a body under the influence of the gravitation and air resistance. First, let's assume that the motion takes place in the x-y-plane with the gravitation force acting in the direction of the negative y axis. Then, if there is no air resistance, the equations of motion of the body are: x''(t)=0, y''(t)=-g. Substituting x0=x, x 1=x', x2=y, x3=y'(t), the equations can be written x'(t) = D(t,x), where
x1 0 D( t , x) := x3 −g
g := 9.81
x1 0 0 0 D( t , x) := + x3 0 0 −g
The second form is the sum of two vectors, the first of which comes from the substitutions, while the second comes from the the gravitational force. This system can be solved by integrating the equations x''(t)=0, y''(t)=-g twice. If the initial condition is that x(0)=0, x'(0)=v0·cos(α), y(0)=0, y'(0)=v0·sin(α), the solution can be written: 1 2 x ( v0, α , t) := v0⋅cos( α ) ⋅t y ( v0, α , t ) := v0⋅sin( α ) ⋅t − ⋅g⋅t 2 You can get y as a function of x by eliminating t between these two equations. The result is:
(
)
1 g 2 2 y ( v0, α , x ) := − ⋅ ⋅ 1 + tan( α ) ⋅x + tan( α ) ⋅x 2 2 v0 For a typical example, it looks like this: α := 30⋅deg
v0 := 200
x := 0 , 10.. 4000
500
y( v0 , α , x) 0
1000
2000
x
169
3000
4000
You can compare this to the result obtained with the Runge-Kutta method: 0 ( ) v0⋅cos α M := rkfixed , 0 , 22, 20, D 0 v0⋅sin( α ) The graphs seem to coincide: i := 0 .. 20 500
Mi ,3 y(v0 , α , x) 0
1000
2000
3000
4000
Mi ,1 , x
Now let's proceed to the air resistance. This force is very difficult to describe, since it depends on the weather conditions among others. Its dependence on the speed is also complicated. You need to make a simplification, which is acceptable under certain conditions: you assume that the air resistance is proportional to the square of the speed of the body. In the model above, if x=(x0,x1,x2,x3)T is the vector that describes the motion, the velocity is (0,x 1,0,x 3), and the speed is the length of this vector. A vector in the opposite direction of the velocity with magnitude proportional to the square of the speed must then be of the form:
0 0 x ( x 1) + ( x 3) x 1 2 2 1 −k⋅ ⋅ = −k⋅ ( x 1) + ( x 3) ⋅ 0 ( x1) 2 + ( x3) 2 0 x3 x3 2
2
Hence the equation of motion can be written x'(t)=D(t,x), where k := 0.00183 (This value is nearly correct for a standard army rifle projectile.)
170
x1 0 0 x 0 0 2 2 1 D( t , x) := + − k⋅ ( x 1) + ( x 3) ⋅ 0 x3 0 x −g 3 0 The initial condition can be expressed as a vector
v0 := 1000
0 v0⋅cos( α ) x ( α ) := 0 v0⋅sin( α ) From an artillerist's point of view, the initial speed v0 can be assumed to be a constant, while the elevation angle α is that parameter that can be controlled. You can find the solution by means of Runge-Kutta's method for several values of α: R( α ) := rkfixed ( x ( α ) , 0 , tH, 20, D)
tH := 4 20
R(2⋅deg)
〈3〉
0
200
400
600
R(2⋅deg)
i := 0 .. 10 M i := R( α i)
α i := (1.5 + i⋅0.1)⋅deg Y 〈i〉
800
1000
〈1〉
M := 0
〈〉 〈〉 := ( M i) 3 X i
〈〉 := ( M i) 1
20 Y
0
200
400
600 X
171
800
1000
Now, the question is: Given a target with coordinates (Dh,Dv), what value of α do I have to use in order to hit the target? This value of α can be found in the following way: First you choose a value of α which gives almost a hit based on a table or a diagram. For instance, if Dh := 900 Dv := 10 then it follows from the diagram above that α is not far from 2 degrees. So you use α1=2 degrees and compute the trajectory: Z1 := R( α1 )
α1 := 2⋅deg
Then you can find an approximate value of the projectile's altitude at the distance Dh by means of linear interpolation:
(
h1 := linterp Z1
〈1〉
, Z1
〈3〉
, Dh
)
h1 = 14.739
This means that α1 = 2 deg is too big. So, you try with a smaller value: This time, you find: Z2 := R( α2 ) h2 := linterp Z2 〈1〉 , Z2 〈3〉 , Dh h2 = 8.462
α2 := 1.6⋅deg
(
)
To find an approximation to the elevation αhit that gives a hit, you use linear interpolation:
h1 α1 , 0 h2 α2
H := csort
(This is to make sure that the first column of H is in ascending order, which is required by the function linterp.)
(
〈〉 α hit := linterp H 0
,H
〈1〉
, Dv
)
α hit = 1.698 deg
Alternatively, you can define αhit as a function: α := 2⋅deg
(This may have to be changed.)
(
(
α hit (Dh, Dv) := root linterp R( α )
〈1〉
, R( α )
〈3〉
You find: α hit (900 , 10) = 1.698 deg
α hit (800 , 20) = 2.209 deg
α hit (900 , 20) = 2.335 deg
α hit (1200, 0) = 2.598 deg
α hit (1200, 500) = 25.646 deg
α hit (2000, 0) = 5.579 deg 172
)
, Dh − Dv, α
)
5 Linear Algebra In section 1.6 we exposed most of the elementary operations on arrays. In this section we shall have a closer look at some standard topics from linear algebra.
5.1 Elementary row operations and Gauss elimination 5.1.1 Elementary row operations The elementary row operations can be programmed as follows: To exchange rows no. m and n in M: Multiplying row m in M by t: RX(M , m, n) := C ← cols( M ) RM(M , m, t) :=
C ← cols( M )
R ← rows( M )
c←0
c←0
R ← rows( M )
while c < C r←0
while c < C
while r < R
r←0
Ar , c ← M r , c
while r < R Ar , c ← M r , c
r←r+ 1 v ← Am , c
r←r+ 1 Am , c ← t ⋅ Am , c
Am , c ← An , c
c←c+1
An , c ← v c←c+1
A A
Notice that I've used while loops instead of the more natural for loops to make the function work symbolically. Example:
1 2 3 M := 2 4 3 RM(M , 1 , 2) = 6 4 5
1 2 3 1 2 3 4 8 6 RM(M , 2 , 2) → 2 4 3 6 4 5 12 8 10 173
To add t times row m to row n: RA(M , m, n , t) :=
C ← cols( M )
Example:
R ← rows( M )
1 2 3 M = 2 4 3 6 4 5
c←0 while c < C r←0 while r < R
M1 := RA(M , 0 , 2 , −3)
Ar , c ← M r , c
1 2 3 M1 = 2 4 3 3 −2 −4
r←r+ 1 An , c ← An , c + t ⋅ Am , c c←c+1 A
To add suitable multiples taken from column n of row m to all the other rows: RsA(M , m, n) :=
R ← rows( M ) C ← cols( M ) c←0 while c < C r←0 while r < R Ar , c ← M r , c if r = m Ar , c ← M r , c −
Mr , n Mm , n
⋅M m , c otherwise
r←r+ 1 c←c+1 A Example
1 2 3 M = 2 4 3 6 4 5
1 2 3 RsA(M , 0 , 1) = 0 0 −3 4 0 −1 174
5.1.2 Naive Gauss elimination Let's demonstrate how to use the elementary row operations to solve a system of linear equations having the following augmented coefficient matrix:
1 4 −2 4 A := 2 7 −1 −2 The function RsA will work effectively to achieve the diagonalization: 2 9 −7 1 A1 := RsA(A, 0 , 0)
A2 := RsA(A1, 1 , 1)
1 4 −2 4 A1 = 0 −1 3 −10 A2 = 0 1 −3 −7
A3 := RM(A2, 1 , −1)
1 0 10 −36 0 −1 3 −10 A3 = 0 0 0 −17
1 0 10 −36 0 1 −3 10 0 0 0 −17
The conclusion is that the system is inconsistent.
5.1.3 Gauss elimination with pivoting Suppose you have a matrix A which is the augmented coefficient matrix for a system of linear equations. The process of Gauss elimination with pivoting goes like this: First, define a function which normalizes the ith row of A. that means to divide by the coefficient of greatest absolute value. Normalize(A, i) :=
(B ← A)⋅(C ← cols( B))
( Max ←
Bi , 0 ) ⋅ ( MM ← Bi , 0)
for j ∈ 1 .. C − 1 M ← Bi , j if M > Max Max ← M MM ← Bi , j for k ∈ 0 .. C − 1 if Max > 0 Bi , k ←
Bi , k MM
B
175
Example. 1 4 −2 4
4 1 4 −2 Normalize(A, 1) = 0.286 1 −0.143 −0.286 1 2 9 −7
A = 2 7 −1 −2 2 9 −7 1
In the next function COrder, the rows of A are if necessary rearranged so that row m on, the uppermost row is the one with the greatest element in absolute value in column k: COrder(A, k , m) := B ← A for i ∈ m + 1 .. rows( B) − 1 B ← RX(B , i , m) if
Bi , k > Bm , k
B Example
1 4 −2 4 A = 2 7 −1 −2 2 9 −7 1
2 9 −7 1 COrder(A, 2 , 0) = 2 7 −1 −2 1 4 −2 4
Here is the function Rref that does Gauss elimination with pivoting: Rref ( M ) :=
B←M R ← rows( B) for m ∈ 0 .. R − 2 for i ∈ m.. R − 1 B ← Normalize(B , i) B ← COrder(B , m, m) B ← RM B , m, ( Bm , m)
−1
if Bm , m ≠ 0
for i ∈ 0 .. R − 1 B ← RA( B , m, i , −Bi , m) if i ≠ m if BR− 1 , R− 1 ≠ 0 B ← RM B , R − 1 , ( BR− 1 , R− 1)
−1
for i ∈ 0 .. R − 2 B ← RA( B , R − 1 , i , −Bi , R−1) B
176
Example.
3 −6 A := 6 12
−13 9
−19
3
4 1 −18 −34 Rref ( A) = 16 −2 2 4 −8 6 10 26
1 0 0 0
1 0 0 1 0 1 0 −2 0 0 1 1 0 0 0 3
The rref function Mathcad has a built-in function rref that performs Gauss elimination with pivoting. The output is a matrix on row reduced form, It works only numerically (at the moment):
1 0 rref ( A) = 0 0
1 0 0 1 0 1 0 −2 0 0 1 1 0 0 0 3
5.2 Rank and subspaces 5.2.1 The rank of a matrix The function that gives the rank of a matrix is of course rank: rank( A) = 4 The rank function also only works numerically. So, if you are asked to find the rank of a matrix with an undetermined parameter like:
1 2 B := 0 2
−2 0
3
0
then the functions rref and rank won't work, but
−5 −3 −2 6 you can use naive Gauss elimination with
5 15 10 0 symbolic evaluation: 6
30 20 t
B1 := RsA(B B , 0 , 0) 1 0 B1 → 0 0
−2 0
B2 := RsA(B1, B1 1 , 1)
1 0 0 −1 −3 −2 B2 → 0 5 15 10 0 10 30 20 t − 6 0 0
3
0
6
0 0 0 t − 6 4
−1 −3 −2 0
0
0
0
3
0
From this it is easily seen that the rank of B is 3, except for t=6, when it is 2. 177
5.2.2 The space spanned by the column/row vectors of a matrix In this subsection all the functions will only work numerically. Here is a function that, given a matrix M, returns a submatrix B of M such that the columns of B are linearly independent and span the same subspace as the columns of M: ColumnBasis(M) :=
C ← cols( M ) R ← rows( M ) 〈〉 〈〉 B 0 ←M 0 for n ∈ 1 .. C − 1
(
D ← augment B , M
〈n〉
)
B ← D if rank(D) > rank( B) B
1 2 3 M = 2 4 3 6 4 5
1 2 3 ColumnBasis( M ) = 2 4 3 6 4 5
You get a similar function for the row vectors by transposition:
( T)
RowBasis(M) := ColumnBasis M
1 2 3 M = 2 4 3 6 4 5
T
1 2 3 RowBasis( M) = 2 4 3 6 4 5
5.2.3 Basis for the null space of a matrix First, here are some auxiliary functions: NullV(n) := identity( n )
〈0〉
( T)
⋅0
RowV(A, m) := A
〈m〉
T
Examples
0 NullV( 3 ) = 0 0
1 2 3 M = 2 4 3 6 4 5 178
RowV(M , 1) = ( 2 4 3 )
The next function NullSpace gives a basis for the null space of a matrix A. NullSpace(A) :=
(B ← RowBasis( A))(R ← rows( B))(C ← cols( B)) (I ← 0)⋅(i ← 0)⋅( j ← 0)⋅(Null ← NullV( R )) return 0 if R = C 〈〉 if B 0 = Null 1 while B
〈i〉
= Null
for c ∈ 0 .. C − 1 Mi , c ← 0
( Mi , i ← 1) ⋅ ( IE j ← i) i←i+1 j←j+1 (I ← i)⋅(k ← 0)
R0 ← RowV(B , k)
M ← if(I = 0 , R0, stack(M , R0))
for c ∈ I.. C − 2
r1 ← rank(submatrix(B , 0 , R − 1 , I, c)) r2 ← rank(submatrix(B , 0 , R − 1 , I, c + 1)) if r1 < r2 k ← k + 1
M ← stack(M , RowV(B , k))
otherwise
(
M ← stack M , identity( C) IE j ← c + 1 j←j+1 T←M
−1
for j ∈ 0 .. last( IE) X
〈 j〉
← T⋅identity( C)
X 179
〈( IE j )〉
〈c+ 1〉
T
)
Example To solve a system of homogeneous equations, you can use a symbolic solve block: Given x − 2y = 0
2x − 5y − 3z − 2u = 0
5y + 15z + 10u = 0
2x + 6y + 30z + 20u = 0
−6⋅z − 4⋅ u −3⋅z − 2 ⋅ u Find(x , y , z, u) → z or the solve keyword on a vector of u equations: x − 2y = 0 2x − 5y − 3z − 2u = 0 solve , x , y , z , u → ( −6⋅z − 4⋅ u −3⋅z − 2 ⋅ u z 5y + 15z + 10u = 0 2x + 6y + 30z + 20u = 0 In any case, the solution vector can be written in the form: −6 −4 −3 −2 z⋅ + u⋅ 0 1 0 1
Thus, a basis for the null space consists of
−6 −4 −3 and −2 1 0 0 1
Using the function NullSpace on the system's coefficient matrix, you get this result directly:
1 2 A := 0 2
−5 −3 −2 5 15 10 6 30 20 −2 0
0
−6 −3 NullSpace( A) = 1 0
−4
−2
1 0
Exercises. Find the null space for the following matrices:
1 1 2 0 1 2 2 A1 := 0 1 −1 −1 0 A2 := 3 2 3 1 3 4 −1
−2 1
−1
−3 4
−3
3 1 1 A3 := 6 2 2 −5 5 −4 −9 −3 −3 1 −3 2
180
5.2.4 Gram-Schmidt's orthonormalization process Suppose you have a basis for a subspace of Rn given as the column vectors of some matrix M. The Gram-Schmidt orthogonalization process returns an orthogonal base given in a matrix E such that the first m column vectors of E span the same subspace as the first m vectors of M. len( V) := V⋅ V
The length of a vector symbolically: GramSchmidt(M) :=
C ← cols( M ) 〈〉 M 0 E← 〈〉 len M 0
(
)
n←1 while n < C a←M
〈n〉
i←0 while i < n
(
a←a− M
〈n〉
〈i〉
⋅E
) ⋅E
i←i+1 E ← augment E,
a
len( a)
n←n+1 E This will return a matrix similar to the argument M. Example 1.
1 1 1 M := 1 −2 2 1 1 3
1⋅ 3 1⋅ 6 3 6 1 −1 ⋅ 6 E→ ⋅ 3 3 3 1 1 ⋅ 3 ⋅ 6 3 6
−1 2
E := GramSchmidt( M )
⋅ 2
0.577 0.408 −0.707 0 = 0.577 −0.816 0 0.577 0.408 0.707 1 ⋅ 2 2 181
〈i〉
Control: i := 0 .. 2
j := 0 .. 2
1 0 0 X = 0 1 0 0 0 1
Xi , j := E
〈i〉
〈 j〉
⋅E
Yi , j := E
〈i〉
⋅E
〈 j〉
1 0 0 Y = 0 1 0 0 0 1
5.2.5 Completion to a full base Suppose you are given a linearly independent set of n-vectors as the column vectors of some matrix M, and you are asked to find an n×n matrix whose column vectors span all of Rn and whose first column vectors are the given vectors. Then you can find suitable vectors that are orthogonal to the given vectors. This is equivalent to finding the nullspace of M, and you can use the function NullSpace from section 5.2.2 (p. 169) to find this: Definitions from 5.1
Complete(M) :=
( T)
N ← NullSpace M augment(M , N)
Example
1 1 A := 1 0
−2 1 1 1
1 1 Complete ( A) = 1 0
1
−1 −0.333
−2 0
0.333
1
1
0
1
0
1
5.3 Eigenvalues and quadratic forms 5.3.1 Eigenvalues and eigenvectors. Mathcad has the functions eigenvals and eiegenvecs to find eigenvectors and eigenvalues. They work both numerically and symbolically, but these two don't gove exactly the same result:
5 4 −2 A := 4 5 2 E := eigenvals( A) ES := eigenvals( A) −2 2 8 EV := eigenvecs( A) EVS := eigenvecs( A) 182
0 9 E= 9 ES → 9 9 0 1 0 ⋅ 5 5 1 0 ⋅ 5 EVS → 5 2 −2 ⋅ 5 ⋅ 5 5 5
−0.278 0.691 0.667 EV = 0.192 0.72 −0.667 0.941 0.058 0.333 −2 3 0 0.447 −0.667 2 0 0.667 = 0.447 3 0.894 −0.894 −0.333 −1 3
The symbolic version is dangerous to use for diagonalizations, since the column vectors are not orthogonal. But the you can use the Gram-Shmidt orthogonalization process to get an orthogonal matrix:
0 1 E2 := GramSchmidt(EVS) → ⋅ 5 5 2 ⋅ 5 5
2 4 ⋅ 5 15 3 −1 −2 ⋅ 5 15 3 1
3
⋅ 5
−2 3
You can check it out: i := 0 .. 2
〈i〉
Di := A⋅ EV
− Ei⋅EV
〈i〉
DS i := A⋅ E2
〈i〉
− ESi⋅E2
〈i〉
T
D = (0 0 0 ) T
DS → ( 9 0 9 )
Furthermore:
9 0 0 E2 ⋅A⋅E2 → 0 9 0 0 0 0
9 0 0 EV ⋅A⋅EV = 0 9 0 0 0 0
−1
−1
This means that the quadratic form: 2
2
2
Q( x , y , z) := 5⋅x + 5 ⋅ y + 8 ⋅ z + 8x⋅y + 4y⋅z − 4x⋅zor Q( x ) := 5⋅ ( x 0) + 5 ⋅ ( x 1) + 8 ( x 2) + 8 x 0⋅x 1 + 4 x 1⋅x 2 − 4 x 0⋅x 2 2
2
2
can be transformed into the quadratic form 2
QQ(X, Y, Z) := 9Y + 9Z
2
or
QQ( X) := 9 ( X1) + 9 ( X2) 2
183
2
by the transformation x = E2⋅X
x 2 2 QE2⋅ y simplify → 9⋅y + 9 ⋅ x z
Note that you don't get the correct result if you use EVS directly:
x You have to use −72 2 2 Gram-Schmidt Q EVS⋅ y simplify → ⋅x⋅y + 9 ⋅ y + 9 ⋅ x 5 orthogonalization first. z A two dimensional example Classify the curve given as 6x 2 + 4x⋅y + 9y 2 = 1 The quadratic form has the matrix A :=
Solution:
6 2 2 9
−2 ⋅ 5 5 5 E := eigenvals( A) → EV := eigenvecs( A) → 10 1⋅ 5 5
Compute
1
⋅ 5
2 ⋅ 5 5
5
Note that Mathcad apparently has a bug here: The first eigenvalue corresponds to the second eigenvector and vice versa: A⋅ EV A⋅ EV
〈0〉 〈1〉
−2 ⋅ 5 −4⋅ 5 〈0〉 → 10⋅EV 5 2⋅ 5 5 2⋅ 5 〈1〉 → → 5 ⋅ EV 4⋅ 5 2⋅ 5
→
In the cases where no eigenvalues are multiple, the matrix of eigenvectors is orthogonal, so you can use it directly: That means that the quadratic form:Q( x ) := 6 ( x 0) 2 + 4 x 0⋅x 1 + 9 ( x 1) 2 is transformed into
QQ( X) := 10⋅ ( X0) + 5 ⋅ ( X1) 2
by the coordinate transformation x = EV⋅X
x simplify → 5⋅x 2 + 10⋅y 2 y
QEV⋅
184
2
In the two dimensional case you can illustrate this by drawing a contour plot of the quadratic. We use the function CreateMesh: 2
f ( x , y) := 6x + 4x⋅y + 9y
2
MM := CreateMesh(f , −1 , 1 , −1 , 1 , 40, 40)
For comparison, you could find the points of minimal distance from the origin: x := 0
y := 0
2
g ( x , y) := x + y
2
x1 := Maximize(g , x , y) x1 = 0.4 y1 y1 −0.2
Given 6x + 4x⋅y + 9y = 1 2
2
x2 := Minimize(g , x , y) x2 = 0.141 y2 y2 0.283
Given 6x + 4x⋅y + 9y = 1 2
2
These are eigenvectors of A:
x1 = 0.4 5 y1 −0.2
A⋅
x2 = 0.141 10 y2 0.283
A⋅
This indicates that the axes of the ellipse are eigenvectors of A.
185
Index A accessing the elements of an array 26
Add Line 41
addition of an array and a scalar 29
Airy’s equation 143
algebra 67
angle between vectors 122
animation 65
area between graphs 110
area bounded by parameter curve 111
area bounded by polar curve 111
arrays 25ff
ASCII files 30
assignment in a program 41
asteroid 93
augment 31
axis format 34
B ballistics 169
Bernoulli’s equation 137
binary 49
binomial coefficient 46
binormal vector 125
boundary value problem 164, 169
break 42
built-in functions 19
C calculator toolbar 12
calculus 83 ff.
cardioid 37, 92
ceil 46
centroid 114
change of coordinates in double
integral 121
characteristic polynomial 138
closing a file 16
collect 72
column vector of a matrix 26
combin 46
combinatorics 46
completion to a full base 182
concat 62
continue 43
contour plots 53, 98
creating an array or vector 25
curl 127
curvature 89, 90, 125
curve length 112 ff.
cycloid 90
D damped pendulum 165
decimal 49
defining a function 32
defining a variable 20
defining array by a formula 27
defining big arrays 30
definite integrals 106
deHeun’s method 167
derivative as a limit 83
derivative of vector function
123
determinant 29
difference equations 146
differential equations 135 ff.
differential equations of order
one 135
differential equations of order
two or higher 138, 141
differential geometry 126
differentiation 83 ff.
differentiation of implicit
functions 86
dimension 23
discrete Fourier transform 153
divergence 127
double integral 121
E eigenvalues 182 ff. eigenvectors 182 ff.
187
elementary operations 11
elementary row operations 173 ff.
elements of an array 26
ellipsoid 59
envelope 93
equation format 21
equations 77
Eratosthenes’ sieve 48
error function 44
Euler’s method 166
evaluation style 68
Evaluation toolbar 18
evolute 89
expand 67, 70
expand nested arrays 30
extra math symbols 61
extrema 94, 97, 99
extrema with side conditions 101
H helix 57
Hessian 99
hexadecimal 49
hitting a target 172 ff.
I if 43
improper integrals 108
indefinite integral 104, 135
index operator 26
inflection point 95
inhomogeneous ODEs with
constant coefficients 139
integral curves 158
integration 104
J
F
Jacobian 133
factor 71
families of integral curves 158, 162
FFT 153
Fibonacci numbers 48
first index of an array 28
float 76
floor 46
for 41
Fourier coefficient 149
Fourier series 149 ff.
Fourier transform 151
functional arguments 38
L Lagrange multipliers 101 Laplace transforms 144 Laplacian 127 least common multiple 46 limits 83 line integrals 126 ff. linear algebra 173 ff. linear equations 80, 136 linear optimisation 103 local maximum 94 local minimum 94 loops 41
G Gauss elimination 175
global assignment 20
gradient 127
Gram-Schmidt 181 ff.
graph of a function of two variables 52
graphs 33
greatest common divisor 46
Green’s theorem 129
188
M Math toolbar 12
matrix addition 28
matrix inversion 29
matrix multiplication 28
matrix transpose 29
Maximize 95
Mersenne numbers 47
polynomial coefficients 76 polynomial equations 79 power series solution of ODEs 142 prime number factorisation 47 prime number function 48 prime numbers 47 principal normal vector 125 printing a file 16 printing to a file 16 product symbol 51 programming 40 Pythagorean triples 49
method of undetermined coefficients 139 midpoint method 167 Minimize 95 moments of inertia 114 ff. multiplication of array by scalar 29
N naiv Gauss elimination 175
nested array 30, 55
newton 24
normal 87, 88
normal to a surface 130
null space 178
num2str 62
number bases 49
number of rows and columns of a
matrix 28 numerical solution of differential equations 155
Q quadratic forms 182 quick sheet 61
R radius of curvature 126
radius of torsion 126
range product 51
range sum 50
range variable 22
rank 177
READPRN 31
resource center 61
result format 18
return 44
Rkadapt 166
rkfixed 159
root 77
round 47
row reduced form 177
rref 177
Runge-Kutta 159
O octal 49
opening an existing file 16
ORIGIN 28
otherwise 43
P parameter curve 36
parameter surfaces 54
parfrac 73
partial derivative 84, 96
partial fractions 73
pendulum 165
periodic extension 148
permut 46
phase diagram for damped pendulum
165
Pi 33
pivoting 175
placeholder 14
plane areas 109 ff.
polar curve 36
S saving a file 15
scalar field 127
scalar product of vectors 28
search 62
189
second order ODEs solved numerically 163 separable equations 135 series 96 several 3D objects 58 several equations solved numerically 79, symbolically 81 shooting method 169 simplify 69 solving an equation numerically 77, symbolically 78 space curves 57 special characters in text 60-61 speed 125 sphere 54 stack 31 starting a new file 15 starting Mathcad 11 step functions 150 str2num 62 str2vec 62 strlen 62 submatrix 29 subscript 60 substitute 74 substr 62 summation symbol 50 superscript 60 surface area of a surface of revolution 119 ff. surface integrals 130 ff. surface of revolution 116 surface plot 52 symbolic computation 17 symbolic differentiation 84 symbolic evaluation 68 symbolic float keyword 18 symbolic mathematics 67 symbolic processor 17
text blocks 60
text on a 2D graph 63
text variables 62
three dimensional plots 52
torsion 125
torus 54
total differentials 137
transpose 29
trapezoid rule 40
trunc 47
U units 23
user-defined functions 32
V value of an expression 39
variable 20ff
variable names 21
vec2str 62
vectors 25, 122
vector calculus 122 ff.
vector field, 127, 155
vector field plot 155 ff.
vector length 29
vector sum 50
vector valued functions 123
vectorize 32
volume of a surface of
revolution 117
W while 42
X XY-plots 34
Z
T
z transform 147
tangent 87, 88, 125 Taylor series 96 text 60
190