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 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
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