Introduction To Mathematica
Introduction To Mathematica
Introduction to
Mathematica®
with
Applications
Introduction to Mathematicar with Applications
Marian Mureşan
Introduction to
Mathematicar
with Applications
123
Marian Mureşan
Faculty of Mathematics and Computer Science
Babeş-Bolyai University
Cluj-Napoca, Romania
vii
viii Foreword
This book is the output of our work over several years. Being involved in
calculus of variations and optimal control problems, we have realized that an
exact calculation and a suggestive visualization are very useful, making the ideas
addressed many times only in an "-ı language clearer. Then we have chosen
Mathematica for computations and visualizations of the ideas. Why did our option
go to Mathematica? The answer is simple: because we had noticed the wonderful
results of Prof. J. Borwein and his colleagues regarding the decimals of number .
Their approach was based on an extensive use of Mathematica.
Wolfram Research, located at Champaign, IL, USA, is the company which has
been developing Mathematica.
Mathematica is continuously developing. We used Mathematica 10.3. It is very
likely there will be newer versions with extra facilities in the future.
We have introduced notions and results in Mathematica in our lectures to master
students at the Faculty of Mathematics and Computer Science of the Babeş-Bolyai
University in Cluj-Napoca, Romania. We did the same thing with our PhD students
at three summer schools organized in the framework of the grant “Center of
Excellence for Applications of Mathematics” supported by DAAD, Germany. The
summer schools have been organized in Struga (Macedonia, FYROM), Sarajevo
(Bosnia and Herzegovina), and Cluj-Napoca (Romania).
This book is not very large, but it collects many examples. In the first part of
the book, the examples are discussed in detail helping the reader to understand the
reasoning in and with Mathematica. Later on, the reader is led to use the benefit
of the Help and other sources freely offered by Wolfram Research. We take into
account mainly the Wolfram community forum as well as the video training and
conferences generously offered by Wolfram Research.
A well-motivated case for visualization in mathematics is contained in [58].
Here is the right place to express my gratitude to the following colleagues of
mine from the Faculty of Mathematics and Computer Science of the Babeş-Bolyai
University for their support: Anca Andreica, Valeriu Anisiu, Paul Blaga, Virginia
ix
x Preface
Niculescu, Adrian Petruşel, and Adrian Sterca. The existence and development of
the MOS (Modeling, Optimization, and Simulation) Research Center of our faculty
was a real help for us in the preparation of this book.
1 About Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 First Steps to Mathematica. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 The Introductory Techniques for Using Mathematica . . . . . . . . . . . . . . 5
2.1.1 Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.2 Bracketing in Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1.3 Set or SetDelayed Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1.4 Some Simple Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Basic Steps to Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1 Problems in Number Theory, Symbolic Manipulation,
and Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.1 Problems in Number Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.2 Symbolic Manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.3 Texts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 Riemann Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 Some Numerical Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.1 The First Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.2 The Second Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.3 The Third Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.4 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.5 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.5.1 Operations with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.5.2 Operations with Matrices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.5.3 Inner and Outer Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.5.4 Again on the Third Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
4 Sorting Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.2 Sorting Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
xi
xii Contents
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Chapter 1
About Mathematica
1.1 Introduction
1.1.1 Warning
2.1.1 Numbers
7:1=2:33 (* Division *)
3.04721
7/6 (* Division *)
7
6
(* The result is given as a fraction. If we wish a result under the form
of a decimal number, use the built-in function N[ ] and write *)
Pi (* or *) (* Input *)
we get that
(* and *)
i.e., no numerical result is returned. To get the expected numerical result, we have
to write
N[Pi,15]
3.14159265358979
2.1 The Introductory Techniques for Using Mathematica 7
or
N[,15]
3.14159265358979
and Mathematica returns it with 15 decimals. Another example is given below:
1 8
2
(* We wrote this expression with the Writing Assistent menu in
Palletes *)
1
256
If we want a numerical result with 10 decimals, we write
h 8 i
N 12 ; 10
0.003906250000
Two interesting cases follow:
2/0
2/1
Power::infy: Infinite expression 10 encountered.
ComplexInfinity
0
In the first case, the answer contains a warning message and a result (Com-
plexInfinity), while in the second case the result is correct according to the usual
convention [53, p. 30].
We now present some notations and built-in functions.
N, Z, R, C (* Used for the sets of natural, integer, real, and complex
numbers *)
x+y
p I (* The complex number x+iy; I is reserved for the imaginary unit
1 *)
Re[z] (* The real part of a complex number z; Re is a built-in
function *)
Im[z] (* The imaginary part of a complex number z; Im is a built-in
function *)
Conjugate[z] (* The complex conjugate of a complex number z;
Conjugate is a built-in function *)
Abs[z] (* The absolute value of a complex number z; Abs is a built-in
function *)
Arg[z] (* The argument in z D jzj.cos C i sin /, the trigonometric
form of a complex number z; Arg is a built-in function *)
N, Z, R, C
x+y I
Re[z]
Im[z]
Conjugate[z]
Abs[z]
Arg[z]
8 2 First Steps to Mathematica
p p p p
Suppose we want to see whether 3 C 5 C p 7C p 11pis an p algebraic number.
Using the Writing Assistant Palette, we write 3 C 5 C 7 C 11 2 Algebraics
and understand it as a question, i.e., “is it true that the sum belongs to the set of
algebraic numbers?”
p p p p
3 C 5 C 7 C 11 2 Algebraics
True
Another way to check that the sum is an algebraic number consists in per-
forming
p p the p following
p steps. First we generate the list f3; 5; 7; 11g, transform it as
f 3; 5; 7; 11g, add its elements, and finally we verify whether it is true that
their sum is an algebraic number or not.
Plus@@Sqrt[f3,5,7,11g]2 Algebraicsnp (* First we mention that
p p p o
Sqrt[f3,5,7,11g] is equivalent to 3; 5; 7; 11 : Here @@ is a
substitution for the
h built-in
np function Apply and the left-hand side is
p p p oi
interpreted Apply Plus; 3; 5; 7; 11 , which gives
p p p p
3 C 5 C 7 C 11. Finally we ask if this sum is an algebraic
number or not *)
True
2.1.1.1 Divisibility
We want to find out how many zero digits the number 32Š has at the end.
ı
IntegerQ 32Š 107 (* Is 32Š=10^ 7 an integer? Yes. This means that
32Š ends withıat least
7 zeros *) ^
IntegerQ 32Š 108 (* Is 32Š=10 8 an integer? No. This means that
32Š ends with exactly 7 zeros *)
True
False
Another example of divisibility follows below.
IntegerQŒ75^ 75 98^ 98=10^ 98 (* Is 75^ 75 98^ 98=10^ 98 an integer?
The answer is yes *)
ModŒ75^ 75 98^ 98; 10^ 98 (* We test the same thing with the
built-in function Mod *)
IntegerQŒ75^ 75 98^ 98=10^ 99 (* The answer is no and we conclude
the number 75^ 75 98^ 98 has exactly 98 zero digits at the end *)
True
0
False
2.1 The Introductory Techniques for Using Mathematica 9
The = operator (called the Set operator) evaluates the expression on its right-hand
side only once, when the assignment is made.
x=3 (* or *) Set[x,3]
The value of the variable x is 3 and it is permanent. It is kept until a different
assignment regarding it occurs or is explicitly removed.
A certain value can be simultaneously set to several variables as
x=y=value (* The same value is set to both variables *)
value
To remove the value of a variable, we write
x=. (* or *) Clear[x]
The := operator (called the SetDelayed operator) used in defining functions
differs from the = operator. When we use := the expression appearing on its right
is evaluated anew each time the expression appearing on its left is called. There are
situations when = and := can be used interchangeably. There are cases when one is
appropriate and the other is not.
a:=3 (* or *) SetDelayed[a,3]
Mathematica uses both upper- and lowercase letters. There is a convention in
Mathematica that the built-in objects always have names starting with uppercase
(capital) letters. To avoid confusion, one should always choose names for one’s own
variables that start with lowercase letters.
mnop is a variable (independent or dependent) name introduced by
the user
Mnop is a built-in object whose name begins with a capital letter
10 2 First Steps to Mathematica
Plus@@.a b c/;
Plus@@.a=b=c/;
Plus@@fa b cg (* This is Apply[Plus, fa b cg], in the curly
braces we have precisely one number *)}
PowerŒx; yDDPower@@fx; ygDD Power@@.x C y/DD
Power@@.x y/DDxy (* We conclude that we can write xy in either
way *)
1 1
a C b C c; a C b C c; a C C ; abc
b c
True
Find all the pairs .n; m/ for 1 n m 20 such that n2 C m2 is a squared number
(Pythagorean pair or Pythagorean numbers). One approach consists in printing all
the satisfactory pairs step by step. If n and m are larger, the resulting list could be
rather large.
Table[ p
If[IntegerQ@ n2 C m2 ==True, (* IntegerQ tests whether its argument is
explicitly an integer *) p p
Print[”n = ”,n,”, m = ”,m, ”, n2 C m2 =”, n2 C m2 ]],fn,20g,fm,n,20g];
Euler’s conjecture was proposed in 1769. It states that for all integers n and k greater
than 1, if the sum of n kth powers of positive integers is itself a kth power, then n
3.1 Problems in Number Theory, Symbolic Manipulation, and Calculus 15
P
is greater than or equal to k, i.e., niD1 ai k D bk ; where n > 1 and a1 ; a2 ; : : : ; an ; b
are positive numbers, then n k. He also provided a complete solution to the four
cubes problem 33 C 43 C 53 D 63 , i.e.,
33 C 43 C 53 DD 63
True
We will study some particular cases.
Do[
Do[
Do[
h i
p
If IntegerQ@ 3 i3 C j3 C k3 DD True; PrintŒ”i D ”; i; ”; j D ”; j; ”; k D ”; k ,
fk,j,20g],
fj,i,15g],
fi,10g]
i = 1, j = 6, k = 8
i = 2, j = 12, k = 16
i = 3, j = 4, k = 5
i = 3, j = 10, k = 18
i = 6, j = 8, k = 10
i = 7, j = 14, k = 17
i = 9, j = 12, k = 15
We can approach this exercise from another point of view.
n p o
header3= i; j; k; ” 3 i3 C j3 C k3 ” ; (* This is the header of the grid *)
n p o
f3[fx ,y ,z g]:= x; y; z; 3 x3 C y3 C z3 ; (* It transforms a triplet into a
four-component list *)
triple=fg; (* The list of triples is empty at the beginning *)
Do[Do[Do[
h i
p
If IntegerQ@ 3 i3 C j3 C k3 DD True; AppendToŒtriple; fi; j; kg ;
fk,j,30g],fj,i,25g],fi,10g] (* The commutativity allows us to consider
only the cases 1 i j k *)
Grid[Join[fheader3g,f3/@triple],Frame!All,Alignment!Right,
FrameStyle!Thin]
16 3 Basic Steps to Mathematica
p
3
i j k i3 C j3 C k3
1 6 8 9
2 12 16 18
3 4 5 6
3 10 18 19
3 18 24 27
4 17 22 25
6 8 10 12
7 14 17 20
9 12 15 18
11 15 27 29
12 16 20 24
15 20 25 30
18 19 21 28
18 24 30 36
On the same line, we have 304 C 1204 C 2724 C 3154 D 3534 , i.e.,
304 C 1204 C 2724 C 3154 DD3534
True
p
Table[If[IntegerQ@ 4 i4 C j4 C k4 C m4 ==True,
Print[“i = ”,i,“, j = ”,j,“, k = ”,k,“, m = ”,m]],
fm,300,315g,fk,250,272g,fj,100,120g,fi,25,30g];
i = 30, j = 120, k = 272, m = 315
For k D 5, the conjecture was disproved by L. J. Lander and T. R. Parkin in 1966
[39] when they found the following counterexample 275 C 845 C 1105 C 1335 D
1445 , i.e.,
275 C 845 C 1105 C 1335 DD1445
True
For k D 4, Noam Elkies in [22] found a counterexample which states that
26824404 C 153656394 C 187967604 D 206156734 , i.e.,
26824404 C 153656394 C 187967604 DD 206156734
True
In 1988, Roger Frye in [27] subsequently found the smallest possible counterex-
ample for k = 4: 958004 C 2175194 C 4145604 D 4224814 , i.e.,
958004 C 2175194 C 4145604 DD 4224814
True
n
Table[PrimeQ 22 ,fn,10g] (* PrimeQ offers an answer whether its
argument is prime or not; then the corresponding Boolean value is
assigned to a list *)
{True,True,True,True,False,False,False,False,False,False}
n
Table If PrimeQ 22 C 1 DD False; Print Œ”for
n D ”; n; ”; number D
n n
22 C1 D"; 22 C 1; “is not a prime” ; fn; 10g I (* Display the
n
nonprime numbers
h of ithe form 22 C 1 for all n2 f1,2,: : :,10g *)
5
FactorInteger 22 C 1 (* For a nonprime number, we display
its decomposition
h in powers
i of primes *)
6
FactorInteger 22 C 1
n
for n = 5, number = 22 +1 = 4294967297 is not a prime
n
for n = 6, number = 22 +1 = 18446744073709551617 is not a prime
2n
for n = 7, number = 2 +1 = 340282366920938463463374607431768211457
is not a prime
n
for n = 8, number = 22 +1 = 115792089237316195423570985008687907853
269984665640564039457584007913129639937 is not a prime
n
for n = 9, number = 22 +1 = 134078079299425970995740249982058461274
7936582059239337772356144372176403007354697680187429816690342
7690031858186486050853753882811946569946433649006084097 is not
a prime
n
for n = 10, number = 22 +1 = 1797693134862315907729305190789024733
61797697894230657273430081157732675805500963132708477322407536
02112011387987139335765878976881441662249284743063947412437776
78934248654852763022196012460941194530829520850057688381506823
42462881473913110540827237163350510684586298239947245938479716
304835356329624224137217 is not a prime
ff641; 1g; f6700417; 1gg
ff274177; 1g; f67280421310721; 1gg
Now we recompose the number from FactorInteger and compare it with the
initial number.
power[fx ,y g]:=Power[x,y]; h 5 i 5
Times@@power/@FactorInteger 22 C 1 ==22 C 1
True
2 ;2 ;2
Some binomial identities are exhibited below:
Clear[n,m]
Pk Pk
FullSimplify[f
Pn mD0 Binomial[n+m,m],
Pn mD0 Binomial[n+m,n],
k
PmD1 m Binomial[n,m], kD0 Binomial[n+k,k]Divide[1,2
Pm ],
n i
Binomial[n,i]
˚ iDm -1+n Binomial[i,m], iD0 .1/ Binomial[n,i]g]
With[{m=10},
If[Times@@Table[Boole[sumBell[n]==BellB[n]],{n,m}]==1,
Print[”True”],
Print[”False”]]]
f1; 2; 5; 15; 52; 203; 877; 4140; 21147; 115975g
f1; 1; 1; 1; 1; 1; 1; 1; 1; 1g
True
Another example of symbolic p manipulation requires checking whether
tan.3=11/ C 4 sin.2=11/ D 11.
Simplify[Tan[3/11]+4Sin[2/11]] (* The built-in function Simplify is of
no help here *)
FullSimplify[Tan[3/11]+4Sin[2/11]] (* The built-in function
FullSimplify
is of real
help *)
Cot 5 C 4Sin 2
p 22 11
11
qp
By the next example, we show that 3 64 .22 C .1=2/2 / 1 D 4.
q
p3
Simplify 2 2
64.2 C .1=2/ / 1 (* The result immediately follows *)
4
We show that eI D 1:
ExpŒI (* The result follows at once *)
1
3.1.3 Texts
An interesting example with texts similar to the one mentioned earlier may be
found in [65, pp. 6–7].
Here there are the values of the Riemann function corresponding to some values
of the argument p.
Table[Zeta@p,fp,9g]
P
Table Limit nkD1 k1p ; n ! 1 ; fp,9g (* We look for the values of
function
Riemann P as limits *)
Table N Limit nkD1 k1p ; n ! 1 ; fp; 3; 9; 2g
n 2 4
o
6 8
ComplexInfinity; 6 ; ZetaŒ3; 90 ; ZetaŒ5; 945 ; ZetaŒ7; 9450 ; ZetaŒ9
n 2 4
o
6 8
1; 6 ; ZetaŒ3; 90 ; ZetaŒ5; 945 ; ZetaŒ7; 9450 ; ZetaŒ9
f1:20206; 1:03693; 1:00835; 1:00201g
p
n
Fig. 3.2 The sequence n
xnC1 D 1 xn 2 ; x1 D a 2 0; 1Œ ; n 2 N :
We take a look at the behaviorof the terms of the sequence considering three
p .
different initial values :25; :9; and 1 C 5 2: The following code is helpful:
3.4 Variables
Just like in mathematics, we need and use variables of different sorts and for
different purposes.
The next command sets the value of the variable x to 7:
x=7
7
If we do not want to display this number, we write
x D 7I . A semicolon “I00 stops printing the value of the variable /
N@PowerŒx;
p .7/1 (* Instead, we want to know the numerical value
7
of 7 *)
1:32047
Now we can use this variable in computations such as
x+Ex +Log@x+Sin@x+Tan@x+Floor[x+.25]+Ceiling[x-.25]
21+e7 +Log[7]+Sin[7]+Tan[7]
In certain cases, if we want a numerical result, then we write
N[%,12] (* % means that the previous result is taken into account
and we ask for a result of 12 *)
1121.10750316
If the input variable is a non-integer real number, we immediately get the desired
result with 5 decimals without using the built-in function N.
x=7.00001
x+Ex +Log@x+Sin@x+Tan@x+Floor[x+.25]+Ceiling[x-.25]
7.00001
1121.12
As we have already seen, if we add a semicolon (;) at the end of an insertion line,
the result is not returned, but it is calculated and kept.
x=2.00001;
x+Ex +Log@x+Sin@x+Tan@x+Floor[x+.25]+Ceiling[x-.25]
N[%,8]
12.8066
24 3 Basic Steps to Mathematica
3.5 Lists
A list is a sequence of objects. It is not a set (in a mathematical sense) because the
rank of an element in a list counts, which is not the case of a set. A proper synonym
of list (in the sense of Mathematica) is sequence (in mathematical sense).
Here are two lists of numbers having the same number of elements. We add and
subtract them, rise to power, and perform other operations. The operations are
performed componentwise.
aa=f1,3,4,2,-3g; (* The first list *)
bb=f2,-3,8,-7,1g; (* The second list *)
faa+bb, (* In order to save space, the results of the operations on the
two lists appear as members of a list *)
bb-aag
faa2 +1, (* The operations are performed componentwise *)
Exp[%]//N,
aa/(bb-1.5), (* Division is operated componentwise *)
xxf1;4;11g ;
f1,2,5.1gf1;4;11g (* The two lists must have the same number of
elements *)g
ff3; 0; 12; 5; 2g; f1; 6; 4; 9; 4gg
ff2; 10; 17; 5; 10g; ff20:0855; 1:; 162755:; 0:00673795; 0:135335g;
{2.71828,0.00247875,54.5982,0.00012341,54.5982}},
{2.,-0.666667,0.615385,-0.235294,6.},fxx; xx4 ; xx111 g,
f1; 16; 1:647130704063058J 108 gg
We often need to know the values of certain elements in a list. There are two
ways to extract some elements in a list.
a=f1,4,5,2,-7g
fa[[3]], (* We extract the third element indicating the rank of the chosen
element *)
Part[a,4], (* We extract the fourth element *)
a[[f5,3g]], (* We extract the fifth and third element, in this order.
The result is a list of two elements *)
Part[a,f5,1g], (* We extract the fifth and first element, in this order.
Again the result is a list *)
a[[-2]] (* It counts from the end, i.e., the second element from the
end *)g
f1; 4; 5; 2; 7g
f5; 2; f7; 5g; f7; 1g; 2g
3.5 Lists 25
Clear[a]
a=f1,4,9,13g
Length@a (* It returns the length of the list *)
fFirst@a, (* It extracts the first element *)
Last@a, (* It extracts the last element *)
Drop[a,3], (* It gives the list with its first three elements dropped *)
Drop[a,f2,4g], (* It gives the list with the second until the fourth
elements dropped *)
Rest@a, (* The first element is removed *)
Most@a, (* The last element is removed *)
Take[a,f2,4g], (* It gives the specified elements in a list *)
Most@Rest@a==Rest@Most@ag (* We ask if the left-hand side
coincides with the right-hand side *)
{1,4,9,13}
4
{1,13,{13},{1},{4,9,13},{1,4,9},{4,9,13},True}
Clear[b,c,d]
fFirst c2 C c C b2 C b C d2 ; (* The elements are sorted ascending;
thenthe first element is selected *)
Last c2 C b2 C d2 C d , (* The elements are sorted ascending; then
the last
element is selected
*)
Drop c2 C b2 C d2 ; 2 , (* The elements are sorted ascending; then
the first
two elements are dropped *)
Rest c2 C b2 C d2 ; (* The elements are sorted ascending; then the
first element
is removed
*)
Most b2 C d2 C c ; (* The elements are sorted ascending; then the last
element
˚ 2 2is removed *)g
b; d ; d ; c2 C d2 ; b2 C c
a=ff1,4g,9,f13,f14,15ggg
Flatten@a (* It flattens out the nested list *)
Flatten[fa[[2]],a[[3]],a[[1]]g]
{{1,4},9,{13,{14,15}}}
{1,4,9,13,14,15}
{9,13,14,15,1,4}
Clear[b,c]
FlattenAt[fa,fb,cg,fd,eg,ffgg,ff2g,f4gg] (* FlattenAt flattens the
positions 2 and 4 *)
{{1,4,9,13},b,c,{d,e},f}
A list can be rearranged as the following examples show. We use the built-in
function Sort and change the places of certain elements according to some criteria.
Clear[a]
a=f1,9,4,4,16g;
Sort@a (* Implicitly it sorts ascending *)
Sort[a,Greater] (* This sorts in descending order *)
Sort[a,#1>#2&] (* We ask for a descending order *)
Sort[ffa,2g,fc,1g,fd,3gg,#1ŒŒ2 < #2ŒŒ2&] (* It sorts ascending with
respect to the second component *)
Sort[ffa,2g,fc,1g,fd,3gg,#1ŒŒ1 < #2ŒŒ1&] (* It sorts ascending with
respect to the first component *)
{1,4,4,9,16}
{16,9,4,4,1}
{16,9,4,4,1}
{{c,1},{{1,9,4,4,16},2},{d,3}}
{{{1,9,4,4,16},2},{c,1},{d,3}}
A deeper discussion on the sorting problem will be given in the next chapter.
Some elements of a list can be reset. Let us reset the third element of the list.
3.5 Lists 27
u=f1,3,9g;
u[[3]]=~;
u
{1,3,~}
Manipulation of lists.
Clear[a,b,c,d,e,x,y,t,u]
Join[fa,c,bg,fx,yg,ft,ag] (* Join concatenates lists. The result is an
unsorted list *)
{a,c,b,x,y,t,a}
m=ff1,2g,f3,4gg
ArrayFlatten[ff0,0,mg,fm,m,0gg]//MatrixForm (* ArrayFlatten
creates a single flattened matrix from a matrix of matrices *)
{{1,2},{3,4}}
0 1
000012
B0 0 0 0 3 4C
B C
@1 2 1 2 0 0A
343400
m=ff1gg
ArrayFlatten[ffm,0,0,0g,f0,m,0,0g,f0,0,m,0g,f0,0,0,mgg]//MatrixForm
ff1gg
0 1
1000
B0 1 0 0C
B C
@0 0 1 0A
0001
80 1 0 1 0 19
ˆ
ˆ 0 0 2 1 7 6 3 7 7 6 3 7 2 >>
<B C B C B =
B 3 1 7 2 C B 2 3 0 4 C B 2 3 0 4 2 C
C
; ;
ˆ@ 6 0 5 1 A @ 3 4 1 2 A @ 3 4 1 2 0 A>
>
:̂ ;
6 0 1 2 5 6 3 6 5 6 3 6 2
We calculate 3ma 2mb, mbT , ma mb; jmaj, jmbj, jma mbj, and ma1 .
matrix1=3ma-2mb//MatrixForm;
matrix2=Transpose@mb//MatrixForm;
matrix3=ma.mb; (* Product of matrices *)
deta=Det@ma; (* Determinant of a matrix *)
detb=Det@mb;
detab=Det@matrix3;
matrix7=Inverse@ma//MatrixForm; (* The inverse of a matrix *)
fmatrix1,matrix2g
fmatrix3//MatrixForm,deta,detb,detab,detab==deta detbg
matrix7
80 1 0 19
ˆ
ˆ 14 12 0 17 7 2 3 5 > >
<B C=
B 5 3 21 2 C B
C ; B 6 3 4 6 C
ˆ@ 24 8 13 7 A @ 3 0 1 3 A> >
:̂ ;
28 12 3 18 7 4 2 6
80 1 9
ˆ
ˆ 1 2 1 2 >
>
<B C =
B 8 25 4 1 C ; 36; 24; 864; True
ˆ@ 52 50 20 46 A >
>
:̂ ;
35 28 13 32
0 1 1 1 1
4 0 12 12
B 1
7 19
12 C
25
B 12 12 C
@ 1 0 1 1 A
6 6 6
2 1
3
0 3
13
We want to find the eigenvalues of a matrix ma.
ma=ff0,0,-2,1g,f3,-1,7,2g,f-6,0,5,-1g,f-6,0,1,-2gg;
Eigenvalues@ma (* Eigenvalues computed by the built-in function
Eigenvalues *)
soleingen=Solve[Det[ma- IdentityMatrix[4]]==0] (* Eigenvalues
computed based on definition *)
N@soleingen
˚
Root 36 15#1 3#12 C #13 &; 1 ,
Root 36 15#13#12 C#13 &; 3 ,
2 3
p p
27 1945
1=3
! 1 16 1 C i 3 1431
2
2
1
p p 1=3
2
1 i 3 12 53 C 1945 ,
p 1=3
p 27 1945
! 1 16 1 i 3 1431
2
2
1
p p 1=3
2
1 C i 3 12 53 C 1945
ff ! 1:g; f ! 6:29279g; f ! 1:64639 C 1:735ig,
f ! 1:64639 1:735igg
We note that the matrix ma has two real and two complex conjugate eigenvalues.
The real eigenvalues are
soleingen[[f1,2g]]
Part[soleingen,f1,2g] (* Equivalent to the previous command *)
%//N
fsoleingen[[1]],soleingen[[1,1]],soleingen[[1,1,1]],soleingen[[1,1,2]]g
(*
Steps to pick
the first real eigenvalue *)
p 1=3 p 1=3
f ! 1g; ! 1 C 3 2 27 21945
1 1431
C 12 53 C 1945
p 1=3 1=3
27 1945
p
f ! 1g; ! 1 C 13 1431 2
2
C 1
2
53 C 1945
ff ! 1:g; f ! 6:29279gg
ff ! 1g; ! 1; ; 1g
We can perform the previous computations directly, using some built-in
functions.
ma=ff0,0,-2,1g,f3,-1,7,2g,f-6,0,5,-1g,f-6,0,1,-2gg;
CharacteristicPolynomial[ma, ]
feigv=Eigenvalues[N@ma],eigv[[1]]g
eigvec=Eigenvectors@N@ma
MatrixForm@%[[3]] (* The third eigenvector *)
-36-51 -18 2 -2 3 + 4
ff6:29279 C 0:i; 1:64639 C 1:735i; 1:64639 1:735i; 1: C 0:ig;
6:29279 C 0:ig
ff0:187245 C 0:i; 0:654212 C 0:i; 0:699033 C 0:i; 0:21977 C 0:ig;
f0:0733515 C 0:107752i; 0:9019 C 0:i; 0:0170554 C 0:121983i;
0:341825 C 0:193829ig; f0:0733515 0:107752i; 0:9019 C 0:i;
0:0170554 0:121983i; 0:341825 0:193829ig;
f0: C 0:i; 1: C 0:i; 0: C 0:i; 0: C 0:igg
0 1
0:0733515 0:107752i
B 0:9019 C0:i C
B C
@ 0:0170554 0:121983iA
0:341825 0:193829i
3.5 Lists 31
Given fx1 ; x2 ; . . . ; xn g and fy1 ; y2 ; . . . ; yn g, how can one produce the lists
The first list can be obtained by the built-in function Riffle or by using the built-in
function Inner[f,fa,bg,fx,yg,g].
Some examples introduced below show the outputs for different uses of the built-
in function Inner.
Clear[a,b,c,d,u,v,w,x,y,z]
Inner[f,fa,b,cg,fx,y,zg,g] (* This is a quite good definition of the
built-in function Inner *)
g[f[a,x],f[b,y],f[c,z]]
fInner[f,ffa,bg,fc,dgg,ffu,vg,fw,xgg,g],
Inner[List,ffa,bg,fc,dgg,ffu,vg,fw,xgg,List]g
{{{g[f[a,u],f[b,w]],g[f[a,v],f[b,x]]},{g[f[c,u],f[d,w]],g[f[c,v],f[d,x]]}},
{{{{a,u},{b,w}},{{a,v},{b,x}}},{{{c,u},{d,w}},{{c,v},{d,x}}}}}
fInner[List,fa,b,cg,fx,y,zg,List],
Flatten[Inner[List,fa,b,cg,fx,y,zg,List]],
Inner[Sequence,fa,b,cg,fx,y,zg,List] (* It coincides with the previous
one *)g
{{{a,x},{b,y},{c,z}},{a,x,b,y,c,z},{a,x,b,y,c,z}}
Inner[Sequence,fa,b,cg,fx,y,zg,List]==Riffle[fa,b,cg,fx,y,zg] (* It
works *)
fInner[Times,fa,bg,fx,yg,Plus], (* Some concrete applications of the
built-in function Inner follow *)
Inner[Power,fa,b,cg,fx,y,zg,Times]g
True
fa x+b y; ax by cz g
Clear[a,b]
myInner[a List,b List]:=(
n=Length@a;m=Length@b;
list=fg;min =Min[n,m];max =Max[n,m];
If[n+m>0,
Do[AppendTo[list,a[[k]]];
AppendTo[list,b[[k]]],fk,ming];
If[n<max,Do[AppendTo[list,b[[k]]],fk,min +1,mg],
Do[AppendTo[list,a[[k]]],fk,min +1,ng],
If[m==0,list=a,list=b]]];
Print[list]
)
We check our code.
x=fg;y=f1,2g; (* The first list is empty *)
myInner[x,y]
x=f1g;y=fg; (* The second list is empty *)
myInner[x,y]
x=f3,4g;y=f1,2g; (* Both lists are nonempty and of equal lengths *)
myInner[x,y]
x=f3,4,5g;y=f1,2g; (* Both lists are nonempty and of different
lengths *)
myInner[x,y]
x=f1g;y=f1,2,7,5g;
myInner[x,y]
x=fg;y=fg; (* Both lists are empty *)
myInner[x,y] f1; 2g
f1g
f3; 1; 4; 2g
f3; 1; 4; 2; 5g
f1; 1; 2; 7; 5g
fg
Now we focus on the second command, i.e., Outer[f,list1 ,list2 ,. . . ].
Clear[a,b,c,d,u,v,w,x,y,z,f]
Outer[f,fa,bg,fx,y,zg,f˛; ˇg] (* It is a simple definition of this
command *)
Outer[List,fa,bg,fx,y,zg,f˛; ˇg]
Flatten[Outer[List,fa,bg,fx,y,zg,f˛; ˇg],1] (* It treats only sublists at
level 1 in the lists as separate elements *)
Flatten[Outer[List,fa,bg,fx,y,zg,f˛; ˇg],2]
Flatten[Outer[List,fa,bg,fx,y,zg,f˛; ˇg],3]
Flatten[Outer[List,fa,bg,fx,y,zg,f˛; ˇg],3]==
Flatten[Outer[List,fa,bg,fx,y,zg,f˛; ˇg]]
3.5 Lists 33
ffff[a,x,˛],f[a,x,ˇ]g,ff[a,y,˛],f[a,y,ˇ]g,ff[a,z,˛],f[a,z,ˇ]gg,
fff[b,x,˛],f[b,x,ˇ]g,ff[b,y,˛],f[b,y,ˇ]g,ff[b,z,˛],f[b,z,ˇ]ggg
ffffa,x,˛g,fa,x,ˇgg,ffa,y,˛g,fa,y,ˇgg,ffa,z,˛g,fa,z,ˇggg,
fffb,x,˛g,fb,x,ˇgg,ffb,y,˛g,fb,y,ˇgg,ffb,z,˛g,fb,z,ˇgggg
fffa,x,˛g,fa,x,ˇgg,ffa,y,˛g,fa,y,ˇgg,ffa,z,˛g,fa,z,ˇgg,
ffb,x,˛g,fb,x,ˇgg,ffb,y,˛g,fb,y,ˇgg,ffb,z,˛g,fb,z,ˇggg
ffa,x,˛g,fa,x,ˇg,fa,y,˛g,fa,y,ˇg,fa,z,˛g,fa,z,ˇg,fb,x,˛g,
fb,x,ˇg,fb,y,˛g,fb,y,ˇg,fb,z,˛g,fb,z,ˇgg
fa,x,˛,a,x,ˇ,a,y,˛,a,y,ˇ,a,z,˛,a,z,ˇ,b,x,˛,b,x,ˇ,b,y,˛;b,y,ˇ;b,z,˛,b,z,ˇg
True
x=f1,2,6,8g;y=f4,5,7g;
myOuter[x,y]
{{1,4},{1,5},{1,7},{2,4},{2,5},{2,7},{6,4},{6,5},{6,7},{8,4},{8,5},{8,7}}
We note that
Clear[a,b,c,x,y,z]
Flatten[Outer[List,fa,b,cg,fx,y,zg],1]
{{a,x},{a,y},{a,z},{b,x},{b,y},{b,z},{c,x},{c,y},{c,z}}
coincides with
34 3 Basic Steps to Mathematica
CartesianProduct[fa,b,cg,fx,y,zg]
{{a,x},{a,y},{a,z},{b,x},{b,y},{b,z},{c,x},{c,y},{c,z}}
CartesianProduct[fa,b,cg,fx,y,z,tg] (* Clearly the lists are of
arbitrary lengths *)
{{a,x},{a,y},{a,z},{a,t},{b,x},{b,y},{b,z},{b,t},{c,x},{c,y},{c,z},{c,t}}
In order to provide a shorter code, we reconsider the third sequence, (Sect. 3.3.3).
The sequence under discussion is
4.1 Introduction
We have mentioned that Mathematica has a built-it Wolfram language symbol for
the sorting problem, that is, Sort. It was used earlier. At the same time each one can
imagine his/her own sorting algorithm. Here we review five sorting methods.
First, we find the smallest element in the input list and interchange it with the first
element. Then we find the next smallest element and interchange it with the second
element. Continue in this way until the entire sequence is sorted.
4.2 Sorting Methods 37
In other words we say that the selection sort divides the input list into two parts:
the sublist of items already sorted, which is built up from left to right located at the
left-hand side of the list (input), and the sublist of items remaining to be sorted that
occupy the rest of the list. Initially, the sorted sublist is null, and the unsorted sublist
is the entire input list.
Mathematica has its own selection sort. It requires the “Combinatorica‘”
package.
Needs[”CombinatoricaJ”]
RandomInteger[f1,20g,15]
SelectionSort[%,Less]
f3; 12; 16; 1; 3; 18; 3; 1; 7; 16; 5; 4; 19; 3; 17g
f1; 1; 3; 3; 3; 3; 4; 5; 7; 12; 16; 16; 17; 18; 19g
A code for selection sort follows. The elements which will be interchanged are
given in blue.
list=RandomSample[Range@10,7];
Print[”initial list ”,list] (* Initial input list *)
Do[If[list[[i]]>list[[j]],plist=list;
plist[[i]]=Style[plist[[i]],Blue,Bold,16];
plist[[j]]=Style[plist[[j]],Blue,Bold,16];
Print@plist;
list[[fi,jg]]=list[[fj,ig]]],fi,Length@listg,fj,i+1,Length@listg];
Print[”sorted list ”,list] (* Sorted output list *)
initial list f7; 6; 4; 3; 9; 1; 5g
{7,6,4,3,9,1,5}
{6,7,4,3,9,1,5}
{4,7,6,3,9,1,5}
{3,7,6,4,9,1,5}
{1,7,6,4,9,3,5}
{1,6,7,4,9,3,5}
{1,4,7,6,9,3,5}
{1,3,7,6,9,4,5}
{1,3,6,7,9,4,5}
{1,3,4,7,9,6,5}
{1,3,4,6,9,7,5}
{1,3,4,5,9,7,6}
{1,3,4,5,7,9,6}
{1,3,4,5,6,9,7}
sorted list {1,3,4,5,6,7,9}
Another short code for selection sort algorithm was introduced in [53, Chap. 10].
selectionSort[a List]:=Flatten[fMin@a,
If[Length@a>1,selectionSort@Drop[a,First@Position[a,Min@a]],]g];
We test it.
38 4 Sorting Algorithms
RandomInteger[f1,9g,15]
selectionSort@%
f6; 2; 2; 7; 2; 3; 8; 5; 7; 8; 2; 2; 4; 6; 5g
f2; 2; 2; 2; 2; 3; 4; 5; 5; 6; 6; 7; 7; 8; 8g
We introduce below a short code for the selection sort algorithm. The feature of
this code is that the keys to be interchanged are written in augmented bold type,
whereas the keys compared are given in augmented italic type.
appendto[char ,ii ,jj ]:=Block[fcg,
c=a;
c[[ii]]=Style[a[[ii]],char,Blue,16];
c[[jj]]=Style[a[[jj]],char,Blue,16];
AppendTo[aa,c]];
With[fm=10,n=10g, (* For a random selection of n terms in
Range[m] *)
a=RandomSample[Range@m,n]];
Print[”Input (unsorted) list : ”,%]
aa=f%%g;
Do[min=a[[i]];minj=i;
Do[If[a[[j]]<min,appendto[Italic,minj,j];
min=a[[j]];minj=j],fj,i+1,Length@ag];
If[i¤minj,appendto[Bold,i,minj]];
a[[fi,minjg]]=a[[fminj,ig]],fi,Length@a-1g
];
AppendTo[aa,a];
aa
Print[”Output (sorted) list : ”,a]
The sorting steps are presented in Fig. 4.1.
Let a D fa1 ; : : : ; an g be the list to be sorted. At the beginning and after each iteration
of the algorithm, the list consists of two parts: the first part a1 ; : : : ; ai1 is already
sorted, and the second part ai ; : : : ; an is still unsorted i 2 f1; : : : ; ng: In order to
insert element ai into the sorted part, it is compared with ai1 ; ai2 ; etc. When an
element aj with aj ai is found, ai is inserted behind it. If no such element is found,
then ai is inserted at the beginning of the sequence. After inserting the element ai , the
length of the sorted part has increased by one. In the next iteration, aiC1 is inserted
into the sorted part. While at the beginning the sorted part consists of element a1
only, at the end it consists of all the elements a1 ; : : : ; an . This is the insertion sort
algorithm.
4.2 Sorting Methods 39
We introduce below a short code in Mathematica for the insertion sort algorithm
in [53, Chap. 10]. The keys to be moved are written in italic type, whereas the key
to be inserted is given in bold blue type:
insertionSort[list List]:=Module[fterm,a=listg, (* Declaring it as
module, the code acts as a function that can be called several times *)
For[i=2,iLength@a,i++,term=a[[i]];j=i-1;
While[j1&&a[[j]]>term,
b=a;b[[i]]=Style[a[[i]],Italic,Blue,16];
b[[j]]=Style[a[[j]],Italic,Blue,16];
a[[j+1]]=a[[j]];
AppendTo[aa,b];j- -];
a[[j+1]]=term;b=a;
b[[j+1]]=Style[a[[j+1]],Bold,Blue,16];
AppendTo[aa,b]];
AppendTo[aa,a]];
aa=ff5,2,4,6,1gg; (* Here is the input (unsorted) list; the list aa
will contain all the steps of the algorithm *)
insertionSort@Flatten[aa,1] (* The last element of the output
consists in the corresponding sorted list *)
The sorting steps are presented in Fig. 4.2.
We can visualize the insertion sort algorithm by converting the successive lists
into permutation plots with animation.
40 4 Sorting Algorithms
permutationPlot[list List]:=ListPlot[list,
PlotRange!ff.5,Length@list+.5g,f.6,Length@list+.6gg,
PlotStyle!PointSize[.4/Length@list],Axes!None,
FrameTicks!None,Frame!True,AspectRatio!1,
ImageSize!3.2Length@list(Length@list-1)]
With[fm=5,n=5g,
a=RandomInteger[f1,mg,n];
Print[”Input (unsorted) sequence : ”,a];
aa=fag;
If[n>1,
Do[min=a[[i]];minj=i;appendto=0;
Do[If[a[[j]]<min,min=a[[j]];minj=j;appendto=1],fj,i+1,ng];
If[appendto==1,a[[fi,minjg]]=a[[fminj,ig]]; AppendTo[aa,a]],fi,n-1g
];
]]
Print[aa]
permutationPlot[#]&/@aa
ListAnimate[%] f2; 1; 5; 5; 1g
Input (unsorted) sequence : f2; 1; 5; 5; 1g
ff2; 1; 5; 5; 1gg
ff2; 1; 5; 5; 1g; f1; 2; 5; 5; 1g; f1; 1; 5; 5; 2g; f1; 1; 2; 5; 5gg
The sorting steps are presented in Fig. 4.3.
4.2 Sorting Methods 41
4.2.3 Mergesort
Mergesort algorithm is based on two steps, [53, Chap. 10]. The first one splits
the input list into smaller groups by halving it until the groups had only one or two
elements. Then it merges the groups back together so that their elements are in order.
This is an algorithm of the “divide and conquer” type. Below we introduce a code
for mergesort.
merge[left List,right List]:=
Module[flindex=1,rindex=1g,
Table[Which[lindex>Length@left,right[[rindex++]],
rindex>Length@right,left[[lindex++]],left[[lindex]]right[[rindex]],
left[[lindex++]],True,right[[rindex++]]],fLength@left+Length@rightg]]
mergeSort[m List]:=Module[fmiddleg,If[Length@m2,
middle=Ceiling[Length@m/2];
Apply[merge,Map[mergeSort,Partition[m,middle,middle,f1,1g,fg]]],m]]
a=RandomInteger[f1,13g,15]
mergeSort@a
f8; 8; 7; 1; 5; 4; 4; 5; 1; 1; 7; 8; 6; 2; 12g
f1; 1; 1; 2; 4; 4; 5; 5; 6; 7; 7; 8; 8; 8; 12g
4.2.4 Heapsort
We mention that there exists a built-in function heapsort which performs sorting
using this algorithm.
Needs[“CombinatoricaJ”]
HeapSort[f9,5,1,8,6,4,3,7,2g]
f1; 2; 3; 4; 5; 6; 7; 8; 9g
The heapsort code introduced below is based on the version of Floyd’s algorithm
published in [26] and the corresponding Mathematica code given by [43, p. 99].
A list a D fa1 ; : : : ; an g is said to be a heap if abi=2c ai , for all i 2 f2; : : : ; ng.
We present the heapsort algorithm in full detail.
ı Heapsort is a sorting algorithm of class O.n ln.n//.
ı It is also known as sorting by the “method of heaps.”
ı It is not recursive but is comparable to the quicksort.
ı Heapsort is a sorting algorithm “in situ,” that is, it does not require additional
structures; the sorting is performed in the space of the input list. We mention that
there are many heapsort implementations, but it is not our aim to review them.
42 4 Sorting Algorithms
ı When the input is transformed step by step into a heap, the smallest element is
at the top. This is interchanged with the last one, that is, an , which is at the top
now, and we put it behind the smallest one, reducing by one the length of the new
input.
We consider an example and perform the heapsort algorithm. Let a D .9; 5; 1; 8;
6; 4; 3; 7; 2/ be the input sequence (list) to be sorted. The binary tree representation
of a is given by the Fig. 4.6.
TreePlot[f9 ! 5; 9 ! 1; 5 ! 8; 5 ! 6; 1 ! 4; 1 ! 3; 8 ! 7; 8 ! 2g;
Automatic,VertexLabeling!True,PlotStyle!Black,ImageSize!150]
The sorting steps start as follow.
F We interchange 8 ! 2.
F We interchange 5 ! 2.
F We interchange 9 ! minf2; 1g.
The steps introduced above are visualized below.
TreePlot[#,Automatic,9,VertexLabeling!True,PlotStyle!Black,
ImageSize!150]&/@
ff9 ! 5; 9 ! 1; 5 ! 8; 5 ! 6; 1 ! 4; 1 ! 3; 8 ! 7; 8 ! 2; 2 ! 8g,
f9 ! 5; 9 ! 1; 5 ! 2; 5 ! 6; 1 ! 4; 1 ! 3; 2 ! 7; 2 ! 8; 2 ! 5g,
f9 ! 2; 9 ! 1; 2 ! 5; 2 ! 6; 1 ! 4; 1 ! 3; 5 ! 7; 5 ! 8; 1 ! 9gg
The first steps are exhibited in Fig. 4.7.
Next we go on as follows.
F We interchange 9 ! minf4; 3g. Such interchanges will go on until 9 will arrive
on the last row, and thus we get a heap. The smallest element lies on top of the
binary tree.
F We interchange 1 with the last element of the last row, that is, 1 ! 8.
F We remove 1 and the link between 1 and 5.
Now we are in the situation introduced in the figure below.
fTreePlot[f1 ! 2; 1 ! 9; 2 ! 5; 2 ! 6; 5 ! 7; 5 ! 8; 9 ! 4; 9 ! 3; 3 !
9g;
Automatic,1,VertexLabeling!True,PlotStyle!Black,ImageSize!150],
Show[Graphics[ffPointSize[Medium],Point[f0,0g]g,
Line[fff1,0g,f1.6,0g,f1.6,2.6gg,ff1,-.08g,f1.68,-.08g,f1.68,2.6ggg]g],
TreePlot[f1 ! 2; 1 ! 3; 2 ! 5; 2 ! 6; 5 ! 7; 5 ! 8; 3 ! 4; 3 ! 9g;
Automatic,1,VertexLabeling!True,PlotStyle!Black],PlotRange!All,
ImageSize!145],
Show[Graphics[ffPointSize[Medium],Point[f0,0g]g,
Line[fff1,0.1g,f1.6,0.1g,f1.6,-.2gg,ff1,.02g,f1.52,.02g,f1.52,-.2ggg]g],
TreePlotŒf8 ! 2; 8 ! 3; 2 ! 5; 2 ! 6; 5 ! 7; 5 ! 1; 3 ! 4; 3 ! 9g;
Automatic,8,VertexLabeling!True,PlotStyle!Black],PlotRange!All,
ImageSize!145]g
The second steps are exhibited in Fig. 4.8.
We further work with the remained binary tree whose length is decreased by 1.
F Due to the previous steps, the vertices 5; 3 and 2 satisfy the condition abi=2c ai .
Furthermore, this condition is also satisfied by the vertices 8 and 2. Therefore,
we interchange 8 ! 2.
F We interchange 8 ! 5.
F We interchange 8 ! 7.
4.2 Sorting Methods 45
heap=heapify[Drop[heap,-1],1];AppendTo[heaps,heap];
min,fn,Length@p,2,-1g],Max@heap]]/;Length@p>0
heapify[p List]:=Module[fj,heap=pg,Do[heap=heapify[heap,j],
fj,Quotient[Length@p,2],1,-1g];heap]
heapify[p List,k Integer]:=Module[fhp=p,i=k,m,n=Length@pg,
While[(m=2 i)n,
If[m<n&&Less[hp[[m+1]],hp[[m]]],m++];
If[Less[hp[[m]],hp[[i]]],hp[[fi,mg]]=hp[[fm,ig]]];
i=m];
hp
]
a=f9,5,1,8,6,4,3,7,2g;
heapSort@a
Table[heaps[[i]],fi,1,Length@heapsg] (* Here is the list of partial
heaps *)
{1,2,3,4,5,6,7,8,9}
{{2,5,3,7,6,4,9,8},{3,5,4,7,6,8,9},{4,5,8,7,6,9},{5,6,8,7,9},{6,7,8,9},
{7,9,8},{8,9},{9}}
trees=fg;
Do[a=heaps[[m]];
n=Length@a
If[n==0,tree=fg;Goto[”print”],
If[n==1,tree=a;Goto[”print”],
tree=fg;
If[n==2,AppendTo[tree,a[[1]]!a[[2]]];Goto[”print”],
tree=fg;j=1;
Do[
j++;AppendTo[tree,a[[i]]!a[[j]]];
j++;If[j>n,Goto[“print”]];
AppendTo[tree,a[[i]]!a[[j]]],fi,bn=2cg]
] ] ];
Label[”print”];
AppendTo[trees,tree],
fm,Length@heaps-1g];
Table[TreePlot[trees[[k]],Automatic,trees[[k,1,1]],VertexLabeling!True,
ImageSize! 10.15 bk=2c/],fk,Length@treesg]
The steps of the above algorithm are exhibited in Fig. 4.18.
52 4 Sorting Algorithms
4.2.5 Quicksort
Cases[x,y /;y==pivot],
quickSortcases@Cases[x,y /;y>pivot]g
]
With[fm=10,n=12g,
list=RandomChoice[Range@m,n]]
quickSortcases@list
f5; 3; 3; 4; 2; 1; 4; 7; 8; 8; 4; 9g
f1; 2; 3; 3; 4; 4; 4; 5; 7; 8; 8; 9g
Another quicksort code is introduced below.
quickSortselect[fg]=fg;
quickSortselect[fx ,xs g]:=Join[ (* It joins all the elements less
than, equal to, or greater than x *)
quickSortselect@Select[fxsg,#x&], (* It applies quickSortselect
to all picked elements in the list xs less than or equal to x *)
fxg,
quickSortselect@Select[fxsg,#>x&] (* It applies quickSortselect to
all picked elements in the list xs greater than x *)
];
With[fm=10,n=12g,
list=RandomChoice[Range@m,n]]
quickSortselect@list
f1; 5; 8; 7; 3; 6; 10; 4; 2; 7; 4; 9g
f1; 2; 3; 4; 4; 5; 6; 7; 7; 8; 9; 10g
Suppose we are interested in the time needed to sort a list.
Timing[expr] evaluates expr and returns a list of the time in seconds used,
together with the result obtained. Then, one can use the next procedure:
With[fm=10,000,n=15,000g,
list=RandomChoice[Range@m,n]]; (* The input list *)
Timing[quickSortcases@list;]
Timing[quickSortselect@list;]
f1:15625; Nullg
f0:65625; Nullg
Based on this simple test, we conclude that the quickSortselect code is clearly
faster than the quickSortcases code.
Chapter 5
Functions
5.1 Definitions
We have already seen some built-in functions along the previous chapters. We can
define our own functions depending on our needs or interests. Thus, we have the
freedom to use plenty of mathematics. First, we consider some simple examples.
f[x ]:=Sin[x]+x2 (* A function is defined f(x) = sin x + x2 *)
g[x ]:=f[f[x]+1]+2 (* A composition of a function g(x) = f(f(x) + 1) +
2 *)
fg[2], (* The value of g at an integer; it is returned noncomputed *)
FullSimplify@g[2], (* It is still noncomputed *)
TraditionalForm@g[2],
g[2.], (* The value is given with five decimal digits *)
g[2]//N,
N[g[2],15], (* The value is given with 15 decimals *)
N[#,15]&/@g[2]g (* The same result as before *)
{2+(5+Sin[2])2 +Sin[5+Sin[2]],2+(5+Sin[2])2 +Sin[5+Sin[2]],
2+(5+sin(2))2 +sin(5+sin(2)),36.5546,36.5546,36.5545586117012,
36.5545586117012}
It is not difficult to construct a function of several variables. An easy example
is the code of finding the solutions of the second-degree polynomial with real
coefficients.
Suppose we need the second solution of the second equation, first under a simple
form and then under a numerical form. We write
Simplify@u[[2]]
N@u[[2]]
p
1 C 5
1:23607
There exists a built-in function which can solve the second-degree equation
easily. This is the built-in function Reduce.
Reduce[a x2+b x+c==0,x]
p p
b2 4ac bC b2 4ac
a ¤ 0&& x== b 2a k 2a
a==0&&b ¤ 0&&x== c .c==0&&b==0&&a==0/
b
We can use this function for constrained equations.
Clear[x,y]
Reduce x2 y2 DD
8; fx; p
yg; Reals
p p
x 2 2&& y DD 8 C x2 ky DD 8 C x2 k
p p p
x 2 2&& y DD 8 C x2 ky DD 8 C x2
Clear[x,y,xy]
Reduce x2 y2 DD 8; fx; yg; Integers (* We look for integer solutions *)
xy=fx,yg//.fToRules[%]g (* The list of solutions *)
fx,yg=xy[[1]] (* We extract the first pair of solutions *)
.x DD 3&&y DD 1/k.x DD 3&&y DD 1/k
.x DD 3&&y DD 1/k.x DD 3&&y DD 1/
ff3; 1g; f3; 1g; f3; 1g; f3; 1gg
f3; 1g
5.1 Definitions 57
Using the built-in function Which (making the graph continuous), we write
f[x ]:=Which[Abs@x<1, -x,
1 Abs@x<2, Sin@x,
True, Cos@x
];
Plot[f@x,fx,-3,3g,ImageSize!200] (* A graph is useful to see how Mathe-
matica interprets the built-in function Which. The graph is continuous; the
function is transformed into a set-valued function *)
f/@f0.5,Pi/2,Pig (* The values of the function f at some values of the
argument *)
Limit[f@x,x! 2] (* 2 is a discontinuity point. The limit is considered side
limit from the right-hand side *)
The graph of the above discontinuous function is exhibited by the built-in
function Which in Fig. 5.1.
We note that if the function is discontinuous at certain points, its graph is
continuous, and thus the function which represents that graph is a set-valued
function or a multifunction.
Using the built-in function Piecewise, the previous function looks like
g[x ]:=Piecewise[ff-x,Abs@x<1g,fSin@x,1Abs@x<2gg,Cos@x]
Plot[g@x,fx,-3,3g,ImageSize!200]
g/@f0.5,Pi/2,Pig
Limit[g@x,x!2]
The graph of the above discontinuous function is exhibited by the built-in
function Piecewise in Fig. 5.2.
We note that the built-in function Piecewise supplies the true graph of the
function which in the present case has points of discontinuity.
The built-in function If supplies a continuous graph even if the function is
discontinuous. Its output coincides with the one given by Which.
h[x ]:=If[Abs@x<1,-x,If[1Abs@x<2,Sin@x,Cos@x]];
Plot[h@x,fx,-3,3g,ImageSize!200]
h/@f0.5,Pi/2,Pig
Limit[h@x,x!2] (* The limit is again considered side limit from the right-
hand side *)
The graph of the above discontinuous function is exhibited by the built-in
function If in Fig. 5.3.
p[x ]:=Piecewise[ff0,x<0g,f1,x0gg]
Plot[p@x,fx,-1,1g,PlotStyle!Thick,ImageSize!150,
Ticks!ff-1,1g,f1gg]
p/@f-1,0,1g (* At the discontinuity point x=0, it is returned the
value 1 *)
Limit[p@x,x!0],Limit[p@x,x!0,Direction!-1],
Limit[p@x,x!0,Direction!1] (* Because at x = 0 the function is
discontinuous, the limit is considered side limit from the right-hand
side *)
Another discontinuous function is exhibited by the built-in function Piecewise in
Fig. 5.4.
5.1 Definitions 59
Collatz conjecture states that the algorithm consisting in substituting a given positive
integer n by Floor[n/2] provided n is even and by 3n C 1 provided n is odd reaches
eventually 1, [47, Chap. 1]. Formally we write
(
bn=2c; n is even;
f .n/ D
3n C 1; n is odd;
5.1 Definitions 61
and state that for any natural n, there exists a natural m such that f iterated m times
at n reaches 1, i.e., f .f .: : : f .n/// D 1.
„ ƒ‚ …
m times
For example, starting with number 7, we write the Collatz successive iterations
obtaining the following sequence:
7 ! 22 ! 11 ! 34 ! 17 ! 52 ! 26 ! 13 ! 40 ! 20 !
10 ! 5 ! 16 ! 8 ! 4 ! 2 ! 1
A code for a step of this algorithm can be written as follows:
f[n Integer]:=If[EvenQ[n],n/2,3n+1]
f[16] (* Because 16 is even, we divide it by 2 *)
f[17] (* Because 17 is odd, we multiply it by 3 and add 1 *)
8
52
We can write the above-defined function under the form:
g[n Integer?EvenQ]:=n/2
g[n ]:=3n+1
g[8]
g[7]
4
22
Suppose we want to find out how many times one needs to apply g to numbers
from 1 to 30 getting 1:
k=Length/@(NestWhileList[g,#,!#==1 &]&/@Range[30];)
m=Max@k (* Find the largest number of steps needed to reach 1 *)
ListPlot[k,Filling!Axis,PlotStyle!Black,ImageSize!250,
PlotRange!All]
array=Table[fi,k[[i]]g,fi,30g];
pointmaxat=Last[Sort[array,#1[[2]]< #2[[2]]&]][[1]] (* We find the point
which requires the largest number of iteration to reach 1 *)
112
Some simple tests for Range[30] of the Collatz conjecture are presented in
Fig. 5.7 below.
Suppose we want to find the square roots of some numbers with certain
approximations. For a D 3 and " D 1011 , we write
N[f[3,11.],10]
1:73205
p
Another approach for 3, a 0, follows
Nest[(# + a/# )/2&,1.0,5]/.a!3
1:73205
p For the square root of a nonnegative number, we can use the built-in function
. For example,
hp i
N 3; 11
1:7320508076
p
3
Now we introduce a very short code to compute, a; a 0, using an
approximate method.
Clear[f,a,"]
"
1
ı 2 = 1.,xp=ag, While[Abs[xp - x] 10 ,xp = x;
f[a ," ]:=Module[fx
x D 3 2x C a x ];N[x]]
Suppose we want to find the cube root of a number with certain approximations,
then we write
f[10,7.]
2:15443
p p
We can use the built-in function 3 to find 3 10. Indeed
hp i
N 3 10; 10
2:154434690
5.2 Differentiation and Integration 63
Clear[f,x,y,z]
f[x ,y ,z ]:=2xy2 z3 +Log[y2 +1]+y Cos[z2 ];
{Derivative[1,2,3][f][x,y,z],
2
Derivative[1,1,0][f][x,y,z], (* @ f.x;y;z/
@x@y
*)
Derivative[1,1,0][f][x,y,z]/.y!1.25,
Derivative[1,1,0][f][x,y,z]/.fy!1.25,z!
˚ 108 g}
We have seen the usage of the built-in function Plot for several functions. The
graphs of different functions are colored with different colors. Here is an example,
f .x/ defined above and g.x/ D 2 sin x on the same interval Œ5; 6.
Plot[ff@x,2Sin@xg,fx,-5,6g,ImageSize!250]
The built-in function Plot acting on two functions is presented in Fig. 5.12 above.
Two other possible ways of coloring are presented below:
Plot[ff@x,2Sin@xg,fx,-5,6g,PlotRange!All,ImageSize!250,#1]&/@
fPlotStyle!fBlack,Redg,fColorFunction!Function[fx,yg,Hue[y]]gg
Two functions colored in different ways are presented in Fig. 5.13.
68 5 Functions
We consecutively display and label the graphs of the three functions. At the third
plot, the label uses the same font and style that is used in graphs and is not included
in the plot.
{Plot[3 Cos@x,fx,-5,7.28g,
PlotStyle!fThickness!0.008,Darker[Green],Dashing[0.03]g,
PlotLabel!”3cos(x)”,ImageSize!180,PlotRange!All],
Plot[2Sin@x,fx,-5,7.28g,PlotStyle!fThickness!0.008,Blue,Dottedg,
ImageSize!180,PlotLabel!Style[”sin(x)”,FontSize!18,
Background!Green],Background!Yellow,PlotRange!All]
Labeled[Plot[Tan@x,fx,-5,7.28g,ImageSize!180],
Style[”Tan(x)”,”Graphics”],Background!LightGreen]}
Different colors for the pictures are introduced in Fig. 5.16.
70 5 Functions
Suppose we want to put several graphs of functions in the same Cartesian system
of coordinates. We can do it in the following way.
Plot[fSin[2x],Cos[x/2]g,fx,0,4g,
PlotStyle!fDirective[Thickness!0.007,Red],
Directive[Thickness!0.007,Blue]g,PlotLegend!f”sine”,”cosine”g,
ImageSize!300]
A way of presentation of functions is introduced in Fig. 5.17 above.
We can also insert the legend exactly where we want.
5.4 Plane and Space Figures 71
ListPlot[Table[fx,f@xg,ff,Sin,Cos,Logg,fx,0,10,0.5g],
PlotLegend!f”Sine”,”Cosine”,”Log”g,LegendPosition!f1.1,-0.4g,
Joined!fTrue,True,Falseg,Ticks!ff5,10g,f-1,1,2gg,ImageSize!300,
PlotMarkers! #1]&/@fAutomatic,f”ı”,””,”*”gg
Next, a way of legend insertion is introduced in Fig. 5.18.
A complete graph with 13 nodes is presented in Fig. 5.19. Its vertices are labeled.
72 5 Functions
PointSize[0.02],Point[fCos[#1/6],Sin[#1/6]g],
fArrowheads[0.05],Thin,Black,Arrow[ff0,0g,
.97fCos[=2 second/30],Sin[=2 second/30]gg]g,
fArrowheads[0.07],Thick,Green,Arrow[ff0,0g,
.95fCos[=2 minute/30],Sin[=2 minute/30]gg]g,
fArrowheads[0.078],Thick,Blue,Arrow[ff0,0g,
.8fCos[=2 hour/6],Sin[=2 hour/6]gg]g,
Text[Style[RomanNumeral[#1],Italic,12,Blue],1.16fCos[(-#1+3)/6],
Sin[(-#1+3)/6]g]gg]&/@Range@12,ImageSize!200]
The color of a figure can vary, thanks to many options. One of them is the
graphics directive Hue. Two simple examples follow in Fig. 5.22.
Graphics[Table[fHue[k/7],Disk[fCos[k 2/7],Sin[k 2/7]g,1/2]g,fk,0,6g],
PlotRange!ff-2,2g,f-2,2gg,ImageSize!200]
We change the previous code so that the discs are laid on the sine function.
Graphics[Table[fHue[k/7],Disk[fk 2/7,Sin[k 2/7]g,1/2]g,fk,0,14g],
PlotRange!ff-1/2,14g,f-2,2gg,ImageSize!250]
See Fig. 5.22.
74 5 Functions
We now present some spirals. In the first example, the polar radius and the angle
are equal. In the second example, the polar radius equals sin.3t/, whereas in the
third is sin.5t/: The next two examples show other polar closed curves Fig. 5.23.
{PolarPlot[t,ft,0,20g,PlotLabel!Style[PolarPlot,16],
Ticks!ff-15,-10,-5,5,10,15g,f-15,-10,-5,5,10,15gg],
p3=PolarPlot[Sin[3t],ft,0,g,PlotStyle!fThickness!0.01,
Darker@Greeng],
PolarPlot[Sin[5t],ft,0,g,PlotStyle!fThickness!0.01,Redg,
Ticks!ff-.5,.5g,f-.5,.5,1gg]}
p1=PolarPlot[f1,1/2,1+1/10 Sin[10t]g,ft,0,2g,
PlotStyle!fDirective[Thick,Darker@Green],Blue,
Directive[Dashed,Thick,Red]g];
p2=PolarPlot[f1,1-1/5,1+1/5,1+1/5 Sin[10t]g,ft,0,2g,
PlotStyle!fDirective[Thick,Darker@Blue],
Directive[Thick,Darker[Black]],Directive[Thick,Darker@Green],
Directive[Dashed,Thick,Red]g,Axes!False];
fp1,p2g
Show[p3,p2,PlotRange!All,Axes! fTrue,Falseg,ImageSize!200]
Some graphs with PolarGraph are shown in Fig. 5.24.
Now we show the simple cross, [19, pp. 130–131].
With[fa=6,b=4g,x[t ]:=a Sec@t; y[t ]:=b Csc@t;
ParametricPlot[ffx@t,y@tg,fx[t+/4],y[t+/4]gg,ft,-,g,
PlotStyle!Blue,Axes!False,ImageSize!180]
We suggest the next code for the Maltese cross.
p p
PolarPlot[f2/ SinŒ4t =2,8/ SinŒ4t C =2g,ft,0,2g,Axes!False,
PlotStyle!fThick,Blueg,ImageSize!180]
The two crosses are presented in Fig. 5.25.
5.4 Plane and Space Figures 75
We present the next code for the Star of David Fig. 5.26.
p=Tuples[Table[fCos@t,Sin@tg,ft,/6,13/6,/3g],2]; (* This is
the complete graph with 6 vertices *) p
p=Select[p,EuclideanDistance[#[[1]],#[[2]]]== 3&]; (* From
the complete graph, we select the edges of a prescribed length *)
Show[Graphics[fThick,Blue,Line[p]g],ImageSize!180]
76 5 Functions
It has horizontal tangents at (2/3, ˙2/3) and vertical tangents at (0,0) and (1,0). Its
graph looks as the left-hand graph in Fig. 5.30.
ContourPlot[x4 +x2 y2 +y4 ==x(x2 +y2 ),fx,0,1g,fy,-2/3,2/3g,
ContourStyle!fDarker@Greeng,FrameTicks!fff-0.6,0,0.6g,Noneg,
ff0,1g,Nonegg,ImageSize!180]
A simple butterfly follows. Its equation is given implicitly, the right-hand graph
in Fig. 5.30.
ContourPlot[y6 DD x2 x6 ,fx,-1,1g,fy,-.9,.9g,ContourStyle!fBlueg,
FrameTicks!fff-0.8,0,0.8g,Noneg,ff-1,0,1g,Nonegg,
ImageSize!180]
A more realistic butterfly curve is a transcendental plane curve discovered by
Temple H. Fay in [23]. This curve is given by the following parametric equations
Fig. 5.31.
xx[t ]:=Sin@t(eCos@t -2Cos[4t]-Sin[t/12]5 );
yy[t ]:=Cos@t(eCos@t -2Cos[4t]-Sin[t/12]5 );
ParametricPlot[fxx@t,yy@tg,ft,0,#1g,ColorFunction!fHue@#&g,
Ticks!ff-3,0,3g,f-2,3gg]&/@f2Pi,4Pi,6Pi,8Pi,12Pi,24Pig
ParametricPlot with two parameters gives a region Fig. 5.32.
5.4 Plane and Space Figures 79
Apply[fParametricPlot[fft,Sin@tg,ft,Sin@t+1gg,ft,0,2g,
Ticks! #1,#2],
ParametricPlot[ft,r Sin@t+(1-r)(Sin@t+1)g,ft,0,2g,fr,0,1g,
Frame!ffFalse,Trueg,fFalse,Truegg,
FrameStyle!ffThick,Directive[Thick,Dashed]g,fRed,Bluegg,
FrameTicks! #1,#2]g&,fff0,Pi/2,Pi,3 Pi/2,2 Pig,f-1,0,1,2gg,
ImageSize!200g]
By default the functions in the built-in function Plot are not evaluated until they
have numeric values for the variables Fig. 5.33.
{Plot[Table[2Sin[x+t],ft,0,4g],fx,0,2g,Ticks!ff,2g,f-2,2gg],
Plot[Evaluate[Table[2Sin[x+t],ft,0,4g]],fx,0,2g,Ticks!ff,2g,
f-2,2gg]}
Evaluating the argument symbolically can sometimes be faster, as the following
example shows Fig. 5.34.
80 5 Functions
Clear[a,b]
fTiming[Plot[Integrate[Sin@x Cos@x,fx,0,ag],fa,0,2g,
Ticks!ff,2g,f.5gg]],
Timing[Plot[Evaluate[Integrate[Sin@x Cos@x,fx,0,bg]],fb,0,2g,
Ticks!ff,2g,f.5gg]]g
It is clear that the first graph needs more than 8 s whereas the second one needs
about 129 times less.
Sometimes numeric evaluation is faster, as the following example shows
Fig. 5.35.
{Timing[Plot[Nest[Sin[#]#&,a,20],fa,0,g,PlotStyle!Blue,
Ticks!ff1.5,2.5,g,f1.5gg]],
Timing[Plot[Evaluate[Nest[Sin[#]#&,a,20]],fa,0,g,PlotStyle!Blue,
Ticks!ff1.5,2.5,g,f1.5gg]]}
We show how the built-in function Nest is used to study the uniform convergence
of a sequence of functions Fig. 5.36.
5.4 Plane and Space Figures 81
Plot[Nest[Sin@#&,a,#(10)],fa,0,2g,
PlotLabel!ToExpression[”iterations”]==TraditionalForm[#(10)],
LabelStyle!Hue[#/10],
PlotStyle!fThickness!0.01,Hue[#/10]g,
Ticks!ff,2g,Automaticg,
TicksStyle!Directive[Bold]]&/@f1/10,3,6,9g
We know the behavior of the function f .x; n/ D sin.sin.: : : .sin x///, n times,
x 2 Œ0; Fig. 5.37.
Table[
Plot[Nest[Sin@#&,a,n],{a,0,},
PlotRange!All,
Ticks!{{{/2,“/2”},},Automatic},
PlotLabel!ToExpression[“iterations”]==TraditionalForm[n],
PlotStyle!{Thickness!0.01,Hue[n/1000]}],
{n,1,1000,200}]
Taking into account that f .x/ D f .x/;, we note that limn!1 f .x; n/ D 0,
uniformly in x 2 R. A rigorous proof can be given, too. p
At the same time, we have the stronger result that limn!1 n=3f .x; n/ D
sgn.x/, for x 2 ; Œ ; Fig. 5.38. A proof is given in the Proposition 5.1.
82 5 Functions
p
Fig. 5.38 The n=3f .x; n// function iteration
Table[Plot[Sqrt[n/3]Nest[Sin,x,n],fx,-,g,
PlotLabel!ToExpression[”iterations ”]==TraditionalForm[n],
PlotStyle!fHue[n/1000],Thickness!0.008g,
Ticks!ff-,g,Automaticg,PlotRange!All],
fn,1,1000,300g]
We can handle the last command dynamically by the next code. Changing by a
click the value of n, the color of the graph changes Fig. 5.39.
5.4 Plane and Space Figures 83
Clear[n]
Dynamic[Plot[Sqrt[n/3]Nest[Sin,x,n],fx,-,g,ImageSize!200,
Ticks!ff-,g,f-1,1gg,PlotStyle!fHue[n/1000],Thickness!0.008g,
PlotLabel!Row[
fStyle[n,Italic],” = ”,PopupMenu[Dynamic[n],f1,301,601,901,1201g]g]]]
1 1 t sin t t t
2
2 D C 1 ! 1=3 as t # 0:
sin t t t3 sin t sin t
1 1
! 1=3 as n ! 1:
f 2 .n C 1; x/ f 2 .n; x/
n1
1X 1 1
n iD1 f 2 .i C 1; x/ f 2 .i; x/
1 1 1
D ! 1=3 as n ! 1
n f 2 .n; x/ f 2 .1; x/
84 5 Functions
and
nf 2 .n; x/ ! 3 as n ! 1: t
u
Clear[a,n]
Assumptions!n2Integers&&n>0;
With[fn=10,a=2.0g,
p p
fNestList a C # &; a; n , (* Exhibit the first ten terms of the first
sequenceh *) i
p # p
NestList a &; a; 3n (* Exhibit the first thirty terms of the
second sequence *)g]
{{1.41421,1.84776,1.96157,1.99037,1.99759,1.9994,1.99985,1.99996,
1.99999,2.,2.},
{1.41421,1.63253,1.76084,1.84091,1.89271,1.927,1.95003,1.96566,1.97634,
1.98367,1.98871,1.99219,1.99459,1.99626,1.99741,1.9982,1.99876,
1.99914,1.9994,1.99959,1.99971,1.9998,1.99986,1.9999,1.99993,1.99995,
1.99997,1.99998,1.99998,1.99999,1.99999}}
We note that the above evaluations suggest that both sequences tend toward 2.
The limits are found rigorously in [53, p. 140] and [54, p. 28]. M
Let us color some planar curves with different colors Figs. 5.40 and 5.41.
Plot[#1,ft,0,15g,PlotStyle!AbsoluteThickness@4,
ColorFunction!”Rainbow”,
ImageSize!200]&/@fSin@t,Sin@t/t,t Sin@tg
{Plot[Sin@t,ft,0,10g,PlotStyle!AbsoluteThickness[5],
ColorFunction!”BlueGreenYellow”,#1],
Plot[Sin@t/t,ft,0,15g,PlotStyle!AbsoluteThickness[4],
ColorFunction!Function[fx,yg,Hue[x]],#1],
Plot[t Sin@t,ft,0,10g,PlotStyle!AbsoluteThickness[2],
ColorFunction!Function[fx,yg,ColorData[”NeonColors”][y]],#1]g
&/@fImageSize!200g}
5.4 Plane and Space Figures 85
meshstyle=fMeshStyle!GrayLevel[1],MeshFunctions!f#3&g,
Mesh!None,fMesh!None,Boxed!False,Axes!Falsegg;
fPlot[Cos@t,ft,0,g,PlotStyle!fThickness!0.01g,
Ticks!ffg,f-1,1gg],
RevolutionPlot3D[Cos@t,ft,0,g,PlotStyle!fGreen,Opacity!0.4,
5.4 Plane and Space Figures 87
Thickness[.02]g,ImageSize!200,
Ticks!ff-,0,g,f-,0,g,f-1,0,1gg]g
RevolutionPlot3D[Cos@t,ft,0,g,PlotPoints!40,
PlotStyle!fGreen,Opacity!0.4,Thickness[.02]g,ImageSize!200,
Ticks!ff-,0,g,f-,0,g,f-1,0,1gg#]&/@meshstyle
A 3D curve very often has several representations. The first example shows a
broken line connecting 3 points in the 3D space Fig. 5.43.
Graphics3D[ffThickness!0.008,Darker[Green],
Line[ff0,0,0g,f1,2,1g,f-1,3,3gg]g,
Text[Style[”A=(0,0,0)”,Bold,12],f0,0.2,-0.4g],
Text[Style[”B=(1,2,1)”,Italic,12,Darker[Blue]],f0.5,1.3,1.2g],
Text[Style[”C=(-1,3,3)”,Bold,16,Red],f0,2.7,3.1g],
fPointSize[0.03],Orange,Point[ff0,0,0g,f1,2,1g,f-1,3,3gg]gg,
Axes!True, PlotRange!All,ImageSize!200,
Ticks!ff-1,0,0.5g,f0,1,2,3g,f0,1,2,3gg]
We now introduce a cube, its vertices, and some facts about these elements.
88 5 Functions
SphericalPlot3D[Sin@+Sin[5
]/5,f;0,/2g,f
,3/2,11/6g,
Ticks!ff-1,0,1g,f-1,0,1g,f-.5,0,.5gg,Mesh!None,
PlotLabel!Style[SphericalPlot3D,14,Bold,Blue],ImageSize!200]g
See Fig. 5.45.
We now introduce two helices. The first is conic and the second is cylindrical.
Then, we put them together into the same frame Fig. 5.46.
Clear[r,t]
{p1=ParametricPlot3D[ft Cos@t,t Sin@t,tg,ft,0,2g,
PlotStyle!fDirective[Thick,Darker[Green]]g,ImageSize!200],
p2=ParametricPlot3D[ffCos@r,Sin@r,0g,fCos@r,Sin@r,rgg,fr,0,2g,
PlotStyle!fDirective[Thick,Red]g,ImageSize!f150,275g,
Axes!True,AxesOrigin!f0,0,0g,Ticks!ff-1,1g,f-1,0,1g,f,2gg,
TicksStyle!16,AxesStyle!Directive[Thickness!.0175,
Darker[Green]],ViewPoint!f3,0,/3g,Boxed!False],
Show[p1,p2,ImageSize!200]}
The Möbius surface is given below Fig. 5.47.
{pmoebius=ParametricPlot3D[
fCos[t](3+r Cos[t/2]),Sin[t](3+r Cos[t/2]),r Sin[t/2]g,fr,-1,1g,ft,0,2g,
Mesh!None,PlotPoints!f75,100g,MaxRecursion! 0,
PlotStyle!fGreen,Opacity!0.4,Thickness[.02]g,
PlotPoints!50,Ticks!ff-3,0,2,4g,f-3,0,3g,f-1,0,1gg,
ImageSize!250],
Show[pmoebius,Boxed!False,Axes!False,ImageSize!250]}
90 5 Functions
ParametricPlot3D[fx,0,x2 /2g,fx,-2,2g,
PlotStyle!fDirective[Thick,Darker[Red]]g],
ParametricPlot3D[f0,y,-y2 /2g,fy,-2,2g,
PlotStyle!fDirective[Thickness!0.006,Darker[Red]]g],
Graphics3D[fPointSize[0.04],Black,Point[ff0,0,0gg]g],
2 2
PlotLabel!Style[”Saddle point of z D x2 y2 ”,FontSize!14,
Background!Yellow],ImageSize!250]
92 5 Functions
A few specialized visualization functions are given in the next sequence Fig. 5.51.
{ContourPlot3D[x2 +y2 -z2 ,fx,-2,2g,fy,-2,2g,fz,-2,2g,
PlotLabel!Style[ContourPlot3D,Red,16]],
RegionPlot3D[x2 +y2 z2 ,fx,-2,2g,fy,-2,2g,fz,-2,2g,
PlotLabel!Style[RegionPlot3D,Orange,16]],
RegionPlot[x2 y2 ,fx,-2,2g,fy,-2,2g,
PlotLabel!Style[RegionPlot,Blue,16]]}
Chapter 6
Manipulate
6.1 Manipulate
Manipulate[Plot[Sin[n x],fx,0,2g,ImageSize!150,
Ticks!ff,2g,f-1,1gg],fn,1,15,Appearance!”Labeled”g]
See Fig. 6.4.
We can also use several control variables as the following example shows:
Manipulate[Plot[Sin[n x]+Sin[m x],fx,0,2g,PlotRange!2,
ImageSize!200,Ticks!ff,2g,Automaticg],
fn,1,10,Appearance!”Labeled”g,fm,1,15,Appearance!”Labeled”g]
See Fig. 6.5.
For a discrete variable, as we did above, let us consider for example Newton’s
binomial formula.
Manipulate[Expand[.˛ C ˇ/n ],fn,1,15,1,Appearance!”Labeled”g]
See Fig. 6.6.
6.1 Manipulate 95
circumcircle, the circle passing through all the three vertices. The circumcircle’s
radius is called the circumradius. An altitude of a triangle is a straight line through a
vertex and perpendicular to the opposite side. The point where the altitude intersects
the opposite side is called the foot of the altitude. The three altitudes intersect in a
single point called the orthocenter of the triangle. An angle bisector of a triangle is
a straight line through a vertex which cuts the corresponding angle into halves. The
three angle bisectors intersect in a single point called the incenter. This is the center
of the triangle’s incircle. The incircle is the circle which lies inside the triangle and
touches all three sides. A median of a triangle is a straight line through a vertex and
the midpoint of the opposite side. The three medians intersect in a single point, the
triangle’s centroid.
We may assume that vertex A of a triangle is free, whereas the vertices B and C
are fixed.
Clear[a1,a2]
bisec=fbisx,bisyg=fx,yg/.sol7[[1]];
sol8=Solve[y==a2 x/a1&&y-c2==(bisy-c2)(x-c1)/(bisx-c1),fx,yg];
cisec=fx,yg/.sol8[[1]];rRadius=Norm[op]; rinradius=Divide[a a2,a+b+c];
ab =vertexA+((vertexA-vertexB).(vertexA-bisec))(vertexB-vertexA)/c2 ;
bc =vertexB+((vertexB-vertexC).(vertexB-bisec))(vertexC-vertexB)/a2 ;
ca =vertexC+((vertexC-vertexA).(vertexC-bisec))vertexA-vertexC)/b2 ;
Show[Graphics[fText[Style[”A”,Italic,12],vertexA+f.01,0.3g],
Text[Style[”B”,Italic,12],vertexB-f.2,.15g],
Text[Style[”C”,Italic,12],vertexC-f-.2,.15g],
Text[Style[”HA ”,Italic,12],fahax,b2-.2g],
Text[Style[”IA ”,Italic,12],faleft,b2-.2g],
Text[Style[”MA ”,Italic,12],f(b1+c1)/2,b2-.2g],
Blue,Line[ffvertexA,vertexBg,fvertexB,vertexCg,
fvertexC,vertexAg,fvertexA,ahag,fvertexC,chcg,fvertexB,bhbg,
fvertexC,midABg,fvertexA,midBCg,fvertexB,midCAg,
fvertexA,opg,fvertexB,opg,fvertexC,opgg],
Darker[Green],Line[ffvertexA,faleft,0gg,fvertexB,bipg,
fvertexC,cisecg,fbisec,ab g,fbisec,bc g,fbisec,ca gg],
Black,Line[ffop,midABg,fop,midBCg,fop,midCAgg],
Black,PointSize[.015],Point[fvertexA,vertexB,vertexC,bip,aha,bhb,
chc,midAB,midBC,midCA,faleft,0g,cisec,ab ,bc ,ca g],
Red,Point[fhp,op,centroid,bisecg],Circle[bisec,rinradius],
Circle[op,rRadius]g],ImageSize!300,PlotRange!All]]]
We can now see the important points of a triangle as well as the circumcircle and
the incircle change. HA , IA , and MA are the feet of, respectively, the height, bisector,
and median of vertex A.
Manipulate[
Quiet@mainPointsinTriangle[a1,a2],
ffa1,1,”abscissa”g,1,6,0.0001,Appearance!”Labeled”g,
ffa2,4,”ordinate”g,4,10,0.001,Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!False
]
See Fig. 6.7.
Euler’s nine points circle is an interesting circle defined by nine points related to
a triangle. The midpoints of the three sides and the feet of the three altitudes all
lie on a single circle, the triangle’s nine-point circle. The remaining three points
for which it is named so are the midpoints of the segments of altitudes between
the vertices and the orthocenter. The radius of the nine-point circle is half of the
circumradius.
98 6 Manipulate
Clear[a1,a2]
sol2=Solve[y-b2==-(a1-c1)(x-b1)/(a2-c2)&&y-c2==
(a2-c2)(x-c1)/(a1-c1),fx,yg];bp=fbhbx,bhbyg=fx,yg/.sol2[[1]];
sol3=Solve[y-c2==-a1(x-c1)/a2&&y==a2 x/a1,fx,yg];
cp=fchcx,chcyg=fx,yg/.sol3[[1]];
sol4=Solve[x==a1&&fx,yg2InfiniteLine[fvertexB,bpg],fx,yg];
hp=fhx,hyg=fx,yg/.sol4[[1]];
sol5=Solve[x==(b1+c1)/2&&(y-(c2+a2)/2)==
-(a1-c1)(x-(c1+a1)/2)/(a2-c2),fx,yg];
op=fox,oyg=fx,yg/.sol5[[1]];
radius=EuclideanDistance[midBC,(op+hp)/2];
Show[Graphics[fText[Style[A,Italic,12],vertexA+f.01,.2g],
Text[Style[B,Italic,12],vertexB-f.2,.15g],
Text[Style[C,Italic,12],vertexC-f-.2,.15g],
Text[Style[”HA ”,Italic,12],fahax,b2-.2g],
Text[Style[”MA ”,Italic,12],f(b1+c1)/2,b2-.2g],
Black,PointSize[.015],Point[fvertexA,vertexB,vertexC,hp,op,centroidg],
Red,Point[fap,bp,cp,midAB,midBC,midCA,(hp+vertexA)/2,
(hp+vertexB)/2,(hp+vertexC)/2g],PointSize[.02],Point[(hp+op)/2],
Blue,Line[ffvertexB,vertexAg,fvertexB,vertexCg,fvertexC,vertexAg,
fvertexA,apg,fvertexC,cpg,fvertexB,bpg,fvertexC,midABg,fvertexA,
midBCg,fvertexB,midCAg,fvertexA,opg,fvertexB,opg,fvertexC,opg,
fop,hpg,fvertexA,(hp+op)/2g,fvertexB,(hp+op)/2g,fvertexC,
(hp+op)/2gg],Red,Circle[(op+hp)/2,radius]g],PlotRange!All]]]
In the next example, we consider that the vertices B and C are fixed, whereas the
vertex A is free, i.e., its coordinates are free. Thus, we can see how the Euler’s circle
varies according to A.
Manipulate[
Quiet@eulersNinePointsCyrcle[a1,a2],
ffa1,1,”abscisa”g,1,6,0.0001,Appearance!”Labeled”g,
ffa2,4,”ordinate”g,3,7,0.001,Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!False]
See Fig. 6.8.
x0 .t/2 Cy0 .t/2 Cz0 .t/2 ¤ 0 for all t, where r.t/ = (x.t/; y.t/; z.t// are the Cartesian
coordinates of a point r.t/ on the curve. Its arc length is
Z tp Z t
0
s.t/ D Œx0 .u/2 C Œy0 .u/2 C Œz0 .u/2 du D r .u/ du:
t0 t0
If x0 .t/2 C y0 .t/2 C z0 .t/2 ¤ 0 for all t in an interval, the curve is said to be smooth
on that interval. If the curve is smooth, the arc length function s.t/ is increasing, and
thus it has an inverse of the form t D t.s/. Then the curve r can be reparametrized
r.s/. In that case s is said to be the natural parameter of the curve.
The unit tangent vector is defined by
tangent.t/ D r0 .t/= r0 .t/
Clear[a,k,r,t]
$Assumptions=a>0&&k>0&&r2Reals&&t2Reals;
r[t ]:=fa Cos@t,a Sin@t,k tg; (* Circular helix with the usual
representation *)
Norm[Cross[r0 @t,r00 @t]]/Norm[r0 @t]3 //Simplify (* Curvature *)
a
a2 Ck2
The Frenet–Serret trihedron is discussed in the last chapter where the Viviani’s
window is exhibited (Sect. 11.1.2). The code presented here is rather elementary.
Clear[a,k,s,circle,helix,vt,vs,tangent,tprime,normal,binormal,sfinal,
vt,vs,t]
$Assumptions=a>0&&k>0&&s2Reals&&t2Reals;
vt[t ]:=fa Cos@t,a Sin@t,k tg; (* Circular helix with the usual
representation
n *)
h i h i o
vs[s ]:= a Cos p 2s 2 ; a Sin p 2s 2 ; p k2 s 2 (* Circular
a Ck a Ck a Ck
helix by the arc parameter; this form is useful when we discuss certain
elements of it: tangent, normal, binormal, etc. *)
tangent[s ]:=vs’@s (* Colored in green *)
tprime[s ]:=vs”@s
normal[s ]:=Simplify[tprime@s/Norm[tprime@s]] (* Colored in red *)
binormal[s ]:=Cross[tangent[s],normal[s]] (* Colored in blue *)
a=1;k=a/4; p (* Values of the constants *)
sfinal=4 a2 C k2 ; (* Final value of the arc parameter *)
helix=ParametricPlot3D[Evaluate[vs@s],fs,0,sfinalg,Axes!None,
Boxed! False];
circle= h hn h i h i oi
ParametricPlot3D Evaluate a Cos p 2s 2 ; a Sin p 2s 2 ; 0 ;
n oi a Ck a Ck
p
s; 0; 2 a2 C k2 ;
Animate[
Show[helix,circle,Graphics3D[ffPointSize[.02],Black,Point[vs@s]g,
fArrowheads[0.03],Thick,Darker[Green],
Arrow[fvs@s,vs@s+tangent@sg],Red,
Arrow[fvs@s,vs@s+normal@sg],Blue,
Arrow[fvs@s,vs@s+binormal@sg]gg],
ImageSize!180],fs,0,sfinalg,AnimationRate!.01,
AnimationRunning!False,SaveDefinitions!True]
See Fig. 6.9.
102 6 Manipulate
Now we will introduce the hyperboloid of one sheet. This surface models a cooling
tower. A hyperboloid cooling tower was patented by the Dutch engineers Frederik
van Iterson and Gerard Kuypers in 1918. The general equation of the hyperboloid
of one sheet is
x2 y2 z2
C D 1; a > 0; b > 0; c > 0:
a2 b2 c2
6.1 Manipulate 103
and
where and are real parameters. Obviously, each generator (straight line) of
each family lies entirely on the surface. We find the generators depending on the
variable z.
Clear[a,b,c,x,y,z, ,]
Solve[x/a-z/c== (1-y/b)&& (x/a+z/c)==1+y/b,fx,yg]
Solve[x/a-z/c==(1+y/b)&&(x/a+z/c)==1-y/b,fx,yg]
a.zC2c z 2 / b.cC2z Cc 2 /
x ! c 1C 2 ; y !
. / c.1C 2 /
a.zC2cz2 / b.cC2zCc2 /
x ! c 1C2 ; y ! c 1C2
. / . /
Thus, its rectilinear generators have the equations
a z C 2c z 2 b c C 2z C c 2
xD ; yD ; z D z;
c .1 C 2 / c .1 C 2 /
this is a straight line depending on the parameter
a z C 2c z2 b c C 2z C c2
xD ; yD ; z D z;
c .1 C 2 / c .1 C 2 /
this is a straight line depending on the parameter :
rectilinearGeneratorsHyperboloid[ ; ]:=
Module[fa=b=1,c=2,denominator1=Divide[1,c(1+ 2 )],
denominator2=Divide[1,c(1+2 )],t,u,zg,
Show[ParametricPlot3D[fa Sqrt[1+u2 ]Cos[t],b Sqrt[1+u2 ]Sin[t],c ug,
ft,0,2g,fu,-3/c,2/cg,
104 6 Manipulate
PlotStyle!fDirective[Opacity[0.3],Lighter[Green],PlotPoints!200]g,
MaxRecursion!12,Mesh!None,Boxed!False,
ViewPoint!f15,0,1g,Axes!False],
ParametricPlot3D[fa(z+2c -z 2 )denominator1,
b(-c+2z +c 2 )denominator1,zg,fz,-3,2g,PlotStyle!Black],
ParametricPlot3D[fa(z+2c-z2 )denominator2,
-b(-c+2z+c2 )denominator2,zg,fz,-3.1,1.95g,PlotStyle!Black],
PlotRange!All,ImageSize!150]]
Manipulate[
Quiet@rectilinearGeneratorsHyperboloid[ ; ],
ff ,2,”parameter”g,-130,130,.1,Appearance!”Labeled”g,
ff,2,”parameter”g,-140,140,.1,Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!False]
See Fig. 6.10.
Chapter 7
Ordinary Differential Equations
Two tutorials issued by Wolfram Research are very helpful along this chapter,
namely, [74] and [75].
One of the simplest ordinary differential equations is x0 .t/ D x.t/: It is known that
if x W Œa; b ! R and is smooth enough, its general solution has the form x.t/ D cet ,
where c is a constant. We admit that the constant is a real number. A good source
for ordinary differential equations is [31]
Clear[t,x]
sol=DSolve[fx’[t]==x[t]g,x[t],t]
ffxŒt ! et CŒ1gg
We pick out a specific solution using /. (ReplaceAll).
y[t ]=x[t]/.sol
Plot[y[t]/.fC[1]!1g,ft,-1,1g,ImageSize!180,Ticks!ff-1,0,1g,f1,Egg]
fet CŒ1g
See Fig. 7.1.
In the next example, we study an initial value problem for a differential equation
of the first order.
Clear[x,y]
Flatten[
fsol=DSolve[fx’[t]==x[t],x[0]-2==0g,x[t],t],eqn=fy’[t]==y[t],y[0]-2==0g;
sol1=DSolve[eqn,y,t] (* Here function y is given without argument and
the output form is changed *)g,1]
Flatten[fPlot[x[t]/.sol,ft,0,1g,#],Plot[y[t]/.sol1,ft,0,1g,#,
PlotStyle!Red]g&/@ffAxesOrigin!f0,0g,ImageSize!150,
Ticks!fff0.5,”1/2”g,1.g,f1,2,3,5gggg]
{{x[t]! 2 et },{y!Function[{t},2 et ]}}
See Fig. 7.2.
We consider the following matrix with real entries ma D ff1; 2; 2g; f1; 4; 2g;
f1; 5; 3gg and present some solutions.
Clear[ma,t,x,y,z, ]
ma=ff1,-2,2g,f1,4,-2g,f1,5,-3gg;
CharacteristicPolynomial[ma, ];
Eigenvalues[N[ma]] (* Matrix ma has real and distinct eigenvalues *)
7.2 Systems of Ordinary Linear Homogeneous Differential Equations 107
sol=Flatten[DSolve[fx’[t]==x[t]-2y[t]+2z[t],y’[t]==x[t]+4y[t]-2z[t],
z’[t]==x[t]+5y[t]-3z[t]g,fx[t],y[t],z[t]g,t]];
x[t ]=sol[[1,2]]; y[t ]=sol[[2,2]]; z[t ]=sol[[3,2]];
functionsarray=Table[fx[t],y[t],z[t]g/.fC[1]!i,C[2]!j,C[3]!kg,
fi,-5,5,2g,fj,-5,5,2g,fk,-2,2g];
tograph=Flatten[functionsarray,1];
ParametricPlot3D[Evaluate[tograph],ft,-2,2g,
PlotStyle!fBlue,Red,Darker[Green]g,Ticks!ff-2,0,2g,f-2,0,2g,
f-2,0,2gg,PlotRange!ff-2,2g,f-2,2g,f-2,2gg],
ParametricPlot3D[Evaluate[tograph],ft,-2,2g,
PlotStyle!fBlue,Red,Darker[Green]g,Boxed!False,
Axes!False,PlotRange!ff-3,3g,f-3,3g,f-3,3gg]
{2.,-1.,1.}
See Fig. 7.3.
The next code shows how the same system as an initial value problem looks like.
The solution is a curve in the solid space.
Clear[x,y,z,t]
A=ff1,-2,2g,f1,4,-2g,f1,5,-3gg;
X[t ]=fx@t,y@t,z@tg;
system=MapThread[#1==#2&,fX’[t],A.X[t]g];
sol=DSolve[Join[system,fx@0==1,y@0==2,z@0==-1g],fx,y,zg,t];
ParametricPlot3D[fx[t],y[t],z[t]g/.sol,ft,-2,1g,
ColorFunction!Function[fx,y,zg,Hue[z]],PlotStyle!fThickg,
PlotRange!All,Ticks!ff0,20g,f-20,0,20g,f-40,-20,0,20gg,
ImageSize!140]
See Fig. 7.4.
108 7 Ordinary Differential Equations
solplanarsystemode[A ,x ,y ]:=
Module[fX,systemg,
X[t ]=fx@t,y@tg;
system=MapThread[#1==#2&,fX’[t],A.X[t]g];
sol=DSolve[system,fx,yg,t]]
Clear[x,y]
A=ff4,-6g,f1,-1gg;
Eigenvalues[A] (* Real and distinct eigenvalues *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-10,10,6g,fj,-10,10,6g]],2];
ParametricPlot[Evaluate[particularsols],ft,-3,3g,
PlotRange!ff-2,2g,f-1,1gg,ImageSize!300]
f2; 1g
See Fig. 7.6.
Clear[x,y]
A=ff7,-8g,f5,-5gg;
Eigenvalues[A] (* Complex eigenvalues *)
110 7 Ordinary Differential Equations
solplanarsystemode[A,x,y];
particularsols=
Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-10,10,8g,fj,-10,10,8g]],2];
ParametricPlot[Evaluate[particularsols],ft,-35,35g,
PlotRange!ff-20,20g,f-10,10gg,ImageSize!300]
f1 C 2i; 1 2ig
See Fig. 7.7.
In some examples we show the behavior of the solutions around their singular points
in the case of several linear homogeneous planar differential equations.
7.2 Systems of Ordinary Linear Homogeneous Differential Equations 111
Clear[x,y]
A=ff0,0g,f-3,0gg;
Print[”eigenvalues = ”,Eigenvalues[A]] (* Both eigenvalues are null *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[
Table[fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-3,2,2g,fj,-3,2,2g]],2];
ParametricPlot[Evaluate[particularsols],ft,-10,3g,
PlotRange!ff-2,2g,f-1,1gg,Ticks!ff-1,1g,f-1,1gg,
ImageSize!200]
eigenvalues = f0; 0g
See Fig. 7.8.
Clear[x,y]
A=ff2,0g,f0,2gg;
Print[”eigenvalues = ”,Eigenvalues[A]] (* Real positive equal
eigenvalues *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-3,2,2g,fj,-3,2,2g]],2];
ParametricPlot[Evaluate[particularsols],ft,-3,3g,
PlotRange!ff-1,1g,f-1,1gg,Ticks!ff-1,1g,f-1,1gg,
ImageSize!200]
eigenvalues = f2; 2g
See Fig. 7.9.
Clear[x,y]
A=ff1/2,1g,f0,1/2gg;
Print[”eigenvalues = ”,Eigenvalues[A]] (* Real equal positive
eigenvalues *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-3,2,2g,fj,-3,2,2g]],2];
ParametricPlot[Evaluate[particularsols],ft,-15,4g,
PlotRange!ff-1.3,1.3g,f-1.1,1.1gg,Ticks!ff-1,1g,f-1,1gg,
ImageSize!180] ˚
eigenvalues = 12 ; 12
See Fig. 7.10.
112 7 Ordinary Differential Equations
Clear[x,y]
A=ff1/2,0g,f0,2gg;
Print[”eigenvalues = ”,Eigenvalues[A]] (* Distinct positive eigenvalues *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-3,2,2g,fj,-3,2,2g]],2];
ParametricPlot[Evaluate[particularsols],ft,-15,3g,
PlotRange!ff-1.3,1.3g,f-1,1gg,Ticks!ff-1,1g,f-1,1gg,
ImageSize!180] ˚
eigenvalues = 2; 12
See Fig. 7.11.
Clear[x,y]
A=ff0,1g,f1,0gg;
Print[”eigenvalues = ”,Eigenvalues[A]] (* Distinct eigenvalues,
one positive, one negative *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-3,2,1.2g,fj,-3,2,1.2g]],2];
7.2 Systems of Ordinary Linear Homogeneous Differential Equations 113
ParametricPlot[Evaluate[particularsols],ft,-4,4g,
PlotRange!ff-1,1g,f-1,1gg,Ticks!ff-1,1g,f-1,1gg,
ImageSize!180]
eigenvalues = f1; 1g
See Fig. 7.12.
Clear[x,y]
A=ff-2,0g,f0,-2gg;
Print[”eigenvalues = ”,Eigenvalues[A]] (* Real equal negative
eigenvalues *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-3,2,1.2g,fj,-3,2,1.2g]],2];
ParametricPlot[Evaluate[particularsols],ft,-3,3g,
PlotRange!ff-1,1g,f-1,1gg,Ticks!ff-1,1g,f-1,1gg,
ImageSize!180]
eigenvalues = f2; 2g
See Fig. 7.13.
114 7 Ordinary Differential Equations
Clear[x,y]
A=ff-1,-1g,f1,-1gg;
Print[”eigenvalues = ”,Eigenvalues[A]] (* Complex distinct eigenvalues
with negative real parts *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-3,2,2g,fj,-3,2,2g]],2];
ParametricPlot[Evaluate[particularsols],ft,-10,4g,
PlotRange!ff-1,1g,f-1,1gg,Ticks!ff-1,1g,f-1,1gg,
ImageSize!180]
eigenvalues = f1 C i; 1 ig
See Fig. 7.14.
7.2 Systems of Ordinary Linear Homogeneous Differential Equations 115
Clear[x,y]
A=ff1/2,-2g,f2,0gg;
Print[”eigenvalues = ”,Eigenvalues[A]] (* Complex distinct
eigenvalues, the real part is positive *)
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/i,C[2]!1/jg,fi,-3,2,2g,fj,-3,2,2g]],2];
ParametricPlot[Evaluate[particularsols],ft,-10,3g,
PlotRange!ff-1,1g,f-1,1gg,Ticks!ff-1,1g,f-1,1gg,
ImageSize!180] n p p o
eigenvalues = 14 1 C 3i 7 ; 14 1 3i 7
Clear[x,y]
mat=fff0,0g,f-3,0gg,ff2,0g,f0,2gg,ff1/2.,1g,f0,1/2gg,
ff1./2,0g,f0,2gg,ff0,1g,f1,0gg,ff-2,0g,f0,-2gg,ff-1,-1g,f1,-1gg,
ff1,-3g,f3,1gg,ff0,-1g,f1,0ggg;
colour=fRed,Blue,Magenta,Green,Darker[Red],Brown,Blue,
Darker[Green],Redg;
font=f”Helvetica”,”Times”,”Courier”,”Old English Text MT”g;
Table[A=mat[[i]];
eigen=Eigenvalues[A];
solplanarsystemode[A,x,y];
particularsols=Partition[Flatten[Table[
fx@t,y@tg/.sol/.fC[1]!1/m,C[2]!1/jg,fm,-3,2,1.2g,fj,-3,2,1.2g]],2];
ParametricPlot[Evaluate[particularsols],ft,-8,3g,
PlotLabel!ToExpression[”eigenvalues”]==TraditionalForm[eigen],
LabelStyle!fcolour[[i]],FontFamily!font[[Mod[i,4]+1]]g,
PlotRange!ff-1.001,1g,f-1,1.1gg,Ticks!ff-1,1g,f-1,1gg,
PlotStyle!fDirective[Thickness!0.008,color[[i]]]g],fi,1,9g]
See Fig. 7.17.
Let P1 , P2 , and P3 be the equilibrium points. The first picture shows the three
equilibrium points. The second picture shows the trajectories of the system in the
phase-plane near the equilibrium points.
118 7 Ordinary Differential Equations
Clear[x,y,h,p,A]
h[x ]=17.76x-103.79x2 +229.62x3 -226.31x4 +83.72x5 ;
A=f0.5f-h[x],yg,0.2f-x,-1.5 y+1.2gg;
diffA=ffD[A[[1,1]],x],D[A[[1,2]],y]g,fD[A[[2,1]],x],D[A[[2,2]],y]gg;
soll=Solve[h[x]-y==0&&-x-1.5y+1.2==0,fx,yg,Reals];
Table[p[i]=fx,yg/.soll[[i]],fi,1,3g];
Table[Eigenvalues[diffA/.fx!p[i][[1]],y!p[i][[2]]g],fi,1,3g];
fShow[Plot[(1.2-x)/1.5,fx,0,1.2g,
PlotStyle!fDirective[Thickness!0.005,Blue]g],
Plot[h[x],fx,0,1.2g,PlotRange!ff0,1.25g,f0,1gg,
PlotStyle!fDirective[Thickness!0.005,Blue]g],
Graphics[ffPointSize[0.017],Red,Point[p[1]],Point[p[2]],Point[p[3]]g,
fText[Style[”P1 ”,Italic,12],p[1]+f0.068,0.04g],
Text[Style[”y=h(x)”,Italic,12],p[3]+f-.028,.67g],
Text[Style[”x+1.5y=1.2”,Italic,12],f.67,.56g],
Text[Style[”P2 ”,Italic,12],p[2]+f0.06,0.04g],
Text[Style[”P3 ”,Italic,12],p[3]+f0.07,0.04g]gg],
PlotRange!All,ImageSize!f250,180g],
eqs=Sequence[u’[t]==0.5(-h[u[t]]+v[t]),v’[t]==0.2(-u[t]-1.5 v[t]+1.2)];
Show[Table[sol2=NDSolve[feqs,u[0]==p[1][[1]]+i/4,
v[0]==p[1][[2]]+j/4g,fu,vg,ft,0,100g];
sol3=NDSolve[feqs,u[0]==p[2][[1]]+i/4,v[0]==p[2][[2]]+j/4g,fu,vg,
ft,0,100g];
sol4=NDSolve[feqs,u[0]==p[3][[1]]+i/4,v[0]==p[3][[2]]+j/4g,fu,vg,
ft,0,100g];
ParametricPlot[fEvaluate[fu[t],v[t]g/.sol2],Evaluate[fu[t],v[t]g/.sol3],
Evaluate[fu[t],v[t]g/.sol4]g,ft,0,100g],fi,-2,2g,fj,-2,2g],
Graphics[fPointSize[0.015],Red,Point[p[1]],Point[p[2]],Point[p[3]],
Black,
Text[Style[”P1 ”,Italic,12],p[1]+f0.068,0g],
Text[Style[”P2 ”,Italic,12],p[2]+f-0.065,0g],
Text[Style[”P3 ”,Italic,12],p[3]+f0.06,-0.065g]g],
PlotRange!ff-.4,1.4g,f-.3,1.3gg,AxesOrigin!f0,0g,
ImageSize!f320,300g]g
See Fig. 7.18.
listofpoints[m ,h ]:=
Module[fk,u,vg,
For[k=0,k<m,k++,
u D x C h.y x3 =2 C x4 =2 C 3y4 =2 3y5 =4/I
v D y C h.4x 6y3 C x4 C 3y4 x5 /I
AppendTo[points,fu,vg];x=u;y=v]]
points=fequipointreal[[5]]g;
fx,yg=equipointreal[[5]]+f-0.01,0.01g;
h=0.02;nsteps=8;listofpoints[nsteps,h];
arrow5nw=fpoints[[Floor[nsteps/2]+3]],points[[Floor[nsteps/2]+4]]g;
plot5nw=ListLinePlot@N@points;
points=fequipointreal[[5]]g;
fx,yg=equipointreal[[5]]+f0.01,-0.01g;
h=0.02;nsteps=14;listofpoints[nsteps,h];
arrow5se=fpoints[[Floor[nsteps/2]+2]],points[[Floor[nsteps/2]+3]]g;
plot5se=ListLinePlot@N@points;
120 7 Ordinary Differential Equations
points=fequipointreal[[5]]g;
fx,yg=equipointreal[[5]]+f-0.01,-0.01g;
h=0.02;nsteps=150;listofpoints[nsteps,-h];
arrow5sw=fpoints[[Floor[nsteps/3]+4]],points[[Floor[nsteps/3]+3]]g;
plot5sw=ListLinePlot@N@points;
points=fequipointreal[[5]]g;
fx,yg=equipointreal[[5]]+f0.01,0.01g;
h=0.02;nsteps=48;listofpoints[nsteps,-h];
arrow5ne=fpoints[[Floor[nsteps/2]+5]],points[[Floor[nsteps/2]+4]]g;
plot5ne=ListLinePlot[N[points]];
points=fequipointreal[[4]]g;
fx,yg=equipointreal[[4]]+f-0.01,0.01g;
h=0.02;nsteps=9;listofpoints[nsteps,h];
arrow4nw=fpoints[[Floor[nsteps/2]+4]],points[[Floor[nsteps/2]+5]]g;
plot4nw=ListLinePlot@N@points;
points=fequipointreal[[4]]g;
fx,yg=equipointreal[[4]]+f0.00001,-0.00001g;
h=0.02;nsteps=265;listofpoints[nsteps,h];
arrow4se=fpoints[[Floor[nsteps/11]+2]],points[[Floor[nsteps/11]+5]]g;
plot4se=ListLinePlot[N[points]];
points=fequipointreal[[4]]g;
fx,yg=equipointreal[[4]]+f-0.01,-0.01g;
h=0.02;nsteps=29;listofpoints[nsteps,-h];
arrow4sw=fpoints[[Floor[nsteps/2]+3]],points[[Floor[nsteps/2]+2]]g;
plot4sw=ListLinePlot@N@points;
points=fequipointreal[[4]]g;
fx,yg=equipointreal[[4]]+f0.01,0.0001g;
h=0.02;nsteps=100;listofpoints[nsteps,-h];
arrow4e=fpoints[[Floor[nsteps/5]+3]],points[[Floor[nsteps/5]+2]]g;
plot4e=ListLinePlot@N@points;
points=fpoints[[Floor[nsteps/2]]]g;
fx,yg=Flatten[points]-f0,0.001g;
h=0.02;nsteps=100;listofpoints[nsteps,h];
arrow3ws=fpoints[[Floor[nsteps/4]+1]],points[[Floor[nsteps/4]+2]]g;
plot3ws=ListLinePlot@N@points;
7.2 Systems of Ordinary Linear Homogeneous Differential Equations 121
points=fequipointreal[[3]]g;
fx,yg=equipointreal[[3]]+f-0.01,0.01g;
h=0.02;nsteps=30;listofpoints[nsteps,h];
arrow3nw=fpoints[[Floor[nsteps/2]+9]],points[[Floor[nsteps/2]+10]]g;
plot3nw=ListLinePlot@N@points;
points=fequipointreal[[3]]g;
fx,yg=equipointreal[[3]]+f-0.1,0.001g;
h=0.02;nsteps=50;listofpoints[nsteps,h];
arrow3w=fpoints[[Floor[nsteps/2]+2]],points[[Floor[nsteps/2]+3]]g;
plot3w=ListLinePlot@N@points;
points=fequipointreal[[2]]g;
fx,yg=equipointreal[[2]]+f0.01,0.005g;
h=0.02;nsteps=173;listofpoints[nsteps,h];
arrow2e=fpoints[[Floor[nsteps/2]+60]],points[[Floor[nsteps/2]+61]]g;
plot2e=ListLinePlot[N[points],InterpolationOrder!2];
points=fequipointreal[[2]]g;
fx,yg=equipointreal[[2]]+f-0.01,-0.001g;
h=0.02;nsteps=1000;listofpoints[nsteps,h];
arrow2sw=fpoints[[Floor[nsteps/6]+24]],points[[Floor[nsteps/6]+25]]g;
plot2sw=ListLinePlot@N@points;
points=fequipointreal[[2]]g;
fx,yg=equipointreal[[2]]+f-0.001,0.001g;
h=0.02;nsteps=150;listofpoints[nsteps,-h];
arrow2n=fpoints[[Floor[nsteps/4]+9]],points[[Floor[nsteps/4]+8]]g;
plot2n=ListLinePlot@N@points;
points=fequipointreal[[2]]g;
fx,yg=equipointreal[[2]]+f0.01,-0.01g;
h=0.02;nsteps=74;listofpoints[nsteps,-h];
arrow2s=fpoints[[Floor[nsteps/3]+9]],points[[Floor[nsteps/3]+8]]g;
plot2s=ListLinePlot@N@points;
fx,yg=f2.7,2.65g;
points=ffx,ygg;
h=0.002;nsteps=18;listofpoints[nsteps,h];
arrow5fs=fpoints[[Floor[nsteps/2]]],points[[Floor[nsteps/2]+1]]g;
plot5fs=ListLinePlot[N@points,InterpolationOrder!2];
fx,yg=f2.7,2.9g;
points=ffx,ygg;
h=0.002;nsteps=9;listofpoints[nsteps,h];
122 7 Ordinary Differential Equations
arrow5fn=fpoints[[Floor[nsteps/2]+3]],points[[Floor[nsteps/2]+4]]g;
plot5fn=ListLinePlot[N@points,InterpolationOrder!2];
h=0.02;
fx,yg=f-2,-1.5g;
points=ffx,ygg;
h=0.02;nsteps=200;listofpoints[nsteps,h];
arrow1fn=fpoints[[Floor[nsteps/5]+10]],points[[Floor[nsteps/5]+11]]g;
plot1fn=ListLinePlot[N[points],InterpolationOrder!4];
fx,yg=f-2.7,-1.1g;
points=ffx,ygg;
h=0.002;nsteps=32;listofpoints[nsteps,h];
arrowfsn=fpoints[[Floor[nsteps/2]+1]],points[[Floor[nsteps/2]+2]]g;
plotfsn=ListLinePlot[N[points]];
Show[plot5nw,plot5se,plot5ne,plot5sw,plot4nw,plot4se,plot4sw,
plot4e,plot3nw,plot2e,plot2sw,plot2n,plot2s,plot5fs,plot5fn,plot1fn,
plotfsn,plot3ws,
Graphics[ffPointSize[0.013],Red,Point[fequipointreal[[5]],
equipointreal[[4]],equipointreal[[3]],equipointreal[[2]],
equipointreal[[1]]g]g,
Arrowheads[0.028],Green,
Arrow[farrow5nw,arrow5sw,arrow5ne,arrow5se,arrow4nw,arrow4se,
arrow4sw,arrow4e,arrow3nw,arrow2e,arrow2sw,arrow2n,arrow2s,
arrow5fs,arrow5fn,arrow1fn,arrowfsn,arrow3wsg]g],
Ticks!ff-3,-2,-1,1,2,3g,f-3,-2,-1,1,2,3,4gg,AxesOrigin!f0,0g,
PlotPoints!40,AspectRatio!1,ImageSize!300,PlotRange!All]
See Fig. 7.19.
The second code follows:
Clear[x,y]
equipoints=Solve[-y-x3 /2+x4 /2+3y4 /2-3y5 /4==0&&
4x-6y3 +x4 +3y4 -x5 ==0,fx,yg,Reals];
eqreal=fx,yg/.equipoints;
vector=ffeqreal[[5]]+f-0.01,0.01g,eqreal[[5]]+f0.01,-0.01g,
eqreal[[5]]+f-0.01,-0.01g,eqreal[[5]]+f0.01,0.01g,
eqreal[[5]]+f0.01,-0.01g,eqreal[[5]]+f-0.01,-0.01g,
eqreal[[5]]+f0.01,0.01g,eqreal[[4]]+f-0.01,0.01g,g
eqreal[[4]]+f0.00001,-0.00001g,eqreal[[4]]+f-0.01,-0.01g,
eqreal[[4]]+f0.01,0.0001g,eqreal[[3]]+f-0.01,0.01g,
eqreal[[2]]+f0.01,0.005g,eqreal[[2]]+f-0.01,-0.001g,
eqreal[[2]]+f-0.001,0.001g,eqreal[[2]]+f0.01,-0.01g,
f2.7,2.65g,f2.7,2.9g,f-2,-1.5g,f-2.7,-1.1gg,
f8,14,150,48,14,150,48,9,265,29,100,30,173,1000,150,74,18,9,200,32g,
7.2 Systems of Ordinary Linear Homogeneous Differential Equations 123
f0.02,0.02,-0.02,-0.02,0.02,-0.02,-0.02,0.02,0.02,-0.02,-0.02,0.02,
0.02,0.02,-0.02,-0.02,0.002,0.002,0.02,0.002g,
f2,2,3,2, 2,3,2,2,11,2,5,2, 2,6,4,3,2,2,5,2g,
ff3,4g,f2,3g,f4,3g,f5,4g,f2,3g,f4,3g,f5,4g,f4,5g,f2,5g,f3,2g,f3,2g,
f9,10g,f60,61g,f24,25g,f9,8g,f9,8g,f0,1g,f3,4g,f10,11g,f1,2ggg;
aArrows=aPlots=fg;
Do[fx,yg=Flatten[vector[[1,m]]];points=ffx,ygg;
nsteps=vector[[2,m]];
For[k=0,k<nsteps,k++,
u=x+vector[[3,m]](-y-x3 /2+x4 /2+3y4 /2-3y5 /4);
v=y+vector[[3,m]](4x-6y3 +x4 +3y4 -x5 );
AppendTo[points,fu,vg];x=u;y=v];
aArrows=AppendTo[aArrows,
fpoints[[Floor[nsteps/vector[[4,m]]+vector[[5,m,1]]]]],
points[[Floor[nsteps/vector[[4,m]]+vector[[5,m,2]]]]]g];
aPlots=AppendTo[aPlots,ListLinePlot[N@points]],
fm,Length[vector[[1]]]g]
Show[Flatten[aPlots],
Graphics[ffPointSize[0.013],Red,
Point[feqreal[[5]],eqreal[[4]],eqreal[[3]],eqreal[[2]],eqreal[[1]]g]g,
Arrowheads[0.028],Green,Arrow[aArrows]g],AxesOrigin!f0,0g,
AspectRatio!1,Ticks!ff-3,-2,-1,1,2,3g,f-3,-2,-1,1,2,3,4gg,
PlotRange!All]
See Fig. 7.20.
124 7 Ordinary Differential Equations
For the explicit fourth-order Runge–Kutta method which requires only three
evaluations at each step, we consider a system of autonomous ordinary differential
equations of the first order in the vectorial form
dx
D f .x/ (7.1)
dt
We suppose that the vector x D x0 is given at t D a. The interval Œa; b, where
the independent variable t belongs, is partitioned by a finite mesh hi > 0 and
m
X
aC hi D b: (7.2)
iD1
We also suppose that the function f is sufficiently smooth such that all that
follow are correct. This implies that the initial value problem stated at x.a/ D x0
has a unique solution which exists on the whole interval Œa; b. The sequence of
approximations .xn / of the unknown function x solution of the initial value problem
consisting in the system of differential equations (7.1) and the initial condition is
defined by
i D 0; 1; 2 and n D 1; 2; : : : ; m:
ki;1 D x0 ; i D 0; 1; 2:
where
60 1 2 .0 C 1 / C 1 D 0:
31 1 1 0
˛0 D ; 1;0 D ;
6 .1 0 / .1 0 / 40 .1 30 /
30 1 .1 0 / .41 1/ .30 1 /
˛1 D ; 2;0 D ;
6 .1 1/ .1 0 / 20 .1 0 / .1 60 1 /
1 60 1 .1 30 / .1 40 1 /
˛2 D ; 2;1 D ;
12 .1 1 / .1 0 / .1 0 / .1 60 1 /
sS D 1=2:
Further, if we denote
1 1
1;1 D ˇ; 2;1 D ; and ı0 D ;
0 1
it results
˛1 ˇ C ˛2 ı
1;0 D ıˇ; 2;0 D ı; 0;1 D ;
˛0
ı .˛1 ˇ C ˛2 ı/ .ı 1/ .˛1 ˇ C ˛2 ı/
0;0 D ; 0;2 D 0 ;
˛0 ˛0
1;2 D 1 1;0 C .ı 1/ˇ; 2;2 D 2 .2;0 C 2;1 / C .ı 1/;
7.3 On Two Runge–Kutta Methods of the Fourth Order 127
1;0 D ı ˇI
1;2 D 1 1;0 C .ı 1/ˇI
2;0 D ı I
2;2 D 2 .2;0 C 2;1 / C .ı 1/ I
0 C1
1 4.13 0 /0
0 1;0 2;1
By some numerical experiments it seems reasonable to consider that
0 D 0:5; 1 D 0:0; 2 D 1:0; 1;1 D 1 .1;0 C 0 1;0 C 2;1 / :
We consider the next example. The approach by our Runge–Kutta method
follows.
f[fx ,y ,z g]:=f1,Cos[x]2 y-(1-Sin[x]Cos[x])z,(1+Sin[x]Cos[x])y+Sin[x]2 zg
(* The right-hand side of the system of differential equations *)
x0=f0,1,1g; (* Initial value *)
a=0; (* Leftmost boundary value *)
b=; (* Rightmost boundary value *)
m=1000; (* Number of steps *)
h=(b-a)/m; (* Step size uniformly distributed *)
kold0=kold1=kold2=x0;
Do[
knew0=f[x0+h( 0;0 kold0+ 0;1 kold1+ 0;2 kold2)];
knew1=f[x0+h( 1;0 kold0+ 1;1 kold1+ 1;2 kold2+1;0 knew0)];
knew2=f[x0+h( 2;0 kold0+ 2;1 kold1+ 2;2 kold2+2;0 knew0+2;1 knew1)];
x1=x0+h(˛0 knew0+˛1 knew1+˛2 knew2);
x0=x1;
kold0=knew0;kold1=knew1;kold2=knew2,
128 7 Ordinary Differential Equations
fi,mg]
N[x1,15]
{3.14159,-23.1405,-0.989255}
The solution by the built-in function NDSolve is presented below.
Clear[x,y,z,t]
sol=NDSolve[fx’[t]==1,y’[t]==Cos[x[t]]2 y[t]-(1-Sin[x[t]]Cos[x[t]])z[t],
z’[t]==(1+Sin[x[t]]Cos[x[t]])y[t]+Sin[x[t]]2 z[t],x[0]==0,y[0]==1,z[0]==1g,
fx,y,zg,ft,g,AccuracyGoal!20,PrecisionGoal!20,
WorkingPrecision!35];
N[fx[],y[],z[]g/.sol,15] (* The solution by integration *)
N[ft,Exp[t]Cos[t]-Sin[t],Exp[t]Sin[t]+Cos[t]g/.t! ,15] (* The exact
solution at *)
{{3.14159265358979,-23.1406926327793,-1.00000000000000}}
{3.14159265358979,-23.1406926327793,-1.00000000000000}
Plot[ft,Exp[t]Cos[t]-Sin[t],Exp[t]Sin[t]+Cos[t]g,ft,0,g,PlotRange!All]
(* The graph of the exact solution *)
See Fig. 7.21.
has a unique solution, which exists on the whole interval Œa; b. The sequence of
approximations .xn / of the unknown function x solution of the initial value problem
consisting in the system of differential equations (7.1) and the initial condition is
well defined.
It is known that to each initial value problem, we assign a table of coefficients of
the form
Clear[a,b,
]
: ::
Grid[ffa ,. . . ,a ,
g,f::, ,::,::g,fa ,. . . ,a ,
g,fb ,. . . ,b ,gg,
1;1 1;q 1 q;1 q;q q 1 q
Dividers!ffFalse,False,False,Trueg,fFalse,False,False,Truegg]
a1;1 : : : a1;q
1
:: :: ::
: : :
aq;1 : : : aq;q
q
b1 : : : bq
where
q
X
aj;i D
j ;
iD1
q
X
xnC1 D xn C hn bi ki;n ; (7.3)
iD1
q
X
ki;n D f argi;n ; argi;n D xn C hn ai;j kj;n :
jD1
1 1 1 1
2
C p 0 2
C p
2 3 2 3
13 1
2
C 1
p 1
1
p
2 3 2 2 3
1 1
2 2
and
Clear[˛]
h i
p
Print “.q; p/ D .3; 4/; ”; “˛ D .2= 3/cos.=18/”
GridŒff.1 C ˛/=2; 0; 0; .1 C ˛/=2g; f˛=2; .1 C ˛/=2; 0; 1=2g;
f1
˚ C ˛; .1
C 2˛/; .1 C ˛/=2;
.1 ˛/=2g;
1= 6˛ 2 ; 1 1= 3˛ 2 ; 1= 6˛ 2 ; Null ;
Dividers!ffFalse,False,False,Trueg,fFalse,False,False,Truegg
p
(q,p)=(3,4), ˛ D .2= 3/cos.=18/
1C˛ 1C˛
2
0 0 2
˛2 1C˛
2
0 1
2
1C˛ 1˛
1 C ˛ 1 2˛ 2 2
1
6˛ 2
1 3˛1 2 1
6˛ 2
ki;1 D x0 :
If we take
q
X
i D i;j C ai;j ;
jD1
P1 .2k3/!! 2
vandermonde= kD0 .2k/!!
4
Regarding the speed of convergence we note some remarks.
P 2
vandermonde[n ]:= nkD0 .2k3/!!
.2k/!!
Table[N[vandermonde[k]],fk,6g] (* The first six terms *)
DiscretePlot[vandermonde[n],fn,50g,
Ticks!fAutomatic,f1.2728,1.27295,1.2731,f4/,“4/”ggg,
PlotStyle!Black,ImageSize!250]
Simplify[vandermonde[1]]
{1.25,1.26563,1.26953,1.27106,1.2718,1.27223}
See Fig. 8.1.
Relevant information on this topic may be found in many papers. We only mention
[70] and [76].
8.1 Various Simple and Not So Simple Formulas 135
˚ 1
FullSimplify 4ArcTan 15 ArcTan 239 ; (* Machbin’s original
formula 1706
1 *) 1 1
8ArcTan 10 ArcTan 239 4ArcTan 515 ; (* Klingenstierna
1730 *)
2ArcTan 12 ArcTan 17 ; ArcTan 12 C ArcTan 13 ; (* Euler 1738 *)
1 3
5ArcTan 7 C 2ArcTan 79 ; (* Euler 1755 *)
1 1
4ArcTan 15 ArcTan 70 C ArcTan 99 ; (* Euler 1764 *)
1 1
ArcTan 2 C ArcTan 3 ; (* Hutton 1776 *)
2ArcTan 13 C ArcTan 17 ; (* Hutton 1776 *)
ArcTan 12 C ArcTan 15 C ArcTan 18 ; (* Strassnitzky 1844 *)
1 1 1
12ArcTan 18 C 8ArcTan 57 5ArcTan 239 ; (* Gauss 1863 *)
1 1 1
6ArcTan 8 C 2ArcTan 57 C ArcTan 239 ; (* Störmer 1896 *)
5ArcTan 1 C 2ArcTan 3
˚ 7 79
; ; ; ; ; ; ; ; ; ; ;
4 4 4 4 4 4 4 4 4 4 4 4
8.1.3.1 Kanada
In December 2002, Kanada computed to over 1.24 trillion decimal digits. His
team first computed in hexadecimal (base 16) to 1,030,700,000,000 places, using
the following two arctangent relations:
˚ 1 1 1
FullSimplify 12ArcTan 49 C 32ArcTan 57 5ArcTan 239 C
1
12ArcTan 110443 ; (* Takano, [63] *)
1 1 1 1
44ArcTan 57 C 7ArcTan 239 12ArcTan 682 C 24ArcTan 12943
˚(* Störmer
1896, [12] *)
;
4 4
This formula may be found in [71, (32)]. The convergence of this series is slow.
The graph below also shows the low speed of convergence.
Clear[s,n]
P k
s[n ]:=4 nkD0 .1/
2kC1
I
DiscretePlot[s[n],fn,50g,
Ticks!fAutomatic,f3,3.1,,3.2,3.3gg,
PlotStyle!Black,ImageSize!250]
s[1] (* The sum of the series *)
See Fig. 8.2.
136 8 Pi Formulas
This formula may be found in [71, (8)]. In this formula number is connected to
the function of Riemann and is computed by it.
P k
vardi[n ]:= nkD1 3 41
k Zeta[k+1];
8.1.7.1 Case 1
p P 1
case1[n ]:= 3 2 3 nkD0 .1/k 3kC1 1
C 3kC2 ;
Table[N[case1[k]],fk,6g] (* The first six terms of the sequence *)
DiscretePlot[case1[n],fn,50g,
Ticks!fAutomatic,f3,3.1,,3.2,3.3gg,
PlotStyle!Black,ImageSize!250]
case1[1] (* The sum of the series *)
{2.72798,3.42389,2.9279,3.31333,2.99812,3.26476}
See Fig. 8.5.
We can prove the previous result in the following way.
Clear[a,b]
P1 a
k b
kD0 .1/ 3kC1
C 3kC2
FullSimplify[%]
p
1
p
9
3a C 3b C aLogŒ8 bLogŒ8
p
1
9
3.a C b/ C .a b/LogŒ8
8.1.7.2 Case 2
Clear[a,b,c]
P1 a
k b c
kD0 .1/ 4kC1
C 4kC2
C 4kC3
FullSimplify[%]
1
2 2
16
a C c C 2bCot 8 C aCot 8 C cCot 8 C
p p
4 2aCot 8 Log Cos 8 4 2cCot 8 Log Cos 8
p p
4 2aCot 8 Log Sin 8 C4 2cCot 8 Log Sin 8 Tan 8
p p p hp i
1
8
2a C b C 2c C 2 2.a c/ArcCoth 2
as=Array[a,3];
product= p42 as.Array[f,3];
asArray=FullSimplify[product]
p p p
1
4
. 2aŒ2 C 2aŒ3 C 2aŒ1. C 2ArcCothŒ 2/ C 2aŒ3LogŒ3 2 2/
Because this intermediate result is complicated, we try to find relations between
the values of transcendental numbers so that the result is simplified. We have found
two such relations and we substitute them. Then
140 8 Pi Formulas
p p
simpler=Simplify[Together[asArray/.fArcCoth[
p p 2]!(1/2)Log[3+2 2],
Log[3-2 2]!-Log[3+2
p 2]g]] p
1
4
..2aŒ1 C 2aŒ2 C 2aŒ3/ C 2.aŒ1 aŒ3/LogŒ3 C 2 2/
collected=Map[Factor,Collect[simpler,transsimpler]]
h
1
p p i
4
2aŒ1 C 2aŒ2 C 2aŒ3 C 12 .aŒ1 aŒ3/Log 3 C 2 2
The previous system is written under matrix form and we find its rank.
TableForm[syst,2]
(coeffMatrixx=Normal[CoefficientArrays[syst,as]][[2]])//MatrixForm
MatrixRank[coeffMatrixx]
aŒ1
2
aŒ3
2
aŒ1
2
C aŒ2
p C
aŒ3
2 2 ! 2
1
2
0 12
1 p1 1
2 2 2 2
2
8.1 Various Simple and Not So Simple Formulas 141
Since the rank is 2, one coefficient becomes parameter and the system is solved
with respect to this parameter. Thus, we find out the next result.
sol=Solve[syst==f0,1g]
p p
ffaŒ2 ! 2 2 2 2aŒ1; aŒ3 ! aŒ1gg
We substitute and check the result.
P1 p p
p4 k a 2 22 2a a
2 kD0 .1/ 4kC1
C 4kC2
C 4kC3
Simplify[%]
p p
2.1C 2/
p
2C 2
The last series converges to for any complex number a.
8.1.7.3 Case 3
By a definite integral
We can try to approach this series by means of the corresponding definite integral.
We have
Clear[a]
R 1 xi1
f[i ]:= 0 1Cx 5 dx;
as=Array[a,4];
as.Array[f,4];
asArray=FullSimplify[%]
p p p
1
p p
100
2 10 5 5.aŒ2 C aŒ3/ C 5 C 5.aŒ1 C aŒ4/ C
p h p i p h i
5aŒ2ArcTanh 6765 15127
5
C 5aŒ1 2 5ArcCoth p3 C LogŒ16
p h i p h 5i
5 2 5aŒ4ArcCoth p3 C aŒ3 2 5ArcCoth p3 LogŒ16 C
5 5
.aŒ2 C aŒ4/LogŒ16//
transsimpler=GetTranscendentals[simpler]
n h i o
; ArcCoth p3 ; LogŒ2
5
collected=Map[Factor,Collect[simpler,transsimpler]]
p p p p
p p p p
p1 5 C 5aŒ1 C 5 5aŒ2 C 5 5aŒ3 C 5 C 5aŒ4 C
5 10 h i
p3
.aŒ1CaŒ2aŒ3aŒ4/ArcCoth
p 5
C 15 .aŒ1 aŒ2 C aŒ3 aŒ4/LogŒ2
2 5
8.1 Various Simple and Not So Simple Formulas 143
system=DeleteCases[Flatten[CoefficientList[collected,
Append[transsimpler,Sqrt[2]]]],0]
TableForm[system,4]
(coeffMatrix=Normal[CoefficientArrays[system,as]][[2]])//MatrixForm
MatrixRank[coeffMatrix]
r
aŒ1 aŒ2 aŒ3 aŒ4 aŒ1 aŒ2 aŒ3 aŒ4 1 1
p
5
5
C 5
;
5 2 5
p C p p p ; 5 C 5 aŒ1 C
2 5 2 5 2 5 10 5
r r r
1 1
p 1 1
p 1 1
p
10 5
5 5 aŒ2 C 10 5
5 5 aŒ3 C 10 5
5 C 5 aŒ4
aŒ1
5
aŒ2
5
C aŒ3
5
aŒ4
5
aŒ1 aŒ2 aŒ3 aŒ4
p C p p p
2 r 5 2 5 2 5 2 5 r r
p p p
1 1 1 1 1 1
10 5
5 C 5 aŒ1 C 10 5
5 5 aŒ2 C 10 5
5 5 aŒ3C
r
1 1
p
10 5
5 C 5 aŒ4
0 1 1
5
15 15 1
5
B 1
p 1
p p 1 C p 1
B r 2 5 r 2 5 r r 2 5 C 2 5
@ p p p p A
1 1 1 1 1 1 1 1
10 5
5 C 5 10 5 5 5 10 5 5 5 10 5 5 C 5
3
sol=FullSimplify[Solve[system==f0,0,1g]]
p p p
ffaŒ2 ! 12 .5 5 C 5 1 C 5 aŒ1/;
p p p
aŒ3 ! 12 5 5 C 5 1 C 5 aŒ1 ; aŒ4 ! aŒ1gg
Taking fa; b; c; dg D f1; 0; 0; 1g, we get the same result as before.
8.1.7.4 Case 4
Another particular case is the next one, which is true for every complex c.
Clear[c]
P1 1
k 0 c 0 1
kD0 .1/ 6kC1
C 6kC2
C 6kC3
C 6kC4
C 6kC5
1
12
.4 C c/
Another easy option is b D 0 and 4a C c D 12. Particularly, a D 1 and c D 8,
P1 k
1 8 1
kD0 .1/ 6kC1
C 6kC3 C 6kC5
Regarding the convergence of this series, we remark the following:
P 1
case4[n ]:= nkD0 .1/k 6kC1 8
C 6kC3 1
C 6kC5 ;
Table[N[case4[k]],fk,6g] (* The first six terms of the sequence *)
DiscretePlot[case4[n],fn,50g,
Ticks!fAutomatic,f3,3.1,,3.2,3.3gg,
PlotStyle!Black,ImageSize!250]
Simplify[case4[1]]
f2:74401; 3:41309; 2:93603; 3:30681; 3:00355; 3:2601g
See Fig. 8.7.
8.1.7.5 Case 5
1
2fCsc 7 bLogŒ4 C cLogŒ4 dLogŒ4 C eLogŒ4 fLogŒ4 C
28
4dCos 7 Log Cos 3 Cot 7 C 4.b C e/Cos 7
14
Log Root 8 C 20#1 12#12 C #13 &; 2 4fCos 7
Log Root 8 C 20#1 12#12 C #13 &; 3 C 4cCos 7
3
Log Sec 14 Tan 7 C 2cSec 14 C 2dSec 14 C 2bSec 3 C
3 2 3
14
2eSec 14 4f Log Root 8 C 20#1 12#1 C #1 &; 2 Sin 14 C
4cLog Root 8 C 20#1 12#12 C #13 &; 3 Sin 14
dLog Root 8 C 20#1 12#12 C #13 &; 3 Sin 14 C
4bLog Root 1 C 12#1 20#12 C 8#13 &; 3 Sin 14
2 3
4eLog Root 1 C 12#1 20#1 C 8#1 &; 3 Sin 14 C
h 2 i h 2 i h 2 i
4 bLog 12 Csc 14 C dLog 12 Csc 3 14
C eLog 2Sin 14
C
h 3 2 i 3 3
c Log 2Sin 14 C fLog Sec 14 Tan 7 Sin 14 C
h 8 i
a 2Csc 7 C LogŒ4 Cos 7 Log 16Sin 14 C
2 3
4Log Root 8 C 20#1 12#1 C #1 &; 2 Sin 14 C
4Log Cos 3 14
Cot 7 Sin 3 14
We note that the result is rather complicated and it is hard to believe that it can
be easily manipulated. Therefore, we pass to a particular form of it.
P a
7 1 kD0 .1/
k
7kC1
b
C 7kC2 c
C 7kC3 c
C 7kC4 b
C 7kC5 a
C 7kC6 ;
FullSimplify[%]
a Csc 7 C c Sec 14 C b Sec 314
FullSimplify %=:Csc 7 ! Sec 14 C Sec 314
.a C c/Sec 14 C .a C b/Sec 3 14
So the result
.a C c/Sec 14 C .a C b/Sec 3
14
depends on the (complex) coefficients a, b, and c.
8.1.7.6 Case 6
By a definite integral
Because the last result is rather long, we approach this series by means of a definite
integral.
Clear[a]
R 1 xi1
f[i ]:= 0 1Cx 8 dx;
as=Array[a,7];
asArray=FullSimplify[as.Array[f,7]]
p p
1
p p
16
2aŒ2 C aŒ4 C 4 2 2.aŒ3 C aŒ5/ C 2aŒ6 C
q p
2.2 C 2/.aŒ1 C aŒ7/
p
2aŒ6ArcCoshŒ3 C 4 .aŒ1 aŒ7/ArcTanh Cos 8 C
.aŒ3 C aŒ5/ArcTanh Sin 8 Cos 8
p h p i
2aŒ2Log 3 2 2 C 4 .aŒ3 aŒ5/ArcTanh Cos 8 C
.aŒ1 aŒ7/ArcTanh Sin 8 Sin 8
8.1 Various Simple and Not So Simple Formulas 147
transsimpler=GetTranscendentals[simpler]
n h
p i
; ArcTanh Cos 8 ; ArcTanh Sin 8 ; Cos 8 ; Log 3 C 2 2 ;
Sin 8
collected=Map[Factor,Collect[simpler,transsimpler]];
system=DeleteCases[Flatten[CoefficientList[collected,
Append[transsimpler,Sqrt[2]]]],0]
TableForm[system,7];
(coeffMatrix=Normal[CoefficientArrays[system,as]][[2]])//MatrixForm
MatrixRank[coeffMatrix]
0 1
1 1
0 16
0 0 0 16 0
B 1
0 0 0 0 0 14 C
B 4 C
B 1 1 C
B 0 0 4 0 0 0 C
B 1
4 C
B 0 0 0 14 0 0 C
B 4 C
B 1
0 0 0 0 0 14 C
B 4 C
B 0 1 C
@ p p 0 p 0 p 16 p 0 p 0 p 0 p A
2C 2 1 2 2 2 2 1 2C 2
16 16 16
0 16 16 16
5
We consider a particular case.
sol=Solve[system==f0,0,0,0,0,1,0g]
p p
p p
ffaŒ2 ! 2 C 2 p aŒ1 2 2 aŒ3;
p aŒ4 ! 16;
p p
aŒ5 ! aŒ3; aŒ6 ! 2 C 2 aŒ1 2 2 aŒ3; aŒ7 ! aŒ1gg
If aŒ1 D aŒ3 D 0, then
sol/.fa[1]!0,a[3]!0g
ffaŒ2 ! 0; aŒ4 ! 16; aŒ5 ! 0; aŒ6 ! 0; aŒ7 ! 0gg
i.e.,
P .1/k
16 1 kD0 8kC4
The last series is the Gregory and Leibniz series.
148 8 Pi Formulas
8.1.7.7 Case 7
8.1.7.8 Case 8
FullSimplify[%]
r
1
p p p
100
10 1 C 5 a C 2 10 5 C 5 b C 10 1 C 5 cC
p p
2 50 10 5d C 5e
8.1.7.9 Case 9
8.1.7.10 Case 10
Because the amount of computation is huge for the next series, we consider a
simplified version of it, namely,
Clear[a,b,c,d,e,f]
P1 k a b c d e f e
kD0 .1/ 12kC1
C 12kC2
C 12kC3
C 12kC4
C 12kC5
C 12kC6
C 12kC7
C
d c b a
12kC8
C 12kC9
C 12kC10
C 12kC11 ;
FullSimplify[%]
1
p p p p p p
72
.6. 2 C 6/a C 12b C 6 2c C 4 3d C 6 2.1 C 3/e C 3f/
We have a simple series
p
FullSimplify[%/.fa!1,e!1,b!-1,c!-1,d! 3=2,f!4g]
p
6
By definite integral
as=Array[a,11];
as.Array[f,11];
asArray=FullSimplify[TrigToExp[%]];
LogExpand[exp ]:=PowerExpand[exp]/.Log[n Integer]:!
Apply[#2.Log[#1]&,Transpose[FactorInteger[n]]]
GetTranscendentals[exp ]:=
Union[Cases[exp,Pi j ArcCosh j Log j ArcTanh,Infinity]]
simpler=Together[LogExpand[asArray]/.fArcCosh[19601]!
p p
ArcCosh[17]-Log[577-408
p p 2],ArcCosh[577]!-Log[577-408 2],p
Log[17-12 2]!Log[3-2 2]+Log[2], h pArcCosh[49]!-Log[49-20
i 6],
p 2 6
ArcCosh[3]!-Log[3-2 2],ArcTanh 5 !ArcCosh[5]g];
transsimpler=GetTranscendentals[simpler];
collected=Map[Factor,Collect[simpler,transsimpler]];
system=DeleteCases[Flatten[CoefficientList[collected,
Append[transsimpler,Sqrt[2]]]],0]
TableForm[system,11]
(coeffMatrix=Normal[CoefficientArrays[system,as]][[2]])//MatrixForm
MatrixRank[coeffMatrix]
system/.fa[1]!1,a[2]!0,a[3]!1,a[4]!0,a[5]!-1,a[6]!0,
a[7]!-1,a[8]!0,a[9]!1,a[10]!0,a[11]!1g
8.1 Various Simple and Not So Simple Formulas 151
Solve
p p
system DD 0; 16 3 ; 0; 72 ; 0; 0; 36 ; 0; 8 3 ; 0; 2C
1p 1 1 1
p
6
3 1
;6 1
p
4 3
8.1.7.11 Case 11
8.1.7.12 Case 12
FullSimplify[%]
1
g C 2 a Csc 14 C b Csc 7 C c Csc 3 C f Sec 14 C e Sec 7
28 3 14
Cd Sec 14
We simplify the last sum
FullSimplify[%/.fb!0,d!0,f!0g]
1
28
g C 2 a Csc 14 C c Csc 3
14
C e Sec 7
Because of
1
FullSimplify 28 4 C 2 3Csc 14 3Csc 3
14
C 3Sec 7
1
we get the first series.
Remark In [71, (35)] the following series is exhibited
P1 k
3 3 3 4 4 4 4
kD0 .1/ 14kC1
C 14kC3 C 14kC5 C 14kC7 C 14kC9
C 14kC11
C 14kC13
;
and it is stated that it converges toward . This is false since
N[%]
3:74057
8.1.7.13 Case 13
Clear[a,b,c,d,e,f,g,h]
P1 a
k b c d e f g
kD0 .1/ 16kC1
C 16kC2 C 16kC3 C 16kC4 C 16kC5 C 16kC6 C 16kC7 C
h g f e d c b a
16kC8
C 16kC9
C 16kC10
C 16kC11
C 16kC12
C 16kC13
C 16kC14
C 16kC15
;
FullSimplify[%]
p
1
32
h C 2 2d C a Csc 16 C b Csc 8 C c Csc 3
16
C g Sec 16 C
3 p
e Sec 16 C 2 2f Sin 8
Suppose that g D 1 and all other coefficients are null. Then we have the following
series
P 1
FullSimplify 1 kD0.1/
k 1
C 16kC9
1
16kC7
8
Csc 8 Sin 16
8.1.7.14 Case 14
8.1.7.15 Case 15
P1 3
kD0 .1/
k 3
C 22kC3 3
C 22kC5 C 3 C 3
C 8
C 3
C
3 3
22kC1
3 3
22kC7 22kC9 22kC11 22kC13
22kC15
C 22kC19 C 22kC19 C 22kC21 I
FullSimplify[%]
Remark In [71, (37)] is exhibited the following series
P1 3 3
kD0 .1/
k
C 22kC3 3
C 22kC5 C 3 C 3
C 8
C 3
C
3 3
22kC1
3 1
22kC7 22kC9 22kC11 22kC13
22kC15
C 22kC19 C 22kC19 C 22kC21 I
and it is stated that it converges toward . This is false since
N[%]
3:075
8.1.7.16 A Case
P .1/kC1
3+4 1kD1 2k.2kC1/.2kC2/
8.1.7.17 A Case
P
6 1 1
kD1 k2
2
This is exactly the function of Riemann evaluated at 2, i.e., .2/.
8.5 by the Golden Ratio 155
8.1.7.18 A Case
P
8 1kD1
1
.2k1/2
2
8.4 by Arcsin
FullSimplify[f[Sin[Pi/10]]]
Pi
%== 5pGoldenRatioC2 (* This is [71, (28)] *)
p
3q 3
1 2p
5
5C 5
True
p
GoldenRatio== 1C2 5
FullSimplify[TrigToExp[FunctionExpand[
h i
1 .kŠ/2
2
Sum 2kC1 ; fk; 0; Infinityg ]]]
h GoldenRatio i .2kC1/Š
Pi
N 5pGoldenRatioC2 ==N[%]
True
1
5
Root 1 5#12 C 5#14 &; 3
True
r
p
FullSimplify 15 10
1
5 5 p 1
5 GoldenRatioC2
0
=GoldenRatio;
p P1 .kŠ/2
==FullSimplify 5
C2 kD0
2
2kC1 .2kC1/Š
True
hP i
1 .kŠ/2
FullSimplify kD0
2kC1 .2kC1/Š
h P i
==FullSimplify
1 1 1
kD0
.2kC1/BinomialŒ2k;k
2k
h P R1 k i
==FullSimplify
1
1 2kC1 k
kD0
2k .2kC1/ 0 x .1 x/ dx
R 1 P1 x 1x k
==FullSimplify
1 0 kD0
dx
h R i
1 1
==FullSimplify
0
2x.1x/ dx
q q q
1
5
2 p2 DD 15 2 p2 DD 15 2 p2
5 5 5
q
1 2 2GoldenRatio
DD 5 2 p DD p p
5 5 5C2 5
We also have
8.6 by Integrals 157
h p R1 i
FullSimplify 5
2
C2 0
2 x.1x/
1
dx
The convergence of the first series is suggested by the following code.
=GoldenRatio; p Pn .kŠ/2
pibygoldenratio[n ]:= 5
C2
2 kD0
2kC1 .2kC1/Š
Table[N[pibygoldenratio[k],10],fk,6g] (* The first six terms *)
DiscretePlot[pibygoldenratio[n],fn,50g,
Ticks!fAutomatic,fgg,
PlotStyle!Black,ImageSize!250]
FullSimplify[pibygoldenratio[1]]
f3:126021252; 3:140314037; 3:141483900; 3:141583199; 3:141591819;
3:141592579g
See Fig. 8.10.
8.6 by Integrals
22
R1 x4 .1x/4
7
0 dx (* [71, (50)] *)
1Cx2
Hence it immediately follows that < 22=7, [41].
158 8 Pi Formulas
355 1
R 1 x8 .1x/8 .25C816x2 /
3164
113 0 1Cx2
dx; (* [71, (51)] *)
Simplify[%]
Hence it immediately follows that < 355=113, [41].
This section is mainly based on [2, 3, 6], and [69]. A BBP formula allows to
compute the n-th decimal of without knowing its previous decimals. The acronym
BBP comes from the names of three mathematicians called (David) Bailey, (Peter)
Borwein, and (Simon) Plouffe.
An interesting result of this kind is proven in [2].
We underline that the next result, called an Adamchik-Wagon formula (AW
formula), is true for any complex number r.
FullSimplifyŒPowerExpand
P1 1 4C8r Œ
8r 4r 2C8r 1C2r 1C2r r
kD0 16k 8kC1 8kC2 8kC3 8kC4
8kC5
8kC6
C 8kC7
8.8 A Method for Finding AW Formulas and Proofs 159
We can approach the previous identity by means of the next two commands
f[i ]:=2i=2 IntegrateŒSumŒz^ .8k C i 1/; fk; 0; Infinityg; fz; 0; 1=SqrtŒ2g
FullSimplify[PowerExpand[
(4+8r)f[1]-8r f[2]-4r f[3]-(2+8r)f[4]-(1+2r)f[5]-(1+2r)f[6]+r f[7]]]
The last command can be written under the form
FullSimplify[PowerExpand[
f4+8r,-8r,-4r,-(2+8r),-(1+2r),-(1+2r),rg.Array[f,7]]]
Setting r D 0 yields the original BBP formula, [6]
FullSimplify[PowerExpand[4f[1]-2f[4]-f[5]-f[6]]]
Setting r D 1=2 yields the BBP formula [71, (39)]
FullSimplify[PowerExpand[f0,4,2,2,0,0,-1/2g.Array[f,7]]]
collected=Map[Factor,Collect[simpler,transsimpler]];
system=DeleteCases[Flatten[CoefficientList[collected,
Append[transsimpler,Sqrt[2]]]],0];
160 8 Pi Formulas
TableForm[system,7];
(coeffMatrix=Normal[CoefficientArrays[system,as]][[2]])//MatrixForm
MatrixRank[coeffMatrix]
0 1 1
4
0 12 0 1 0 2
B 1 0 1 1 1 0 1 C
B 8 C
B 0 1 04 1 02 1 0 C
B C
B 1 4 1 C
B 4 0 2 0 1 0 2 C
B 1 C
B 4 12 21 0 1 2 2 C
B C
@ 0 1 0 0 0 1 0 A
4
1
8
0 14 0 12 0 1
6
sol=Solve[system==f0,0,0,0,0,1,0g]
nn
aŒ2 ! 4 aŒ1; aŒ3 ! 2 aŒ1 2
; aŒ4 ! 1 aŒ1
2
; aŒ5 ! aŒ1
4
;
oo
aŒ6 ! aŒ1
4
; aŒ7 ! 12 C aŒ1
8
1 P1 1
256 256 384
TrigToExp FunctionExpand 64 kD0 4096k 24kC1 C 24kC2 24kC3
256 64 96 64 16 8 4 6
24kC5 C 24kC8 C 24kC9 C 24kC10 C 24kC12 24kC13 C 24kC15 C
24kC4
6 1 1 1 1
24kC16
C 24kC17
C 24kC18
24kC20
24kC21
g]
f; ; ; ; ; g
The last series converges rapidly. This fact is suggested by the following results.
1 Pn 1
256 256 384 256 64
bbptypeŒn_WD 64 kD0 4096k 24kC1 C 24kC2 24kC3 24kC4 24kC5 C
96 64 16 8 4 6 6 1
C 24kC9 C 24kC10 C 24kC12 24kC13 C 24kC15 C 24kC16 C 24kC17 C
24kC8
1 1 1
24kC18
24kC20
24kC21
8.9.1 A Case
Another series of the same kind is formula (48) in [71] and we introduce it bellow.
1 P1 1
256 64 128 352 64 288
sum24 D 96 kD0 4096k 24kC2 C 24kC3 C 24kC5 C 24kC6 C 24kC7 C 24kC8 C
128 80 20 16 1 6 2 1
C 24kC10 C 24kC12 24kC14 24kC15 C 24kC16 24kC17 24kC19 C
24kC9
1 2
24kC20
24kC21
;
FullSimplify[FunctionExpand[%]];
162 8 Pi Formulas
Because of
FullSimplify[20ArcCot[4-Sqrt[3]]+20ArcCot[4+Sqrt[3]]]
5 7
FullSimplify[-16 Sqrt[3]ArcCot[ SqrtŒ3 ]-4 Sqrt[3]ArcCot[ SqrtŒ3 ]
-4 Sqrt[3]ArcCot[3
Sqrt[3]]+12 Sqrt[3]ArcCot[11 Sqrt[3]]];
20ArcTan 23
p h p i
2 3 C ArcTan 153373
one has
13651680 h p i
1
18
21 4SqrtŒ3 ArcTan 815616479 4SqrtŒ3ArcTan 153373 C
p h
1
p i
16.1/1=12 2 .1/5=6 ArcTan 11 43 3 C
h p i h p i
1 1
ArcTan 11 4 C 3 3 C i C .1/1=3 ArcTan 11 4C3 3 ;
sum24a=ComplexExpand[%] h p i
7 2 1
13651680 2ArcTan 15337 3
6
p 18 ArcTan 815616479 p C
3 3 3 3 p
h p i 1
4 1 4ArcTan Œ 11p .43 3 /
9
ArcTan 11 4 3 3
3 3p
h p i 1
4 1 4ArcTan Œ 11 . 43 3 /
9
ArcTan 11 43 3 p C
3 3
h p i 1
p
4 1 4ArcTan Œ 11p . 4C3 3/
9
ArcTan 11 4 C 3 3 C
3 3p
h p i 1
4 1 4ArcTan Œ 11 . 4C3 3 /
9
ArcTan 11 4C3 3 C p C
3 3
h p 2i
2 1
p 2 1
2Log 1C 121 .43 3/
i 9 Log 1 C 121 4 3 3 C p C
3 3
h p 2i
2 1
p 2 1
2Log 1C 121 .43 3/
9
Log 1 C 121 4 3 3 C p
3 3
h p i
2 1
p 2 1
2Log 1C 121 .4C3 3/
2
9
Log 1 C 121 4 C 3 3 p C
3 3
h p i !
p 2 1
2Log 1C 121 .4C3 3/
2
2 1
9
Log 1 C 121
4 C 3 3 p
3 3
We now show that the above number is real and equals : Firstly we show that
its imaginary part is zero.
FullSimplify[Im[sum24a]]
0
8.10 Formulas by Binomial Sums 163
We introduce three formulas by binomial sums, called in some references BBP like
binomial sums. For this kind of sums, a great help is offered by the identity
Z 1
1
mn D .m n C 1/ xpn .1 x/.mp/n dx; (8.1)
pn 0
[53, p. 277].
Below we introduce three series of this sort. The first series belongs to W. Gosper,
whereas the second to S. Plouffe. The third one belongs to Gourevitch and Guillera,
[28]. The first two series follow.
P1 50k 6
D kD0
BinomialŒ3k; k2k
P k2k .kŠ/2 P k2k
3C D 1kD1 D 1kD1
.2k/Š BinomialŒ2k; k
We transform the right-hand sides substituting the binomial coefficients with the
definite integrals given above by (8.1).
h hnR P
1 1 .50k6/.3kC1/ k
FullSimplify FunctionExpand 0 kD0 2k
x .1 x/2k dx;
R 1 P1 oii
k k k
0 kD1 k2 .2k C 1/x .1 x/ dx
f; 3 C g
164 8 Pi Formulas
We are referring to an identity of the form given below, [9, 10], and [7].
p
8 P1 .4k/Š 1103 C 26390k
9801 kD0 .kŠ/4 3964k
1
Regarding the convergence of this series we note the following.
p P
8 n .4k/Š 1103C26390k
ramanujan1[n ]:= 9801 kD0 .kŠ/4 3964k
fTable[N[1/ramanujan1[n],30],fn,4g], (* The first four terms *)
Table[N[ -1/ramanujan1[n],30],fn,4g]g (* It clearly appears that at
each step twelve exact decimals are added *)
DiscretePlot[1/ramanujan1[n],fn,50g,Ticks!fAutomatic,f3.1,gg,
PlotStyle!Black,ImageSize!250,AxesOrigin!f0,3g]
ramanujan1[1]
ff3:14159265358979387799890582631;
3:1415926535897932384626490657;
3:14159265358979323846264338328;
3:14159265358979323846264338328g;
f6.3953626244302651021001947563J30.*^ -16;
5.68242325601395950808108722896J30.*^ -24;
5.23889628048110452740902417593J30.*^ -32;
4.94418757924803001220998690737J30.*^ -40gg
See Fig. 8.12.
Another example on the same line is the following, [30, (1)],
1
1 1 X 6k C 1 .1=2 C k/. .1=2//3
D :
4 kD0 4k kŠ3
h h P ii
.GammaŒ1=2Ck=GammaŒ1=2/3
FullSimplify FunctionExpand 14 1
kD0
6kC1
4k
kŠ3
p 2 2 2
CEllipticKŒ 14 .2 3/ GammaŒ 16 GammaŒ 13
2
p
16 3 3
8.11 S. Ramanujan Series 165
1 P1 p
1
p
D 32 kD0 42 5 C 30 k C 5 5 1
3
p 8k
.GammaŒ1=2Ck=GammaŒ1=2/ 51
kŠ3
216k 2
FullSimplifyŒFunctionExpandŒ
p
1 P1
p
32 kD0 42 5 C 30 k C 5 5 1
p ii
.GammaŒ1=2Ck=GammaŒ1=2/3 1 51 ^
3 26k
.8k/
kŠ 2
Chop@N % 1
h p p i
1p 1
2EllipticK 16 7 3 15
.267119
r 5/
2 32
p p h p p i
1
2 6 27 C 7 5 125 C 56 5 EllipticE 32 16 7 3 15 C
r
p p
328 267 15 C 5 234319 35224 15
h p p i
1
EllipticK 32 16 7 3 15
0
A short series of Ramanujan follows, [59],
P1 3
kD0 .1/
k
.4k C 1/ .GammaŒ1=2Ck=GammaŒ1=2/ I
kŠ3
Chop@N % 2
0
FullSimplifyŒFunctionExpandŒ%
h i 1Ci
2
2 ArcCot .1Cz/ 2 C .1 C i/ArcCot 1Cz C
1Ci 1C 12 .1Cz/2 1C 14 .1Cz/4
.1 C i/ArcCoth 1Cz Log p 1 4
Log p 1 8
1 4 .1Cz/ 1 16 .1Cz/
Define
8.12 R. W. Gosper Series 167
h 2
i
f1Œz WDPi C 4ArcTanŒz C 2Log 12zz 2
z C1
I
h i 1Ci
2
f2Œz WD2 ArcCot .1Cz/ 2 C .1 C i/ArcCot 1Cz C
1Ci 1
1C 2 .1Cz/2
1C 14 .1Cz/4
.1 C i/ArcCoth 1Cz Log p 1 4
Log p 1 8
I
1 4 .1Cz/ 1 16 .1Cz/
We show that the derivatives of the two functions coincide and the functions
coincide at a value of the argument.
{FullSimplify[f1’[z]],FullSimplify[f2’[z]]}
{f1[0],FullSimplify[ComplexExpand[f2[0]]]}
n o
16z 16z
;
1C2zC2z3 Cz4 1C2zC2z3 Cz4
f; g
We find the domain of convergence of the series.
Clear[k]
SumConvergence
h i
1 4.zC1/8kC1 2.zC1/8kC4 .zC1/8kC5 .zC1/8kC6
16k 8kC1
8kC4
8kC5
8kC6
;k
AbsŒ1 C z8 < 16
Let us note that the point z D 0 belongs to the domain of convergence.
RegionPlot[Abs[1+x+I y]2 <2,fx,-2.6,.6g,fy,-1.6,1.6g,
FrameTicks!fff-1.5,-.5,0,.5,1.5g,Noneg,ff-2.5,-1.5,-.5,0,.5g,Nonegg,
ImageSize!200]
See Fig. 8.13.
168 8 Pi Formulas
P .1/k .6k/Š.aCk b/
chudnov[n ]:=12 nkD0 kŠ3 .3k/Šc3kC3=2
I
Block[fMaxExtraPrecision=1000g,N[chudnov[#]- 1 ,30]]&/@f0,1,2,3g
Chop@N[FullSimplify[FunctionExpand[chudnov[1]]]- 1 ]
f1.61283781816166378669791721927J30.*^ -25;
1.42103169042706048703554408064J30.*^ -50;
1.33768392030277996437842472463J30.*^ -75;
1.30889434942233465826255332062J30.*^ -100g
0
An interesting sum was found by Cloitre, sum in which the golden ratio is present.
The result appeared in [18] and follows below.
g:=GoldenRatio;
2
P g1 g2 g5 2g5
50 1 1 g
kD0 g5k .5kC1/2 .5kC2/2
.5kC3/2
C .5kC4/2
C .5kC5/2
2
8.15 F. Bellard Series 169
Some details regarding the convergence of the series are given below.
P 2
g1 g2 g5 2g5
cloitre[n ]:=50 nkD0 g15k .5kC1/
g
2 .5kC2/2 .5kC3/2 C .5kC4/2 C .5kC5/2
Fabrice Bellard in [8] has introduced the following rapidly convergent series.
FullSimplify[FunctionExpand[
1 P1 .1/k
32 1 256 64 4 4 1
64 kD0 210k 4kC1
4kC3 C 10kC1 10kC3 10kC5 10kC7 C 10kC9
]]
Let us see the behaviour of the first partial sums of this series.
170 8 Pi Formulas
bellard[n ]:=
1 Pn .1/k 32 1 256 64 4 4 1
64 kD0 210k 4kC1
4kC3
C 10kC1
10kC3
10kC5
10kC7
C 10kC9
For brevity, below we introduce an abbreviated version of Theorem 4.2.i in [15, 48].
Let the Mayer problem of optimal control be expressed as
Let us define
X n
@fi @fi @H
fit D ; fixj D ; Hxj D D i fixj ;
@t @xj @xj iD1
X n
@H @H
Ht D D i fit ; H j D D fj :
@t iD1
@ j
h D .
1 ; 1 ;
2 ; 2 / ; 1 D 11 ; : : : ; 1n ; 2 D 21 ; : : : ; 2n ;
or by
h D . da; dx1 ; db; dx2 / ; dx1 D d11 ; : : : ; d1n ; dx2 D d21 ; : : : ; d2n :
Theorem 9.1 Assume the above eight hypotheses and let .x ; u / be an optimal
pair for the Mayer problem (9.1) and (9.2). Then the optimal pair .x ; u /
necessarily has the following properties:
(a) There exists an absolutely continuous function .t/ D . 1 .t/; : : : ; n .t// such
that
d i
D Hxi t; x .t/; u .t/; .t/ ; i D 1; : : : ; n; t 2 Œa; b .a:e:/:
dt
If dg is not identically zero at eŒx , then .t/ is never zero in Œa; b.
(b) For almost any fixed t 2 Œa; b (a.e.), the Hamiltonian, as a function depending
only on u, takes its minimum value in U at the optimal strategy u D u .t/. This
implies M .t; x .t/; .t// D H .t; x .t/; u .t/; .t//, t 2 Œa; b (a.e).
(c) The function M.t/ D M .t; x .t/; .t// coincides a.e. in Œa; b with an absolutely
continuous function, and
9.2 Zermelo’s Navigation Problem 173
dM d
D M t; x .t/; .t/ D Ht t; x .t/; u .t/; .t/ ; t 2 Œa; b .a:e:/:
dt dt
for all D .1 ; : : : ; m / 2 Rm , all a < t < b; where the derivatives Huj uk are
computed at .t; x .t/; u .t/; .t//.
9.2.1 Introduction
According to [21, p. 150], Zermelo was the first to formulate and solve in [78]
and [79] a problem that now is called the navigation problem of Zermelo. The
problem came to Zermelo’s mind when the airship Graf Zeppelin circumnavigated
the Earth in August 1929. He considered a vector field given in the Euclidean plane
that describes the distribution of winds as depending on place and time and treated
the question of how an airship or ship, moving at a constant speed against the
174 9 Optimization of Trajectories
surrounding air, has to fly in order to reach a given point B from a given point A
in the shortest possible time. With:
• x D x.t/ and y D y.t/ the Cartesian coordinates of the airship at time t,
• u D u.t; x; y/ and v D v.t; x; y/ the corresponding components of the vector field
representing the velocity of the wind (water) with respect to the Cartesian system,
• ˇ D ˇ.t; x; y/ the angle between the momentary speed .u0 ; v0 / of the airship
against the surrounding air and the x-axis and normalizing to k .u0 ; v0 / k D 1;
one has the system of differential equations that describes the problem
dx dy
D u C cos ˇ and D v C sin ˇ:
dt dt
Using the calculus of variations, Zermelo obtained the following differential
equation for the heading angle ˇ:
dˇ @v @u @v @u
D sin2 ˇ C sin ˇ cos ˇ cos2 ˇ :
dt @x @x @y @y
where .x; y/ are Cartesian coordinates giving the position of the boat at time t, and
.u; v/ are the velocity components of the boat at the current point .x; y/ at time t
in the x and y directions, respectively. The magnitude of the velocity of the boat
relative to the water is supposed to be a constant V > 0.
9.2 Zermelo’s Navigation Problem 175
The problem requires to steer the boat in such a way as to minimize the time
necessary to travel from a given point A D .x1 ; y1 / at instant a to another given
point B D .x2 ; y2 / at instant b. The equations of motion are
(
x0 .t/ D V cos ˇ.t/ C u.t; x.t/; y.t//
y0 .t/ D V sin ˇ.t/ C v.t; x.t/; y.t//; t 2 Œa; b;
where ˇ is the heading angle of the boat’s axis relative to a fixed coordinate axis.
Let the fixed coordinate axis be the horizontal axis and ˇ the control function.
In a more compact form, the navigation problem can be stated as follows:
8
ˆ 0
ˆx .t/ D V cos ˇ.t/ C u.t; x.t/; y.t//;
ˆ
ˆ
ˆ
ˆ
ˆy0 .t/ D V sin ˇ.t/ C v.t; x.t/; y.t//;
ˆ
ˆ
ˆ
ˆ
ˆ
ˆx.a/ D x1 ; y.a/ D y1 ; (initial conditions);
ˆ
ˆ
ˆ
ˆ
<x.b/ D x2 ; y.b/ D y2 ; (final conditions);
Œa; b; (finite horizon); (9.5)
ˆ
ˆ
ˆ
ˆ ˇ 2 C.Œa; b; R/; (control function);
ˆ
ˆ
ˆ
ˆ
ˆ
ˆu; v W Œa; b R R ! R; (components of the velocity of water);
ˆ
ˆ
ˆ
ˆV; (relative speed of the water);
ˆ
ˆ
:̂
g.b/ D b ! min; (cost functional):
We suppose that the functions u and v are continuous in the first variable and of
class C1 in the second and third variables.
By the maximum principle of Pontryagin under the form in (a) and (b) of
Theorem 9.1, we have that
0 @H @u @v 0 @H @u @v
1 D D 1 2 ; 2 D D 1 2 ; (9.6)
@x @x @x @y @y @y
@H
0D D V . 1 sin ˇ C 2 cos ˇ/ H) 1 sin ˇ D 2 cos ˇ; (9.7)
@ˇ
If we solve the system of differential Eqs. (9.5) and (9.6), we find x; y; ˇ; 1 ; and
2 . By (9.7) and the initial and final conditions, we find the solutions that solve
the navigation problem. Based on [44] or [45], we have that there exists such a
solution.
176 9 Optimization of Trajectories
Example If the functions u and v do not depend explicitly upon t, that is,
(
x0 .t/ D V cos ˇ.t/ C u.x.t/; y.t//;
(9.9)
y0 .t/ D V sin ˇ.t/ C v.x.t/; y.t//; t 2 Œa; b;
then the problem is autonomous, and therefore we take a D 0 as the initial instant
and .0; 0/ as the final point.
From now on, we suppose that the functions u and v do not depend on t, i.e.,
Eqs. (9.9) hold. Then the Hamiltonian does not explicitly depend on t, and then
H D constant is a prime integral. Because we minimize time, this constant has to
be 0. Then from (9.7) we have that H D 0. We invoke (9.6) and get
cos ˇ sin ˇ
1 D and 2 D : (9.10)
V C u cos ˇ C v sin ˇ V C u cos ˇ C v sin ˇ
Substituting
ı (9.10) in (9.6) (or asking for consistency between (9.8) and
d @ˇ H dt D 0), what follows is the Zermelo’s navigation formula:
dˇ @v @u @v @u
D sin2 ˇ C sin ˇ cos ˇ cos2 ˇ : (9.11)
dt @x @x @y @y
Now the nonlinear Eqs. (9.9) and (9.11) give the general solution for our
navigation problem. If we take into account the initial and final conditions, we get
the concrete solution if the data are consistent.
We study a special case considering for the current of water the following
functions:
where h is a nonzero real number. Now we express the data of the problem as
functions depending on the angle ˇ. From (9.11) to (9.12), we write
dˇ V V
D cos2 ˇ H) tan ˇ D tan ˇf C t tf ; (9.13)
dt h h
where tf is the final time and ˇf is the final angle, both still unknown.
From the second equation of the system we have
dy
D V sin ˇ H) y D y.ˇ/ D h sec ˇ sec ˇf :
dt
9.2 Zermelo’s Navigation Problem 177
Now we take into account the first equation of the system, that is,
dx V
D V cos ˇ y H) x D x.ˇ/ D
dt h
h sec ˇf C tan ˇf
ln C tan ˇf tan ˇ sec ˇf sec ˇf sec ˇ tan ˇf :
2 sec ˇ C tan ˇ
The angular limits of the navigation problem, the initial angle ˇ0 , and the final
one ˇf can be obtained requiring the following system of nonlinear equation to be
valid:
Now all the elements of the trajectory are determined, and we pass to the
numerical approach.
Clearly the initial time is a D 0. We choose the initial position at .x.0/; y.0// D
.7:32; 3:727/. The final position is at the origin .0; 0/. Then by (9.14), we have
that the initial angle is 105ı , whereas the final angle is 240:004ı . The minimum
time to steer the boat from the initial point to the origin by formula (31) is 5:46439:
The trajectory of this problem is given in blue, whereas the heading direction vectors
appear in red.
boatZermelo[vbig ,h ,x0 ,y0 ,m ]:=Module[
ftorig,thetaf,thetaff,theta0,degree0,degreef,t1,t2,t,timef,x,y,n,ig,
x[t ]:=
-(h/2)(Sec[thetaf](Tan[thetaf]-Tan[t])-Tan[t](Sec[thetaf]-Sec[t])+
Log[Divide[Tan[thetaf]+Sec[thetaf],Tan[t]+Sec[t]]]);
y[t ]:=h(Sec[t]-Sec[thetaf]);
sol=FindRoot[
f-(h/2)(Sec[thetaff](Tan[thetaff]-Tan[torig])-Tan[torig](Sec[thetaff]-
Sec[torig])+
Log[Divide[Tan[thetaff]+Sec[thetaff],Tan[torig]+Sec[torig]]])==x0,
h(Sec[torig]-Sec[thetaff])==y0g,fftorig,1.85g,fthetaff,4.17gg];
theta0=torig/.sol; (* Initial angle *)
thetaf=thetaff/.sol; (* Final angle *)
timef=(h/vbig)(Tan[thetaf]-Tan[theta0]); (* Final time *)
degree0=(theta0360)/N[2]; (* Initial angle *)
degreef=(thetaf360)/N[2]; (* Final angle *)
thetaa[i ]:=theta0+i(thetaf-theta0)/m; (* Arrow at each thetaa[i]
angle *)
178 9 Optimization of Trajectories
Show[ParametricPlot[fx[t],y[t]g,ft,theta0,thetafg,
PlotStyle!fBlue,Thickg],
Graphics[ffRed,Arrowheads[0.02],
Arrow[Table[ffx[thetaa[n]],y[thetaa[n]]g,
fx[thetaa[n]]+vbigCos[thetaa[n]],
y[thetaa[n]]+vbigSin[thetaa[n]]gg,fn,0,mg]]g,
fBlack,Arrowheads[0.02],Arrow[ffx[thetaa[0]],y[thetaa[0]]g,
fx[thetaa[0]]-(vbig/h)y[thetaa[0]],y[thetaa[0]]gg]g,
fBlue,Arrowheads[0.025],
Arrow[ffx[thetaa[0]],y[thetaa[0]]g,fx[thetaa[0]]-(vbig/h)y[thetaa[0]]+
vbigCos[thetaa[0]],y[thetaa[0]]+vbigSin[thetaa[0]]gg]g,
fPointSize[0.015],Blue,Point[fx[thetaa[0]],y[thetaa[0]]g],
Point[fx[thetaa[m]],y[thetaa[m]]g]g,
Text[Style[Row[fStyle[”minimum time of traveling”,Italic],” = ”,timefg],13],
fx0/2,y0/3g],
Text[Style[A,Italic,12],fx0-0.3,y0g],Text[Style[O,Italic,12],f-0.25,0.25g],
Text[Style[Row[fStyle[”from A”,Italic],” = (”, x0,”,”,y0,”) ”,
Style[”to O=(0,0)”,Italic]g],13],fx0/2,y0/2g],
Text[Style[Row[fStyle[”initial angle in A”,Italic],” = ”,
Superscript[degree0,” ı”]g],13],fx0/2,2y0/3g],
Text[Style[Row[fStyle[”final angle in O”,Italic],” = ”,
Superscript[degreef,” ı”]g],13],fx0/2,4y0/4.8g]g],
PlotRange!All,ImageSize!500]]
The graph below exhibits the dynamics of the navigation problem when the initial
data belong to some intervals. The black (horizontal) vector at A represents the
direction and magnitude of the current vector at the initial point. The blue (oblique)
vector at A represents the tangent to the trajectory.
Manipulate[
Quiet@boatZermelo[vbig,h,x0,y0,m],
ffvbig,2,Vg,.5,5,0.5,Appearance!”Labeled”g,
ffh,2,hg,0.5,5,0.5,Appearance!”Labeled”g,
ffx0,7,”x0 ”g,5,20,1,Appearance!”Labeled”g,
ffy0,-2,”y0 ”g,-7,3,1,Appearance!”Labeled”g,
ffm,15,”number of arrows”g,10,30,1,Appearance!”Labeled”g,
SaveDefinitions!True
]
See Fig. 9.1.
Remark A forthcoming interesting work on the Zermelo’s navigation problem is
[25].
9.3 Optimal Guidance for Planar Lunar Ascent 179
The present section is focused on the equations of the optimal guidance for
planar Lunar ascent from rest to insertion in a circular parking orbit. The goal
is to minimize the flight time under certain assumptions. We point out some
computational and graphical aspects for flights reaching two altitudes.
The problem of soft landing a spacecraft on the Moon is a known topic, and we
mention [55] and the references therein.
9.3.1 Introduction
The studies on the problem of the ascent from the lunar surface are not new; they
started before 1965 as it clearly follows from [14, 46] to [40]. At that time, the
methods of calculus of variations were still in force, and more and more strongly
the optimal control approach pointed out to be the way to get newer results.
In the lunar ascent problem, the spacecraft is launched from the surface of the
moon and ascends to the orbit insertion point, which is defined by a given final
velocity vector and a given altitude (sometimes also called free downrange). In spite
180 9 Optimization of Trajectories
of the gravity perturbations, it is expected that the ascent trajectory will stay on or
close to the plane containing the great circle defined by the launch point and the
insertion point.
We point out some computational and graphical aspects of this problem in
connection with certain recent results given by D. G. Hull in [35, 36] and [37].
A pertinent introduction on this topic together with a large list of references may
be found in [36] and [37].
For constant thrust, minimum time and minimum fuel consumption are the same
problem; see [13] or [15].
This section is organized as follows: Sect. 9.3.2 introduces the problem of the
ascent from the lunar surface giving some references to make clear the framework
of the paper and introduces equations, approximations, and initial values of the
ascent problem; Sect. 9.3.3 contains an optimal control approach of determining
the optimal trajectory under certain assumptions; and Sect. 9.3.4 discusses some
computational and graphical issues of earlier results connected to the problem of
determining the optimal trajectory. The Sect. 9.3.4.1 exhibits some figures on the
case of 15240 m altitude, whereas the Sect. 9.3.4.2 introduces some figures for the
case of 100 km altitude.
Following [36], we consider the ascent problem of a spacecraft from the surface
of Moon. The equations of motion in three-dimensional flight over a nonrotating,
spherical Moon are written in the local horizontal/local vertical frame as shown in
Fig. 9.2. For applications, we considered the data in [36].
The parameters involved in the flight are as follows:
1. Œ0; tf is the horizon of time. The final instant tf of the insertion on the coast or
parking trajectory is to be found in many cases.
2. x and y are the in-plane curvilinear distance (downrange) and altitude. The
altitude is specified. More specifically, in Sect. 9.3.4, we discuss in detail the
problem for two altitudes of 15240 m and 100 km.
3. u and v are the in-plane horizontal and vertical velocity components. We assume
that v.tf / D 0:
4. z and w are the out-of-plane curvilinear distance and velocity components.
5. is the thrust pitch angle.
6. is the thrust yaw angle.
7. rm D 1738:14 103 m is the radius of the Moon.
8. gm D m =rm2 D 1:619 m=s2 is the acceleration of gravity on the surface of the
Moon, m being the gravitational constant of the Moon.
9.
is the thrust to mass ratio T=m; where the thrust is T D 60030 N, and the mass
equation is m.t/ D m0 ˇt; m0 D 17512:6836 kg: For constant thrust, T D ˇVe ;
9.3 Optimal Guidance for Planar Lunar Ascent 181
rm
where ˇ D 19:118 kg=s is the constant propellant mass flow rate, and Ve is the
constant exhaust velocity.
From 9 it follows that
D
.t/ D T=m D T=.m0 ˇt/ D Ve =.t ˛/; where
˛ D m0 =ˇ:
If z
w
0; then the ascent is said to be planar.
The equations of motion for a spacecraft in three-dimensional flight over a non-
rotating spherical moon are written in the local horizontal/local vertical coordinate
system. Then x; y; u; and v are the in-plane downrange, altitude, horizontal velocity,
and vertical velocity component, suggested by Fig. 9.2.
According to [36] and [37], the equations of motion are given by
The ascent trajectory from the lunar surface to a coast orbit consists of a
(sometimes vertical) rise, constant rate pitchover, and optimal guidance phases. It
is designed that the trajectory lies in the plane of the great circle plane containing
the launch point and the orbit insertion point. Small deviations from the plane occur
because of perturbations on gravity and spacecraft performance. Because the out-
of-plane motion is small, the trajectory is said to be quasiplanar.
Because the insertion point is relatively close to the lunar surface, we consider
the following approximations:
y=rm 1; so that r rm and g m =rm2 D gm :
The out-of-plane components z and w are assumed to be sufficiently small that
the terms z=rm and w=rm can also be neglected.
182 9 Optimization of Trajectories
y
q
O
y
For typical launch trajectories, it can be shown that uv=rm T=m; so the last
term in the equation of u0 is neglected.
Under these circumstances, the Eq. (9.15) can be written under the simplified
form as
x0 D u; u0 D
cos cos ;
y0 D v; v0 D
sin gm ; (9.16)
z0 D w; w0 D
cos sin :
By flight over a flat moon, as in [36], we mean flight over a spherical moon at
low altitude and low velocities. While the low altitude condition .y=rm 1/ is met
so that r rm ; g gm ; the low velocity requirement .u2 =.gm rm / 1/ is not; see
[36]. Flight over a flat moon is suggested by Fig. 9.3.
We note that for flight over a flat moon, the gravity vector is along the radius
vector.
D Œx; y; z; ; D tf (9.17)
9.3 Optimal Guidance for Planar Lunar Ascent 183
By (a) in Theorem 9.1, we have the first necessary condition under the form of
the system of differential equations to the multipliers
01 D 0; 02 D 0; 03 D 0; 04 D 1 ; 05 D 2 ; and 06 D 3 :
1 D c1 ; 2 D c2 ; 3 D c3 ;
4 D c1 tCc4 ; 5 D c2 t C c5 ; 6 D c3 t C c6 ;
Under the necessary condition of ascent, we suppose that cos > 0, and
therefore from the latter equation in (9.20), it follows that
and for j j small also 4 tan 6 D 0: Thus, the first equation in (9.20) is written
as
From (9.21) to (9.22), we have the multipliers 5 and 6 depending on 4 ; that is,
6 D 4 tan and 5 D 4 tan sec :
The second-order partial derivatives of H with respect to the control variables are
H D
4 sec sec ; H D
4 cos sec ; and H D 0:
184 9 Optimization of Trajectories
Thus, necessarily 4 0:
From (9.21) for some real " D ˙1, we write
" 6 " 4
q cos q sin D 0:
24 C 26 24 C 26
H D 0 H)
" 4 " 6
4 sin q C 5 cos 6 sin q D 0 H)
24 C 26 24 C 26
2 C 26
"q4 sin C 5 cos D 0 H)
24 C 26
q
ı 24 C 26 ı 5
"q sin C q cos D 0:
24 C 25 C 26 24 C 25 C 26
Now we impose condition (9.23) and get that ı D 1: For " we choose 1 because
4 is negative, and for small values of ; cos is positive. From the last equation,
we can now deduce the equations of the optimal pitch angle
q
5 24 C 26
sin D q and cos D q : (9.24)
24 C 25 C 26 24 C 25 C 26
9.3 Optimal Guidance for Planar Lunar Ascent 185
With these results on the expressions of angles, by (9.16), the velocity and
acceleration equations of the optimal trajectory, if any, are of the form
4
x0 D u; u0 D
q ;
24 C 25 C 26
5
y0 D v; v0 D
q gm ; (9.25)
24 C 25 C 26
6
z0 D w; w0 D
q :
4 C 25 C 26
2
Lemma 9.1 The tangential velocity function u is of class C1 ; positive and increas-
ing on the interval Œ0; tf :.
Proof It is enough to note that u0 in (9.25) is continuous and positive. t
u
1
Lemma 9.2 The radial velocity function v is of class C and positive on the interval
Œ0; tf :
Following [36] and [37], we introduce the functions A; B; and C by
Z t
ds
A.t/ D A.t; c1 ; : : : ; c6 / D p ;
0 .s ˛/ ps2 C qs C r
Z t
s ds
B.t/ D B.t; c1 ; : : : ; c6 / D p ; (9.26)
0 .s ˛/ ps2 C qs C r
Z t
s2 ds
C.t/ D C.t; c1 ; : : : ; c6 / D p ;
0 .s ˛/ ps2 C qs C r
Lemma 9.3 Assume that t 0: The following two integral equalities hold
Z t Z t
A.s/ ds D tA.t/ B.t/ and B.s/ ds D tB.t/ C.t/: (9.27)
0 0
Proof The two integral equalities are obtained by integration by parts; see [53,
Chap. 6]. t
u
Theorem 9.2 From (9.25) with (9.26) and (9.27) by integration, we get the
equations of velocities and states.
186 9 Optimization of Trajectories
u.t/ D u.0/ Ve c1 B C Ve c4 A;
v.t/ D v.0/ Ve c2 B C Ve c5 A gm t;
w.t/ D w.0/ Ve c3 B C Ve c6 A;
(9.28)
x.t/ D x.0/ C tu.t/ C Ve c1 C Ve c4 B;
y.t/ D y.0/ C tv.t/ C Ve c2 C Ve c5 B C gm t2 =2;
z.t/ D z.0/ C tw.t/ C Ve c3 C Ve c6 B;
Remark By Eq. (9.28), we know the form of the optimal trajectories, if any. What
has remained is the step of determining the constants of integration cs such that the
seeking trajectory satisfies the boundary conditions.
In order to solve the minimum time optimal control problem, let us suppose that
all the initial and final values are given, that is, we know x.0/; y.0/; z.0/; u.0/; v.0/;
w.0/; y.tf /; z.tf /; u.tf /; v.tf /; and w.tf /: The variables to be found are tf and x.tf /:
Then we write
and further
Theorem 9.3 If the nonlinear system formed by the Eqs. (9.30) and (9.31) has a
unique system of real solutions, then the minimum time optimum control problem
has a unique solution. If the nonlinear system has several systems of real solutions,
then we have to check on which one the minimum time is achieved.
Remark Based on Filippov’s existence Theorem for Mayer problem of optimal
control as it is given in [15, p. 310], the problem discussed by Theorem (9.3) has a
solution.
This subsection uses the results of the previous one and analyzes their effectiveness.
Remark Denote X D ax2 CbxCc; where a D p; b D 2p˛Cq; and c D p˛ 2 Cq˛Cr:
We have that
ˇ ˇ
Z
dx 1 ˇ 2c C bx C 2pcX ˇ
ˇ ˇ
P.x/ D p D p ln ˇ ˇ ; [29, p. 97]
x X c ˇ x ˇ
ˇ ˇ
Z
dx 1 ˇ b C 2ax C 2paX ˇ
ˇ ˇ
Q.t/ D p D p ln ˇ p ˇ ; [29, p. 94]
X a ˇ b 4ac ˇ
2
Z p
x dx X b
R.x/ D p D Q.x/; [29, p. 96]:
X a 2a
3 D 6 D 0:
Remark Hereafter, we discuss the planar case using the initial and final values as
they are given in [35]. In this case, the multipliers are of the form
If we integrate the velocity equations in (9.32), then the equations of states can be
written also as
Then by the L’Hospital Theorem, [53, p. 207], the angle of the spacecraft at the
take-off moment is
p
Ve c4 C gm ˛ r
arctan : (9.33)
Ve c3
In this subsection, we present the numerical results and graphics for the ascent to
the 15;240 m altitude.
Remark Consider the data given in Sect. 9.3.2 and the guess (approximate) values
as they are listed in [35], i.e.,
With these initial values, we have obtained the following final moment and
coefficients of the Lagrange multipliers:
9.3 Optimal Guidance for Planar Lunar Ascent 189
According to our calculation, the downrange in this setting is x.tf / D 279; 348:
If we introduce the following constants to measure the degree of accuracy of our
calculations
ı1 D Vx Ve .c1 B.tf / c3 A.tf //; ı2 D Vy Ve .c2 B.tf / c4 A.tf //;
ı3 D Rx C Ve .c1 C.tf / c3 B.tf //; ı4 D Ry C Ve .c2 C.tf / c4 B.tf //;
ı5 D c1 u.tf / C c2 v.tf / .c2 tf C c4 /gm
q
.T=.m0 ˇtf // .c1 tf C c3 /2 C .c2 tf C c4 /2 ;
Applying (9.33), the angle of the spacecraft in respect to the local horizon at the
take-off moment is 8:27585ı :
In the last part of the present subsection, we introduce some graphics visualizing
the numerical results.
Figures 9.4 and 9.5 introduce the variations of the tangential and the radial
velocities.
O t
393.784
190 9 Optimization of Trajectories
30
O t
231.997 393.784
Coast trajectory
A B
Surface of the Moon
Figure 9.6 represents the whole trajectory of the spacecraft with a part of the
lunar surface. A is the initial point, whereas B is the final point.
The picture that we introduce in Fig. 9.7 represents the trajectory of the spacecraft
constrained on the time interval t 2 Œ0; 1: The parameter of the lunar arc belongs to
the interval Œ=2 0:0000008; =2 C 0:0000002:
Figures 9.4, 9.5, 9.6, and 9.7 are obtained by means of the code which follows:
(* Optimal guidance for a planar Lunar ascent *)
Clear[”c*”,”t*”,”u*”,”v*”,”x*”,”y*”,m0,beta]
t0=.0; (* initial time s *)
x0=.0;y0=.0; (* initial site in local horizontal system of
coordinates *)
u0=.0;v0=.0; (* initial velocities m/s *)
rm=1738.14103 ; (* radius of the Moon m *)
gm=1.619; (* gravitational acceleration on the moon m/s2 *)
thrust= 60030.0; (* thrust N *)
m0=17512.6836; (* initial mass kg *)
beta=19.118;
9.3 Optimal Guidance for Planar Lunar Ascent 191
vexit=thrust/beta;
(* xb=279280.0; downrange m *)
yb=15240.0; (* final altitude m *)
ub=1625.0; vb=0.0; (* final velocities m/s *)
(* tb=394.0; final time s *)
velx=ub-u0; vely=vb-v0+gmtb; (* residues of velocities *)
radx=x[tb]-x0-ubtb; rady=yb-y0-vbtb-0.5gmtb2 ;
(* residues of states *)
alpha=m0/beta;
p=a=c12 +c22 ;q=-2(c1c3+c2c4);r=c42 +c32 ;
b=2 palpha+q;c=palpha2 +qalpha+r;
pbig[x ,c1 ,c2 ,c3 ,c4 ]:=-(1/Sqrt[c])Log[Abs[
Divide[2 c+bx+2Sqrt[c(ax2 +bx+c)],x]]];
qbig[x ,c1 ,c2 ,c3 ,c4 ]:=(1/Sqrt[a])Log[Abs[Divide[
2Sqrt[a(a x2 +b x+c)]+2ax+b,Sqrt[b2 -4ac]]]];
rbig[x ,c1 ,c2 ,c3 ,c4 ]:= Divide[Sqrt[ax2 +bx+c],a]-
Divide[b,2a]qbig[x,c1,c2,c3,c4];
aa[c1 ,c2 ,c3 ,c4 ]:=pbig[tb-alpha,c1,c2,c3,c4]-
pbig[-alpha,c1,c2,c3,c4];
b[c1 ,c2 ,c3 ,c4 ]:=qbig[tb-alpha,c1,c2,c3,c4]-
qbig[-alpha,c1,c2,c3,c4]+alpha(pbig[tb-alpha,c1,c2,c3,c4]-
pbig[-alpha,c1,c2,c3,c4]);
cc[c1 ,c2 ,c3 ,c4 ]:=rbig[tb-alpha,c1,c2,c3,c4]-
rbig[-alpha,c1,c2,c3,c4]+2alpha(qbig[tb-alpha,c1,c2,c3,c4]-
qbig[-alpha,c1,c2,c3,c4])+alpha2 (pbig[tb-alpha,c1,c2,c3,c4]-
pbig[-alpha,c1,c2,c3,c4]);
c1=0.0; (* because of the downrange is free *)
root=FindRoot[fc1ub+c2vb+(c2tb-c4)gm-
(thrust/(m0-betatb))Sqrt[(c1tb-c3)2 +(c2tb-c4)2 ]==-1,
velx==-vexitc1bb[c1,c2,c3,c4]+vexitc3aa[c1,c2,c3,c4],
vely==-vexitc2bb[c1,c2,c3,c4]+vexitc4aa[c1,c2,c3,c4],
rady==vexitc2cc[c1,c2,c3,c4]-vexitc4bb[c1,c2,c3,c4]g,
fftb,404.0g,fc2,-5.87105 g,fc3,-.0399g,fc4,-.029gg,
AccuracyGoal!10,PrecisionGoal!10];
tb=root[[1,2]];c2=root[[2,2]];c3=root[[3,2]];c4=root[[4,2]];
(* Build up the trajectory *)
aaa[t ]:=pbig[t-alpha,c1,c2,c3,c4]-pbig[-alpha,c1,c2,c3,c4];
bbb[t ]:=qbig[t-alpha,c1,c2,c3,c4]-qbig[-alpha,c1,c2,c3,c4]+
alpha(pbig[t-alpha,c1,c2,c3,c4]-pbig[-alpha,c1,c2,c3,c4]);
ccc[t ]:=rbig[t-alpha,c1,c2,c3,c4]-rbig[-alpha,c1,c2,c3,c4]+
2alpha(qbig[t-alpha,c1,c2,c3,c4]-qbig[-alpha,c1,c2,c3,c4])+
alpha2 (pbig[t-alpha,c1,c2,c3,c4]- pbig[-alpha,c1,c2,c3,c4]);
u[t ]:=u0-vexit(c1bbb[t]-c3aaa[t]);
v[t ]:=v0-vexit(c2bbb[t]-c4aaa[t])-gmt;
192 9 Optimization of Trajectories
fPlot[tau[t],ft,t0,tbg],Plot[vprime[t],ft,t0,tbg]g
ArcTan[(ycart[1]-ycart[0])/(xcart[1]-xcart[0])]180/N[Pi];
theta[t0];Limit[theta[t],t!0]
Limit[Divide[ycart[t]-ycart[0],t],t!0];
Limit[Divide[xcart[t]-xcart[0],t],t!0];
anglestarting=Divide[vexitc4+gmalphaSqrt[r],vexitc3];
anglestartingdegree=ArcTan[Divide[vexitc4+gmalphaSqrt[r],
vexitc3]]180/N[Pi];
anglestartingarctan=ArcTan[anglestarting];
circle[radius , ]:=radiusfCos[],Sin[]g;
fShow[ParametricPlot[fxcart[t],ycart[t]g,ft,t0,1g],
ParametricPlot[circle[rm,a],fa,Pi/2-0.0000008,Pi/2+0.0000002g],
Graphics[fPointSize[0.015],fRed,Point[fxinit,yinitg]g,
Text[Style[” ”,Italic,12],fxinit,yinit+0000g],
Text[Style[A,Italic,12],fxinit-.07,yinit+.05g],
Text[Style[”Surface of the Moon”,Italic,12],fxinit+.2,yinit-.11g],
Text[Style[” ”,Italic,12],fxinit,yinit-0.2g],
Text[Style[anglestartingdegree”ı ”,Italic,12],fxinit+1.,yinit+.06g],
circle[fxinit,yinitg,0.5,f0,anglestartingarctang],
fArrowheads[.02],Arrow[ffxinit+0.5Cos[anglestartingarctan-0.0001],
yinit+0.5Sin[anglestartingarctan-0.0001]g,
fxinit+0.5Cos[anglestartingarctan],yinit+0.5Sin[anglestartingarctan]g
g]gg],
PlotRange!All,Axes!False,ImageSize!250],
radius=Sqrt[xfinal2 +yfinal2 ];
Show[ParametricPlot[fxcart[t],ycart[t]g,ft,t0,tbg],
ParametricPlot[circle[rm,a],fa,13.4Pi/30,24.2Pi/48g],
ParametricPlot[circle[radius,a],fa,13.3Pi/30,24.0Pi/48g],
Graphics[fPointSize[0.010],fRed,Point[fxinit,yinitg]g,
fGreen,Point[fxfinal,yfinalg]g,
Text[Style[” ”,Italic,12],fxinit,yinit+90000g],
Text[Style[A,Italic,12],fxinit-7000,yinit+7000g],
Text[Style[B,Italic,12],fxfinal,yfinal+11000g],
Text[Style[”Coast trajectory”,Italic,12],(radius+15000)fCos[Pi/2-0.1],
Sin[Pi/2-0.1]g],
Text[Style[”Surface of the Moon”,Italic,12],fxinit+50000,yinit-15000g],
Text[Style[” ”,Italic,12],fxinit,yinit-70000g],
Text[Style[” ”,Italic,12],fxfinal-10000,yfinal+45000g]g],
PlotRange!All,Axes!False,ImageSize!400]g
Show[ParametricPlot[fxcart[t],ycart[t]g,ft,t0,tbg],
ParametricPlot[circle[rm,a],fa,0,2Pig],
Graphics[fPointSize[0.010],fBlack,Point[f0,0g]g,
fRed,Point[fxinit,yinitg]g,fGreen,Point[fxfinal,yfinalg]g,
Text[Style[” ”,Italic,12],fxinit,yinit+130000g],
194 9 Optimization of Trajectories
Text[Style[A,Italic,18],fxinit,yinit+130000g],
Text[Style[B,Italic,18],fxfinal,yfinal+85000g],
Text[Style[”Surface of the Moon”,Italic,18],fxinit,yinit-210000g],
Text[Style[” ”,Italic,12],fxinit,yinit-70000g],
Text[Style[” ”,Italic,12],fxfinal-10000,yfinal+45000g]g],
PlotRange!All,Axes!False,ImageSize!400]
In this subsubsection, we present the numerical results and graphics for the ascent
to 100;000 m altitude, that is, y.tf / D 100;000: The results and figures of the present
case follow the previous code by changing the initial data.
Remark We use the same guess (approximate) values as they are given in the pre-
vious subsubsection. We have obtained the following final moment and coefficients
of the Lagrange multipliers
tf D 440:185; c1 D 0; c2 D 0:000715588;
c3 D 0:114418; and c4 D 0:244682:
According to our calculation, the downrange in this setting is x.tf / D 273; 612:
Similar to Table 9.1, we introduce the values of the degree of accuracy in the
present setting. The results are given by Table 9.2.
Lemma 9.6 The angle of the thrust vector at the take-off moment is given by
D 64:9383ı ;
45:6646ı :
O t
440.185
O t
287.121 440.185
B
A
Coast trajectory
B
45.6646 °
A
Surface of the Moon
Figure 9.11 represents the whole trajectory of the spacecraft with a part of the
lunar surface.
The picture that we introduce in Fig. 9.12 represents the trajectory of the
spacecraft constrained on the time interval t 2 Œ0; 1: The parameter of the lunar
arc belongs to the interval Œ=2 0:0000006; =2 C 0:00000035:
Remark The maximal orbit transfer problem is connected to the problem of lunar
ascent. An approach to the maximal orbit transfer problem with similar method is
realized in [57]. Other flying problems are discussed in [48].
Chapter 10
Miscellany in the Euclidean Plane
The conchoid of Nicomedes is the locus of points a fixed distance away from a line
as measured along a line from the focus point. The polar equation of the conchoid
of Nicomedes is r.t/ D b C a sec.t/, whereas in Cartesian coordinates, we write the
parametric equations for it t ! .b C a cos.t//.cos.t/; sin.t//. Clearly the function
is 2-periodic. In the graph bellow we set b D 1.
Clear[a, ]
GraphicsGrid[With[feps=105 g,
Function[a,Show[ParametricPlot[(1+a Sec@ )fCos@ ,Sin@ g,
Evaluate[f,#[[1]]+eps,#[[2]]-epsg],PlotStyle!Red]&/@
Partition[Range[0,2,1/2],2,1],PlotRange!ff-2,2g,f-2,2gg,
Ticks!None,PlotLabel!ToExpression[“a”]==a]]/@f0,.03,.3,.5,1,2g]g,
ImageSize!600]
See Fig. 10.1.
ParametricPlot[(1+a Sec@)fCos@ ,Sin@g/.a!0.003,
f ,0,2g,ColorFunction!“Rainbow”,ImageSize!100,
Ticks!ff-1,-.5,.5,1g,f-3,-2,2,3gg]
See Fig. 10.2.
Animate[
Show[conchoid,Graphics[ffPointSize[.07],Red,Point[point[ ]]gg],
ImageSize!75],f,0,2g,AnimationRate!.025,
AnimationRunning!False,SaveDefinitions!True]
See Fig. 10.3.
200 10 Miscellany in the Euclidean Plane
Now we show the evolution of the conchoid under the assumption that the
parameter a varies. We set a 2 Œ2; 2.
Clear[a]
conchoidofNicomedes[a ]:=Module[fg,
Show[ParametricPlot[(1+a Sec[])fCos[ ],Sin[]g,f,0,2 Pig,
PlotStyle!Red,PlotRange!ff-3,3g,f-3,3gg,ImageSize!200,
Ticks!None]]]
Manipulate[Quiet@conchoidofNicomedes[a],
ffa,-.46,”a”g,-2,2,.00001,Appearance!”Labelled”g,
SaveDefinitions!True]
See Fig. 10.4.
10.1.2 Cycloid
Clear[a,x]
x[t ]:=aft-Sin[t],1-Cos[t]g
ParametricPlot[x[t]/.a!3,ft,0,#g,ImageSize!f140,140g,
PlotStyle!Red,Ticks!fNoneg]&/@(f1/2,1,3/2,2g)
See Fig. 10.5.
The visualization of the geometric definition of the cycloid follows.
cycloid[tend ]:=
Module[fa=3,x,y,t,xx,yy,gridlinex,gridlinesyg,
x[t ]:=a(t-Sin[t]); y[t ]:=a(1-Cos[t]);
gridlinesx=Table[fxx,GrayLevel[.5]g,fxx,0,4a,a/4g];
gridlinesy=Table[fyy,GrayLevel[.5]g,fyy,0,2a,a/2g];
ParametricPlot[fx[t],y[t]g,ft,0,tendg,
PlotStyle!fRed,Thickg,
Epilog!fThick,Blue,Circle[aftend,1g,a],
Red,PointSize[Large],Point[fx[tend],y[tend]g],
Black,PointSize[Medium],Point[aftend,1g]g,
GridLines!fgridlinesx,gridlinesyg,
Ticks!faRange[4],af1,2gg,PlotRange!2aff0,2g,f0,1gg]]
Manipulate[Quiet@cycloid[tend],ftend,.0001,4,
Appearance!”Labeled”g,SaveDefinitions!True]
See Fig. 10.6.
202 10 Miscellany in the Euclidean Plane
10.1.3 Epicycloid
Manipulate[Quiet@epicycloid[tend],ftend,0.001,2,
Appearance!”Labeled”g,SaveDefinitions!True]
See Fig. 10.8.
10.1.3.1 Cardioid
10.1.3.2 Nephroid
The polar equation of the Freeth’s nephroid is r D a.1 C 2 sin.=2//, [19, 6.8,
pp.175–178] or according to http://www-groups.dcs.st-and.ac.uk/~history/Curves/
Freeths.html. Below we use its parametric equations.
10.1 Some Planar Curves 207
ParametricPlot[(1+2 Sin[t/2])fCos[t],Sin[t]g,#,Ticks!None,
ImageSize!150]&/@fft,-2,0g,ft,0,2g,ft,-2,2gg
See Fig. 10.12.
10.1.4 Hypocycloid
Manipulate[Quiet@hypocycloid[tend],ftend,.0001,2,
Appearance!”Labeled”g,SaveDefinitions!True]
See Fig. 10.15.
10.1 Some Planar Curves 209
Line[fff0,0g,afCos[tend],Sin[tend]gg,
ffx[tend,0],y[tend,0]g,fx[tend,0],0gg,
ffx[tend,0],y[tend,0]g,fx[tend],y[tend]gg,
ffx[tend],y[tend]g,fx[tend],0gg,ff0,0g,fa,0gg,
ffx[tend,0],y[tend]g,fx[tend],y[tend]ggg],
fText[Style[O,Italic,12],f0,-.4g],
Text[Style[P,Italic,12],fx[tend]-.33,y[tend]-.25g],
Text[Style[C,Italic,12],fx[tend,0],y[tend,0]+.3g],
Text[Style[T,Italic,12],fx[tend],-.4g],
Text[Style[B,Italic,12],fx[tend,0],-.4g],
Text[Style[S,Italic,12],fa+.35,.15g],
Text[Style[R,Italic,12],fx[tend,0]+.28,y[tend]+.2g],
Text[Style[Q,Italic,12],fa Cos[tend]+.35,a Sin[tend]+.1g]g,
fPointSize[Medium],Point[ff0,0g,afCos[tend],Sin[tend]g,
fx[tend,0],y[tend,0]g,fx[tend,0],0g,fx[tend],0g,
fx[tend,0],y[tend]g,fa,0gg],Red,Point[fx[tend],y[tend]g]gg],
PlotRange!All,ImageSize!300]]
See Fig. 10.16.
Data: OQ D OS D a; CQ D CP D b, angle(SOQ) = t, arc(SQ) = at; arc(QP),
OB D .a b/ cos t; CB = .a b/ sin t; Q = a.sin t; cos t/; angle(QCP) D at=b;
angle(OCB) = =2 t; angle(BCP) = at=b t =2:
10.1 Some Planar Curves 211
10.1.4.1 Deltoid
Maria Agnesi’s curve is obtained starting with a fixed circle, and a point O on the
circle is chosen. For any other point A on the circle, the secant line OA is drawn.
The point M is diametrically opposite to O. The line OA intersects the tangent of
M at the point N: The line parallel to OM through N and the line perpendicular
to OM through A intersect at P: As the point A varies, the path of P is the Maria
Agnesi’s curve. We consider O D .0; 0/, M D .0; 2a/, and a circle of diameter OM
of equation x2 C .y a/2 D a2 . Let A be an arbitrary point on this circle. Then
A D a.sin.2t/, 1 cos.2a//, N D a.2ctan.t/; 2/, and P D a.2ctan.t/; 1 cos.2t//.
Thus the parametric equations of the curve are x.t/ D 2a cot t; y.t/ D a.1cos.2t//,
a > 0:
With[fa=6,eps=.3g,
ParametricPlot[f2a Cot[t], a(1-Cos[2t])g,ft,eps,-epsg,
PlotStyle!Red,Ticks!ff-30,30g,fa,2agg,Prolog!ffDashing[f.01g],
Blue,Circle[f0,ag,a]gg,ImageSize!500]]
See Fig. 10.18.
We can generate Maria Agnesi’s curve with the next code. The circle is
x2 C .y a/2 D a2 ; where a is a real positive constant. We consider the parameter
m varying on the interval Œmend; Cmend; (mend > 0).
Animate[
With[fa=4,mend=15g,
Show[ParametricPlot[fmm,8 a3 /(4 a2 +mm2 )g,
fmm,-mend-0.00001,mg,Ticks!ff-mend,mendg,fagg,
PlotStyle!Red,AxesOrigin!f0,0g], Graphics[fBlue,Circle[f0,ag,a],
Line[fff-mend,2ag,fmend,2agg,ff0,0g,fm,2agg,
ff4a2 m/(4a2 +m2 /,8a3 /(4a2 +m2 )g,fm,8a3 /(4a2 +m2 )gg,
ffm,0g,fm,2aggg],
PointSize[.015],Black,
Point[ff0,0g,fm,2ag,fm,0g,f4a2 m/(4a2 +m2 ),8a3 /(4a2 +m2 )gg],
Red,Point[ffm,8a3 /(4a2 +m2 )gg],
Text[Style[M,Italic,12],f0,2.2ag],Text[Style[”0”,Italic,12],f0,-1.2g],
Text[Style[N,Italic,12],fm,2.2ag],
Text[Style[P,Italic,12],fm+a/4,8a3 /(4a2 +m2 )-a/5g],g],
PlotRange!All]],fm,-15,15g,
AnimationRunning!False,AnimationRate!.3]
See Fig. 10.19.
The Cassini ovals are described by a point such that the product of its distances
from two fixed points (called foci) a distance 2a apart is a constant b2 . Immediately
the Cartesian equation follows, that is,
.x a/2 C y2 .x C a/2 C y2 D b2 ; a; b > 0:
We note that generally there are two arcs that form the ovals.
bmax=2;
cassini[b ]:=Module[fa=1g,
Show[PolarPlot[Sqrt[a2 (Cos[2t]+Sqrt[(b/a)4 -Sin[2t]2 ])],ft,0,2g,
PlotStyle!Red],
PolarPlot[Sqrt[a2 (Cos[2t]-Sqrt[(b/a)4 -Sin[2t]2 ])],ft,0,2g,
PlotStyle!Blue],Ticks!fAutomatic,Noneg,
ImageSize!225,PlotRange!All]];
Manipulate[Quiet@cassini[b],
ffb,1g,0,bmax,0.001,Appearance!”Labeled”g,SaveDefinitions!True]
See Fig. 10.20.
A Cassini oval with a D b is said to be a Bernoulli lemniscata.
214 10 Miscellany in the Euclidean Plane
10.1.7 Orthoptics
In plane geometry, an orthoptic is the set of points for which two tangents of a given
curve meet at a right angle.
Example It is well-known that the orthoptics of
2 2
(i) an ellipse ax2 C by2 D 1 is the director (Monge) circle x2 C y2 D a2 C b2 ,
a > 0, b > 0;
2 2
(ii) a hyperbola ax2 by2 D 1 is the (Monge) circle x2 C y2 D a2 b2 ,
0 < b < a (in caseıof a b there are no orthogonal tangents);
(iii) a parabola y D x2 .2p/, p > 0, is its directrix;
2=3 2=3
(iv) an astroidp x C y D 1 is a quadrifolium with the polar equation
r D .1= 2/ cos.2t/, 0 t < 2.
Below we introduce some codes for these orthoptics.
mongeEllipse[˛ ]:=
Module[fa=3,b=2,c,m,m1,m2,t,eps=0.0001,x,x0,y0,x1,y1g,
c=Sqrt[a2 +b2 ];pointA=cfCos[˛],Sin[˛]g;
10.1 Some Planar Curves 215
Manipulate[
Quiet@mongeEllipse[˛],
ff˛,0,”˛”g,0,2,.0001,Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!False]
See Fig. 10.21.
mongeHyperbola[˛ ]:=
Module[fa=3,b=2,c,m,m1,m2,t,eps=0.01,x,x0,y0,x1,y1,pointA,pointB,
pointCg,c=Sqrt[a2 -b2 ];pointA=cfCos[˛],Sin[˛]g;
hyperbola=ParametricPlot[ffa Cosh[t],b Sinh[t]g,
f-a Cosh[t],b Sinh[t]gg,ft,-/2,/2g,PlotStyle!fBlueg];
circleMonge=ParametricPlot[cfCos[t],Sin[t]g,ft,0,2g,
PlotStyle!fRedg];
216 10 Miscellany in the Euclidean Plane
If[Abs[ArcTan[b/a]-˛]<eps,pointB=fa,0g;pointC=f0,bg;,
If[Abs[-ArcTan[b/a]-˛]<eps,pointB=f0,bg;pointC=f-a,0g;,
If[Abs[+ArcTan[b/a]-˛]<eps,pointB=f-a,0g;pointC=f0,-bg;,
If[Abs[2-ArcTan[b/a]-˛]<eps,pointB=f0,-bg;pointC=fa,0g;,
msol=Solve[(c2 Cos[˛]2 -a2 )m2 -(c2 Sin[2˛])m+c2 Sin[˛]2 +b2 ==0,m];
fm1,m2g=fRe[msol[[1,1,2]]],Re[msol[[2,1,2]]]g;
sol1=Solve[x2 /a2 -(c Sin[˛]+m1(x-c Cos[˛]))2 /b2 -1==0,x];
x0=Re[sol1[[1,1,2]]];y0=c Sin[˛]+m1(x0-c Cos[˛]);
pointB=fx0,y0g;
sol2=Solve[x2 /a2 -(c Sin[˛]+m2(x-c Cos[˛]))2 /b2 -1==0,x];
x1=Re[sol2[[1,1,2]]];y1=c Sin[˛]+m2(x1-c Cos[˛]);
pointC=fx1,y1g;];];];];
Show[hyperbola,circleMonge,
Graphics[ffRed,Thickness!0.008,Line[fpointC,pointA,pointBg]g,
fPointSize[.018],Point[fpointA,pointB,pointCg]gg],
PlotRange!All,Ticks!ff-3,3g,f-2,2gg,ImageSize!300]]
Manipulate[
Quiet@mongeHyperbola[˛],
ff˛,0,”˛”g,-,,.0001,Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!False]
See Fig. 10.22.
10.1 Some Planar Curves 217
mongeParabola[u ]:=
Module[fp=2,xg,
pointA=fu,-p/2g;
parabola=ParametricPlot[fx,x2 /(2p)g,fx,-6.65,6.65g,
PlotStyle!fBlueg];
circleMonge=ParametricPlot[fx,-p/2g,fx,-6,6g,PlotStyle!fRedg];
pointB=fu,-p/2g;pointA=fu-Sqrt[u2 +p2 ],(u-Sqrt[u2 +p2 ])2 /(2p)g;
pointC=fu+Sqrt[u2 +p2 ],(u+Sqrt[u2 +p2 ])2 /(2p)g;
Show[parabola,circleMonge,
Graphics[ffRed,Thickness!0.008,Line[fpointA,pointB,pointCg]g,
fPointSize[.018],Point[fpointA,pointB,pointCg]gg],PlotRange!All,
Ticks!fpf-3/2,3/2g,pf-1/2,1/2,1,2,4gg,ImageSize!200]]
218 10 Miscellany in the Euclidean Plane
Manipulate[
Quiet@mongeParabola[u],
ffu,0,”parameter”g,-3,3,.0001,Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!False]
See Fig. 10.23.
we set D =2. Thus the equations of the orthoptic of an astroid result by solving
the system of equations
(
y sin3 t D tan.t/ x cos3 t
y cos3 t D cot.t/ x C sin3 t :
Manipulate[
Quiet@mongeAstroid[˛],
ff˛,0,”˛”g,0,2,.0001,Appearance!”Labeled”g,
SynchronousUpdating!False,SaveDefinitions!True]
See Fig. 10.24.
The pedal curve of a curve and a fixed point is the locus of the orthogonal projection
of the fixed point on the tangent lines of the curve.
Our first example is the pedal curve of a circle. This pedal curve is a cardioid. Its
shape depends on the distance of the point to the center of the circle.
Clear[a,b,˛]
pedalofCircle[a ,b ,˛ ]:= (* a is the radius of the circle; we suppose that
it is centered in the origin of the axes *)
(* b is the distance of the fixed point from the center of the circle *)
Module[fx,y,t,solg,
sol=Solve[a Cos[t]x+a Sin[t]y==a2 &&Cos[t] y-Sin[t] x==-b Sin[t],fx,yg];
sol=Flatten[FullSimplify[sol]];
Show[ParametricPlot[fx,yg/.sol,ft,0,2g,PlotStyle!Green],
Graphics[ffBlack,Circle[f0,0g,a],
PointSize[.02],Point[ff0,0g,fb,0g,afCos[˛],Sin[˛]gg]g,
fBlue,Thickness[.007],
Line[ffa Cos[˛],a Sin[˛]g,fa Cos[˛]+b Sin[˛]2 ,(a-b Cos[˛])Sin[˛]gg]g,
fMagenta,PointSize[.03],
Point[fa Cos[˛]+b Sin[˛]2 ,(a-b Cos[˛])Sin[˛]g]g,
fRed,Thickness[.007],
Line[ffb,0g,fa Cos[˛]+b Sin[˛]2 ,(a-b Cos[˛])Sin[˛]gg]gg],
Axes!False,ImageSize!220,PlotRange!All]]
Manipulate[
Quiet@pedalofCircle[a,b,˛],
ffa,1,ag,1,4,0.01,Appearance!”Labeled”g,
ffb,1.5,bg,1,4,0.01,Appearance!”Labeled”g,
ff˛,0,”˛”g,0,2,0.01,Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!True]
See Fig. 10.25.
The second example is the pedal curve of an astroid. The parametric equations
of the astroid are .a cos3 t; b sin3 t/ and the fixed point is .xp; yp/.
Clear[a,b,xp,yp,˛]
pedalofAstroid[a ,b ,xp ,yp ,˛ ]:=
Module[fx,y,t,solg, (* a is the horizontal semiaxis of the astroid;
we suppose that is centered in the origin of the axes *)
(* b is the vertical semiaxis of the astroid *)
(* xp is the abscisa of the fixed point and yp is the ordinate of it *)
sol=Flatten[Solve[b Sin[t] x+a Cos[t] y-a b Sin[t] Cos[t]==0&&
a Cos[t] x-b Sin[t] y-a Cos[t] xp +b Sin[t] yp==0,fx,yg]];
10.1 Some Planar Curves 221
Manipulate[
Quiet@pedalofAstroid[a,b,xp,yp,˛],
ffa,1,”a”g,1,3,0.01,Appearance!”Labeled”g,
ffb,1,”b”g,1,3,0.01,Appearance!”Labeled”g,
ffxp,0,”xp”g,0,6,0.01,Appearance!”Labeled”g,
ffyp,0,”yp”g,0,6,0.01,Appearance!”Labeled”g,
ff˛,0,”˛”g,0,2,0.01,Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!True]
See Fig. 10.26.
10.2 Attractors
The Hénon map is a discrete-time dynamical system, [62, 12.2]. It is one of the
most studied examples of dynamical systems that exhibit a chaotic behavior. The
Hénon map takes a point .xn , yn / in the plane and maps it to a new point according
to the recurrences
(
xnC1 D 1 axn 2 C yn ;
ynC1 D bxn :
The map depends on two real parameters, a and b, which for the classical Hénon
map have values of a D 1:4 and b D 0:3. For the classical values, the Hénon map
is chaotic. For other values of a and b, the map may be chaotic, intermittent, or
converge to a periodic orbit. Below we show the behavior of the Hénon map for
a D 1:4 and b D 0:3.
For[n=2000;x=1;y=1;a=1.4;b=0.3;points=ffx,ygg;
k=0,kn,k++,u=1+y-a x2 ;v=b x;
AppendTo[points,fu,vg];x=u;y=v]
Show[ListPlot[points,PlotStyle!fRed,PointSize[0.005]g,
Axes!False,ImageSize!250],Graphics[ffPointSize[0.012],
Green,Point[First[points]],
PointSize[0.02],Blue,Point[Last[points]]g,
Text[Style[”initial point”,Italic,Darker@Green,12],First[points]+f0,-.1g],
Text[Style[”final point”,Italic,Blue,12],Last[points]+f-.10,.15g]g]]
The Lorenz attractor was found by Ed. N. Lorenz around 1963, [62, 9.4]. It was
derived from a simplified model of convection in the Earth’s atmosphere. The
system is most commonly expressed as three coupled nonlinear ordinary differential
equations.
8
ˆ
ˆ dx
D a.y x/;
< dt
dy
D x.b z/ y;
ˆ dt
:̂ dz
D xy cz:
dt
One commonly used set of constants is a D 10; b D 28; c D 8=3. A code using
the Euler’s method of integration of this case follows.
For[n=300;x=0.0001;y=.0001;z=.0001;a=10;b=28;c=8/3;h=0.015;
point1=fx,y,zg;points=fpoint1g;
k=0,k<n,k++,
u=x+h a(y-x);v=y+h(x(b-z)-y);w=z+h(x y-c z);
AppendTo[points,fu,v,wg];
x=u;y=v;z=w]
point2=fx,y,zg;
226 10 Miscellany in the Euclidean Plane
Show[
ListPointPlot3D[points,ColorFunction!”Rainbow”,
PlotStyle!PointSize[0.008],Axes!False],
Graphics3D[ffPointSize[0.018],Green,Point[point1],Red,Point[point2]g,
Text[Style[”initial point”,Italic,Darker@Green,12],point1+f0,0,-7g],
Text[Style[”final point”,Italic,Red,12],point2+f0,0,-9g]g],
ViewPoint!f25,2,6g,ImageSize!400]
See Fig. 10.30.
Here we use the built-in function NDSolve and consider the smooth case of the
Lorenz equations. In this case a good source is
http://reference.wolfram.com/language/example/VisualizeTheLorenzAttractor.
html. Then we color the trajectory in two ways.
Clear[x,y,z]
Module[fa=10,b=28,c=8/3,tend=100,s,tg,
s=NDSolve[fx’[t]==a(y[t]-x[t]),y’[t]==-x[t] z[t]+b x[t]-y[t],
z’[t]==x[t] y[t]-c z[t],x[0]==y[0]==z[0]==0.0001g,fx,y,zg,ft,0,tendg,
MaxSteps! 1];
fParametricPlot3D[Evaluate[fx[t],y[t],z[t]g/.s],ft,0,tendg,
PlotPoints!1000,PlotStyle!Directive[Thick,RGBColor[.8,0,0]],
Boxed!False,Axes!False,ImageSize!275,
ColorFunction!(ColorData[”SolarColors”,#4]&)],
ParametricPlot3D[Evaluate[fx[t],y[t],z[t]g/.s],ft,0,tendg,
PlotPoints!1000,Boxed!False,Axes!False,ImageSize!275,
ColorFunction!(ColorData[”Rainbow”][#4]&)]g
]
10.3 Limit Cycles and Hopf Bifurcation 227
The previous two pictures are all static. Below we consider the constants a; b; and
c parameters. It is also interesting to us the case when the final instant, here denoted
p, is a parameter. This case is interesting because one can see the appearance and
evolution of the chaotic movement.
Clear[a,b,c,p,x,y,z,xsol,ysol,zsol]
lorenzTrajectory[a ,b ,c ,p ]:=
Module[fx,y,z,xsol,ysol,zsolg,
fxsol,ysol,zsolg=NDSolveValue[fx’[t]==a(y[t]-x[t]),y’[t]==x[t](b-z[t])-y[t],
z’[t]==x[t] y[t]-c z[t],x[0]==0.0001,y[0]==0.0001,z[0]==0.0001g,fx,y,zg,
ft,0,pg];
Show[ParametricPlot3D[fxsol[t],ysol[t],zsol[t]g,ft,0,pg,PlotRange!All,
PlotPoints!250,ImageSize!250,Boxed!False,Axes!False,
ColorFunction!(ColorData[”Rainbow”][#4]&)],
Graphics3D[ffPointSize[0.018],Green,Point[fxsol[0],ysol[0],zsol[0]g]g,
Text[Style[”initial point”,Italic,Darker@Green,12],fxsol[0],ysol[0],
zsol[0]g+f0,0,-6g]g]]]
Manipulate[
Quiet@lorenzTrajectory[a,b,c,p],
ffa,10g,9,11,.01,Appearance!”Labeled”g,ffb,28g,6,40,.01,
Appearance!”Labeled”g,
ffc,8/3g,1.5,23,.01,Appearance!”Labeled”g,ffp,18g,0.001,80,.01,
Appearance!”Labeled”g,
SaveDefinitions!True,SynchronousUpdating!False]
See Fig. 10.31.
[52]. We assume that each initial value problem has a solution which is continuable
(
x D x.t/;
y D y.t/:
228 10 Miscellany in the Euclidean Plane
The Van der Pol differential equation is a second-order nonlinear ordinary differen-
tial equation with nonlinear damping:
x00 .t/ 1 x2 .t/ x0 .t/ C x.t/ D 0;
Below we show the existence of a limit cycle for 2 Œ0; 5: The green closed
trajectory is the singular periodic orbit. The blue trajectory always remains outside
the periodic orbit, whereas the red orbit always remains inside the periodic orbit.
These two nonperiodic orbits converge to the periodic one.
Clear[]
vanderPol[ ]:= Module[fa1=a3=2,a2=1,b1=3,b2=1,b3=0,x,y,t,sol1,
sol2,sol3,p1,p2,p3,system,point1,point2,point3g,
(* Outer trajectory *)
point1=fa1,b1g;
system=fx’[t]==y[t],y’[t]==(1-x[t]2 )y[t]-x[t],fx[0],y[0]g==point1g;
sol1=NDSolve[system,fx,yg,ft,100g];
p1=ParametricPlot[Evaluate[fx[t],y[t]g/.sol1],ft,0,100g,
PlotStyle!Blue,PlotPoints!200];
freqouter=Plot[x[t]/.sol1,ft,0,100g,PlotStyle!Blue,PlotPoints!100,
Ticks!ff50,100g,f-3,-2,-1,1,2,3gg];
(* Inner trajectory *)
point2=fa2,b2g;
system=fx’[t]==y[t],y’[t]==(1-x[t]2 )y[t]-x[t],fx[0],y[0]g==point2g;
sol2=NDSolve[system,fx,yg,ft,100g];
p2=ParametricPlot[Evaluate[fx[t],y[t]g/.sol2],ft,0,100g,PlotStyle!Red,
PlotPoints!300];
freqinner=Plot[x[t]/.sol2,ft,0,100g,PlotStyle!Red,PlotPoints!100,
Ticks!ff50,100g,f-2,-1,1,2gg];
(* Periodic trajectory; limit cycle *)
point3=fa3,b3g;
system=fx’[t]==y[t],y’[t]==(1-x[t]2 )y[t]-x[t],fx[0],y[0]g==point3g;
sol3=NDSolve[system,fx,yg,ft,100g];
p3=ParametricPlot[Evaluate[fx[t],y[t]g/.sol3],ft,0,100g,
PlotStyle!Green,PlotPoints!200];
freq=Plot[x[t]/.sol3,ft,0,100g,PlotStyle!Green,PlotPoints!100,
Ticks!ff50,100g,f-2,-1,1,2gg];
fShow[p1,p2,p3,Graphics[f
Text[Style[”initial outer point”,Italic,Blue,12],point1+f-.3,0.2g],
Text[Style[”initial inner point”,Italic,Red,12],point2+f-.5,0.2g],
fPointSize[0.03],Blue,Point[point1],Red,Point[point2],PointSize[0.035],
Green,Point[point3]gg],PlotRange!All,Ticks!ff-2,1,2g,f-3,-2,2,3gg,
ImageMargins!5,ImageSize!300],
Show[freq,PlotRange!ff0,50g,Automaticg],
Show[freqouter,PlotRange!ff0,50g,f-2,3gg],
Show[freqinner,PlotRange!ff0,50g,f-2,2gg]g]
Manipulate[Quiet@vanderPol[],
ff,.4g,0,3.1,0.01,Appearance!”Labeled”g,SaveDefinitions!True,
SynchronousUpdating!False]
See Fig. 10.32.
230 10 Miscellany in the Euclidean Plane
Clear[]
Manipulate[Block[f$PerformanceGoal=”Quality”g,
Show[fContourPlot[ -x2 ==0,fx,-3,3g,fy,-3,3g,Mesh!None,
ContourStyle!fDashed,Blueg],ContourPlot[y==0,fx,-3,3g,fy,-3,3g,
ContourStyle!fDashed,Blueg],
StreamPlot[f -x2 ,yg,fx,-3,3g,fy,-3,3g,
StreamColorFunction!Hue]g,ImageSize!300]],ff,0.08g,0,1g]
See Fig. 10.33.
232 10 Miscellany in the Euclidean Plane
Manipulate[
Quiet@supercriticalHopfbifurcation[],
ff,.6,””g,-1,2,0.1,Appearance!”Labeled”g,
SynchronousUpdating!False,SaveDefinitions!True]
See Fig. 10.34.
Chapter 11
Miscellany in the Euclidean Space
Viviani’s window is defined as the intersection of the cylinder of radius a and center
.a; 0/
.x a/2 C y2 D a2 ; a>0
x2 C y2 C z2 D 4a2 ; a > 0:
In order to see how this curve looks like, we consider the next code:
Clear[s,c,x,y]
s=x2 +y2 +z2 -4a2 ;c=(x-a)2 +y2 -a2 ;
Block[fa=1g,
ContourPlot3D[fs==0,c==0g,fx,-2a,2ag,fy,-2a,2ag,fz,-2a,2ag,
MeshFunctions!fFunction[fx,y,z,fg,s-c]g,MeshStyle!ffThick,Bluegg,
Mesh!ff0gg,ContourStyle!Directive[LightGreen,Opacity[0.1],
Specularity[White,30]],Boxed!False,Axes!False,ImageSize!250]]
See Fig. 11.1.
It is easy to find its parametric equations
Its picture was given above by the red line. We show the Viviani’s window
dynamically. The red dot moves on the curve describing it.
Clear[a,t]
With[fa=1g,
v1=ParametricPlot3D[fa(1+Cos[t]),a Sin[t],2a Sin[t/2]g,ft,0,4g,
Boxed!False,Axes!False];
v2=ParametricPlot3D[ff2afCos[t],Sin[t],0gg,
f2afCos[t],0,Sin[t]gg,f2af0,Cos[t],Sin[t]ggg,ft,0,2g];
pointviviani[t ]:=af1+Cos[t],Sin[t],2Sin[t/2]g];
Animate[Show[v1,v2,
Graphics3D[fPointSize[.04],Red,Point[pointviviani[t]]g],
PlotRange!All,ImageSize!200,ViewPoint!f6a,a/4,a/3g],ft,0,4g,
AnimationRate!.02,AnimationRunning!False,
SaveDefinitions!True]
See Fig. 11.2.
Suppose a smooth curve in Euclidean space is given, r.t/, with r0 .t/ ¤ 0, for all t.
Then we use the following vectors:
r0 .t/
tangent.t/ D ;
kr0 .t/k
tangent0 .t/
normal.t/ D ;
ktangent0 .t/k
binormal.t/ D tangent.t/ normal.t/: (cross product)
11.1 Some Space Curves 235
It is known that these vectors are of unitary norm, and each one is orthogonal
on the plane determined by the other two. Thus, they define a trihedron called the
Frenet–Serret trihedron. We do not need to find these three vectors because there
is a built-in function FrenetSerretSystem which supplies them. Then we take the
Viviani’s window and write its corresponding trihedron. The tangent unit vector is
colored in green, and the normal unit vector is colored in red, whereas the binormal
unit vector is colored in blue.
Clear[r,t,u,tangent,normal,binormal]
With[fa=1g,
r=af1+Cos[#],Sin[#],2Sin[#/2]g&;
vectors=Last[FrenetSerretSystem[r[t],t]]//Simplify;
ftangent,normal,binormalg=Map[Arrow[fr[t],r[t]+#g]&,vectors]];
Manipulate[
Show[ParametricPlot3D[r[s],fs,0,4Pig,Boxed!False,Axes!False,
ImageSize!225,PlotStyle!fThick,Blueg,ViewPoint!f8a,a/2,ag],
Graphics3D[ffPointSize[.03],Black,Point[r[t]]g,
Thick,Darker[Green],tangent,Red,normal,Blue,binormalg],
PlotRange!2]//Evaluate,ft,0,4Pi,Appearance!f”Labelled”gg]
See Fig. 11.3.
236 11 Miscellany in the Euclidean Space
Along this section we use the tangent, normal, and binormal unit vectors at a point
of a space curve to construct interesting surfaces.
11.2.1 Torus
Clear[t,,r,u,tube,normal,binormal]
With[fa=1,radius=0.2g,
r[t ]:=af1+Cos[t],Sin[t],2 Sin[t/2]g;
fss[t ]:=FrenetSerretSystem[af1+Cos[t],Sin[t],2Sin[t/2]g,t];
normal[u ]:=fss[t][[2,2]]/.t!u;
binormal[u ]:=fss[t][[2,3]]/.t!u;
(* The circle is centered at r[t] in the plane of normal and binormal
unit vectors *)
tube[v , ]:=r[v]+radius(Cos[]normal[v]+Sin[ ]binormal[v]);
ParametricPlot3D[Evaluate[tube[u,]],fu,0,4g,f ,0,2g,
PlotPoints!f100,12g,Ticks!ff0,1,2ag,f-a,0,ag,f-2a,0,2agg,
Mesh!0,PlotStyle!Directive[Darker[Green],Opacity[0.5],
Specularity[White,40]],ImageSize!200]]
See Fig. 11.6.
Clear[t,,r,radius,u,v,tube,normal,binormal]
With[fa=1g,
r[v ]:=af1+Cos[v],Sin[v],2Sin[v/2]g;
fss[t ]:=FrenetSerretSystem[af1+Cos[t],Sin[t],2Sin[t/2]g,t];
11.2 Some Surfaces in R3 239
normal[u ]:=fss[t][[2,2]]/.t!u;
binormal[u ]:=fss[t][[2,3]]/.t!u;
radius[v ]:=0.2+.04Sin[20v]; (* The radius of the tube *)
(* The circle is centered at r[t] in the plane of normal and binormal
unit vectors *)
Tube[v , ]:=r[v]+radius[v](Cos[]normal[v]+Sin[ ]binormal[v]);
ParametricPlot3D[Evaluate[tube[u,]],fu,0,4 Pig,f ,0,2 Pig,
PlotPoints!f100,12g,Ticks!ff0,1,2g,f-1,0,1g,f-2,0,2gg,
Mesh!None,PlotStyle!Directive[Darker[Green],Opacity[0.5],
Specularity[White,30]],ImageSize!200]]
See Fig. 11.7.
PlotStyle!fGreen,Opacity!0.3,Specularity[White,40]g,
PlotRange!All,Mesh!None,
Ticks!ff-2a,0,2ag,Automatic,f0,2cgg,ImageSize!225]]
See Fig. 11.8.
11.2 Some Surfaces in R3 241
PlotPoints!50,ImageSize!200],f˛,0,1g,AnimationRate!0.1,
AnimationRunning!False]
See Fig. 11.11.
11.3 Some Bodies 243
Now we introduce Costa’s minimal surface. For reference, see [24]. The code
follows.
cw=N[WeierstrassInvariants[f1/2,I/2g],10][[1]];
ew=WeierstrassP[0.5,fcw,0g];
costa[u ,v ]:=
f(1/2)Re[-WeierstrassZeta[u+I v,fcw,0g]+Pi u+Pi2 /(2ew)+(Pi/(2ew))
(WeierstrassZeta[u+I v-1/2,fcw,0g]
-WeierstrassZeta[u+I v-I/2,fcw,0g])],
(1/2)Re[-I WeierstrassZeta[u+I v,fcw,0g]+Pi v+Pi2 /(2ew)-(Pi I)/(2ew)
(WeierstrassZeta[u+I v-1/2,fcw,0g]
-WeierstrassZeta[u+I v-I/2,fcw,0g])],
(Sqrt[2Pi]/4)Log[Abs[(WeierstrassP[u+I v,fcw,0g]-ew)/
(WeierstrassP[u+I v,fcw,0g]+ew)]]g
costaplot=
With[feps=106 g,
ParametricPlot3D[costa[u,v],fu,eps,1-epsg,fv,eps,1-epsg,
Boxed!False,Axes!False,ViewPoint!f2.9,-1.4,1.5g,
PlotStyle!Directive[Green,Opacity!0.3,Specularity[White,40]],
Mesh!3,PlotPoints!80,ImageSize!325]]
See Fig. 11.12.
Clear[x,y,z]
With[fa=1g,
s=x2 +y2 +z2 -4a2 ;
c=(x-a)2 +y2 -a2 ;
reg=RegionPlot3D[s0&&c0,fx,-2a,2ag,fy,-2a,2ag,fz,-2a,2ag,
PlotStyle!Directive[Green,Opacity[0.3],Specularity[0.5]],
PlotPoints!80,Mesh!None];
Show[reg,PlotRange!All,Boxed!False,Axes!False,
ImageSize!250]]
See Fig. 11.13.
The volume of the body is calculated in two ways as follows:
Integrate[Boole[s0&&c0],fx,-2a,2ag,fy,-2a,2ag,fz,-2a,2ag],
Integrate[If[s0&&c0,1,0],fx,-2a,2ag,fy,-2a,2ag,fz,-2a,2ag]
˚ 16
9
.4 C 3/; 16
9
.4 C 3/
The next body is the intersection of a 3D ball with a full double circular
paraboloid.
Clear[x,y,z]
With[fa=1g,
s=x2 +y2 +z2 -4a2 ; p=x2 +y2 -Abs[z];
RegionPlot3D[s0&&p0,fx,-2a,2ag,fy,-2a,2ag,fz,-2a,2ag,
PlotStyle!Directive[Green,Opacity!0.15,Specularity[White,5]],
PlotPoints!50,Mesh!2,Boxed!False,Axes!False,
ImageSize!250]]
See Fig. 11.14.
11.3 Some Bodies 245
We introduce a code to find the local extrema which is suitable for functions of two
variables.
Clear[x,y,f]
f[x ,y ]:=y4 -y3 -3x2 y+x4
fdfx[x ,y ]=D[f[x,y],x],dfy[x ,y ]=D[f[x,y],y],dfxx[x ,y ]=D[f[x,y],x,x],
dfyy[x ,y ]=D[f[x,y],y,y],dfxy[x ,y ]=D[f[x,y],x,y],
dff[x ,y ]=dfxx[x,y]*dfyy[x,y]-dfxy[x,y]2 g;
solns=DeleteDuplicates[fx,yg/.Solve[fdfx[x,y]==0,dfy[x,y]==0g,fx,yg]];
If[Length@solns==0,Print[”No stationary point”],
solnsheader=f”no.”,”stationary point”g;
Print[Grid[Join[fsolnsheaderg,Table[Prepend[fsolns[[k]]g,k],
fk,Length@solnsg]],Frame!All,FrameStyle!Thin,
Alignment!Right]];
realsolns=Select[solns,FreeQ[#,Complex]&];
If[Length@realsolns==0,Print[”No real stationary point”],
realsolnsheader=f”no.”,”real stationary point”g;
Print[Grid[Join[frealsolnsheaderg,Table[Prepend[frealsolns[[k]]g,k],
fk,Length@realsolnsg]],Frame!All,FrameStyle!Thin,
Alignment!Right]];
extremum=
Select[realsolns,dff[#[[1]],#[[2]]]>0&&dfxx[#[[1]],#[[2]]]¤0&];
If[Length[extremum]==0,Print[”No point of extremum”],
line=fg;
Do[If[dfxx[extremum[[k,1]],extremum[[k,2]]]<0,text=”maximum”,
text=”minimum”];
line=AppendTo[line,fk,extremum[[k]],text,f@@extremum[[k]]g],
fk,Length@extremumg];
extremumheader=f”no.”,”point of extremum”,”nature”,
”value of the function”g;
Print[Grid[Join[fextremumheaderg,Table[line[[k]],
fk,Length@extremumg]],
Frame!All,FrameStyle!Thin,Alignment!Right]]]]]
11.4 Local Extrema of Real Functions of Several Real Variables 249
No. Stationary
n point
o
3i 3
1 2p ; 4 No. Real stationary point
n 2 o
2 3i
p ; 34 1 f0;
˚ 0g
2 2
2 0; 3
3 f0; ˚ 34 3
˚ 0g
4 3 3 ˚ ;
2
2
˚0; 34 3
4 3 3
;
5 ˚ ; 2 2
2
2
3 3
6 ;
2 2
No. Point
˚ 3 of
extremum Nature Value of the function
1 2 ; 32 minimum 27
˚3 3
8
2 ;
2 2
minimum 27
8
The example we discuss below appears in [53, Chap. 7]. The statement of the
exercise reads as follows: find the local extreme values of the following function
1; x ! 33 10
; y ! 1; z ! 85
Maximize::natt: The maximum is not attained at any point satisfying the given
˚constraints
˚
1; x ! 1; y ! 33 10
; z ! 85
We conclude that this function has neither global maximum point nor global
minimum point.
Now we make use of the tools of differential calculus and find the stationary
points.
11.4 Local Extrema of Real Functions of Several Real Variables 251
Clear[x,y,z]
variables=fx,y,zg;
f[x ,y ,z ]:=2x2 -x y+2x z-y+y3 +z2
solns=DeleteDuplicates[
variables/.Solve[Thread[Grad[f[x,y,z],variables]==0],variables]];
If[Length[solns]==0,Print[”No stationary point”],
solnsheader=f”no.”,”stationary point”g;
Print[Grid[Join[fsolnsheaderg,Table[Prepend[fsolns[[k]]g,k],
fk,Length[solns]g]],Frame!All,FrameStyle!Thin,Alignment!Right]];
realsolns=Select[solns,FreeQ[#,Complex]&];
If[Length[realsolns]==0,Print[”No real stationary point”],
realsolnsheader=f”no.”,”real stationary point”g;
Print[Grid[Join[frealsolnsheaderg,Table[Prepend[frealsolns[[k]]g,k],
fk,Length[realsolns]g]],Frame!All,FrameStyle!Thin,
Alignment!Right]];
seconddiff[x ,y ,z ]:=D[f[x,y,z],fvariables,2g];
extremum=fg;
Do[matrix=seconddiff[x,y,z]/.Thread[variables!realsolns[[j]]];
If[PositiveDefiniteMatrixQ[matrix]kNegativeDefiniteMatrixQ[matrix],
AppendTo[extremum,realsolns[[j]]]],fj,Length@realsolnsg];
If[Length[extremum]==0,Print[”No point of extremum”],
line=fg;
Do[matrix=seconddiff[x,y,z]/.Thread[variables!extremum[[k]]];
If[NegativeDefiniteMatrixQ[matrix],text=”maximum”,text=”minimum”];
line=AppendTo[line,fk,extremum[[k]],text,f@@extremum[[k]]g],
fk,Length@extremumg];
extremumheader=f”no.”,”point of extremum”,”nature”,
”value of the function”g;
Grid[Join[fextremumheaderg,Table[line[[k]],fk,Length@extremumg]],
Frame!All,FrameStyle!Thin,Alignment!Right]
]]]
No. Stationary
˚ 1 point
No. Real
˚ 1 stationary
point
1 4 ; 12 ; 14 1 4 ; 12 ; 14
˚1 2
˚1 2
2 ; ; 13
3 3
2 ; ; 13
3 3
No. Point
˚ 1 2 of extremum
Nature Value of the function
1 ; ; 13
3 3
minimum 13
27
252 11 Miscellany in the Euclidean Space
Along this subsection we consider the problem of finding the extreme values of the
function
(
x2 C y2 C z2 D 1;
f Œx; y; z/ D xyz; subject to
x C y C z D 1; x; y; z 2 R:
In order to see where these results come from, we use the method of Lagrange
multipliers [53, Chap. 7].
We have three real variables and two constraints. Consequently, we have two
multipliers.
Clear[x,y,z, ,]
function[x ,y ,z ]:=x y z
variables=fx,y,zg;
condition1[x ,y ,z ]:=x2 +y2 +z2 -1
condition2[x ,y ,z ]:=x+y+z
multipliers=f ,g;
allvariables=Flatten[fvariables,multipliersg];
The function we need is the Lagrangian
lagrangian[x ,y ,z , , ]:=ffunction[x,y,z],condition1[x,y,z],
condition2[x,y,z]g.f1, ,g
We look for the stationary points.
solns=DeleteDuplicates[allvariables/.
Solve[Thread[Grad[lagrangian[x,y,z, ,],allvariables]==0],
allvariables]];
If[Length@solns==0,Print[”No stationary point”],
Print[Grid[Join[ff”no.”,”stationary point”gg,
Table[Prepend[fSimplify[solns[[k]]]g,k],fk,Length@solnsg]],
Frame!All,FrameStyle!Thin,Alignment!Right]];
realsolns=Select[solns,FreeQ[#,Complex]&];
If[Length@realsolns==0,Print[”No real stationary point”],
Print[Grid[Join[ff”no.”,”real stationary point”gg,
Table[Prepend[fSimplify[realsolns[[k]]]g,k],fk,Length@realsolnsg]],
Frame!All,FrameStyle!Thin,Alignment!Right]];
seconddiff[x ,y ,z , , ]=D[lagrangian[x,y,z, ,],fallvariables,2g];
dxyz=fdx,dy,dzg;
xyzsolns=Table[Take[realsolns[[k]],3],fk,Length@realsolnsg];
cond1:=Table[D[condition1[x,y,z],fvariables,1g].dxyz/.
Thread[variables!xyzsolns[[j]]],fj,Length@realsolnsg];
11.4 Local Extrema of Real Functions of Several Real Variables 253
cond2:=Table[D[condition2[x,y,z],fvariables,1g].dxyz/.
Thread[variables!xyzsolns[[j]]],fj,Length@realsolnsg];
soldydz=Table[Flatten[Solve[cond1[[k]]==0&&cond2[[k]]==0,
fdx,dy,dzg,Reals]],fk,Length@realsolnsg];
soldydz1=dxyz/.soldydz;
dxyzall=Table[Join[soldydz1[[k]],fd ,dg],fk,Length@realsolnsg];
lines=fg;
Do[
matrix=Simplify[
dxyzall[[k]].(seconddiff[x,y,z, ,]/.
Thread[allvariables!realsolns[[k]]]).dxyzall[[k]]];
value=FullSimplify[function[x,y,z]/.Thread[variables!xyzsolns[[k]]]];
point=Simplify[xyzsolns[[k]]];
If[PositiveDefiniteMatrixQ[matrix],
lines=Append[lines,fk,point,matrix,valueg],
If[NegativeDefiniteMatrixQ[matrix],
lines=Append[lines,fk,point,matrix,valueg],
lines=Append[lines,fk,point,matrix,valueg]]],
fk,Length@realsolnsg]]]
header=f”no.”,”point of extremum”,”second order diff.”,”value of
the function”g;
Grid[Join[fheaderg,Table[lines[[k]],fk,Length@realsolnsg]],
Frame!All,FrameStyle!Thin,Alignment!Right]
Solve::svars: Equations may not give solutions for all “solve” variables.
Solve::svars: Equations may not give solutions for all “solve” variables.
Solve::svars: Equations may not give solutions for all “solve” variables.
General::stop: Further output of Solve::svars will be suppressed during this
calculation.
254 11 Miscellany in the Euclidean Space
3 p1 6 ; p1 6 ; 23 6dx2 1
p
n q o p
3 6
4 p1 6 ; 32 ; p1 6 6dx2 1
p
n q o p
3 6
1 2 p 1
5 p ; 3
; 6dx2 1
3p
n 6 q6o p
6
1 1 2
6 p
6
; p
6
; 3
6dx2 1
3p 6
We conclude that the first, fifth, and sixth points are points of minimum, whereas
the other ones are points of maximum. The values of the function on all these points
have been already established in the last column.
Print[Grid[Join[frealsolnsheaderg,Table[Prepend[frealsolns[[k]]g,k],
fk,Length[realsolns]g]],Frame!All,FrameStyle!Thin,
Alignment!Right]];
seconddiff[x ,y ,z ]:=D[function[x,y,z],fvariables,2g];
extremum=fg;
Do[matrix=seconddiff[x,y,z]/.Thread[variables!realsolns[[j]]];
If[PositiveDefiniteMatrixQ[matrix]kNegativeDefiniteMatrixQ[matrix],
AppendTo[extremum,realsolns[[j]]]],fj,Length@realsolnsg];
If[Length[extremum]==0,Print[”No point of extremum interior”],
line=fg;
Do[matrix=seconddiff[x,y,z]/.Thread[variables!extremum[[k]]];
If[NegativeDefiniteMatrixQ[matrix],text=”maximum”,text=”minimum”];
line=AppendTo[line,fk,extremum[[k]],text,function@@extremum[[k]]g],
fk,Length@extremumg];
extremumheader=f”no.”,”point of extremum”,”nature”,”value of the
function”g;
Grid[Join[fextremumheaderg,Table[line[[k]],fk,Length@extremumg]],
Frame!All,FrameStyle!Thin,Alignment!Right]
]]]
No. Stationary point interior K No. Real stationary point interior K
1 f0; 0; 1g 1 f0; 0; 1g
2 f0; 0; 0g 2 f0; 0; 0g
3 f0; 0; 1g 3 f0; 0; 1g
Let us look for the points of extreme located on the boundary of K. Then we
apply the method of Lagrange multipliers.
Clear[x,y,z,condition,lagrangian, ,seconddiff]
condition[x ,y ,z ]:=x2 +y2 +2z2 -8;
multiplier=f g;
allvariables=Flatten[fvariables,multiplierg];
The function we need is
lagrangian[x ,y ,z , ]:=ffunction[x,y,z],condition[x,y,z]g.f1, g
We look for the stationary points
solns=DeleteDuplicates[allvariables/.
Solve[Thread[Grad[lagrangian[x,y,z, ],allvariables]==0],allvariables]];
If[Length@solns==0,Print[”No critical point on the boundary of K”],
Print[Grid[Join[ff”no.”,”critical point on the boundary of K”gg,
Table[Prepend[fSimplify[solns[[k]]]g,k],fk,Length@solnsg]],
256 11 Miscellany in the Euclidean Space
Frame!All,FrameStyle!Thin,Alignment!Right]];
realsolns=Select[solns,FreeQ[#,Complex]&];
If[Length@realsolns==0,Print[”No real critical point on the boundary of
K”],
Print[Grid[Join[ff”no.”,”real critical point on the boundary
of K”gg,Table[Prepend[fSimplify[realsolns[[k]]]g,k],
fk,Length@realsolnsg]],Frame!All,FrameStyle!Thin,
Alignment!Right]];
seconddiff[x ,y ,z , ]=D[lagrangian[x,y,z, ],fallvariables,2g];
dxyz=fdx,dy,dzg;
xyzsolns=Table[Take[realsolns[[k]],3],fk,Length@realsolnsg];
cond:=Table[D[condition[x,y,z],fvariables,1g].dxyz/.
Thread[variables!xyzsolns[[j]]],fj,Length@realsolnsg];
soldydz=Table[Flatten[Solve[cond[[k]]==0,fdx,dy,dzg,Reals]],
fk,Length@realsolnsg];
soldydz1=dxyz/.soldydz;
dxyzall=Table[Join[soldydz1[[k]],fd g],fk,Length@realsolnsg];
lines=fg;
Do[
matrix=Simplify[
dxyzall[[k]].(seconddiff[x,y,z, ]/.Thread[allvariables!realsolns[[k]]]).
dxyzall[[k]]];
value=FullSimplify[function[x,y,z]/.Thread[variables!xyzsolns[[k]]]];
point=Simplify[xyzsolns[[k]]];
If[PositiveDefiniteMatrixQ[matrix],
lines=Append[lines,fk,point,matrix,valueg],
If[NegativeDefiniteMatrixQ[matrix],
lines=Append[lines,fk,point,matrix,valueg],
lines=Append[lines,fk,point,matrix,valueg]]],fk,Length@realsolnsg]
]]
header=f”no.”,”point of extremum”,”second order diff.”,”value of f ”,
”nature”g;
Grid[Join[fheaderg,Table[lines[[k]],fk,Length@realsolnsg]],
Frame!All,FrameStyle!Thin,Alignment!Right]
11.4 Local Extrema of Real Functions of Several Real Variables 257
No. Critical point on the boundary of K No. Real critical point on the boundary of K
˚
˚
2 2; 2; 0; 52 2 2; 2; 0; 52
3 f0; 0; 2; 3g 3 f0; 0; 2; 3g
4 f0; 0; 2; 3g 4 f0; 0; 2; 3g
˚
˚
5 2; 2; 0; 52 5 2; 2; 0; 52
˚ 3
˚ 3
6 n2;q 2; 0; q
2 q o 6 n2;q 2; 0; q
2 q o
7 3; 3; 5;3 7 3; 3; 5;3
n q 2 q 2 q 2 o2 n q 2 q 2 q 2 o2
8 3 ; 32 ; 52 ; 32 8 3 ; 23 ; 52 ; 32
nq 2q q o nq 2q q o
3
9 ; 3 ; 5 ; 32
2 q2 q 2
9 3
; 3 ; 5 ; 32
2 q2 q 2
nq o nq o
3
10 2
; 32 ; 52 ; 32 10 3
2
; 23 ; 52 ; 32
n q o n q o
11 p1 2 ; p1 2 ; 72 ; 52 11 p1 2 ; p1 2 ; 72 ; 52
n q o n q o
12 p1 2 ; p1 2 ; 72 ; 52 12 p1 2 ; p1 2 ; 72 ; 52
n q o n q o
1
13 p ; p1 2 ; 72 ; 52 13 1
p ; p1 2 ; 72 ; 52
n 2 q o n 2 q o
1
14 p
2
; p1 2 ; 72 ; 52 14 1
p
2
; p1 2 ; 72 ; 52
Solve::svars: Equations may not give solutions for all “solve” variables.
Solve::svars: Equations may not give solutions for all “solve” variables.
Solve::svars: Equations may not give solutions for all “solve” variables.
General::stop: Further output of Solve::svars will be suppressed during this calculation.
1. Abell, M.L., Braselton, J.P.: Differential Equations with Mathematica. AP Professional, Boston
(1993)
2. Adamchik, V., Wagon, S.: A 2000-year search changes direction. Math. Educ. Res. 5(1),
11–19 (1996). www.cs.cmu.edu/~adamchik/articles/pi/pi.htm
3. Adamchik, V., Wagon, S.: A simple formula for . Am. Math. Mon. 104(9), 852–855 (1997)
4. Alexander, R.: Diagonally implicit Runge-Kutta methods for stiff O.D.E’s. SIAM J. Numer.
Anal. 14(6), 1006–1021 (1977)
5. Backhouse, N.: Pancake functions and approximations to . Math. Gaz. 79, 371–374 (1995).
Note 79.36
6. Bailey, D.H., Borwein, P.B., Plouffe, S.: On the rapid computation of various polylogarithmic
constants. Math. Comput. 66(218), 903–913 (1997)
7. Baruah, D.N., Berndt, B.C., Chan, H.H.: Ramanujan’s series for 1=: a survey. Am. Math.
Mon. 116(7), 567–587 (2009)
8. Bellard, F.: Computation of the n’th digit of in any base in O.n2 / (1997). fabrice.bellard.
free.fr/pi/
9. Borwein, J.M., Borwein, P.B.: The class three Ramanujan type series for 1=. J. Comput.
Appl. Math. 45(1–2), 281–290 (1993)
10. Borwein, J.M., Borwein, P.B., Bailey, D.H.: Ramanujan, modular equations, and approxima-
tions to or how to compute one billion digits of . Am. Math. Mon. 96(3), 201–219 (1989)
11. Borwein, J.M., Skerritt, M.P.: An Introduction to Modern Mathematical Computing with
Mathematica. Springer Undergraduate Text in Mathematics and Technology. Springer, New
York (2012)
12. Brun, V.: Carl Störmer in memoriam. Acta Math. 100(1–2), I–VII (1958)
13. Bryson, A.E., Ho, Y.C.: Applied Optimal Control: Optimization, Estimation, and Control.
Halsted Press, New York (1975)
14. Burns, R.E., Singleton, L.G.: Ascent from the lunar surface. Technical report TN D-1644,
NASA, George C. Marshall Space Flight Center, Huntsville (1965)
15. Cesari, L.: Optimization–Theory and Applications. Problems with Ordinary Differential
Equations. Applications of Mathematics, vol. 17. Springer, New York (1983)
16. Champion, B.: General Visualization Quick Start. Wolfram Research, Champaign (2013).
www.wolfram.com/training/courses/vis412.html
17. Chudnovsky, D.V., Chudnovsky, G.V.: The computation of classical constants. Proc. Natl.
Acad. Sci. USA 86(21), 8178–8182 (1989)
18. Cloitre, B.: A BBP formula for 2 in golden base (2003). abcloitre@wanadoo.fr
19. Dennis Lawrence, J.: A Catalog of Special Plane Curves. Dover, New York (1972)
20. Don, E.: Mathematica. Schaum’s Outlines Series. McGraw Hill, New York (2009)
21. Ebbinghaus, H.D., Peckhaus, V.: Ernst Zermelo. An Approach to His Life and Work. Springer,
Berlin/Heidelberg (2007)
22. Elkies, N.D.: On a4 C b4 C c4 D d4 . Math. Comput. 51(184), 825–835 (1988)
23. Fay, T.H.: The butterfly curve. Am. Math. Mon. 96(5), 442–443 (1989)
24. Ferguson, H., Gray, A., Markvorsen, S.: Costa’s minimal surface via Mathematica. Math.
Educ. Res. 5(1), 5–10 (1966)
25. Finch, S.R.: Zermelo’s navigation problem. In: Mathematical Constants II. Encyclopedia of
Mathematics and Its Applications. Cambridge University Press, Cambridge (Forthcoming)
26. Floyd, R.W.: Algorithm 245: treesort. Commun. ACM 7(12), 701 (1964)
27. Frye, R.E.: Finding 958004 C 2175194 C 4145604 D 4224814 on the connection machine. In:
Proceedings of Supercomputing’88. Science and Applications, vol. 2, pp. 106–116 (1988)
28. Gourevitch, B., Guillera Goyanes, J.: Construction of binomial sums for and polylogarithmic
constants inspired by BBP formula. Appl. Math. E-Notes 7, 237–246 (2007). www.math.nthu.
edu.tw/~amen
29. Gradshteyn, S.G., Ryzhik, I.M.: Tables of Integrals, Series, and Products, 7th edn. Elsevier,
Amsterdam (2007)
30. Guillera, J., Zudilin, W.: Ramanujan-type for 1=: the art of translation. In: Bruce, D.P.,
Berndt, C. (eds.) The Legacy of Srinivasa Ramanujan. Lecture Notes Series, vol. 20, pp. 181–
195. Ramanujan Mathematical Society (2013). arXiv 1302.0548
31. Hartman, P.: Ordinary Differential Equations, 1st edn. Wiley, Hoboken (1964)
32. Hastings, C., Mischo, K., Morrison, M.: Hands-On Start to Wolfram Mathematicar and
Programming with the Wolfram LanguageTM . Wolfram Media, Champaign (2015)
33. Hazrat, R.: Mathematicar : A Problem-Centered Approach. Springer Undergraduate Mathe-
matics Series, vol. 53. Springer, London (2010)
34. Hoare, C.A.R.: Algorithm 64: quicksort. Commun. ACM 4(7), 321 (1961)
35. Hull, D.G.: Optimal guidance for Lunar ascent. Adv. Astronaut. Sci. 134, 275–285 (2009).
Proccedings of the AAS Space Flight Machanics Meeting, Savannach
36. Hull, D.G.: Optimal guidance for quasi-planar Lunar ascent. J. Optim. Theory Appl. 151(2),
353–372 (2011)
37. Hull, D.G., Harris, M.W.: Optimal solutions for quasiplanar ascent over a spherical Moon. J.
Guid. Control Dyn. 35(4), 1218–1224 (2012). doi: 10.2514/1.55443
38. Knuth, D.E.: The Art of Computer Programming. Sorting and Searching. Computer Science
and Information Processing, vol. 3. Addison-Wesley, Reading (1973)
39. Lander, L.J., Parkin, T.R.: Counterexample to Euler’s conjecture on sums of like powers. Bull.
Am. Math. Soc. 72(6), 1079 (1966)
40. Lawden, D.F.: Analytical Methods of Optimization. Dover Books on Mathematics. Dover,
Mineola (2006)
41. Lucas, S.K.: Integral proofs that 355=113 > . Gaz. Aust. Math. Soc. 32(4), 263–266 (2005)
42. Lucas, S.K.: Integral approximations to with nonnegative integrands (2007). carma.
newcastle.edu.au/jon/Preprints/Papers/By%20Others/more-pi.pdf
43. Mangano, S.: Mathematica Cookbook. O’Reilly, Sebastopol (2010)
44. Manià, B.: Sopra un problema di navigatione di Zermelo. Math. Ann. 113(1), 584–589 (1937)
45. McShane, E.J.: A navigation problem in the calculus of variations. Am. J. Math. 59(2), 327–
334 (1937)
46. Miele, A.: Flight Mechanics. Theory of Flight Path. Addison-Wesley Series in the Engineering
Sciences Space Science and Technology, vol. 1. Addison-Wesley, Reading (1962)
47. Mureşan, M.: Classical Analysis by Mathematica (Forthcoming)
48. Mureşan, M.: A Primer on the Calculus of Variations and Optimal Control. Trajectories
Optimization (Forthcoming)
49. Mureşan, M.: On a Runge-Kutta type method. Rev. Anal. Numér. Théorie Approx. 16(2),
141–147 (1987)
50. Mureşan, M.: Some computing results of a Runge-Kutta type method. Seminar on
Mathematical Analysis Nr. 7, Univ. Babeş-Bolyai, Cluj-Napoca, pp. 101–114 (1987)
References 261
51. Mureşan, M.: A semi-explicit Runge-Kutta method. Seminar on Differential Equations Nr. 8,
Univ. Babeş-Bolyai, Cluj-Napoca, pp. 65–70 (1988)
52. Mureşan, M.: Qualitative Properties of Differential Equations and Inclusions. Ph.D. thesis,
Babeş-Bolyai University, Cluj-Napoca (1996)
53. Mureşan, M.: A Concrete Approach to Classical Analysis. CMS Books in Mathematics.
Springer, New York (2009)
54. Mureşan, M.: Instructor Solution Manual, for A Concrete Approach to Classical Analy-
sis. Springer, New York (2012). www.springer.com/mathematics/analysis/book/978-0-387-
78932-3?changeHeader
55. Mureşan, M.: Soft landing on Moon with Mathematica. Math.r J. 14 (2012). doi:
dx.doi.org/doi:10.3888/tmj.14–16
56. Mureşan, M.: On Zermelo’s navigation problem with Mathematica. J. Appl. Funct. Anal.
9(3–4), 349–355 (2014)
57. Mureşan, M.: On the maximal orbit transfer problem. Math.r J. 17 (2015). doi:
dx.doi.org/10.3888/tmj.17–4
58. Palais, R.S.: The visualization of mathematics: towards a mathematical exploratorium. Not.
Am. Math. Soc. 46(6), 647–658 (1999)
59. Rao, K.S.: Ramanujan and important formulas. In: Nagarajan, K.R., Soundararajan, T. (eds.)
Srinivasa Ramanujan: 1887–1920: A Tribute, pp. 32–41. MacMillan India, Madras (1988)
60. Robinson, E.A., Jr.: Man Ray’s human equations. Not. Am. Math. Soc. 62(10), 1192–1198
(2015)
61. Sedgewick, R.: Implementing Quicksort programs. Commun. ACM 21(10), 847–857 (1978)
62. Strogatz, S.H.: Nonlinear Dynamics and Chaos: with Applications to Physics, Biology,
Chemistry, and Engineering. Perseus, New York (1994)
63. Takano, K.: Pi no arctangent relation wo motomete [finding the arctangent relation of ]. Bit
15(4), 83–91 (1983)
64. Torrence, B., Torrence, E.: The Student’s Introduction to Mathematicar . A Handbook for
Precalculus, Calculus, and Linear Algebra, 2nd edn. Cambridge University Press, Cambridge,
UK (2009)
65. Trott, M.: The Mathematica GuideBook for Graphics. Springer, New York (2004)
66. Trott, M.: The Mathematica GuideBook for Programming. Springer, New York (2004)
67. Trott, M.: The Mathematica GuideBook for Numerics. Springer, New York (2006)
68. Trott, M.: The Mathematica GuideBook for Symbolics. Springer, New York (2006)
69. Weisstein, W.E.: BBP-Type formula. Technical report, MathWorld-A Wolfram Web Resources.
mathworld.wolfram.com/BBP-TypeFormula.html
70. Weisstein, W.E.: Machbin-Like formulas. Technical report, MathWorld-A Wolfram Web
Resources. mathworld.wolfram.com/Machin-LikeFormulas.html
71. Weisstein, W.E.: Pi formulas. Technical report, MathWorld-A Wolfram Web Resources.
mathworld.wolfram.com/PiFormulas.html
72. Wolfram, S.: The Mathematicar Book, 5th edn. Wolfram Media, Champaign (2003)
73. Wolfram, S.: An Elementary Introduction to the Wolfram Language. Wolfram Media,
Champaign (2015)
74. Wolfram, S.: Differential Equation Solving with DSOLVE. Wolfram Research, Champaign
(2008). Wolfram Mathematicar Tutorial Collection. htpps:/reference.wolfram.com/language/
tutorial/DSolveOverview.html
75. Wolfram, S.: Advanced numerical differential equation solving in Mathematica. In: Wolfram
Mathematicar Tutorial Collection. Wolfram Research, Champaign (2008). htpps:/www.scrib.
com/doc/122203558/Advanced-Numerical-Differential-Equation-Solving-in-Mathematica
76. Wolfram, S.: http://numbers.computation.free.fr/Constants/Pi/piclassic.html
77. Wolfram, S.: http://numbers.computation.free.fr/Constants/Pi/piramanujan.html
78. Zermelo, E.: Über die Navigation in der Luft als Problem der Variationsrechnung. Jahresbericht
der deutschen Mathematiker – Vereinigung, Angelegenheiten 39, 44–48 (1930)
79. Zermelo, E.: Über das Navigationproblem bei ruhender oder veränderlicher Windverteilung.
Z. Angew. Math. Mech. 11(2), 114–124 (1931)
Index
A Maltese, 74
altitude, 96 curvature, 100
angle bisector, 96 curve
ascent Maria Agnesi’s, 212
Lunar, 179 pedal, 220
planar, 181 smooth, 100
cycloid, 200
B
BBP like D
binomial sum, 163 deltoid, 211
bean, 77
bifurcation
hopf, 230 E
binormal, 99 epicycloid, 202
butterfly, 78 equation
differential
Van der Pol, 228
C
cardioid, 205
centroid, 96 F
circle foot, 96
nine points formula
Euler’s, 97 BBP, 158
circumcenter, 95 binomial
circumcircle, 96 Newton’s, 94
circumradius, 96 de Bruijn, 83
coefficient function
binomial, 17 zeta
conchoid of Nicomedes, 197 Riemann, 20
conjecture
Collatz, 60
Euler’s, 14 H
Fermat, 16 Hamiltonian, 171
cross, 74 heap, 41
hyperboloid P
of one sheet, 102 pair
hypocycloid, 207 admissible, 171
feasible, 171
Pythagorean, 13
I paraboloid
incenter, 96 hyperbolic, 90
incircle, 96 parameter
natural, 100
perpendicular bisector, 95
L pivot, 52
lemniscata point
Bernoulli, 213 equilibrium, 117
limit cycle, 227 saddle, 90
list, 24 Preface, ix
problem
M Mayer, 171
map navigation
Hénon, 223 Zermelo’s, 173
mean sorting, 35
arithmetic, 12
geometric, 12
harmonic, 12 Q
median, 96 quadrifolium, 214
method
Runge-Kutta, 124
diagonally implicit, 129
explicit, 124, 129 R
semi-explicit, 124, 129 rhodonea, 76
multiplier, 171 rose, 76
multipliers
method of Lagrange, 252
S
sort
N heap, 41
nephroid, 205 insertion, 38
Freeth’s, 206 merge, 41
normal, 99 quicksort, 52
number selection, 37
Bell, 18 star
polygonal, 134 of David, 75
numbers surface
Pythagorean, 13 minimal
Costa’s, 243
Möbius, 89
O
orbit
parking, 179
orthocenter, 96 T
orthoptic, 214 tangent, 99
ovals vector
Cassini, 213 unit, 100
Index 265
tetrahedron trihedron
Reuleaux, 245 Frenet-Serret, 99, 235
timing, 53
torus, 237
trajectory W
quasiplanar, 181 window
tree Viviani’s, 101, 233
binary, 42