Computer Algebra in Applied Mathematics: An Introduction to MACSYMA
RHRand i
Cornell University
Computer Algebra in Applied Mathematics: An Introduction to MACSYMA
PITMAN PUBLISHING LIMITED 128 Long Acre, London WC2E 9AN PITMAN PUBLISHING INC. 1020 Plain Street, Marshfield, Massachusetts 02050
Associated Companies Pitman Publishing Pty Ltd, Melbourne Pitman Publishing New Zealand Ltd, Wellington Copp Clark Pitman, Toronto
© R H Rand 1984 First published 1984 AMS Subject Classifications: 34ElO, 68A15, 98A30; 98A35
Library of Congress Cataloging in Publication Data Rand, Richard H. Computer algebra in applied mathematics. Bibliography: p. Includes index. 1. Algebra-Data processing. 2. MACSYMA (Computer system) I. Title. QAI55.7.E4R36 1984 512' .028'54 83-17328 ISBN 0-273-08632-4 British Library Cataloging in Publication Data Rand, Richard H. Computer algebra in applied mathematics.(Research notes in mathematics'; 94) 1. Algebra-Data processing 2. MACSYMA system 1. Title II. Series 512' .028'5425 QA154.2 ISBN 0-273-08632-4 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording and/or otherwise, without the prior written permission of the publishers. This book may not be lent, resold, hired out or otherwise disposed of by way of trade in any form of binding or cover other than that in which it is published, without the prior consent of the publishers.
Preface
This book has the dual goal of showing how computer algebra can play an important role in applied mathematics, while introducing the reader to the computer system MACSYMA which is designed to
s~pport
such computations.
In order
to provide realistic applications of computer algebra, the level of the mathematical treatment must be sUbstantially beyond a first course in calculus.
Thus this book is aimed
at a reader who has had at least three years of college level calculus and differential equations. At the time of this writing, computer algebra is utilized mainly as a research tool, and rather infrequently at that.
It is virtually absent from undergraduate
education in the sciences and engineering.
The author
believes however that it is destined for a major role in engineering and applied mathematics.
In particular, the
use of systems like MACSYMA in college calculus and differential equations courses is planned at Cornell for the immediate future.
It is expected that it will not be
long before computer algebra is as common to an engineering student as the now obsolete slide rule once was. MACSYMA is fully described in the MACSYMA Reference Manual (2 volumes, 1983, Massachusetts Institute of Technology).
However the author has found that this
reference, while technically complete, does not offer the wot1ld-be user a "friendly" introduction.
The MACSYMA
Primer (in Introductory MACSYMA Documentation: A Collection of Papers, 1982, Massachusetts Institute of Technology) on the other hand, is much too brief and in any case offers little indication of where computer algebra could be used in typical applications. at bridging the gap.
The present introduction is aimed
The MACSYMA user is encouraged to experiment in the medium.
The question of whether a particular function will
do such and such a job is most easily answered by trying it.
As the old engineering dictum prescribes, "when all
else fails, read the instructions."
Anybody working in
MACSYMA must have access to the MACSYMA Reference Manual. In fact, the material in this book is best assimilated if a computer terminal running MACSYMA is before the reader, with the MACSYMA reference manual close at hand for frequent consultation. Technical discussions with the following people are gratefully acknowledged:
Frank Moon, Les Schaffer, Ken
Brown, Jim Geer, Joe Keller, Herbert Hui, and Larry Fresinski.
Special thanks to Phil Holmes for reading the
manuscript.
Richard Rand June 1983
MACSYMA is a trademark of SymbOlics, Inc. 257 Vassar St. Cambridge, MA 02139
Comems page i
Chapter 1. Introduction to MACSYMA
1
Example 1. Complex variables Example 2. EUler-Lagrange equation
1 3
Example 3. First order O.D.E.'s
5
Example 4. Period of a nonlinear oscillator
9
Example 5. Laplace transforms
15
Example 6. Eigensolution of a system of O.D.E.'s.
18
Exercise. Boundary Value Problem
25
Chapter 2. Housekeeping in MACSYMA
31
Disk files
31
Special keys
36
The editor
38
Chapter 3. Programming in MACSYMA
41
Example 1. Taylor series solution of O.D.E's Example 2. Lagrange's equations
42 52
Example 3. Hamilton's equations
57
Exercise. Laplace Transforms
64
Chapter 4. Perturbation Methods
71
Example 1. Van der Pol's equation
71
Example 2. Mathieu's equation
87
Example 3. DUffing's equation
124
Questions of convergence
135 145
Exercise. The Two Variable Expansion Method
page Epilogue. On Bugs
162
Appendix. Sample BATCH program
164
Glossary of MACSYMA Functions
169
References
175
Index
177
1 Introduction to MACSYMA
In this chapter we will consider some typical problems in applied mathematics and how they can be implemented using MACSYMA.
In the process we will encounter a variety
of MACSYMA functions. ,For more information about any of these, see the 2 volume MACSYMA Reference Manual (Version 10, Jan. 1983) written by the Mathlab Group, Laboratory for Computer Science, MIT. To enter MACSYMA from cownand level, type MACSYMA (return) The computer will display something like: @MACSYMA This is MACSYMA 302 (c) Copyright 1982 Massachusetts Institute of Technology Loading fix file (MACSYM)TOPS20.FIX.302 (C1) The (C1) is a label which references your first input command.
Labels will be either C's (input commands), D's
(output displays) or E's (intermediate output expressions). Be sure not to inadvertently name any variables C1, D3, E19, etc., as these names may be confused with labeled expressions. Example 1. Complex Variables The problem of solving Laplace'S equation by conformal mapping often results in a sOlution being expressed as the real or imaginary part of a complex function.
In this
example we will define a function F(Z), compute its real
and imaginary parts, and check that the imaginary part s~tisfies Laplace's equation:
(Cl)
Z:X+%I*Y~
%1 Y + X
(Dl) (C2)
F:LOG(SIN(Z)**2)~
2 LOG(SIN(%I Y + X»
(D2)
(C3) REALPART(F)~ RPART FASL 9 PS MACSYM being loaded Loading done HYPER FASL 1 PS MAXOUT being loaded Loading done ATAN2 FASL 1 PS MAXOUT being loaded Loading done 2
2
IMAGPART(F)~
(D4) (C5)
2
LOG(COS (x) SINH (Y) + SIN (X) COSH (Y»
(D3) (C4)
2
2 ATAN2(COS(X) SINH(Y), SIN(X) COSH(Y» RATSIMP(DIFF(D4,X,2)+DIFF(D4,y,2»~
a
(D5)
Comments: (Cl) The COLON in MACSYMA is used for variable assignment.
The EQUAL SIGN is used for equations as well
as for conditions in IF statements. (Cl) The SEMICOLON, rather than RETURN, is the signal to MACSYMA that you have finished your command.
The DOLLAR
SIGN will also work, but it suppresses the corresponding output display.
This is useful when you are creating an
intermediate result which you don't care to see displayed. (Cl) The quantities i, e and pi are represented in MACSYMA as %1, %E and %PI respectively. (C3) To suppress the annoying file-loading messages, set LOADPRINT:FALSE. (D4) The function ATAN2(u,v)
=
arctan(u/v).
(C5) The function RATSIMP() is a general purpose expression-simplifier.
Without it here we would have
displayed a dozen lines of hyperbolic and trig functions 2
which, in fact, identically sum to zero. (C5)
Try it'!
DIFF() can also be used to obtain mixed partials.
~.g. DIFF{F,X,2,Y,3)
=
F
• For brevity, DIFF(F,X)
=
F •
XXyyy
Example 2.
X
EUler-Lagrange Equation
EUler's equation, d
'4L
'4L
= dt
'4 q'
0,
where L
=
L(q,q',t),
~q
is a second order o.d.e. which governs the stationary sOlutions q(t) of the usual fixed endpoint problem in the calculus of variations.
When applied to dynamics,
Hamilton's Principle yields Lagrange's equation for the motion of a single degree of freedom system in which the Lagrangian L is the difference between kinetic and potential energies.
In this example we will take L to
correspond to the motion of a plane pendulum whose length r(t) is a prescribed (but unspecified) function of time. We will compute and display the associated EUler-Lagrange equation. Note that in contrast to the previous example, where partial derivatives of a specific function were taken, here we require MACSYMA to keep track of functional dependencies, without ever specifying how r or q depend on t.
We begin by declaring r,q and q' to be functions of t:
(Cl) R:R(T); (Dl)
R(T)
(C2) Q:Q(T); (D2)
Q(T)
(C3) QDOT:QDOT(T); (D3)
QDOT(T)
(C4) L:M*(DIFF(R,T)**2+R**2*QDOT**2)/2+M*G*R*COS(Q); 3
2 2 d 2 M «-- (R(T») + QDOT (T) R (T» dT + G M R(T) COS(Q(T»
(D4) 2
(C5) DERIVABBREV:TRUE; TRUE
(D5) (C6) D4i 2
2
2
+ QDOT (T) R (T»
M «R(T)
T
(D6)
----------------------------- + G M R(T) COS(Q(T» 2
(C7) DIFF(DIFF(L,QDOT),T)-DIFF(L,Q)i 2
(D7) 2 M QDOT(T) R(T) R(T)
+ M R (T) QDOT(T) T
T
+ G M R(T) SIN(Q(T»
(C8) SUBST(DIFF(Q,T),QDOT,%)i 2
(D8) 2 M R(T) Q(T)
R(T) T
+ M R (T) Q(T)
T
T T + G M R(T) SIN(Q(T»
Comments: (C5) By setting DERIVABBREV:TRUE, all derivatives are displayed using subscript notation. (C7) The EUler-Lagrange equation.
Cf. (D4) and (D6).
MACSYMA knows the chain
rule. (C8) In order to replace QDOT in (D7) by dQ/dT, the SUBST() function is used.
SUBST(A,B,C) replaces B by A"in C.
Note
the use of % to refer to the immediately preceding result.
4
Example 3. First Order Linear O.D.E's In order to illustrate the evaluation of indefinite i~tegrals
in MACSYMA, we recall the formula for the general
s0lution of the equation dy + P(x) Y = Q(x), dx namely,
-f y ==
JP
p
e
dx
e
We begin by setting LOADPRINT:FALSE in order to suppress the file-loading messages: (Cl) LOADPRINT:FALSE$ (C2) P:TAN(X) i (D2)
TAN(X)
(C3) Q:SEC(X)i (D3)
SEC(X)
(C4) INTEGRATE ( P , X) i (D4)
LOG(SEC(X) )
(C5) %E**%i (D5)
SEC (X)
(C6) INTEGRATE(Q*%,X)i (D6)
TAN(X)
(C7) (C+%)/D5i TAN(X) + C (D7)
----------
SEC(X) (C8) TRIGSIMP(%) i (D8)
SIN(X) + C COS (X)
5
Comments: (C5) Note the two distinct uses of % in this command. MACSYMA represents the logarithmic base e by %E. The second % refers to D4. (C8) TRIGSIMP() replaces TAN, SEC etc. by their SIN and COS equivalents and simplifies the result. This example will now be reworked in order to illustrate how to define a function in MACSYMA: (Cl) LOADPRINT:FALSE$ (CZ) F(P,Q):=%E**INTEGRATE(-P,X)* (C+INTEGRATE(Q*%E**INTEGRATE(P,X),X»; (DZ) F(P, Q) ":= INTEGRATE(-P, X) %E
INTEGRATE(P, X) (c + INTEGRATE(Q %E
(C3) F(TAN(X),SEC(X»i TAN(X) + C (D3) SEC(X) (C4) F(2/X,X**Z)i 5 X
+ C
5
(D4) Z X
(C5) RATSIMP(%)i 5
X
+ 5 C
(D5) Z
5 X
6
,X»
(C6) EXPAND(%); 3 X
C
+
(D6)
5
2 X
Comments: (C2) A function is defined in MACSYMA by the := symbols. (C3) We repeat the preceding example. 2
(C4) Another example using P = 2/x and Q = x . (C5) We already encountered RATSIMP() in example 1. Another expression-simplifier is EXPAND().
Cf. (D5) and
(D6) . A third pass at this example will illustrate how input and output may be handled in a MACSYMA program: (C1) LOADPRINT:FALSE$ (C2) G():=(P:READ("ENTER p(X)"),Q:READ("ENTER Q(X)"), Y:%E**INTEGRATE(-P,X)* (C+INTEGRATE(Q*%E**INTEGRATE(P,X),X )), PRINT ( "GENERAL SOLUTION IS", Y) ) ; (D2) G() := (P : READ ( "ENTER p(X)"), Q : READ ( "ENTER Q(X)"), INTEGRATE(- P, X) Y:%E
INTEGRATE(P, X) (C + INTEGRATE ( Q %E
,X) ),
PRINT ( "GENERAL SOLUTION IS", Y))
7
(C3) G()$ ENTER P(X)
TAN(X)i ENTER Q(X)
SEC(X)i TAN (X) + C GENERAL SOLUTION IS ---------SEC(X) (C4) TRIGSIMP(%)i SIN (X) + C COS(X)
(D4)
Comments: (C2) G() is defined here as a function with no arguments, in contrast to F(P,Q) in the previous run.
The
general format for the right-hand side of a function is (sl,s2,s3, ..• ,sn) where each of the si's is a statement. Note use of READ() to input from the keyboard, and PRINT() to output messages and variable values. (C3) Typing G()$ or G()i calls the function G and is equivalent here to RUN in BASIC. (C4) The function G() returns the value of the last statement executed, here the PRINT statement, the value of which is the last item printed, here Y.
Hence the % in
(C4) produces the desired result. The fact is that this example was strictly pedagogical since MACSYMA already contains a sophisticated O.D.E. package called ODE2.
To use it, type ODE2(EQ,Y,X), where
EQ is a first or second order O.D.E. to be solved for Y as a function of X: (Cl) LOADPRINT:FALSE$ (C2) Y:Y(X)i (D2)
Y(X)
(C3) ODE2(DIFF(Y,X)+Y*TAN(X)=SEC(X),Y,X)i
TAN(X) + %C (D3)
Y(X) = ----------SEC(X)
(14) TRIGSIMP(%)i (~4)
Y(X) = SIN(X) + %C COS (X)
Comments: (C2) Here we declare Y to be a function of X so that DIFF(Y,X) in (C3) is interpreted as a functional derivative (and not zerQ). ~
Alternately we can use
'DIFF(Y,X) in (C3) and omit the Y:Y(X) statement.
The
quote in front of DIFF means "do not evaluate". (D3) ODE2 represents arbitrary constants by %C, %Kl or %K2. Example 4. Period of a Nonlinear Oscillator This example will illustrate the use of definite integrals, Taylor series, and trigonometric identities in MACSYMA.
The period of the nonlinear oscillator
x"
+
f(X) odd
f(X) = 0,
may be found by multiplying by x' and integrating with respect to t
("conservation of energy"): 2
X
x' g(x) = 0,
+
g(x) =
2
I
f(x) dx
A
where x'=O and x=A(=Amplitude) at t=O.
Then
dx dt
=
J- 2
g(x)
9
and the period T is given by the definite integral A
dx T
=
J v-
------------
4
2 g(x)
0
We begin by first considering the case of a linear oscillator:
(Cl) LOADPRINT:FALSE$ (C2) F:W**2*X; 2 (D2)
W X
(C3) G:INTEGRATE(F,X,A,X); 2
2 (D3)
2
X
W
A
(-- - --) 2
2
(C4) 4*INTEGRATE(I/SQRT(-2*G),X,0,A); Is
A
positive, negative, or zero?
P; 2 %PI (D4) ABS(W)
(C5) F:X**3; 3 (D5)
(C6)
X I
I
C3; 4
4
X
A
4
4
(D6)
10
(C7)
Is
"C4;
A
positive, negative, or zero? A
/ [
1 ------------- dX 4 4
I ]
4
/.
X
A
SQRT(--
0
- --)
4
4
(D7) SQRT(2) Comments:
(C3) The same function name INTEGRATE applies to
both indefinite and definite integrals.
In the case of
definite integrals, the third and fourth arguments represent the lower and upper limits of integration, respectively. (C4) Queries about the sign of a quantity may be answered with a single letter (P,N or z) to save typing. (D4) The period of the linear oscillator. (C5) Now we try a nonlinear oscillator with cubic restoring force. (C6) The quote-quote
I
I
in front of C3 means "reevaluate C3
using current variable values", in particular of F. (D7) When MACSYMA is unable to evaluate an integral, it displays the integral in this literal format. In fact, the last integral may be written as a complete elliptic integral of the first kind.
The cubic
3
oscillator with f(x)=x 4 K(
T
has period
1/ .[2)
7.416 •••
= ---------- = A
A
11
We may use MACSYMA to obtain an approximation to this period by expanding the integrand in (D7) above in a Taylor series about x = 0: (C1) LOADPRINT:FALSE$ (C2) F:X**3; 3
(D2)
X
(C3) G:INTEGRATE(F,X,A,X);
4
4
X
A
4
4
(D3) (C4) TAYLOR(1/SQRT(-2*G),X,0,8); 4
SQRT(2) (D4)/T/
8
SQRT(2) X
(3 SQRT(2»
X
+ ---------- + -------------- + . 2 A
.
10
6
8 A
2 A
(C5) 4*INTEGRATE(%,X,0,A); 137 SQRT(2) (D5) 30 A (C6) %,NUMER; 6.45824194 (D6) A
Comments: (C4) TAYLOR(A,B,C,D) expands expression A in a Taylor series in variable B about point B=C up to and including terms of degree D.
MACSYMA also supports Taylor
series in more than one independent variable.
See the
manual. (D4) The notation /T/ refers to the way in which Taylor series are internally represented in MACSYMA, and includes
12
information about the degree of truncation.
This becomes
relevant, for example, when two Taylor series with d{fferen~ degrees of truncation are added together.
The
s4ffiilled series will have a degree of truncation equal to the m~nimum
of the two, and hence some terms may be truncated
upon addition.
(C6) The command EXPRESSION,NUMER causes EXPRESSION to be converted to floating point format.
While exact arithmetic
(without roundoff errors) is MACSYMA's strength, floating point is useful for comparing two numbers. An entirely different approach to this same problem
will provide an introduction to trigonometric manipulations in MACSYMA.
We use the fact that the oscillator's behavior
is periodic and expand x(t) in a Fourier series, truncating the series after one term:
=
x(t)
A cos(wt) +
We substitute this expression into the differential equation of the oscillator and equate the coefficient of cos(wt) to zero (lithe method of harmonic balance"): (Cl) LOADPRINT:FALSE$
(C2) X:A*COS(W*T)i (D2)
A COS(T W)
(C3) DIFF(X,T,2)+X**3i 3
(D3)
A
3
,2
COS (T W) - A W COS(T W)
(C4) TRIGREDUCE(%)i 3
A
3
COS(3 T W)
(D4)
2
3 A
COS(T W)
- A W COS(T W) + 4
4
13
(C5) COEFF(%,COS(W*T»i 3 2
3 A -
(D5)
A W
4
(C6) SOLVE(%,W)i SQRT(3) A
[W
(D6)
SQRT(3) A
= - ---------,
W=
---------]
2
2
(C7) PART(%,2)i SQRT(3) A (D7)
W = --------2
(C8) 2*%PI/%i 2 %pI
4 %PI
W
SQRT(3) A
(D8) (C9) RHS(%),NUMERi 7.2551974 (D9) A
Comments:
(C4) TRIGREDUCE() uses trig identities to convert
products and powers of SIN and COS into a sum of terms, each of which contains only a single SIN or COS.
This
function is to be contrasted with TRIGEXPAND(), which expands a trig expression using the sum-of-angles formulas so that each SIN or COS has a simple argument. (C5) COEFF(A,B,C) extracts the coefficient of B raised to the C power in A.
For brevity, C may be omitted if it is
unity. (C6) SOLVE(A,B) solves the equation A for B, or if A is an expression, it solves A=O for B.
In either case it returns
a list of the sOlutions as in (D6). (C7) In order to specify which solution we want in the list returned by SOLVE, we use the PART() function. returns the Bth part of the expression A. 14
PART(A,B)
(C9) RHS() and LHS() return the right- and left-hand sides of an equation, respectively. ,
E~ample
5. Laplace Transforms
MACSYMA supports Laplace transforms and their inversions.
To illustrate the procedure, we consider the
following example:
Find x(t) and y(t), where
x' = - 3 x + Y + sin t -2t
y' =
x - 3 Y + e
subject to the initial conditions x(O)=I, y(O)=2. (Cl) LOADPRINT:FALSE$ (C2) DERIVABBREV:TRUE$ (C3) DIFF(X(T),T)=-3*X(T)+Y(T)+SIN(T)i (D3)
X(T)
= SIN(T) + Y(T) - 3 X(T) T
(C4) DIFF(Y(T),T)=X(T)-3*Y(T)+%E**(-Z*T)i - 2 T (D4)
Y(T)
= - 3 Y(T) + X(T) + %E T
(C5) LAPLACE(D3,T,S)i (D5) S LAPLACE(X(T), T, S) - X(O) = 1
LAPLACE(Y(T), T, S) - 3 LAPLACE(X(T), T, S) + 2
S
+ 1
15
(C6) SUBST(1,X(0),%); (D6) S LAPLACE(X(T), T, S)
1 = 1
LAPLACE(Y(T), T, S) - 3 LAPLACE(X(T), T, S) + 2
S
+ 1
(C7) LAPLACE(D4,T,S); (D7) S LAPLACE(Y(T), T, S) - yeO)
= 1
- 3 LAPLACE(Y(T), T, s) + LAPLACE(X(T), T, s) + S + 2
(C8) SUBST(2,Y(0),%); (D8) S LAPLACE(Y(T), T, s) - 2
= 1
- 3 LAPLACE(Y(T), T, s) + LAPLACE(X(T), T, S) + S + 2
(C9) SOLVE«(D6,D8],(LAPLACE(X(T),T,S),LAPLACE(Y(T),T,S)]); (D9) ((LAPLACE(X(T), T, s) = 4
S
2
3
+ 7 S
+ 13 S
+ 12 S + 17
-------------------------------------,
4
5
S
+ 8 S
2
3
+ 21 S
+ 24 S
+ 20 S + 16
432 2 S LAPLACE(Y(T),T,S)
+ 12 S
+ 19 S
+ 13 S + 19
= -------------------------------------]] 543 S
16
+ 8 S
+ 21 S
2 + 24 S
+ 20 S + 16
(C10) PART(%,l,l)i (D10) LAPLACE(X(T), T, S) = 432 S
+ 7 S
+ 13 S
+ 12 S + 17
543 2 S + 8 S + 21 S + 24 S + 20 S + 16 (C11) ILT(%,S,T)i - 2 T 27 SIN(T)
T %E
11 COS(T)
(D11) X(T) =
85
-
2 T
27 %E
+ --------- + ---------2 20
85
-
4 T
15 %E 68
(C12) PART(D9,1,2)i (D12) LAPLACE(Y(T), T, s) = 4
2 S
+ 12 S
5
S
2
3
+ 19 S 3
4
+ 8 S
+ 13 S + 19 2
+ 21 S
+ 24 S
+ 20 S + 16
(C13) ILT(%,S,T)i -
7 SIN(T) (~13)
YeT) = -------85
6 COS(T) 85
T %E
- 2 T
2 T
37 %E
+ --------- + ---------20 2
- 4 T 15 %E + ---------68
17
Comments: (C2) As in example 2, we display derivatives using subscript notation. (C3),(C4) The differential equations to be solved. (C5) Here we take the Laplace transform of the first equation.
LAPLACE(A,B,C) represents the transform of A
with respect to Busing C as parameter. (C6) We use the SUBST function tb apply the initial condition (cf. example 2, (C8)). (C7),(C8) Ditto for the second equation. (C9) The function SOLVE is used here to solve two simultaneous algebraic equations. (D9) SOLVE returns a list of solutions.
Here there is only
one element in the list, which is itself a list consisting of two elements, the transforms of x(t) and y(t). (CIO) The PART function is used to extract the individual transforms from the list (D9).
PART(A,B1,B2) first takes
the B1th part of A, then the B2th part of that. (Cl1) ILT(A,B,C) gives the inverse transform of A with respect to the parameter B in terms of the variable C. (D11) The solution for x(t). (C12)-(D13) Repeat of (C10)-(D11) for y(t). Example 6. Eigensolution of a System of O.D.E's In order to illustrate the various matrix manipulation functions in MACSYMA, we consider the following problem in electric circuits:
Find the general behavior of the system
shown. R
1
2
1
18
3 C
C
1
R
R
2
We set the current in the nth loop equal to the derivative of the associated charge, i
q'
=
n
n i
and choose the parameters as I =
R
=
R
1
C
3
=
C
=
1,
lip
2
2
1
=
R
giving the differential equations: q'
=
where
q =
mJ
A
=
A q
u
1
-2p 1
To solve this system we set At q(t) = u e
where u is an eigenvector to be found.
This gives the
eigenproblem
u = Au which we solve by requiring the determinant of A I to vanish, and so on. The general sOlution is obtained by taking a linear combination of the eigensolutions since the problem is linear. A
A
(Cl) LOADPRINT:FALSE$ (C2) A:ENTERMATRIX(3,3); Is the matrix
1. Diagonal
2. Symmetric
3. Antisymmetric
4. General Answer 1, 2, 3 or 4 4; Row 1 Column 1 : Row 1 Column 2:
-1;
Row 1 Column 3: Row 2 Column 1:
0;
1;
P;
Row 2 Column 2: Row 2 Column 3:
P;
Row 3 Column 1:
0;
-2*P;
19
Row 3 Column 2:
1;
Row 3 Column 3:
-1;
Matrix entered. [ -
1
1
]
0
]
[
P
[
(D2)
- 2 P
]
P
]
[ [
- 1 ]
1
0
(C3) A-;-L*IDENT(3); [ - L - 1
1
]
0
]
[
(D3)
[
P
]
P
- 2 P - L
[
]
[
- L - 1 ]
1
0
(C4) DETERMINANT(%); (D4) (- L - 1)
«-
L - 1) (- 2 P - L) - p) -
(- L - 1) P
(C5) SOLVE(%,L); (D5)
[L
=-
2 P - 1, L
=-
1, L
= 0]
(C6) FOR I:l THRU 3 DO L[I]:RHS(PART(%,I»;
DONE
(D6)
(C7) U:MATRIX([Ul],[U2],[U3]);
[ Ul ]
[
]
[ U2 ]
(D7)
[
]
[ U3 ] (C8) D3. U; [
U2 + (- L - 1) Ul
]
[
(DB)
[ P U3 + (- 2 P - L) U2 + P Ul ]
[ [
20
]
]
(- L - 1) U3 + U2
]
(C9) EQ1:D8[1,l]; (D9) (~10) EQ2:D8[2,l]i (D10)
U2 + (- L - 1) U1
P U3 + (- 2 P - L) U2 + P U1
!
(ell) SOLVE([EQ1,EQ2],[U2,U3])i 2
(L (2 P + 1) + P + L ) U1 (D11) [[U2
=
(L + 1) U1~ U3
= -------------------------]] P
(C12) %,U1:1; 2 L (2 P + 1) + P + L (D12)
[[U2
=L
+ 1, U3
= --------------------]] P
(C13) U,U1:1,%i
[
[ (D13)
[ [ [ [ [ [
] ] L + 1 ] ] 2 ] L (2 P + 1) + P + L ] -------------------- ] P ] 1
(C14) FOR I:1 THRU 3 DO (L:L[I], V[I]:EXPAND(EV(%»); (D14) DONE (CIS) Q:O; (DIS)
0
(C16) FOR I:1 THRU 3 DO Q:Q+K[I]*%E**(L[I]*T)*V[I]; (D16) DONE
21
(C17) Qi
(- 2 P - 1) T
[
[ K [
]
- T + K
%E
3 ]
2
1
]
+ K
%E
]
[
(DI7)
]
(- 2 P - 1)· T
[ [
[
]
P %E
- 2 K
K
]
1
3
]
[
(- 2 P - 1) T
[
[ K [
]
- T
%E
-
%E
K
2
1
]
+ K
3
J
(C18) EXPAND(DIFF(Q,T)-A.Q)i [ 0 ]
(018)
[
]
[ 0
J
[
]
[ 0 ]
Comments: (C2) ENTERMATRIX(N,M) offers a convenient method for entering an N x M matrix. (C3) IDENT(N) gives the N x N identity matrix. (C6) Our first example of a FOR-DO loop.
A more general
form is FOR A:B STEP C THRU D DO (sl,s2, ••. ,sn) where the si's are statements. array L[].
Here we store the eigenvalues in an
In MACSYMA arrays need not be dimensioned.
(C7) In contrast to ENTERMATRIX, an alternate method for entering a matrix is MATRIX([rowl],[row2], ••. ,[rowN]).
For
a column matrix each [rowi] consists of a single element. (C8) Matrix mUltiplication is represented by a period. (C12),(C13) Since an eigenvector is never unique, we arbitrarily choose the first component of U equal to unity. For example (C13) says, "evaluate U when Ul is 1 and when U2 and U3 are given by D12." (CI4) The eigenvector V[IJ is formed by first setting the eigenvalue L equal to L[I], and then evaluating the 22
expression D13 by using the EV() function.
EXPAND is
included here to clean up the algebra. (C15)-(C16) The general sOlution (D17) is formed by s~perimposing
the eigensolutions.
The arbitrary constants
ar~ represented by the array K[], the elements of which
have not been assigned any values, and which therefore appear as in (D17). (CI8) As a check on the computation, we substitute the final result back into the differential equation. Although this example has served the purpose of showing how matrices are handled in MACSYMA, the actual task of solving eigenvalue problems is most efficiently accomplished by using the MACSYMA EIGEN package.
Inthe
following example we will redo the above computation and check the result by diagonalizing A via a similarity transformation. (C1) LOADPRINT:FALSE$ (C2) LOADFILE(EIGEN,FASL,DSK,SHARE)i (D2)
DONE
(C3) A:MATRIX([-1,1,O],[P,-2*P,P],[O,1,-1]); [ - 1
1
0]
[
( D3 )
]
[
P
- 2 P
P
]
[
]
[0
1
-
1 ]
(C4) EIGENVECTORS(A); (D4) [[[- 2 P - 1, - 1, 0], [1, I, I]], [I, - 2 P, I],
[1, 0, - I], [I, I, 1]] (C5) M:ADDCOL(TRANSPOSE(PART(%,2)), TRANSPOSE(PART(%,3)), TRANSPOSE(PART(%,4))); [1
1
1]
[ ( D5 )
]
[ - 2 POI ]
[
r
] 1
-
1
1]
23
(C6) INVERT(M),DETOUT;
[
-
1
[ [ 2 P + 1
2
0
1
-
2 P
[ [
2 P
2 P
2
-
] ] 1 ] ] ]
---------------------------
(D6)
4 P + 2
(C7) FACTOR(EXPAND(%.A.M»; [ -
[ [ [ [
(D7)
0 0
0 ]
0
(2 P + 1) -
1
] 0 ] ] 0 ]
Comments: (C2) The procedure for loading the EIGEN package.
(C3) Here A is entered using the MATRIX function. Cf. previous run.
(C4) This single command obviates some 12 statements in the previous run. (D4) EIGENVECTORS() outputs a list of the eigenvalues and their mUltiplicities, followed by the corresponding eigenvectors.
(C5) The similarity transformation M is constructed by using TRANSPOSE to form column vectors out of the list of eigenvectors supplied by the EIGEN package, and then using ADDCOL to pack them together.
(C6) The appearance of DETOUT here keeps the determinant outside the inverse.
(C7) The similarity transformation diagonalizes the matrix A.
FACTOR and EXPAND are necessary to clean up the
algebra.
24
Exercise. Boundary Value Problem
Use MACSYMA to solve the following fourth order boundary value problem: 5 y' , " yeO)
- y'"
=
y'(O)
- y"
=
- y' - 2y = x
y(TI')
=
y'("Tr)
=
0
Since the equation is linear, look for the general sOlution as the superposition of a complementary and a particular solution, then fit the boundary conditions. Find the particular soiution by the method of undetermined coefficients.
25
Exercise solution.
Boundary Value Problem
(Cl) LOADPR1NT:FALSE$ (C2) L**4-L**3-L**2-L-2; 4
3
2
L - L - L - L - 2 (D2) (C3) SOLVE(%,L); (D3) [L = 2, L = - 1, L = - %1, L = %1]
(C4) Kl*%E**(2*X)+K2*%E**(-X)+K3*S1N(X)+K4*COS(X); 2 X - X (D4) K3 S1N(X) + K4 COS (X) + K1 %E + K2 %E (C5) Y:A+B*X+C*X**2+D*X**3+E*X**4+F*X**5; 543 2 (D5) F X + E X + D X + C X + B X + A (C6) EXPAND(D1FF(y,X,4)-D1FF(y,X,3)-D1FF(y,X,2)-D1FF(Y,X) -2*Y=X**5) ; 4
5
(D6) - 2 F X
- 60 F X
2
- 12 E X
- 20 F X
- 2 E X
- 5 F X
2
3
4
2
- 3 D X
3
3 -
4 E X
- 2 D X
2
- 2 C X
+ 120 F X - 24 E X 5
- 6 D X - 2 C X - 2 B X + 24 E - 6 D - 2 C - B-2 A = X (C7) FOR 1:0 THRU 5 DO (EQ[1]:COEFF(%,X,1), PR1NT(EQ[1]));
°
24 E - 6 D - 2 C - B-2 A = 120 F - 24 E - 6 D - 2 C - 2 B = - 60 F - 12 E - 3 D - 2 C = 0 - 20 F - 4 E - 2 D = - 5 F - 2 E 2 F = 1
=
°
°
°
(D7)
DONE (C8) SOLVE(MAKEL1ST(EQ[1],1,0,5),[A,B,C,D,E,F]); 255
225
15
5
5
(D8) [[A = ---, B = - ---, C = --, D = -, E = -, F 84424 26
1 -]] 2
(C9) Y,%; 5 X
4 5 X
2 15 X + ----4
+
+
(D9)
3 5 X
2
4
2
255
225 X -
-----
+
4
8
(CI0) Y:%+D4; 5 2 X (DI0) K3 SIN(X) + K4 COS(X) + Kl %E
x
X + K2 %E
2 4 5 X
2 15 X
3 5 X
225 X
+ ---- + ----- - ----- + 4 4 2
+ 4
255 8
(Cll ) Y,X:O; 255 K4 + K2 + Kl +
(Dl1 )
8
(CI2) Y,X:%PI; 5 2 %PI
- %PI (DI2) - K4 + %E
5 %PI
%PI
+ -----4
Kl -
K2 + %E
2 3
2
raP I
15 raP I + ------ + ------5
4
2
4
225 %PI
-
255
------- + 4
8
(CI3) DIFF(Y,X); 2 X (DI3) - K4 SIN(X) + K3 COS(X) + 2 Kl %E
- X - K2 %E
27
4 5 X
2 15 X
225 15 X 3 - --- ---- + 5 X + ----- + 2 4 2 2 (CI4)
%,X:O~
225 (DI4)
K3
-
K2 + 2 Kl
4
(CIS)
DI3,X:%PI~
4
- K3 - %E
(DIS)
2 %PI
'YePI
3
5 %PI Kl - ------ + 5 %PI 2
K2 + 2 %E
2 15 %PI
+ ------- + -----2 2
(CI6) SOLVE([Dll,DI2,DI4,DI5],[Kl,K2,K3,K4])~ (DI6) [[Kl = 5
2 %pI
+ 5 %pI
2
3
4
- 30 %PI
- 45 'YePI
+ 195 %PI + 195
---------------------------------------------------, 2 %PI 12 %E
+ 12
%PI 5 %PI 4 %PI K2 = (120 %E %PI + %PI %E - 5 %PI %E %PI %PI - 240 %E )/(3 %E + 3),
?A
225
15 %PI
4
3 o/oPI + 90 %E
K3 = (%PI (480 %E
3 o/oPI
(4 %E
- 4) - 285 %E
4 + o/oPI
o/oPI
3 %PI
2
2 %PI - 10) + 675 %E
- 30 %E
(- 20 %E %PI (90 %E
+ o/oPI
- 390)
3 %PI
5 + %PI
%PI
3 + 90) + o/oPI
3 %PI
(60 %E
2 o/oPI
+ 12 %E
3 %PI K4 =
-
(%PI (960 %E
+ o/oPI
+ o/oPI
(- 40 %E
2
%PI
2 o/oPI
- 30 %E
o/oPI
+ o/oPI
+ 4) - 1155 %E
3 %PI
- 765 %E
3 %PI
+ 12 %E
4
+ 390)
o/oPI
(8 %E
+ 10) + 765 %E
%PI
3 + %PI
+ 12),
%PI + 1350 %E
3 %PI
5
+ 60) - 675 %E
%PI
+ 12 %E
+ 285)/(12 %E
%PI
%PI
(- 60 %E
- 60)
%PI (- 90 %E 3 %PI
2 %PI
+ 24 %E
I( 24 %E
- 90) + 1155) %PI + 24 %E
+ 24)]]
(C17) Y:Y,%$
(C18)
I
'C6; 5
(D18)
X
5 = X
29
Comments: (C2) The characteristic equation, obtained by substituting y = exp(Lx) in the differential equation. (C4) The complementary solution to the homogeneous problem. (C5)-(C9) A particular solution is obtained by the method of undetermined coefficients. (DIO) The general sOlution. (CII)-(CI6) The boundary conditions are used to find the arbitrary constants KI, K2, K3, K4. (CI?) Plugging the values of the arbitrary constants into the general solution. (CI8) A check.
30
2 Housekeeping in MACSYMA
In this chapter we shall discuss some practical questions about how to work with MACSYMA.
None of this is
really part of computer algebra, but you have to know it to be able to use MACSYMA
~fficiently.
The topics which
follow are: disk files, special keys, and the editor. Disk Files There are several distinct schemes for saving disk files in MACSYMA, each with its own purpose: 1) To save function definitions or variable values, to be reloaded into a future MACSYMA. are SAVE and LOADFILE.
The associated commands
The disk file is in LISP format.
2) To make a literal copy of a MACSYMA session, to be read from hardcopy, but not to be reloaded into a future MACSYMA. The associated commands are WRITEFILE and CLOSEFILE. 3) To save an algebraic expression in a linear form which is compatible with languages like FORTRAN or BASIC. The associated command is STRINGOUT. Here are some examples of these processes: Example 1. SAVE and LOADFILE In the following run we assign values to the variables A and GO, and then SAVE them to disk: (CI) A:B+C*D; (DI)
C D + B
(C2) GO:"This is a test."; (D2)
This is a test.
(C3) SAVE([TEST,MAC],GO,A)i (D3) [[TEST, MAC,I, PS, F.RHR-RAND-RICHARD],I BLOCK, GO, A] 31
Note the syntax of the SAVE command.
The command
SAVE([A,B],ALL), will save the entire session in a file named A.B, so that you can return to it at a later time with all variables, including labels, unchanged. Let us have a look at the file TEST.MAC just created. We leave MACSYMA and return to command level in order to have the file displayed on the terminal: @TYPE (FILE) TEST.MAC.l
;;; -*-
Mode: LISP; Package: Macsyma;
-*-
Saved by
F.RHR-RAND-RICHARD (SETQ SAVENO 3020.) (DSKSETQ MGO (QUOTE
&This is a test.
»
(ADD2LNC (QUOTE MGO) $VALUES) (DSKSETQ $A (QUOTE «MPLUS SIMP) $B «MTIMES SIMP) $C
$D» » (ADD2LNC (QUOTE $A) $VALUES) Luckily we do not have to understand LISP to be able to use MACSYMA.
Let us load up a new MACSYMA and use the
LOADFILE command to access the file TEST. MAC.
We begin by
confirming that the variables GO and A are unassigned: (Cl) GO; (Dl)
GO
(C2) A; (D2)
A
(C3) LOADFILE(TEST,MAC); TEST MAC 1 PS F.RHR-RAND-RICHARD being loaded Loading done (D3)
DONE
(C4) GO; (D4)
This is a test.
(C5) A; (D5)
C D + B
Note that the syntax of the LOADFILE command is not quite the same as that of the SAVE command.
In particular,
the SAVE command requires square brackets [] around the file specification. Let us delve a little deeper into the question of file specification.
While the file specification for SAVE may
be simply taken as [FILENAME, FILETYPE] (as in the first run of this example), the full file specification is of the form [FILENAME, FILETYPE, DSK, "F.RHR-RAND-RICHARD"]. Omitting the last two arguments causes them to be automatically assigned to the last directory visited. (They are initially assigned to your own directory.)
This
can cause a problem if you have loaded a file from another directory, e.g. SHARE.
Here is an example:
(Cl) LOADFILE(EIGEN,FASL,DSK,SHARE); EIGEN FASL 3 PS SHARE being loaded Loading done DONE
(Dl)
(C2) GO:"This is a test."; (D2)
This is a test.
(C3) SAVE([TEST,MAC],GO); [!SAVEI, OUTPUT, *, DSK, SHARE]
DIRECTORY ACCESS
PRIVILEGES REQUIRED
(C4) SAVE([TEST,MAC,DSK,"F.RHR-RAND-RICHARD"],GO); (D4) [[TEST, MAC, 2, PS, F.RHR-RAND-RICHARD], 1 BLOCK, GO] Comments: (Cl) The EIGEN package referred to in example 6, chapter 1.
(C3) SAVE([TEST,MAC],GO) doesn't work since MACSYMA has replaced the user's directory with SHARE as the default.
(C4) SAVE([TEST,MAC,DSK,"F.RHR-RAND-RICHARD"],GO) reassigns the default directory to be the user's.
33
An alternate sOlution is to set the variable CHANGE_FILEDEFAULTS to FALSE, whereupon the default directory doesn't change when a new directory is visited: (Cl) CHANGE_FILEDEFAULTSi (Dl)
TRUE
(C2) CHANGE_FILEDEFAULTS:FALSEi (D2)
FALSE
(C3) LOADFILE(EIGEN,FASL,DSK,SHARE)i EIGEN FASL 3 PS SHARE being loaded Loading done DONE
(D3) (C4) GO:"This is a test."i
This is a test.
(D4)
(C5) SAVE([TEST,MAC],GO)i (D5) [[TEST, MAC, 3, PS, F.RHR-RAND-RICHARD], 1 BLOCK, GO] Comments: (Cl) CHANGE_FILEDEFAULTS is initially TRUE. (C5) This time SAVE([TEST,MAC],GO) works, cf. (C3) in. the previous run. Example 2. WRITEFILE and CLOSEFILE In this run we start the MACSYMA session with a WRITEFILE command, then make the same assignments as in the previous example.
The CLOSEFILE command dumps a record of
the session to disk: (Cl) WRITEFILE()i (Dl)
[DSK, F.RHR-RAND-RICHARD]
(C2) A:B+C*Di (D2)
C D + B
(C3) GO:"This is a test."i (D3)
This is a test.
(C4) CLOSEFILE(TEST,TXT)i (D4)
34
[TEST, TXT, 1, PS, F.RHR-RAND-RICHARD]
Note the syntax of the WRITEFILE and CLOSEFILE commands.
The resulting file was named TEST. TXT in the
CLOSEFILE command.
Now we return to command level in order
tq display the file TEST.TXT on the terminal: @TYPE (FILE) TEST.TXT.l (Dl)
[DSK, F.RHR-RAND-RICHARD]
(C2) A:B+C*D; (D2)
C D + B
(C3) GO:"This is a test."; (D3)
This is a test.
(C4) CLOSEFILE(TEST,TXT);
A convenient procedure for recording a session which you chose not to begin with a WRITEFILE command is to give the WRITEFILE command at any time, followed by the command PLAYBACK().
This will cause the entire session up to the
current line to be displayed on the screen, and can be written to disk by giving a CLOSEFILE command. Example 3. STRINGOUT This time we want to dump variable valu~s to disk in a format which could be merged into a FORTRAN or BASIC program for further processing: (Cl) A:B+C*D;
C D + B
(Dl) (C2) EXPAND(%**2);
222 (D2)
C
D
+ 2 BCD + B
(C3) STRINGOUT([TESTl,TXT],A,D2); (D3)
[TESTl, TXT, 1, PS, F.RHR-RAND-RICHARD] Note the syntax of STRINGOUT, which is similar to that
of SAVE.
Let's see what the file TESTl.TXT looks like:
35
@TYPE (FILE) TESTl.TXT.l
C*D+B; ~*DA2+2*B*C*D+BA2;
These expressions could, with a minimum Of alteration performed using a word processor (outside of MACSYMA), be tailored to fit into a FORTRAN or BASIC program.
They
could also be incorporated into a MACSYMA BATCH program. (See the appendix for an example of a BATCH program.) Special Keys
As the reader by now knows, the symbols ; and $ are used to signal to MACSYMA that the user has finished typing a line to be entered.
The "return" key, usually used for
this purpose, has no effect in MACSYMA.
Thus the return
key can be pressed at anytime when entering data, without effect.
This is particularly useful if one has to enter a
long expression of, say, half a dozen terms, for then the return key can be pressed after each term is typed, resulting in a neat and easy to read input display. If you make a mistake while typing some input, pressing the "delete" or "rubout" key will cancel the last character typed.
We have found that on most video
terminals this has the effect of displaying the cancelled character twice, as if the video terminal was a hardcopy terminal which could not backspace. display to be rather confusing.
This causes the
However, pressing the
control key and the K key simultaneously (control K for short) causes the current input line to be redisplayed in its current state (i.e. including erasures). too messed up, you can begin
th~
If things get
current input line afresh
by typing a double question mark ?? . A very important special key is control G, which causes interruption of the current computation and returns you to the next input line (C label). 36
This is useful both
if the computation is taking too long, as well as if the displayed output is much longer than you anticipated. If a line which you entered contains a _grammatical error, MACSYMA will print an error message and tell you to fix things up.
One approach to doing so is to press
control Y, which "yanks" the previous input line down to the current input line, whereupon the delete key may be used to correct the trouble.
Here's an example:
(Cl) A:B+(C*Di Missing ")"
A : B + ( C * D **$** Please rephrase or edit.
(Cl) "Y (Cl) A:B+(C*D)i C D + B
(Dl)
Note that control Y was pressed after the "Please rephrase or edit" message.
This caused the original input
line to be reprinted, whereupon the necessary right parenthesis was added. Another approach to fixing up an input line is to press the "escape" key, which causes MACSYMA to enter its editor, the operation of which will be discussed next.
37
The Editor
The editor permits you to change one (possibly very long) line at a time.
This might be a function definition
or the value of a "variable or label.
We shall refer to the
characters in the line currently being edited as residing in the editor's "buffer". Upon pressing escape and entering the editor, the contents of the current input line are placed in the buffer.
(If no key besides escape was pressed since the
last output statement (D label or "Please rephrase or edit" message), the bUffer will contain the previous input line.) In order to place a previously defined function or variable in the bUffer, type MYFNAME (esc)(esc) or MYVNAME (esc)(esc), respectively, where NAME is the name of the function or variable to be edited, and (esc) stands for the escape key. Now let's suppose you have the line you want to change in the buffer.
Changes of the form of deletions or
insertions are made relative to the position of a cursor, displayed on the terminal as a $.
Before considering
commands which change characters in the bUffer, we will discuss how to move the cursor around. T moves the cursor to the Top (i.e. the beginning) of the bUffer, while B moves it to the Bottom (Le. the end) of the buffer. C moves it one Character forward, while R moves it one character in the Reverse direction. Similarly, 5C moves the cursor 5 characters forward and 7R moves it 7 characters in reverse. You can also move the cursor to a position based on searching for a particular character string. S •.• (esc) moves the cursor immediately to the right of the first string .•. to be found after the current cursor position.
Similarly 3S .•• (esc) Searches for the third
occurrence of ... after the current cursor position. 38
Once you have the cursor positioned where you want it;
you may delete the character immediately to its right by pressing D.
Similarly 4D Deletes the next 4 characters.
K deletes (i.e. KillS) the portion of the buffer to the right of the cursor, up to the next occurrence of a "return".
If there are no returns, it kills everything to
the right of the cursor. To insert a string of characters immediately after the cursor, type I ... (esc), where .•• represents the string to be Inserted. FR ... (esc) ... (esc) Finds and Replaces the first occurrence after the cursor of the first ... string with the second ... string.
Similarly 4FR ... (esc) ... (esc) does
4 such find-and-replaces. A record of a single passage through the editor looks like a conversation between the user and MACSYMA, with the user supplying lines of instructions to change the bUffer, and with MACSYMA displaying the updated buffer. Each line of instructions by the user is terminated by (esc)(esc).
Note that the edit commands in a given line of
instructions are not performed until (esc)(esc) is pressed. This permits a limited amount of editing of the edit instructions using the rubout (or delete) key, control K, and ?? In order to exit from the editor and return to the usual MACSYMA input mode, type (esc)(esc) with an otherwise empty line of instructions.
The current contents of the
buffer will then be printed next to the latest C label, to be entered with ; or $ at your discretion. If one types Q (esc)(esc) at the end of a line of instructions, the editor is exited without printing the buffer next to the C label.
Note however that the buffer
has been placed in the input stream, and pressing ; or $ will have its usual effect.
39
Here is an example of the editing process:
(Cl) ABCDEFXXXJKLMNOXXXSTUVXXXZXXXi (Dl) (C2)
ABCDEFXXXJKLMNOXXXSTUVXXXZXXX
DUMMYi
(D2)
DUMMY
(C3) $ In editor:
(Type just
$DUMMY MYVD1$$ $ABCDEFXXXJKLMNOXXXSTUVXXXZXXX SX$R3DIGHI$$ ABCDEFGHI$JKLMNOXXXSTUVXXXZXXX FRXXX$PQR$$ ABCDEFGHIJKLMNOPQR$STUVXXXZXXX 4CIWXYZ$K$$ ABCDEFGHIJKLMNOPQRSTUVWXYZ$ $$ (C3) ABCDEFGHIJKLMNOPQRSTUVWXYZi (D3)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
As an exercise, see if you can figure out exactly how each line of edit instructions works-. Note that the $ i p used both for the cursor position in the buffer displays, as well as to represent the escape key in the lines of instructions. The MACSYMA manual has a complete discussion of the editor, including various features not discussed here.
40
3 Programming in MACSYMA
MACSYMA, and LISP, the language in which MACSYMA is written, are functional languages.
That is, a MACSYMA
program consists of a number of functions, and possibly variables, which have been designed to accomplish a particular task.
They are generally SAVE'd into a disk
file and loaded into the computer with a LOADFILE command. (Alternately the program can be written outside MACSYMA using a word processor, and then BATCH'ed.
The
difficulty with this approach is that debugging requires jumping from MACSYMA to the word processor, and back. BATCHing is chiefly useful for demonstration programs in which it is desirable for the user to see the commands as they would be entered from the keyboard.
See the appendix
for a sample BATCH program.) Along with each file of SAVE'd functions one should also include either directions to the user, and/or a reminder to yourself as to the purpose of the functions in the file.
Such information can be assigned as a string to
a variable which has a common name in all of your program files.
I use the variable GO because it is short and
suggests the beginning of a session.
For example,
GO:"This program does such and such a job." will cause the message, "This program does such and such a job." to be displayed when the user enters GO. After a program has been loaded into a MACSYMA session from a disk file (using the LOADFILE -command), one may examine all user defined functions by entering DISPFUN(ALL) (i.e., display all functions), a command equivalent to LIST in BASIC.
Alternately, entering FUNCTIONS will give the
names of the user defined functions, without their definitior. 41
Example 1. Taylor Series Solutions of O.D.E's
The first order O.D.E. y' = f(x,y) with the initial condition y = z, x
=
0
can be formally solved by the Taylor series 2
y(x) = y(O) + y'(O) x + y"
(0) x
/ 2 + •..
The first two coefficients follow directly from the initial condition and the differential equation:
=z
y(O)
y' (0) = f(O,z) The value of y' '(0) may be found by differentiating y'(x)
=
f(x,y) and evaluating the result at the initial
condition:
of
of y' , (x)
=
+ "ax
y'
"Of +
'ax
f "ay
~f
= ---(O,z) l)x
=
~y
'Of y' , (0)
'Of
+ ---(O,z) f(O,z) -ay
By differentiating in turn the derived expression for y' '(x), we can find y"
'(x) and the coefficient y'
and so on for higher derivatives.
I
I
(0),
All the coefficients so
found will depend only upon the given function f(x,y) and the initial value z. 42
This computation is quite repetitive and thus well suited for treatment by computer algebra.
Here is a sample
run of a program which automates this procedure, to be followed by a discussion of how it works: (C1) LOADPRINT:FALSE$ (C2) LOADFILE(ODE1,MAC); (D2)
DONE
(C3) GO; (D3) This program generates the Taylor series sOlution to the 1st order ODE: Y' = F(X,y) for an arbitrary initial condition at X = O. To call it, type: SOLUTION ( ) (C4) SOLUTION()$ ENTER DEGREE OF TRUNCATION 8; ENTER RIGHT HAND SIDE OF ODE Y'=F(X,Y) Y; Y' = Y
ENTER INITIAL VALUE OF Y 1; 7
8 X
40320
+ 5040
X
120
X
X
+ 24
2
3
X
+
+ 720
4
5
X
X
Y = ----- + /
6
+ X + 1
+ 6
2
43
(C5) SOLUTION()$ ENTER DEGREE OF TRUNCATION 5; ENTER RIGHT HAND SIDE OF ODE Y'=F(X,Y) SIN(X*Y)+COS(X*Y); Y' = SIN(X Y) + COS(X Y) ENTER INITIAL VALUE OF Y
1; 4
5
31 X
Y
120
X
X
= - ----- -
2
3
X + X + 1
+ -- +
6
6
2
Comments: (C2) The program has been SAVE'd in a file named ODEl.MAC. (C3) The variable GO, which was SAVE'd along with the program functions, contains user instructions. (C4) A test case: The problem Y' = Y, Y(O) = 1 has the solution Y(X) = %E**X, the Taylor series of which is correctly computed. (C5) A sample equation with an unknown sOlution. In order to see how this program works, we will continue this run with the commands FUNCTIONS and DISPFUN(ALL): (C6) FUNCTIONS; (D6) [SOLUTION(), INPUT(), DERIV(G), EVALUATE(G), OUTPUT()] (C7) DISPFUN(ALL);
44
z, v
(E7) SOLUTION() := (INPUT(), U
F, I
EVALUATE (V) X FOR I THRU M DO (U
U + --------------, V
DERIV(V» ,
I!
OUTPUT ( ) ) (E8) INPUT() := (M : READ("ENT!:R DEGREE OF TRUNCATION"), F : READ(IIENTER RIGHT HAND SIDE OF ODE Y'=F(X,Y)"), PRINT (" "), PRINT ( "Y' =", F), PRINT (II "), Z : READ ( II ENTER INITIAL VALUE OF Y"»
DERIV(G)
(E9)
:= DIFF(G, X) + DIFF(G, Y) F
EVALUATE (G) := EV(G, X : 0, Y : Z)
(E10)
OUT~=-PRINT(IIY
(Ell )
=", U)
DONE
(Dll)
As, the command FUNCTIONS shows, this program consists of five functions.
In fact, it was SAVE'd to disk with the
command: SAVE([ODE1,MAC],GO,SOLUTION,INPUT,DERIV,EVALUATE,OUTPUT)J The main function SOLUTION works as follows:
First
the function INPUT is called, which cues the user to enter from the keyboard the degree of truncation M, the function F which defines the O.D.E., and the initial condition Z. Then the variables U and V are equated to Z and F respectively.
U is used to hold the progressively updated
Taylor series sOlution of the O.D.E., while V represents the currently computed derivative of Y, expressed in terms 45
of X and Y via the function F.
A FOR-DO loop EVALUATES the
latest value of V at the initial condition, adds the corresponding term to U, and updates V by taking its total DERIVative with respect to X (and using the chain rule). Finally the function OUTPUT displays the resulting function U on the terminal. This program may be easily extended to cover the case of an Nth order O.D.E. with appropriate initial values. The Ith derivative of Y is represented by the SUbscripted variable Y[I].
Here is a sample run:
(Cl) LOADPRINT:FALSE$ (C2) LOADFILE(ODEN,MAC)1 (D2)
DONE
(C3) G01 (D3) This program generates the Taylor series sOlution to the Nth order ODE: (N)
Y
:
F ( X, Y, Y', Y' "
... ,
for arbitrary initial conditions at X : To call it, type: SOLUTION() (C4) SOLUTION()$ ENTER ORDER OF D.E. ENTER DEGREE OF TRUNCATION 7; ENTER RIGHT HAND SIDE OF ODE. REPRESENT Y BY Y[O], Y' BY Y[l], ETC. -Y[O];
- Y
o
46
o.
(N-l)
Y
a
ENTER INITIAL VALUE OF Y[
]
A; ENTER INITIAL VALUE OF Y[ 1 ] B;
7 B X
AX -
Y = 5040
4
5
6
B X
AX
120
+ ---- 24
---- + 720
2
3
B X
AX -
6
---- + B X + A
2
(C5) SOLUTION()$ ENTER ORDER OF D.E.
4; ENTER DEGREE OF TRUNCATION 8; ENTER RIGHT HAND SIDE OF ODE. REPRESENT Y BY Y[O], Y' BY Y[l], ETC.
X; X ENTER INITIAL VALUE OF Y[ 0 ] A;
\
ENTER INITIAL VALUE OF y[ 1 ]
B; ENTER INITIAL VALUE OF y[ 2
l-
C; ENTER INITIAL VALUE OF y[ 3 ] D; 5 DX
X
+
Y =
120
2
3
C X
+ B X + A
+ 6
2
47
(C6) SOLUTION()$ ENTER ORDER OF D.E. 2J
ENTER DEGREE OF TRUNCATION 7J
ENTER RIGHT HAND SIDE OF ODE. REPRESENT Y BY Y[O], Y' BY Y[l], ETC. (2*X*Y[1]-P*(P+1)*Y[0])/(1-X**2);
2 Y X - Y P (P + 1) 1
0
2 1 - X ENTER INITIAL VALUE OF y[ 0 ] 1J
ENTER INITIAL VALUE OF Y[ 1 ] OJ
Y = (- P (p + 1) (- P (P + 1) (2 - P (P + 1» + 2 (2 - P (P + 1»
- 10 P (P + 1) + 20)
- P (P + 1) (2 (2 - P (P + 1»
- 6 P (P + 1) + 20)
- 2 P (P + 1) (2 - P (P + 1» 6 - P (P + 1) (20 - 2 P (P + 1»
- 48 P (p + 1»
X /720
4 (- P (P + 1) (2 - P (P + 1»
- 4 P (P + 1»
X
+ ---------------------------------------------24
2
P (P + 1) X + J
2 48
(C7) RATSIMP(%) ; 6 (D7) -
5
4
+ 3 P
«P
- 23 P
4 + (- 30 P 2
+ (360 P
- 51 P
3 - 60 P
3
2 + 94 P
2 + 150 P
6 + 120 p) X
4 + 180 p) X
2 + 360 P) X
- 720)/720
(C8) %,P:2; 2
2160 X
- 720
(D8) 720 (C9) EXPAND(%); 2
(D9)
1 - 3 X
Comments: (C2) The program is kept in a file named ODEN.MAC. (C4) A test case: The simple harmonic oscillator Y"
+ Y =0
has the sOlution Y = A*COS(X) + B*SIN(X), the Taylor series of which is correctly computed. (C5) A simple fourth order equation, to check the program's ability to deal with higher order O.D.E. 's. (C6) Legendre's Equation. (C7) The algebra of the program's output is manually cleaned up using RATSIMP. (C8),(C9) By setting P=2 we should pick up a sOlution proportional to the Legendre polynomial 2
P (X) = ( 3 X - I ) /
2 ,
2
and we do.
49
In order to understand how this program differs from ODEl.MAC, we continue the run with a DISPFUN(ALL) command: (CI0) DISPFUN(ALL)i (EI0) SOLUTION() := (INPUT(), STEPl(), STEP2(), V
F,
I
EVALUATE(V) X FOR I FROM N THRU M DO (U
U + --------------,
I!
V : DERIV(V», OUTPUT(» (Ell) INPUT() M
:=
(N : READ(IIENTER ORDER OF D.E."),
READ("ENTER DEGREE OF TRUNCATION"),
F : READ("ENTER RIGHT HAND SIDE OF ODE. REPRESENT Y BY Y[O], Y' BY Y[I], ETC.II), PRINT(" II), PRINT(F), PRINT(" II), FOR I FROM DO Z
a
THRU N - 1
: READ("ENTER INITIAL VALUE OF Y[II, I, 11]11»)
I I Z
X
I
(EI2)
STEPl () := U
a,
SUM(-----, I,
N - 1)
I!
(E13) STEP2() := INITIAL
MAKELIST( [Y
= Z ], I, I
50
I
a,
N -
1)
(E14) DERIV(G) :: DIFF(G, X) + SUM(DIFF(G, Y ) Y I
, I,
1+1
0, N - 2) + DIFF(G, Y N -
(E15)
EVALUATE(G) :: EV(G, X :
a,
) F 1
INITIAL)
(E16)
OUTPUT() ::= PRINT ( "Y :=", U)
(D16)
DONE Once again the main function SOLUTION begins by
calling a function named INPUT which this time reads. from the keyboard the order N of the D.E., the degree M of truncation, the function F and the initial values of Y[O], Y[l],
.•• , Y[N-l].
Then a function named STEPI uses the
initial conditions to generate the first N terms of the Taylor series sOlution U.
Next STEP2 prepares a list of
the initial conditions (using the MACSYMA command MAKELIST), to be stored in the variable INITIAL, and to be used in the function EVALUATE.
The rest of the main
function SOLUTION is quite similar to that of ODEl.MAC. Note that the chain rule in DERIV now involves partial derivatives with respect to X, Y[O], Y[l],
••. , Y[N-l.].
51
Example 2. Lagrange's Equations with N Degrees of Freedom
The motion of a conservative mechanical system with N degrees of freedom is governed by N second order ordinary differential equations which may be written in the form: d
~L
'aL
= dt
1q'
= 1,2,3, ••• ,N
")q
i where L
i
0,
i
= L(q
, q' i
, t) is called the Lagrangian, and
i
equals the difference between the kinetic and potential energies of the system. In a system with many degrees of freedom, the job of deriving Lagrange's equations, given the Lagrangian, is difficult to do correctly by hand.
Recent applications
include the new field of robotics, in which a robot might be modeled as a system of several connected rigid bodies. This is an ideal job for computer algebra.
In example
2 of chapter 1 we presented a MACSYMA derivation of Lagrange's equation in a one degree of freedom system. Here we present a program which extends the computation to any number of degrees of freedom: (Cl) LOADPRINT:FALSE$ (C2) DERIVABBREV:TRUE$ (C3) LOADFILE(LAGRANGE,MAC);
(D3)
DONE
(C4) GO;
(D4) This program computes
La~range's
N degreee of freedom system. LAGRANGE ( )
52
equations for an
To call it, type:
(C5) LAGRANGE()$ ENTER THE NUMBER OF DEGREES OF FREEDOM 1;
ENTER THE LAGRANGIAN: USE Q[I] FOR THE ITH GENERALIZED COORDINATE AND V[I] FOR THE ITH GENERALIZED VELOCITY M*(DIFF(R(T),T)**2+R(T)**2*V[1]**2)/2+M*G*R(T)*COS(Q[1]); 2
M «R(T) )
2 2 + V (T) R (T» 1
T
L = -------------------------- + G M R(T) COS(Q (T» 2 1 EQ[ 1 ] : 2
2 M R(T) Q (T)
R(T)
1 T
+ M R (T) Q (T) T 1 T T
+ G M R(T) SIN(Q (T»
= 0
1
(C6) LAGRANGE()$ ENTER THE NUMBER OF DEGREES OF FREEDOM 2; ENTER THE LAGRANGIAN: USE Q[I] FOR THE ITH GENERALIZED COORDINATE AND V[I] FOR THE ITH GENERALIZED VELOCITY «Ml+M2)*Ll**2*V[1]**2)/2 +(M2*L2**2*V[2]**2)/2 +M2*Ll*L2*V[1]*V[2]*COS(Q[2]-Q[1]) +(Ml+M2)*G*Ll*COS(Q[1]) +M2*G*L2*COS(Q[2]);
53
L = Ll L2 M2 V (T)
v
1
(T) COS(Q (T) - Q (T»
2
2
+ G L2 M2 COS(Q (T»
1
+ G Ll (M2 + Ml) COS(Q (T»
2 2
1
2
L2
2
M2 V (T)
2
(M2 + Ml) V (T)
Ll
1
2
+ ------------ + ------------------2 2 EQ[ 1 ] 2 - Ll L2 M2 (Q (T)
SIN(Q (T) - Q (T» 2 1
2 T + Ll L2 M2 Q (T)
COS(Q (T) - Q (T» 2 1
2 T T 2
+ (Ll
2
M2 + Ll
Ml)
(T)
Q
1 T T
+ (G Ll M2 + G Ll Ml) SIN(Q (T» 1
54
= 0
EQ( 2 ] : 2
Ll L2 M2 (Q (T) )
SIN(Q (T) - Q (T»
2
1
1
T
2 + Ll L2 M2 Q (T)
COS(Q (T) - Q (T»
2
1
+ L2
M2 Q (T)
1
2
T T + G L2 M2 SIN(Q (T»
T T
=a
2
Comments: (C5) Example 2 of chapter 1, presented here as a check on the program. (C6) The double pendulum. In order to examine the functions which make this program work, we continue the run with a DISPFUN(ALL) command: (C7) DISPFUN(ALL); (E7) LAGRANGE() := (INPUTl(), SETUP(), INPUT2(), FOR I THRU N DO EQ
DIFF(DIFF(L, V ), T) - DIFF(L, Q ), I
I
I
SUB(), OUTPUT(» (E8) INPUTl() := N : READ("ENTER THE NUMBER OF DEGREES OF FREEDOM")
(E9) SETUP() := (REMARRAY(Q, V), FOR I THRU N DO (Q
(T), V
Q
I
I
I
V (T») I
55
(EIO) INPUT2() := (L : READ("ENTER THE LAGRANGIAN: USE Q[I] FOR THE ITH GENERALIZED COORDINATE AND V[I] FOR THE ITH GENERALIZED VELOCITY"), PRINT(" II), PRINT("L
=", L), PRINT("
II))
(Ell) SUB() := FOR J THRU N DO (FOR I THRU N T), V , EQ ))
SUBST(DIFF(Q
DO EQ J
I
I
J
(E12) OUTPUT() := FOR I THRU N DO (PRINT("EQ[", I, II] :"), PRINT(RATSIMP(EQ ), "= a"), I
PRINT(" II)) DONE
(D12)
The function LAGRANGE first calls INPUTl, which reads the number of degrees of freedom N from the keyboard.
Then
the function SETUP uses the MACSYMA command REMARRAY to wipe clean the arrays Q and V, which are used to hold the N generalized coordinates and velocities respectively.
SETUP
then goes through a FOR-DO loop in order to declare the arrays Q and V to depend on time T, so that MACSYMA interprets their derivatives functionally (otherwise dQ[I]/dt and dV[I]/dt would simply be zero). Next INPUT2 reads the Lagrangian L from the keyboard, and LAGRANGE computes the Ith Lagrange equation and stores it in the array EQ.
Finally dQ[I]/dt is SUBstituted for
V[I] and the results are displayed by OUTPUT.
56
Example 3. Hamilton's Equations
Hamilton's equations of motion, like Lagrange's equations, govern the motion of an N degree of freedom conservative mechanical system (Goldstein, 1980).
In
contrast to Lagrange's equations, however, Hamilton's equations offer the following features: i) A generalized coordinate which does not appear in the Lagrangian (although the corresponding generalized velocity is present) is called a cyclic or ignorable coordinate.
Hamilton's equations permit automatic removal
of cyclic coordinates from the equations of motion, a step that must be accomplished by the user in the case of Lagrange's equations. ii) In an autonomous system, the Hamiltonian is conserved, resulting in an immediate first integral.
The
corresponding first integral in the Lagrangian formulation must be derived by the user. iii) Hamilton's equations take the form of 2N first order O.D.E's, while Lagrange's equations consist of N second order O.D.E's. The procedure for computing Hamilton's equations, given the Lagrangian L, may be outlined as follows: Step 1.
Define the "generalized momentum" p
by i
'3L
=
P
i
,
(q "aq' i
j
q" , t) ,
i
=
1,2, ••• ,N
j
and solve these N equations for q' i
= q' (q i
, p, t). j
j
57
Step 2.
Define the Hamiltonian H by N
H = - L +
~
P
i=l
q'
i
i
in which q' is replaced by q'(q , P , t), so that j
i
i
j
H = H(q , p , t) i
Step 3.
i
Compute Hamilton's equations: ~H
~H
q' = i
p' = i
~p
i
-
---~q
i
We shall next present a MACSYMA program which automates this process.
The user enters the Lagrangian in
the same format as in the previous example, LAGRANGE. MAC, and the computer outputs the definitions of the generalized momenta, the Hamiltonian H, and Hamilton's equations: (Cl) LOADPRINT:FALSE$ (C2) LOADFILE(HAMILTON,MAC)i (D2)
DONE
(C3) GO; (D3) This program computes Hamilton's equations for an N degree of freedom system. HAMILTON()
58
To call it, type:
(C4) HAMILTON()$ ENTER THE NUMBER OF DEGREES OF FREEDOM , ENTER THE LAGRANGIAN: USE Q[I] FOR THE ITH GENERALIZED COORDINATE AND V[I] FOR THE ITH GENERALIZED VELOCITY M*(DIFF(R(T),T)**2+R(T)**2*V[1]**2)/2+M*G*R(T)*COS(Q[1]); d
M
2
2
«--
+ V
(R(T)))
2 R (T))
1
dT
L = --------------------------- + COS(Q ) G M R(T) 2 1 GENERALIZED MOMENTA: 2
=V
P
1
M R (T)
1 2
2
d
2
M R (T) (-- (R(T)))
232 + 2 COS(Q ) G M R (T) - P 1
dT
1
H = - ------------------------------------------------2 2 M R (T)
P
1
QDOT[ 1 ]
= 2 M R (T)
PDOT[ 1 ] - - SIN(Q ) G M R(T) 1
59
(C5) HAMILTON()$ ENTER THE NUMBER OF DEGREES OF FREEDOM 2~
ENTER THE LAGRANGIAN: USE Q[I] FOR THE ITH GENERALIZED COORDINATE AND V[I] FOR THE ITH GENERALIZED VELOCITY V[1]**2/2+V[2]**2/2 +W*(Q[1]*V[2]-Q[2]*V[1]) +W**2*(Q[1]**2+Q[2]**2)/2 -POTENTIAL(Q[1],Q[2])~
2
2 2 + Q ) W
2
1
(Q
L = ------------ + (Q 2
V
V 1
2
,
Q ) W - POTENTIAL(Q
1
1
2
Q ) 2
2 V 2 +
2 V 1 +
2
2
2
2
GENERALIZED MOMENTA: = V
P 1
1 = Q
P 2
Q
W
2 W+ V 2
1
(2 P
Q 1
2
- 2 Q
P ) W + 2 POTENTIAL(Q , Q ) + P + P 1 2 1 2 2 1
H = 2
QDOT[ 1 ]
=
Q
W+ P
2
1 d
PDOT[ 1 ]
=
P
(POTENTIAL(Q , Q 1 2
W2
dQ
»
1 QDOT[ 2 ]
=P
Q 2
W
1 d
PDOT[ 2 ]
=-
P
(POTENTIAL(Q , Q » 2 1
W1
dQ 2
Comments: (C4) The one degree of freedom system of example 2, chapter 1. (C5) A two degree of freedom system representing a particle in the plane (with cartesian coordinates Q[I] and Q[2]), including so called gyroscopic effects (centripetal and Coriolis accelerations).
Note that the potential energy
POTENTIAL is left in functional form. We continue the run with a DISPFUN(ALL) in order to discuss the program: (C6) DISPFUN(ALL)i (E6) HAMILTON() := (INPUT(), STEPl(), OUTPUTl(), STEP2(), OUTPUT2(), STEP3(), OUTPUT3(»
61
( E7 ) INPUT () : = (N : READ(IIENTER THE NUMBER OF DEGREES OF FREEDOM"), L : READ("ENTER THE LAGRANGIAN: USE Q[I] FOR THE ITH GENERALIZED COORDINATE AND V[I] FOR THE ITH GENERALIZED VELOCITY"), PRINT(" If),
- , L), PRINT(" PRINT ( "L -II
If»~
(E8) STEP1() := (FOR I THRU N DO E
: P I
VINTOP
::: DIFF(L, V ), I
I
SOLVE(MAKELIST(E , I, 1, N), I
MAKELIST(V , I, 1, N») I
(E9) OUTPUTl() := (PRINT("GENERALIZED MOMENTA:"), PRINT(" "), FOR I THRU N DO (PRINT(E ), PRINT(" If) I
(E10) STEP2() := (H
- L + SUM(P
V, I, 1, N), I
I
H : EV(H, VINTOP (Ell) OUTPUT2() := (PRINT("H =", RATSIMP(H», PRINT("
If»~
(E12) STEP3() := FOR I THRU N DO (QDOT
DIFF(H, P ), PDOT I
62
I
- DIFF(H, Q I
I
(El3) OUTPUT3() := FOR I THRU N Dp (PRINT("QDOT[", I, II] -- I I , RATSIMP(QDOT )), PRINT(" II), I
PRINT("PDOT[", I, II]
=",
RATSIMP(PDOT)), PRINT(" II)) I
DONE
(Dl3)
The main function HAMILTON first calls INPUT to read from the keyboard the number of degrees of freedom Nand the Lagrangian L, which is displayed on the terminal. The rest of the program closely follows the three step procedure for computing Hamilton's equations given previously.
STEPl obtains the generalized velocities V[I]
in terms of the generalized momenta P[I] by first storing the definition of P[I] in a list E[I], and then using SOLVE to simultaneously solve the N E[I] equations for the N unknowns V[I].
Here the MACSYMA command MAKELIST is used
to create a single list for each of the two arguments of SOLVE.
The result of this computation is stored in the
list VINTOP (i.e., V INTO p). After displaying the definitions of the generalized momenta on the terminal with OUTPUT2, the program calls STEP2 which computes the Hamiltonian H.
The substitution
of the momenta for the velocities is accomplished by EValuating H subject to the condition VINTOP.
Then OUTPUT2
displays H on the terminal. STEP3 computes the derivatives of H which comprise the right-hand sides of Hamilton's equations.
Note that the
function SETUP, which was used in LAGRANGE.MAC to declare the array variables Q and V to depend on time T, is not needed here since no derivatives are taken with respect to T.
Finally OUTPUT3 displays Hamilton's equations on the
terminal.
63
Exercise. Laplace Transforms
Write a MACSYMA program to use Laplace transforms to solve a system of N first order linear nonhomogeneous constant coefficient O.D.E.'s X
I
=
f
(x , x
i i I
, 2
•••
, x
, t) N
with the initial conditions
x
(0)
=z
i
i
The program should cue the user to enter the number of equations N, the functions f
, and the initial conditions
i z.
See example 5 of chapter 1, where a system of two such
i equations is treated.
64
Exercise Solution. Laplace Transforms
(Cl) LOADPRINT:FALSE$ (:C2) LOADFILE(LAPLACE,MAC); (D2)
DONE
(C3) GO; (D3) This program solves a system of N first order linear nonhomogeneous constant coefficient O.D.E's of the form X[I]' = F(X[I], ••• ,X[N],T) by using Laplace transforms. To call it, type: LAPTR( ) ( C4 ) LAPTR ( ) $ ENTER NUMBER OF 1ST ORDER ODE'S 3;
ENTER RHS'S OF ODE'S USE X[I] FOR ITH DEPENDENT VARIABLE AND T FOR INDEPENDENT VARIABLE RHS OF EQ 1 : X[2] ; X (T)
2
RHS OF EQ 2 X[ 3]; X (T) 3 RHS OF EQ 3 X[1] ; X (T) 1 ENTER INITIAL CONDITIONS VALUE OF x[ 1 ] AT T=O: 1; VALUE OF X[ 2 ] AT T=O: 0;
VALUE OF X[ 3 ] AT T=O: 0;
SQRT(3) T
- T/2
T
COS(---------)
2 %E
%E
2
X[ 1 ]
= ------------------------
+ 3
3
SQRT(3) T SIN(---------) - T/2 X[ 2 ]
=
%E
-------------SQRT(3) T
SQRT(3) T
2
(-------------SQRT(3)
(C5) LAPTR()$ ENTER NUMBER OF 1ST ORDER ODE'S 2;
ENTER RHS'S OF ODE'S USE XlI] FOR ITH DEPENDENT VARIABLE AND T FOR INDEPENDENT VARIABLE RHS OF EQ 1 : -3*X[1]+X[2]+SIN(T)i SIN(T) + X (T) - 3 X (T)
1
RHS OF EQ 2 : X[1]-3*X[2]+%E**(-2*T); - 2 T
- 3 X (T) + X (T) + %E 2
66
1
%E
- --------------) +
3
- T/2
T
2
SQRT(3)
SIN(---------)
2
COS(---------)
2
(-
X[ 3 ] = %E
SQRT(3) T
3
COS(---------) 2 --------------) + 3
T %E 3
ENTER INITIAL CONDITIONS VALUE OF x[ 1 ] AT T=O; 1; ViALUE OF x[ 2 ] AT T=O;
Z; x[ 1 ] =
27 SIN(T)
11 COS(T)
---------
- .... _-------
85
- 85
2 T
27 %E
T %E +
-
2 T
--------- + ---------20
2
- 4 T
15 %E 68 - 2 T
7 SIN(T) x[ 2 ] =
6 COS(T)
37 %E
T %E
-------- + 85
85
- 2 T
2
+ ---------20 -
4 T
15 %E + ---------68
Comments: (C4) The equation x' 1'= x with the initial conditions x(O) = 1 r x'(O) = x' '(0) = o. (C5) Example 5 of chapter 1 r presented here as a check on the program. We continue the run with a DISPFUN(ALL) before discussing how it works; (C6) DISPFUN(ALL)i (E6) LAPTR() ;= (INPUT1()r SETUP1(), INPUT2(), SETUP2(), STEP1(), STEP2(), STEP3(), STEP4(), STEP5()r OUTPUT()) 67
(E7) INPUTl() := N READ("ENTER NUMBER OF 1ST ORDER ODE'S") (E8) SETUPl() := (REMARRAY(X), FOR I THRU N DO X
X (T)) I
I
(E9) INPUT2() := (PRINT(IIENTER RHS'S OF ODE'S USE X[I] FOR ITH DEPENDENT VARIABLE AND T FOR INDEPENDENT VARIABLE"), READ ( "RHS OF EQII, I, ":"),
FOR I THRU N DO (F I
PRINT(F )), PRINT("ENTER INITIAL CONDITIONS"), I
FOR I THRU N DO Z
READ("VALUE OF X[", I, II] AT T=O:"))
I
DIFF(X , T) = F
(ElO) SETUP2() := FOR I THRU N DO EQ
I
I
I
(Ell) STEPl() := FOR I THRU N LAPLACE(EQ , T, S)
DO TRANSF
I
I
(E12) STEP2() := FOR I THRU N : SUBST(- Z , PART(LHS(TRANSF ), 2), TRANSF )
DO TRANSF I
co
I
I
I
(EI3) STEP3() := FOR I THRU N DO (FOR J THRU N DO TRANSF J
SUBST(L , LAPLACE(X , T, S), TRANSF » I
I
(EI4) STEP4() := LT
J
SOLVE (MAKELIST (TRANSF
I, 1, N), I
MAKELIST(L , I, 1, N) I
(EI5) STEP5() := (REMARRAY(X), FOR I THRU N DO X
: ILT(RHS(PART(LT, 1, I», S, T»
I
(EI6) OUTPUT() := FOR I THRU N DO ( PRINT ( "X [ ", I, ,,] = ", X ), PRINT (II II» I
(D16)
DONE The main function LAPTR simply calls the other
functions in sequence:
INPUTI accepts the number of
equations N from the keyboard.
SETUPI declares the array
X[I] to be a function of T after wiping out any previous values of X[I] with a REMARRAY(X).
INPUT2 reads the
differential equations and the initial conditions from the keyboard.
SETUP2 defines the array EQ[I] to contain the
Ith equation.
STEP1 stores the transformed equation in the
array TRANSF[I].
STEP2 uses the SUBST function to insert
the initial conditions Z[I] into the transformed equations, while STEP3 SUBSTitutes L[I] for the Laplace transform of X[I].
The latter step is necessary in order for STEP4 to
SOLVE the algebraic TRANSFormed equations for the Laplace
transforms L[I].
STEPS again uses REMARRAY(X) to wipe out
the array X[I], and redefines X[I] to be the appropriate inverse Laplace transform. results on the terminal.
70
Finally OUTPUT displays the
4 Perturbation Methods
There have been a variety of perturbation methods invented since the time of Poincare for obtaining approximate sOlutions to differential equations which contain a small paramet.er l.
(Bender and Orszag, 1978).
While quite diverse, these methods all have one thing in COmmon:
They burden the user with the necessity of doing
alot of precise algebraic computation, especially if they ~.
are carried out to include higher order terms in
Thus
these methods present an ideal application for computer algebra.
In this chapter we will give some examples of how
MACSYMA can be used to treat classical perturbation problems. Example 1. Van der Pol's Equation (Andersen and Geer, 1982) In 1926 Van der Pol derived the following differential equation which modeled a triode oscillator: 2
d x
2
+
E-
x
dx
x )
(l -
=
0
d1:
2
d't' This equation possesses, for all f
, a unique,
asymptotically stable periodic sOlution called a limit cycle (Stoker, 1950).
We wish to obtain an approximation
for the limit cycle valid for small
c.
The procedure (called Lindstedt's method) is to "stretch time" via the transformation t
=
w(
e)
1: 71
2
c.
where w = 1 + k
1
+ •••
€.
+ k
2
in which the k are constants to be determined.
~
Replacing
i
by t we obtain 2
+
X'I
2
t
x / w
(1 - x ) x' /
w
=
0
where primes represent differentiation with respect to t.
c :
We then expand x(t) in a power series in 2
x(t) =
(t) + y (t)
y
o
+ y (t)
f
1
+ •••
2 n
and collect terms in
£.
Equating the coefficient of
to zero gives an equation on y (t). are:
E
The first two of these
n
y' , (t) + Y (t) = 0 0 0 2
y'
I
(t) + y (t) - 2 k
1
1
Y (t) + ( Y (t) - 1 ) y' (t) = 1 0 0 0
0
Since the Van der Pol equation is autonomous (i.e. contains no terms with explicit t
depen~ence),
the limit
cycle as a function of time contains an arbitrary phase angle.
For a unique sOlution we take the initial velocity
to be zero (Stoker, 1950): x'(O)
=0
implies
y'(O) n
72
=0
for n
= 0,1,2, .••
The y (t) equation has the sOlution
o
=A
y (t)
0
0
=o
ahd we choose A
sin t + B
cos t 0
to satisfy y I (0)
=
O.
0
0
The y (t) equation then becomes 1 2
3
y'
I
(t) +
Y (t)
(B /4) sin 3t - (B /4)(B
o
o
1
1
0
- 2 k
B 1
We choose Band k
- 4) sin t
cos t
=
0
0
so that y (t) contains no resonant
011 (nonperiodic, secular) terms.
That is, we require the
coefficients of sin t and cos t to vanish. B
= 2,
o
This gives
= o.
k 1
We would then solve for y (t) which would contain two 1
arbitrary constants, A
and B
1 .
The zero initial velocity 1
condition would yield a value for A , but B 1
undetermined.
would remain 1
Then we would substitute y (t) and y (t)
o into the y (t) equation.
1
Removal of resonant terms would
2
give values to Band k , and so on. I
2
73
Although our goal is to have this computation automated on MACSYMA so that it can be carried out to arbitrary order in
E,
we begin, for purposes of clarity, 2
by reproducing the above computation to order of
€
When
writing a complicated program in MACSYMA it is generally a good strategy to work out the details before beginning to design the functions which will constitute the program. the later discussion we will refer to the following computation as RUN A: (C1) LOADPRINT:FALSE$ (C2) DERIVABBREV:TRUE$ (C3) W: 1; (D3)
1
(C4) FOR 1:1 THRU 2 DO W:W+K[1]*E**I; (D4)
DONE
(CS) Wi 2 (DS)
K
+ K
E
2
E + 1
1
(C6) X:2*COS(T) ; (D6)
2 COS(T)
(C7) FOR 1:1 THRU 2 DO X:X+Y[1](T)*E**Ii (D7) DONE (C8) X; 2
(D8)
2 COS(T) + E
Y (T) + E Y (T)
2
1
(C9) D1FF(X,T,2) +X/W**2 -E*(1-X**2)*DIFF(X,T)/Wi
74
In
2
2
(D9) E
Y (T)
- E (1 -
2
(2 COS(T) + E
Y (T) + E Y (T» 2
2
1
T T
II I
CE
2
2
- 2 SIN(T»/(K
+ E Y (T)
Y (T)
2
+ K
E
2
1
E + 1)
1
T
T
2
2 COS(T) + E
Y (T) + E Y (T)
2
1
+ ----------------------------- - 2 COS(T) 2 2
+ E Y (T)
1
(K
T T
E
+ K
2
E + 1)
1
(CI0) TAYLOR(%,E,O,2)i 2
+ (- 8 cos (T) + 2) SIN(T) - 4 K
(DI0)/T/ (Y (T) 1
COS(T)
1
T T 2 + Y (T»
E + (Y (T)
1
+ (4 COS (T) - 1) Y (T)
2
1
T T
T
2 + (8 K
cos (T) - 8 Y (T) COS(T) - 2 K ) SIN(T) 1
1
1
2 + (- 4 K
2
2
+ 6 K ) COS(T) + Y (T) -
2
2 K 1
Y (T» E
+ .
1
75
"
(Cll) FOR 1:1 THRU 2 DO EQ[I]: COEFF(%,E,I); (Dll) DONE (C12) EQ[l]; 2
(D12)/R/
+ (- 8 COS (T) + 2) SIN(T) - 4 K
(T)
Y
COS(T)
1
1
T T + Y (T)
1 (Cl3) EQ [2] ;
2
+ (4 COS (T) - 1) Y (T)
(D13 )/R/ Y (T)
1
2
T
T T
2 + (8 K COS (T) - 8 Y (T) COS(T) - 2 K ) SIN(T) 1 1 1
2 + (- 4 K + 6 K ) COS(T) + Y (T) - 2 K Y (T) 1 1 2 1 2 (C14) EXPAND(TRIGREDUCE(EXPAND(EQ[l]))); (D14) Y (T) - 2 SIN(3 T) - 4 K COS(T) + Y (T) 1 1 1 T T (CIS) SOLVE(COEFF(%,COS(T)),K[l])i (DIS) [K = 0] 1
(C16) EV(D14,DlS)i (D16) Y (T)
- 2 SIN(3 T) + Y (T)
1
1
T T
76
(CI7) ODE2(%,Y[I](T),T); SIN(3 T) , (D17) Y (T) - - -------- + I
O/~I
SIN(T) + %K2 COS(T)
4
(CI8) EV(%,%KI:A[I],%K2:B[I]); SIN(3 T) Y (T) - - -------- + A (DI8) I
4
SIN(T) + B
I
COS(T)
I
( CI 9) RHS ( %) ;
SIN(3 T) (D1g)
+ A
SIN(T) + B
I
I
4
COS(T)
(C20) DIFF(%,T); 3 COS(3 T) (D20) - ---------- - B SIN(T) + A COS(T) 4 I I
(C21) SOLVE(EV(%,T:O),A[I]); 3 [A
(D21)
= -]
I
4
(C22) EV( DI8 ,%) ; SIN(3 T) (D22)
Y (T) I
- - -------4
3 SIN(T) + -------- + B
4
1
COS(T)
(C23) EV(EQ[2],D15,D22)i (D23)/R/ SIN(3 T)
2
3 SIN(T)
cos (T) )
+ -------- + B
(4 COS (T) - 1) (-
4
4
1 T
2
+ Y (T)
+ 2 COS(T) SIN(T) SIN(3 T) - 6 COS(T) SIN (T)
2
T T 2
- 8 B
COS (T) SIN(T) - 4 K 1
COS(T) + Y (T)
2
2
(C24) EV(%,DIFF)i (D24)/R/ (4 Y (T)
+ 8 COS(T) SIN(T) SIN(3 T)
2
T T 2
2
+ (- 12 COS (T) + 3) COS(3 T) -
24 COS(T) SIN (T)
2 + (- 48 B
COS (T) + 4 B ) SIN(T) + 12 COS (T) 1
+ (- 16 K
2
78
3 1
- 3) COS(T) + 4 Y (T))/4 2
(C2S) EXPAND(TRIGREDUCE(EXPAND(%»)1 S COS(S T) (JS2S) Y (T)
-
SIN(3 T) + ---------1 2
3 B
---------- -
2
3 COS(3 T)
4
T T COS(T) SIN(T) - 4 K COS(T) - ------ + Y (T) 1 2 4 2
- 2 B
(C26) COEFF(D2S,COS(T»1 1 (D26)
- 4 K
2
4
(C27) COEFF(D2S,SIN(T»1
- 2 B
(D27)
1 (C28) SOLVE([D26,D27],[K[2],B[l]])1 1 (D28)
- - --, B = 0]]
[[K 2
16
1
(C29) EV(D2S,%) 1 (D29)
Y (T)
-
S COS(S T)
3 COS(3 T)
---------- +
---------- + Y (T)
2
4
2
2
T T (C30) ODE2(%,Y[2](T),T)1 S COS(S T) - 18 COS(3 T) (D30) Y (T) 2
------------------------ + %Kl SIN(T) 96 + %K2 COS(T)
79
(C31) EV(%,%Kl:A[2],%K2:B[2])i 5 COS(5 T) - 18 COS(3 T) + A
(D31) Y (T) 96
2
SIN(T) 2
+ B
COS(T) 2
( C3 2 ) RHS ( %) i
5 COS(5 T) - 18 COS(3 T) + A
(D32) 96
SIN(T) + B
COS(T)
2
2
- B
SIN(T) + A
(C33) DIFF(%,T) i 54 SIN(3 T) -
25 SIN(5 T)
(D33) 96
2
COS(T) 2
(C34) SOLVE(EV(%,T:0),A[2])i
=
[A
(D34)
0]
2
(C35) EV(D31,%)i 5 COS(5 T) - 18 COS(3 T) (D35)
Y (T)
=
2
B
COS(T) - -----------------------96
2
Comments: (C3),(C4) Here we set up the stretching factor w. (C6),(C7) Similarly here we set up the expansion of x(t). (C9) The differential equation.
Since Wand X have already
been defined, this command causes their substitution into the d.e.
=a
(CI0),(Cll) We Taylor-expand the d.e. about E collect coefficients.
and
EQ[I] is the d.e. on y (t), here
known as Y[I](T).
i
(CI4) We clean up the trig in EQ[I]. (CI5),(CI6) Elimination of the resonant term determines K[I], which is then substituted back into EQ[I] via the EV() function. (CI7) ODE2() is used to solve the resulting d.e. 80
(CI8) The arbitrary constants of integration from ODE2, %Kl" and %K2, are renamed A[I] and B[I]. (GI9)-(C21) The zero initial velocity condition is used to determine A[l]. (q22) The result is substituted back into the expression f olr Y [ 1 ] (T) •
(C23) Having temporarily finished with Y[I](T), we go on to EQ[2].
Here we substitute the derived expressions for K[l]
and Y[I](T) into EQ[2]. (C24) Note that the expression (D23) for EQ[2] contains some unevaluated differentiations.
EV(A,DIFF) causes all
differentiations in expression A to be performed. (C25)-(C35) These commands repeat for Y[2](T) the commands (CI4)-(C22) already discussed for Y[I](T).
The only,
exception is that B[l] must be found while B[O] didn't appear here since it had already been taken equal to zero. Cf. (CIS) and (C28). Using the previous run as a guide, a program was written which permits the perturbation procedure to be continued to an arbitrary order of
t ,
limited only by the
memory size of the particular computer being used.
We will
first present a sample run of the program, and then discuss the programming details: (CI) LOADPRINT:FALSE$ (C2) LOADFILE(VANDERPOL,MAC); (D2)
DONE
(C3), GO; ,
(D3) This program computes a perturbation solution for the limit cycle in Van der Pol's equation.
Call it by typing:
VANDERPOL(N) where N is the order of truncation.
81
(C4) VANDERPOL(6)$ SIN(3 T) 3 SIN(T) Y (T) = -------- -
--------
1 4 4 5 COS(5 T)
3 COS(3 T)
Y (T) =
+ ----------
96
2
Y (T)
COS(T)
16
8
7 SIN(7 T)
35 SIN(5 T)
576
576
=
3
7 SIN(T)
+ -----------
61 COS(9 T)
256
2149 COS(7 T)
Y (T) = ----------- -
4
21 SIN(3 T)
256
1085 COS(5 T)
------------- + -------------
20480
110592
27648 47 COS(3 T)
73 COS(T) + ---------
1536
12288
You have run out of LIST space. Do you want more? Type ALL; NONE; a level-no. or the name of the space. ALL; 5533 SIN( 11 T) Y (T)
+ ------------1228800
7372800
5
7457 SIN(9 T)
52885 SIN(5 T)
110621 SIN( 7 T) 6635520
2591 SIN(3 T)
12971 SIN(T)
294912
4423680
+ -------------- -
2654208 6
4
35 E 17 E W= ------ + ----884736 3072 82
2 E
+ 1
16
Comments: (C2) The program was saved on disk using filespec" VANDERPOL.MAC.
(q3) As usual the user instructions are saved as a string in a variable named GO. (d4) The program outputs Y[l)(T) thru Y[N-l)(T) as well as I
W'truncated at the Nth power in E.
Note that W is the
fr,equency of the limit cycle oscillation and hence has pqysical significance. The memory management message "You have run out of LIST space" is an indication that the problem is getting large enough to strain the available computer memory. Typing ALL; will increase the memory allocation.
See the
MACSYMA Reference Manual chapter on Storage Management. In order to understand how this program works, we continue the above run with a DISPFUN(ALL) command which displays all user defined functions: (C5) DISPFUN(ALL); (E5) VANDERPOL(N)
:= (SETUPl(N),
SETUP2(N),
FOR I THRU N DO BLOCK(STEPl(I), STEP2(I), IF I
>
1 THEN OUTPUTl(I), IF I = N THEN GO(END), STEP3(I),
STEP4(I), STEP5(I), END), OUTPUT2(N» I
(E6) SETUPl(N) := (W : 1, FOR I THRU N DO W
W + K
E,
I
I
x
2 COS(T), FOR I THRU N DO X
X + Y (T) E ) I
83
x DIFF(X, T, 2) + --
(E7) SETUP2(N) := (TEMPI
2
W 2 E (1 - X ) DIFF(X, T) -
---------------------, TEMPI w
FOR I THRU N DO EQ
TAYLOR(TEMPl, E, 0, N),
COEFF(TEMPl, E, I» I
(E8) STEPl(I) := TEMPI
EXPAND(TRIGREDUCE(EXPAND(EV(EQ, I
MAKELIST((E , F ], J, 1, I - I ) , DIFF»» J
J
(E9) STEP2(I) := (IF I = 1 THEN F I
SOLVE(COEFF(TEMPl, COS(T», K ) I
ELSE F
SOLVE((COEFF(TEMPl, COS(T», I
COEFF(TEMPl, SIN(T»], (K , B I
TEMPI
EV(TEMPl, F » I
84
]),
I-I
EV(ODE2(TEMP1, Y (T), T),
(E10) STEP3(1) := TEMPI
I
%K1
A , %K2 I
B ) I
I
(E'll) STEP4(1) := (TEMP2: RHS(TEMP1), TEMP2 : D1FF(TEMP2, T), TEMP2 SOLVE(EV(TEMP2, T
0), A » I
(E12)
STEP5(1) := E
EV(TEMP1, TEMP 2 )
I
(E13) OUTPUT1 (I) := (PR1NT(EXPAND(EV(E
, F»), I-I I PRINT("
II»~
EV(W, MAKELIST([F], J, I, N»), PRINT("
II»~
(E14) OUTPUT2(N) := (PRINT( "W=",
J
(D14)
DONE
This program consists of a main function, VANDERPOL, which calls 9 other functions with the names SETUP1, SETUP2, STEP1, STEP2, STEP3, STEP4, STEPS, OUTPUT1, and OUTPUT2. Each of these 9 functions automatically performs some task which had to be manually-commanded in the previous run, referred to here as RUN A. In discussing each of them we will refer to the corresponding labels in RUN A. VANDERPOL begins by calling SETUP1 which prepares W and X as power series in E, as in (C3)-(C8) in RUN A (except that here the FOR-DO loops go from 1 to N.) Next 8S
SETUP2 substitutes these expressions into the differential equation, expands in a Taylor series and collects terms in as in (C9)-(CII) in RUN A. Throughout the program, TEMPI and TEMP2 are used to represent temporary variables whose values need not be kept.
These temporary variables playa role similar to the
use of % to refer to the immediately preceding result in RUN A. The main difference between the program and RUN A is how the (non-temporary) intermediate results are stored. In RUN A, values of K[I] and Y[I](T) once found could always be referred to by their D-labels (see (C23) in RUN A).
Since there are no D-labels in the program, we store
these as arrays named F[I] and E[r].
F[r] contains K[I]
and B[I-I] and is assigned in STEP2, while E[I] contains Y[I](T) and is assigned in STEP5.
In order to use these
arrays to substitute into EQ[I] the already-obtained values of K[l], K[2], ••• , K[I-I] and Y[I](T), Y[2](T), .•. , Y[I-I](T), we create a list of them in STEPI using the MAKELIST function.
MAKELIST(A,B,C,D) creates a list of A's
(in which each A presumably depends on the variable B), concatenated from B
=C
to D.
Here then is a correspondence between the STEP functions in the program and the associated labels in RUN
A: STEPI
(C23)-(C25)
STEP2
(C26)-(C29)
STEP3
(C30)-(C31)
STEP4
(C32)-(C34)
STEP5
(C35)
In addition to these functions, the program requires some means of outputing the results.
The function
OUTPUTI(I) prints the expression for Y[I](T) and then skips a line for Clarity of display. expression for the frequency W. 86
OUTPUT2 prints the final
As a final corrunent about this program, note the use
of'~
BLOCK and GO() in the function VANDERPOL. The purpose of the GO() is to avoid wasteful computation on the final l
i~eration,
S~EP2. I
I=N, since K[I] and Y[I-l](T) are found after
In order for the GO() to work, it must be contained
inside a BLOCK.
Example 2. Mathieu's Equation The following linear differential equation, called Mathieu's equation, plays an important role in parametrically excited systems (e.g. the vertically-driven pendulum), as well as in the stability of periodic mqtions of autonomous systems (Stoker, 1950): 2 d
x +
( ~
+
f
cos 1: ) x
=
0
2
d'l: For given values of the parameters ~
and
f. ,
either
all sOlutions are bounded (in which case the equation is said to be stable), or there exists an unbounded solution (whereupon the equation is said to be unstable).
We are
thus led to consider the question of which points in the
b-
Eo plane are stable, and which are unstable.
The
regions of stability are separated from those of instability by "transition curves". At a point ( Si , € ) on a transition curve, it can be shown that there exist periodic sOlutions x( 1:) having period 211" or 4"1T in 1: (Stoker, 1950). all transition values of
~
and f
In order to obtain
, it is thus sUfficient
to consider sOlutions with period 4lr/N, N = 0,1,2, ••. , since all such solutions will have period 41T.
(E.g. a
solution of period 2lf may also be considered to have 87
period 41T, but not vice versa.)
N = 0 corresponds to a
constant solution (which occurs in the above equation for ~
=
e.
= 0), which may also be thought of as a periodic
function of period 41T. Now when and cos
Kt:,
f, = 0, this equation has solutions
all of which have period 2"R" /
transi tion points on the
S -axis
IS.
sinK1:
Thus
occur when 2
211"
41r
N
or
=
~
= 4
N
For
f = 0 and
b<
0, Mathieu's equation has unbounded
solutions, and hence the entire negative
i -axis is
unstable. Thus we expect two transition curves to intersect each of the foregoing transition points on the '-axis, the sOlution on one behaving like sin like cos N1:/2 for
~
= O.
N~/2,
and on the other
(Except for N = 0, where we
expect a single transition curve with solution behaving like a constant for
~
= 0.)
s
1/+ Transition curves in Mathieu's equation. U=unstable. 88
S=stable,
We wish to obtain approximations for these transition
E.
curves valid for small
,
We expand
2
2
N
b=
w(
£ ),
where w( f. )
=1
e.
+ K 1
4
and set
E.
+ K
+
...
2
N't
t
-
----,
= 1,2,3, •..
N
2 (N
=0
will be handled separately.)
This gives
4 E.
+ ( w( f. ) +
x"
2t
) x = 0
cos 2
N
N
where primes represent differentiation with respect to t. Then we expand 2
x(t) = y (t) + Y (t) f
o
+ Y (t)
1
E. +
... ,
2
substitute into the d.e., collect terms and equate the N
coefficient of
C
to zero, giving an equation on y (t). N
We separately consider each of the initial conditions i) x'(O)
=0
implies y '(0)
=0
for N
= 1,2,3, ... ,
=0
for N
= 1,2,3, ..• ,
N
and
ii)
x(O)
=0
implies
y (0) N
in order to generate each of the two transition curves 2
through the point have y (t)
o
= cos
S =
N /
4,
E. =
O.
In case i) we will
t, while in case ii) y (t)
=
sin t.
0 89
(Since Mathieu's equation is linear we can choose the mUltiplicative arbitrary constant as unity.) As in the previous treatment of Van der Pol's equation, the constants K
which describe the transition
M
curve are found by eliminating resonant terms from the equation on y (t).
Since we are interested in any periodic
M
solution of period 21(" or 4"T1" in 'to (as opposed to the most general such solution), we may choose the constants of integration in the y (t) equation to be zero (Stoker,1950). M
In order to accomplish this task in MACSYMA, we have modified the previous program used to treat Van der Pol's equation. program.
Ths following run demonstrates the resulting "Note that N = 0 must be treated separately.
(C1) LOADPRINT:FALSE$ (C2) LOADFILE(MATHIEU,MAC);
DONE
(D2)
(C3) GO; (D3) This program computes the transition curves in Mathieu's equation using a perturbation method. Call it by typing: MATHIEU ( ) (C4) MATHIEU()$ ENTER TRANSITION CURVE NUMBER N 1; ENTER DEGREE OF TRUNCATION
6; 6
49 E
5
HE
4 E
DELTA=
E
2 E
4608
384
32
E
1
- - + -
+ -- 36864
90
3
8
2
4
5
6 49 E
,
4
11 E
E
DELTA= ----- + -----
384
E
1
+ - +
-
2
4
E
E
- --- -
4608
36864
2
3
32
8
(C,5) MATHIEU ( ) $
ENTER TRANSITION CURVE NUMBER N 2; ! ENTER DEGREE OF TRUNCATEON 6; 6 1002401 E
4
2 5 E
763 E
DELTA= ---------- - ------ + 4976640
3456
6 289 E DELTA=
-
12 2
4 5 E
E
------- + ---- 4976640
+ 1
3456
+ 1 12
In order to understand how this program works, we continue the above run with a DISPFUN(ALL) command which displays all user defined functions:
(C6) DISPFUN(ALL); (E6) MATHIEU() := (INPUT(), YO
COS(T), FINDTC(), YO : SIN(T), FINDTC(»)
(E7) INPUT() := (N:READ("ENTER TRANSITION CURVE NUMBER Nil), M : READ("ENTER DEGREE OF TRUNCATION"» (E8) FINDTC() := (SETUP1(), SETUP2(), FOR I THRU M DO (STEP1(), STEP2(), STEP3(», OUTPUT())
91
I
1, FOR I THRU M DO W
(E9) SETUPl() := (W
W+ K
E,
I
I
X
YO, FOR I THRU M DO X
X + Y (T) E ) I
(ElO) SETUP2() := (TEMPI
4 + X
(W +
DIFF(X, T, 2)
2 T E COS(---», TEMPI 2 N
TAYLOR(TEMPl, E, 0, M),
N
FOR I THRU M DO EQ
COEFF(TEMPl, E, I» I
(Ell) STEPl() := TEMPI
EXPAND(TRIGREDUCE(EXPAND(EV(EQ, I
MAKELIST([E , F ], J, 1, I - I ) , DIFF»» J
J
SOLVE(COEFF(TEMPl, YO), K ),
(E12) STEP2() := (F
I
I
TEMPI
EV(TEMPl, F » I
(E13) STEP3() := (TEMPI
ODE2(TEMPl, Y (T), T), I
EV( TEMPI, %1<1
E I
92
0, %1<2
0»
(E14) OUTPUT() := (PRINT("DELTA=", 2 N
EXPAND(-- EV(W, MAKELIST([F], J, 1, 4
M»»,
PRINT("
II»
J
DONE
(D14)
This program works as follows:
The main function
MATHIEU first calls INPUT in order to read the parameters N and M from the keyboard.
Then the two transition curves
are found by setting YO respectively equal to cos t and sin t, and calling the function FINDTC in each case. Thus FINDTC is where all the work is done.
This
function in turn calls 6 other functions, SETUP1, SETUP2, STEP1, STEP2, STEP3, and OUTPUT, each of which is similar to a function in VANDERPOL.MAC previously studied.
We
leave it to the reader to compare these functions with their predecessors, but point out these differences: Note that all the functions in MATHIEU.MAC have no arguments, while all those in VANDERPOL.MAC have one argument. Thus the variables in MATHIEU.MAC are global, and their values are altered after MATHIEU() has been called, while the variables passed to the functions in VANDERPOL.MAC are local, and their values after VANDERPOL(N) has been called are restored to what they were before the call.
This is purely of technical interest, and
is of little importance in the present applications. STEP4 and STEPS of VANDERPOL.MAC have been omitted here since it turns out that choosing the arbitrary constants %K1 and %K2 equal to zero automatically satisfies the foregoing initial conditions i) or ii).
Further,
OUTPUT1 of VANDERPOL.MAC was omitted here since it printed the values of the functions Y[I](T), which are of no particular interest in this application. Finally, note that the truncation variable N in 93
VANDERPOL.MAC has been changed to M in MATHIEU.MAC, since N is used here to represent the number of the transition curve. It remains to treat the special case N
~
0 which
involves only a single transition curve through the origin in the ~ -
E. plane.
We expand 2
~ =
f. )
w(
=
f.
K
+
1
e
K
+
...
2
t = ~
and we take giving x"
w( f ) +
+
c
cos t
) x
~
O.
Then we expand 2
...
x(t) = 1 + Y (t) Eo + Y (t) £ + , 2 1 collect terms, etc. The resulting program is demonstrated below: (C1) LOADPRINT:FALSE$ (C2) LOADFILE(MATHIEUO,MAC)1 (D2)
DONE
(C3) G01 (D3) This program computes the transition curve through the origin (N = 0) in Mathieu's equation using a perturbation method.
To call it, type MATHIEUO( )
(C4) MATHIEUO()$ ENTER DEGREE OF TRUNCATION 81 8 68687 E
29 E
DELTA= -------- - ----- + 294912 144
94
2
4
6
7 E
E
32
2
(C5) DISPFUN(ALL)i (E5) MATHIEUO() := (INPUT(), SETUPI(), SETUP2(), ~
, FOR
I THRU M DO (STEPI(), STEP2A(), STEP3A(», OUTPUT(»
I
<:E:6) INPUT ( )
:=
M : READ ( "ENTER DEGREE OF TRUNCATION") I
( E7) SETUP 1 () : = (W
0, FOR I THRU M DO W
W+ K
E,
I
I
1, FOR I THRU M DO X
X
X + Y (T) E ) I.
(E8) SETUP2() := (TEMPI: DIFF(X, T, 2) + X (W + E COS(T», TEMPI: TAYLOR(TEMPI, E, 0, M),
FOR I THRU M DO EQ
: COEFF(TEMPI, E, I» I
(E9) STEPI() := TEMPI
EXPAND(TRIGREDUCE(EXPAND(EV(EQ I
MAKELIST ( [E , F ], J, I, I - I ) , DIFF»» J
(EIO) STEP2A() := (TEMPI
J
ODE2(TEMPI, Y (T), T), I
TEMPI
EV(TEMPI, %KI
0, %K2
0»
95
(Ell) STEP3A() := (F I
SOLVE(COEFF(EXPAND(RHS(TEMP1», T, 2), K ), I
E
EV(TEMP1, F » I
I
(E12) OUTPUT() := (PRINT("DELTA=", EV(W, MAKELIST([F], J, 1, M»), PRINT(" ,,» J
DONE
(D12)
This program is quite similar to the previous one and requires little comment.
Here we have removed secular
terms after the equation on y (t) was solved by ODE2(), i
requiring STEP3 of the previous program to be executed before STEP2 (here renamed STEP2A and STEP3A, respectively). The procedure, by inspection of the form of 2
the equations, is to require that the coefficient of t vanish in the expression for y (t), a task accomplished by i
STEP3A. We will now make another pass at Mathieu's equation using a very different approach.
Since we are interested
in periodic solutions of period 21f or 41f, we expand x( t) in a Fourier series (we drop the distinction between t in what follows):
96
and~
cO
x(t)
L
=
kt
kt
k=O
+
cos
a k
b
2
sin 2
k
I
and substitute into Mathieu's equation, ~
+
x"
c.
+
cos t
)
x
0,
=
collect terms and equate to zero the coefficients of sin kt/2 and cos kt/2, k=0,1,2, ••• (Stoker, 1950).
This
gives four sets of infinite-order, homogeneous linear algebraic equations on the a
and b . k
exclusively with a
,
k
and b
, a
b
even
even
Each set deals , respectively. odd
odd
Since each system of equations is homogeneous, the determinants of the coefficients must vanish for a nontrivial solution.
This gives four infinite determinants
(called Hill's determinants) which are of the form: a
even
C./2
o
~-1
f/2
o o
E./2
b -4
f./2
f./2
f/2
o o
o
S; -9
€-/2
o
~ -1
b
even
~ -1/4+ f./2
a
odd
€./2
o
f/2
o
~ -9/4
t./2
t./2
f) -25/4
=
0
=
0
o o £/2
=
0
97
~
b
-1/4- £/2
C/2
f./2
~ -9/4
odd
o
o
° t. /2 ~ -25/4
f/2
=
E°/2
In all four determinants the typical row is of the form: 2
o
b-k /4
E./2
o
£/2
(except for the first one or two rows). Each of these four determinants represents a ~
functional relationship between
and E. , i. e., each
represents a set of transition curves in the , - E plane. The curves so obtained from the a
and b even
sets even
2
intersect the
b -axis
at
S=k
curves obtained from the a
, k=O,1,2, ••• , while the
and b odd
sets intersect the odd
2 ~
-axis at b = (2k-1) /4, k=1,2,3, •••. We propose to use computer algebra to generate
expressions for truncations of these Hill's determinants, and then to develop
~
as a power series in E.
in order to
obtain the desired asymptotic expressions for the transition curves. Although we could simply use the DETERMINANT() function in MACSYMA to obtain the truncated Hill's determinants, we choose instead to use the following more efficient method of generation:
We obtain a second order
difference equation for the kth Hill's determinant truncation, denoted here by
~
• k
98
0
Consider the case of a even ~
f/2
f
~-1
o
e. /2
o o
o t/2 b -4
E./2
=
2
k
g- (k-2)
E/2
o
C/2 2
o
~ -(k-1)
f./2
E./2 2
o
o
f./2
f>-k
We perform a Laplace expansion across the bottom row and obtain 2
2
~
=
(e
(b-k)~
k
/4)
6.
k=2, 3,4, •••
k-Z
k-l
This difference equation, together with the initial values
=
a
o
even
~
2 =
S(~ -
1) -
E.
for k
=
/2
1
are sufficient to generate ~
2,3,4, •.••
k
For b
we find the same difference equation holds even
for k=3,4,5, ••. , with the following initial values:
6. =
b even
~- 1
1
A =
2
( ~ - 1)( ~ - 4) -
E. /4
2
99
we find the difference
and b
Similarly, for a
odd
odd
equation: 2
2 =
( b-
- ( E /4)
(2k-1) /4) k-1
k
k=3, 4, 5, •••
k-2
with the initial values
b. =
a
odd
~ - 1/4 + f. /2 2
1
I:J. 2 =
( ~ - 1/4 + f /2) (
6- = b -
b
odd
1/4 -
h -
9/4) -
f.. /4
E. /2 2
1
~
=
( ~ - 1/4 -
E. /2) ( b -
9/4) -
E.
/4
2
from which it follows that the results obtained from the b
set are the same as those obtained from the a odd
odd
set with f. replaced by - Eo • Before being able to use these results to obtain power series expansions of ~ in terms of E:. using MACSYMA, we must first be able to generate the foregoing determinants. The following functions accomplish this: (C1) LOADPRINT:FALSE$ (C2) LOADFILE(HILL1,MAC)J (D2)
100
DONE
(C3) GOJ (D3) This program generates truncated Hill's determinants
f~r Mathieu's equation.
To call it, type one of:
MAKEAEVEN(P) or
MAKEBEVEN(P)
or
MAKEAODD(P)
where P is the truncation size.
(t4) MAKEAEVEN(4)J 2
2
E
D E
(D4) (D - 16) ((D - 9) ((D - 4) «D - 1) D - --) - ----) 2
4
2
2 E
E «D - 1) D - --) 2
-
-------------------) 4 2 2 E
( (D -
4)
( D -
1) D -
2
E
D E
--) -
----)
2
4
4
(C5) EXPAND(%) J 4
5 D E
4
5 E
(D5)
3
5 D
2
2 E
105 D
- ------- + 16
2
+ 288 E
+ D
E
2
- 163 D E
4 5
2
4 4
- 30 D
3
+ 273 D
2
- 820 D
+ 576 D
101
(C6) DISPFUN(ALL) ; (E6) MAKEAEVEN(P) := (ICAEVEN( ) , U, U
w), U)
FOR K FROM 3 THRU P DO (w : EQEVEN() , V : U, U
w), U)
FOR K FROM 2 THRU P DO (w : EQEVEN() , V (E7) MAKEBEVEN(P) := (ICBEVEN( ) ,
(E8) MAKEAODD(P) := (ICAODD( ) , FOR K FROM 3 THRU P DO (w
EQODD( ) , V
U, U
w), U)
2 2 (E9)
E
EQEVEN( ) := (D - K ) U -
V 4
2
2
(2 K - 1 ) (E10)
EQODD( ) := (D -
----------)
E U -
V
4
4 2 E
(Ell )
ICAEVEN( ) := (U
D (D - 1 ) - --, V : D) 2 2 E
(E12) ICBEVEN( ) := (U
(D - 1 ) (D - 4) -
--, V 4
102
D - 1)
2
1
, (E13) ICAODD() := (U
(D -
-
9
E
E
+ -) (D - -) - --,
4
2
4
4
1 V
D -
E + -)
-
4
2
DONE
(Dl3)
This program uses separate functions to generate truncated Hill's determinants for each of the three cases a
I
even
, and a
b
even
The corresponding functions'are odd
named MAKEAEVEN, MAKEBEVEN, and MAKEAODD.
(It is
unnecessary to generate the determinants for b
since odd
these can be obtained from MAKEAODD by replacing E by -E.) Let us consider MAKEAEVEN.
It begins by calling
ICAEVEN which sets up the initial conditions for ~
and
o ~.
Then a FOR-DO loop calls EQEVEN which invokes the
1
previously derived second order difference equation. variable U represents ~
, while V represents ~
The The
k-2
k-1
variable W is used as a temporary variable in MAKEAEVEN to update U and V during each iteration. Using these functions we are now ready to develop in a power series in
f.
S
We continue the previous run in
order to show how this may be accomplished.
Then we will
incorporate the fOllowing commands into a program.
103
(C14) DET:MAKEAODD(3)i E 2 25
E
9
1
E
(DI4) (D - --) «D - -) (- + D - -) - --) 2 4 4 4 4 (CIS) D:l/4+K[I]*Ei 1
K
(015)
E + -
1
4
(C16) TAYLOR(EV(DET),E,O,I)i (DI6)/T/ (12 K + 6) E + • • • 1
(CI7) SOLVE(%,K[I])i 1 (DI7)
- -
[K
-]
1
2
(CI8) D:EV(D,%) i (D18)
1
E
4
- -
2
E
2
(C19) D:D+K[2]*E**2i (DI9)
K E 2
- - + -
2
(C20) TAYLOR(EV(DET),E,0,2)i 2 (24 K + 3) E 2 (D20)/T/
104
1
-------------- + 2
4
1
(- + D 2
-
2
-) E 4
-------------4
(C21) SOLVE(%,K[2]);
1 (021)
-
[K
- -]
2
8
d~22) D:EV(D,%) ; 2 E (D22)
--
-
E 1 - + -
-
2
8
4
(C23) D:D+K[3]*E**3;
2 3 (D23)
E
E
K
-
-
--
3
1
E
- + 2 4
8
(C24) TAYLOR(EV(DET),E,O,3); 3 (96 K
-
3) E
3
(D24)/T/
-------------- + • .
8
(C25) SOLVE(%,K[3]);
1 (D25)
= --]
[K 3
32
(C26) D:EV(D,%);
3
2
E (D26)
E
32
--
E
- - + 2
8
(C27) D: 'D;
(D27)
1
D
4
.
(C28) DET:MAKEBEVEN(4)i 2 E
(D28) (D - 16) «D - 9) «D - 4) (D - 1) - --) 4
2 2
2
E
E ( (D -
4)
(D -
(D - 1) E
-
4
4
(C29) D:1+K[2]*E**2i 2
K
E
+ 1
2
(C30) TAYLOR(EV(DET),E,0,2)i 2
(D30)/T/
(- 360 K
- 30) E
+.
2
(C31) SOLVE(%,K[2])i (D31)
1 --]
- -
[K 2
12
(C32) D:EV(D,%)i 2 E (D32)
1 -
12 (C33) D:D+K[4]*E**4i 2 4
(D33)
K 4
106
--)
4
----------)
(D29)
1) -
E
E
+ 1
12
,
(C34) TAYLOR(EV(DET),E,O,4); 4
(17280 K
- 25) E
4 i
(P34)/T/
- ------------------ + • . .
I
48
(~3S) SOLVE(%,K[4]);
5 = ----]
[K
(D3S)
4
3456
4
2
(C36) D:EV(D,%); 5 E ----
(D36)
3456
E
-
+ 1 12
Comments: (CI4)-(CI6) With DET defined as D14 and D defined as DIS, EV(DET) in (CI6) substitutes D15 into D14.
Then
TAYLOR picks off the constant and linear terms. (CI7),(CI8) SOLVE is used to obtain K[I] and EV substitutes it into an updated expression for D. (CI9)-(C22) The approximation for D as a function of E is extended to include a quadratic term ancl the process is repeated. (C23)-(C26) Similarly, a cubic term is included. (C27)-(C36) An example is presented of the even case.
Here
we use the fact that all the K[odd] coefficients are zero, and repeat the previous scheme skipping all the odd steps. Note the use of the quote function in (C27) to remove the previous value of the variable D, assigning it simply the letter "D" (for use in the function MAKEBEVEN in (C28». The following run presents a program which automates the previous steps.
107
(C1) LOADPRINT:FALSE$ (C2) LOADFILE(HILL2,MAC); (D2)
DONE
(C3) GO; (D3) This program generates the transition curves in Mathieu's equation via Hill's determinants.
Call it by
typing: HILL( ) NOTE: File HILL1.MAC must be loaded before calling HILL(). (C4) LOADFILE(HILL1,MAC); (D4)
DONE
(C5) HILL() $ ENTER TRANSITION CURVE NUMBER N 0;
ENTER DEGREE OF TRUNCATION 6; 6
4
2
29 E 7 E DELTA= - ----- + 144 32
E 2
(C6) HILL() $ ENTER TRANSITION CURVE NUMBER N 1; ENTER DEGREE OF TRUNCATION 6; 5
6
49 E
4
11 E
2
3
E
E
E
E
DELTA= ----- - ----- - --- + 36864
4608
6 49 E
5
11 E
DELTA= ----- + ----36864
108
4608
-
- - + -
384
32
4
3
E ---
384
1
E
8
2
4
E
1
2 E
-
+ - + -
32
8
2
4
(C7) HILL()$ E~TER
TRANSITION CURVE NUMBER N
•
21 EfTER DEGREE OF TRUNCATION
6; 4
6 1002401 E
2
763 E
5 E
DELTA= ---------- - ------ + 4976640 3456
289 E
5 E
E
+ ]
DELTA= - ------- + 4976640
12
2
4
6
+ 1
12
3456
Comment: (C2)-(C4) This file, named HILL2.MAC, is distinct from HILL1.MAC discussed previouslY.
So we insert a
reminder into the user instructions "GO" to load HILL1.MAC. In order to see how HILL2.MAC works, we use a DISPFUN(ALL) command.
(In order not to have the functions
in HILL1.MAC displayed again, we load another MACSYMA.) (C1) LOADPRINT:FALSE$ (C2) LOADFILE(HILL2,MAC)1 (D2)
DONE
(C3) DISPFUN(ALL)1 (E3) HILL() := (INPUT(), IF EVENP(N) THEN (FINDAEVEN(N, M, N + M), IF N
>0
THEN FINDBEVEN(N, M, N +
M»
ELSE FINDAODD(N, M, N + M»
109
(E4) INPUT(} := (N:READ("ENTER TRANSITION CURVE NUMBER N"), M : READ("ENTER DEGREE OF TRUNCATION"}} (E5) FINDAEVEN(N, M, p} := (D :
'D, 2
N DET
+ K
EXPAND(MAKEAEVEN(P)}, D 4
2 E,
2
FOR I FROM 2 STEP 2 THRU M DO (LOOP(I),
IF I
< M THEN
I + 2 D
D + K
E
}, PRINT ( "DELTA=", D},
I + 2 PRINT(" II}} (E6) FINDBEVEN(N, M, p} := (D
'D, 2 N
DET
2
+ K
EXPAND(MAKEBEVEN(P)}, D 4
E,
2
FOR I FROM 2 STEP 2 THRU M DO (LOOP(I),
IF I
< M THEN D
I + 2 D + K
I + 2 PRINT(" II}}
110
E
}, PRINT ( "DELTA=", D},
(E7) FINDAODD(N, M, p) := (D
I
D, 2
N D$T
+ K
EXPAND(MAKEAODD(P», D 4
:
FOR I THRU M DO (LOOP(I), IF I
E,
1
<M
I + 1 THEN D
D + K
E
), PRINT ( "DELTA=", D),
I + 1
PRINT(" II), PRINT("DELTA=", EV(D, E : - E», PRINT("·"» (E8) LOOP(I) := D : EV(D, SOLVE(TAYLOR(EV(DET), E, 0, I),
»
K I
DONE
(D8)
This program works as follows:
HILL first calls INPUT
which reads from the keyboard the transition curve number N and the degree of truncation of the series M.
Then the
MACSYMA function EVENP is used to determine if N is even or odd.
If N is even then a function named FINDAEVEN is
called, generating one of the desired transition curves, and if N isn't zero, a second function named FINDBEVEN is called to generate the other. (If N is zero there is only one transition curve to be found, and FINDBEVEN isn't called.)
In the case that N is odd, FINDAODD is called,
producing both transition curves.
(Recall that if N is odd
a substitution of -E for E produces the second transition curve. ) The three FIND functions call the associated MAKE functions in file HILL1.MAC (discussed previously) to construct the appropriate truncated Hill's determinant. 111
Then the FIND functions execute a FOR-DO loop in order to produce the M degree truncation of D as a power series in E.
The function LOOP(I) solves for the coefficient K[I]
and substitutes it into D. Note that the Hill's determinant size, P, is assigned the value N+M by the function HILL in its function calls to FIND ••• (N,M,P).
This choice of the dependency of P on N
and M was obtained by inspection. We have so far presented two methods for obtaining transition curves in Mathieu's equation.
The first method
used a perturbation scheme, while the second used Fourier series to obtain Hill's determinants.
We shall now present
yet a third method due to Levy and Keller (1963) which uses Fourier series to solve the perturbation equations.
In the
process we shall see how recursive functions are handled in
MACSYMA. We begin by substituting into Mathieu's equation x"
(~
+
+
€. cos t
) x
=
0
the following perturbation expansions for 1>
and x:
2 N
2
~
+
=
f + 1
4
f
~
+ •••
2 2
x
=
Y (t) + Y (t)
a Collecting terms in
1
E
differential equation
Z
as usual, we obtain this o~
y (t), j
112
f. + Y (t) f. +
j=1,Z,3, •.. :
2 N yl"
+
j
j
4
-
cos t
= - Y
Y
;
~
(
y 1
j-l
+
~
j-l
+ ••• +
y 2
b
y )
j
j-2
0
To find each of the two transition curves which pass 2
through
~ = N /2,
f
= 0, we make each of the following
substitutions: oc&
(i)
y
cos Nt/2 and y
=
= j
0
2-
cos kt/2, j=I,2,3, ...
A
jk
k=O ClIO
(ii) y
sin Nt/2 and y
=
= j
0
L-
sin kt/2, j=I,2,3, •••
A
jk
k=1
(Only case (i) applies to the N=O transition curve.) Note that the solution to the y (t) equation is not j
unique, as a "complementary" sOlution may be added to any particular solution.
We follow Levy and Keller (1963) and
make the sOlution unique by requiring that A
= 0, j)O, jN
i.e., by requiring that the coefficient of cos Nt/2 or sin Nt/2 in y (t) vanishes.
Note however that A
j
= 1.
ON
Now we make the substitutions (i) or (ii) and once again we collect terms, this time equating to zero the coefficient of cos kt/2 or sin kt/2, respectively.
We
find: 2 N k=O:
=
A 4
j
1 jO
A
2
j-l,2
L i=1
~
A
i
j-i,O
113
2 2 N - k
1
A
k>O:
jk
4
j-1,k+2
j-1,k-2
2
±A
+A
(A
=
j-1,2-k
j
L i=1
A i
j-i,k
where we have used the convention that A
a
=
for j< a or
jk k
+
sign refers to the two cases (i) and
(ii) respectively. These linear algebraic equations recursively define
A
, i.e. they may be used to solve for A
by starting
jk jk with j=O and increasing j. In order to obtain an expression for the coefficients ~, we set k=N in these j
equati9ns and use the fact that A
=
a
for j
> 0,
while
jN = 1:
A
ON 1
N=O:
= j
A
2
j-1,2
1
=
N)O:
+
A
2
j-1,N-2
±A
A
j-1,N+2
j-1,2-N
We now present a MACSYMA program which implements these recursive equations. DISPFUN(ALL) :
114
Here is a sample run and a
(Cl) LOADPRINT:FALSE$ (C2) LOADFILE(RECURSIVE,MAC);
(D2)
DONE
(C3) GO; (ID3) This program uses recursive functions to find I
tne transition curves in Mathieu's equation.
To call it,
us
(C6) TC()$ ENTER TRANSITION CURVE NUMBER N 2; ENTER DEGREE OF TRUNCATION 4
2
763 E
5 E
DELTA= - ------ +
+ 1
3456
12
4 5 E
2 E
DELTA=
+ 1
3456
12
(C7) DISPFUN(ALL); (E7) TC() := (INPUT(), SIGN: 1, FIND(), IF N> 0 THEN (SIGN
- 1, FIND(»)
(E8) INPUT() := (N:READ("ENTER TRANSITION CURVE NUMBER N"), M : READ("ENTER DEGREE OF TRUNCATION"» 2 N
(E9) FIND() := (DELTA
--, 4 I
FOR I THRU M DO DELTA: DELTA + D(I) E , PRINT ( "DELTA=", DELTA), PRINT ( " ,,»
116
(E10) A(J, K) := IF J
~LSE (IF J i
ELSE (IF J
<0
OR K
=o
AND K
=o
THEN 0 ELSE (IF K
=
THEN 0
N THEN 1
=
N THEN 0
A(J - 1, 2) ELSE (IF K
=o
THEN (-
----------2 2 N
- SUM ( D( I) A (J - I, 0), I, 1, J)}/ ( - - ) 4
ELSE (- (A(J - 1, K - 2) + A(J - 1, K + 2) + SIGN A(J - 1, 2 - K»/2 - SUM(D(I) A(J - I, K), I, 1,
2 N
2 -
K
J»/(-------»») 4
A(J - 1, 2) (Ell) D(J)
:=
IF N = 0 THEN - ----------2
ELSE - (A(J - 1, N - 2) + A(J - 1, N + 2) + SIGN A(J - 1, 2 - N»/2 (Dll )
DONE
117
The function TC (for Transition Curve) first calls INPUT, then assigns the variable SIGN the value +1 and calls FIND, causing the first transition curve to be found and displayed.
Then TC assigns SIGN the value -1 and calls
FIND again, in order to obtain the second transition curve. (The two values of SIGN correspond to the
± sign
present in
the derived equations.) Now let's look at FIND.
Using a FOR-DO loop, the
variable DELTA is defined in terms of a function D(), and its value is displayed. Thus the functions D(J) and A(J,K) are where all the work is done.
Examination of these functions, however,
shows that they are virtually literal copies of the mathematical definitions of
b
and A
j
derived.
as previously jk
Therefore the amount of programming involved in
setting up these recursive functions is minimal. A remarkable aspect of recursive functions is that in order to compute a particular A(J,K), the function A() must be able to call itself, a situation which would be unacceptable in BASIC, for example.
MACSYMA, however,
supports recursive functions, as does LISP, the language which MACSYMA is written in. However, this program,
whil~
easy to write, is very
inefficient (i.e. takes a long time to run) exactly because the recursive function A() is wastefully calling itself many times for the same argument values.
Here is a simple
and often quoted example, the Fibonacci sequence: F(I):; IF 1;0 THEN 1 ELSE( IF 1=1 THEN 1 ELSE F(I-1)+F(I-2) ) Consider how F(5)=8 is computed. gives F(4)+F(3). is F(2)+F(1).
A single call to F(5)
F(4), however, is F(3)+F(2), while F(3)
Thus F(2) is called three times in order to
evaluate F(5). This kind of inefficiency can be avoided by computing and storing in an array all of the relevant values of 118
A(J,K).
This can be accomplished by computing A(J,K) the
first time it is called, and then storing it in an array eiement, B[J,K], say.
01
Then whenever that particular value
A(J,K) is called again, it can be retrieved from the
anray B rather than being recalculated. In terms of the , F~bonacci sequence example, this procedure can be incorporated into the previous function F() as follows: F(I):= IF 1=0 THEN 1 ELSE( IF 1=1 THEN 1 ELSE( IF NOT NUMBERP(B[I]) THEN B[I]:F(I-1)+F(I-2) ELSE B[I] )) The MACSYMA function NUMBERP() returns TRUE if its argument is a number, and FALSE otherwise.
Here it is assumed that
the array B has no values bound to it before F() is called. To be sure that such is the case, one may give the command REMARRAY(B), which removes any values bound to the array B. This scheme is the basis of our next, and final shot at the transition curves in Mathieu's equation:
(Cl) LOADPRINT:FALSE$ (C2) LOADFILE(IMPROVED,MAC); (D2)
DONE
(C3) GO; (D3) This program uses an improved recursive
function scheme involving arrays to more efficiently generate transition curves in Mathieu's equation. Call it by typing: TC()
(C4) TC()$ ENTER TRANSITION CURVE NUMBER N OJ
ENTER DEGREE OF TRUNCATION 10; 10 68687 E
123707 E DELTA= -
4
6
8
29 E
2
7 E
E
32
2
+ -------- - ----- +
294912
409600
144
(C5) TC()$ ENTER TRANSITION CURVE NUMBER N 1;
ENTER DEGREE OF TRUNCATION 10; 10 114299 E DELTA=
83 E
12121 E
- ---------- +
---------
- ------ -
6370099200
117964800
552960 5
-
11 E
E
-----
- ---
10
9
114299 E
- ---------6370099200
83 E
32
1
E
E
8
- + 4 2
7
6
55 E
49 E
--------- - ------ + ------ + ----552960
117964800 5
+ ----4608
294912
4
11 E
120
2
+
8
12121 E
-
3 E
384
6
55 E 49 E ... ----- + ----294912 36864
4
4608
DELTA=
7
8
9
E
3 E
2 E
384
32
36864
8
E
1
+ - + 4 2
(C6) TC()$ ENTER TRANSITION CURVE NUMBER N
2~ ENTER DEGREE OF TRUNCATION i
lQ; I
10 4363384401463 E
8
1002401 E
1669068401 E
DELTA= 14447384985600 _
6 + ---------4976640
7166361600
4
763 E
2
5 E
------ +
10 2499767 E
3456
12
6
4
8
21391 E
DELTA= - -------------- + 14447384985600 7166361600
+ 1
289 E
2
5 E
E
3456
12
------- +
4976640
+ 1
(C7) DISPFUN(ALL); (E7) TC() := (INPUT(), SIGN: 1, FIND(), IF N
>
0 THEN (SIGN
- 1, FIND()))
(E8) INPUT() := (N:READ("ENTER TRANSITION CURVE NUMBER Nfl), M : READ(IIENTER DEGREE OF TRUNCATION"))
121
2 N
(E9) FIND() := (REMARRAY(B, E), DELTA
--, 4 I
FOR I THRU M DO DELTA: DELTA + D(I) E , PRINT ( II DELTA= ", DELTA), PRINT (II II» (EI0) A(J, K) := IF J
<0
OR K
<0
THEN 0
ELSE (IF J = 0 AND K = N THEN 1 ELSE (IF J = 0 THEN 0 ELSE (IF K = ·N THEN 0 THEN (IF K = 0
ELSE (IF NOT NUMBERP(B
J, K A(J - 1, 2) THEN B
J, K
(- ----------- - SUM(D(I) A(J - I, 0), I, 1, 2
2 N
J»/(--) ELSE B
(- (A(J - 1, K - 2) + A(J - 1, K + 2)
J, K
4
+ SIGN A(J - 1, 2 - K»/2 - SUM(D(I) A(J - I, K), I, 1,
2 N
2 - K
J»/(-------» 4
122
ELSE B » » J, K
:= IF NOT NUMBERP(E )
(Ell ) D(J)
J
i '}:'HEN (IF N I
=o
THEN E
-
-----------
J -
ELSE E
1, 2)
A(J 2
(A(J - 1, N -
2) + A(J - 1, N + 2)
J
+ SIGN A(J -
1, 2 - N»/2) ELSE E J
(Dll )
DONE This program is a slight modification of the previous
program RECURSIVE.MAC, but represents a tremendous improvement in reduction of execution time, especially for large truncation sizes.
It is by far the most practical of
the various programs presented here to treat Mathieu's equation. The only changes that IMPROVED.MAC has relative to RECURSIVE.MAC occur in the functions A(), D() and FIND(). A() has been modified to store its values in an array B the first time they are called, cf. the Fibonacci example. Similarly D() stores its values in an array E.
FIND()
contains the statement REMARRAY(B,E) to wipe the arrays B and E clean each time a transition curve is computed. It is to be noted that MACSYMA has provision for automatically storing the values of a function in an array the first time they are called.
The procedure is to define
the function using square bracket notation, e.g. A[J,K] rather than A(J,K).
We have chosen not to use this
procedure here, however, since the REMARRAY function wipes out not only the values of the "array associated function" A[J,K], but also its definition.
123
Example 3. DUffing's Equation The undamped DUffing's equation, 2
d x
3
x
+
+
E- x
=
€. F cos w"t
2
d't is a model for a nonlinear mechanical system which is driven by a sinusoidal forcing function (Stoker, 1950). When
E. is small and the forcing frequency w is close to
the natural frequency of unity, this equation exhibits one or more steady state periodic motions at the driving frequency.
Physically this represents entrainment of the
free oscillator (F=O) by the periodic forcing function. Our goal will be to obtain a small f
asymptotic expansion
for this periodic response. In contrast to Van der Pol's equation, which is nonlinear and autonomous, and to Mathieu's equation, which is linear and nonautonomous, Duffing's equation is both nonlinear and nonautonomous.
Nevertheless all three of
these classic differential equations can be put into the general class of problems having the form X"
+ x + € f(x,t, €. ) =
o.
For example, Van der Pol's equation, after the transformation t =
w~
, corresponds to the choice 2
f. ( 1 - x )
1
€.
f
=
- 1 2
w
124
x w
X'
while Mathieu's equation, after the transformation t = N
~
/2, corresponds to
f.
4
f
f
(w -
=
2t
+
1)
x •
cos
2
N
N
Now if we stretch time in DUffing's equation by setting t
= w(
f ) 1:
2
f.
where w = 1 + k
f
+ k
+. •• ,
1
2 we again obtain an equation of the same general class, this time with 3
1
tf
-
:;::
1
x +
fX
f.F cos t
-----
-----------
2
2
w
2
w
w
The significance of this observation is that we can expect that the MACSYMA program that treats one of these equations should be easily modified to treat any in this general class.
In particular we shall modify
VANDERPOL. MAC, presented in example 1 of this chapter, to handle DUffing's equation as well. As usual we expand 2
x( t) = y (t) + y (t)
f
+ y (t)
f.
+ •••
012 and substitute into the d.e., collect terms in f , and so on.
The resulting equations on y (t) and y (t) are:
a
1
125
Y
I
I
(t) + Y (t) = 0
0
0 3
y "(t) + y (t) + y (t) - 2 k Y (t) - F cos t = 0 1
1 0
0
1
Following Stoker (1950) we require that x(t) satisfy the initial conditions: x(O) = A implies y (0) = A and y (0) = 0 for i=I,2,3, .••
o
i
x'(O) = 0 implies y'(O) = 0 for i=0,1,2, •.. i
where A is the amplitude of the response x(t).
In fact, it
turns out that if x(t) is to represent a periodic response, then it
~
satisfy the zero initial velocity condition
(otherwise there would exist resonant terms in the sOlution).
Physically speaking, the steady state response
turns out to be in phase (or 180 degrees out of phase) with the driver in this frictionless system. We find y (t) = A cos t
o and the y (t) equation becomes 1 3
3
3A
A
Y 1
I
I
(t) + Y (t)
cos 3t + (F -
1
+ 2 k A) cos t
4
4
For no resonant terms, we require that
F
2
+ - A •
k
1
126
3
2 A
8
1
This relationship between the amplitude A and the frequency w (=l+k E + ••• ), with F as a parameter, is of physical j
1
~nterest. We would then find y (t), evaluate the arbitrary 1 ~onstants
of integration so as to satisfy the initial
donditions, substitute y (t) and y (t) into the y (t) . 0 1 2 equation, compute k
such that there are no resonant terms, 2
and so on. We present the following sample run of a program which completes this process to arbitrary order in
E :
(C1) LOADPRINT:FALSE$ (C2) LOADFILE(DUFFING,MAC);
(D2)
DONE
(C3) GO;
(D3) This program computes a perturbation sOlution for the periodic response of DUffing's equation. Call it by typing: DUFFING(N) where N is the order of truncation. (C4) DUFFING(3)$ 3
3
cos(3 T)
A
A
Y (T) = ----------- -
32
--------32
5 A Y (T)
2
COS(T)
5
2
COS(5 T)
= 1024
9 A
F COS(3 T)
+ --------------- 256
3 A
COS(3 T)
128
127
2
9 A
5
23 A
F COS(T)
COS(T)
+ ------------
256
1024 7
4
I
cosO
A
T)
13 A
F COS(5 T)
3 A
COS(5 T)
Y (T) = ----------- + ---------------- - -------------
3
32768
6144
2048 7
4
2
81 A F
COS(3 T)
423 A
+ ---------------2048
297
F COS(3 T)
+ ---------------
8192
2
16384 7
4
81 A F
COS(T)
COS (3 T)
A
1217 A
F COS(T)
547 A
COS(T)
- -------------- + ---------------- - ------------2048 24576 32768
3 E
3 (128 F
326 - 236 A
F
+ 221 A
9 F - 81 A )
W= - -----------------------------------------3
2048 A 223 E
(32 F
- 44 A
6
F + 21 A )
3 E (4 F -
3 A )
+ 1
2
8 A
256 A (C5) DISPFUN(ALL); (E5) DUFFING(N) := (SETUP1(N), SETUP2(N), FOR I THRU N DO (STEPl(I), STEP2(I) , STEP3(I), STEP4(I), STEP5(I) , OUTPUT1(I», OUTPUT2(N»
128
I
(E6) SETUP 1 (N) := (W
1, FOR I THRU N DO W
W+ K
E,
I
I
X : A COS(T), FOR I THRU N DO X
X + Y (T) E ) I
3 X
(E7) SETUP2(N) := (TEMPI
E X
DIFF(X, T, 2) +
+ 2
2
W
W
E F COS(T) -
----------, TEMPI
TAYLOR(TEMPI, E, 0, N),
2 W
FOR I THRU N DO EQ
COEFF(TEMPI, E, I» I
(E8) STEPI(I) := TEMPI
EXPAND(TRIGREDUCE(EXPAND(EV(EQ, I
MAKELIST([E , F ], J, I, I - 1), DIFF»» J
(E9) STEP2(I) := (F
J
SOLVE(COEFF(TEMPI, COS(T», K ), I
I
TEMPI
EV(TEMPI, F » I
129
(EI0) STEP3(I) := TEMPI
EV(ODE2(TEMPl, Y (T), T) , I %Kl
A , %K2
B )
I
I (Ell ) STEP4(I) := (TEMP2 : RHS ( TEMP 1 ) , TEMP2 : DIFF(TEMP2, T), TEMP2 SOLVE([EV(RHS(TEMPl), T
0) , EV(TEMP2, T
0) ] , [A , B ])) I
(EI2)
I
EV(TEMPl, TEMP2)
STEP 5 ( I ) : = E I
(E13)
OUTPUTl(I) := (PRINT(EXPAND(E )), PRINT(" II)) I
(EI4) OUTPUT2(N) := (PRINT( II W= II , EV(W, MAKELIST([F ], J, 1, N))), PRINT(" II)) J
(DI4)
DONE This program was written by making minor modifications
in VANDERPOL. MAC.
It is suggested that the reader compare
the two programs in order to see how they differ.
Instead
of explaining what each function called by DUFFING(N) does, we present the following semi-manual run, showing all the intermediate results:
130
(Cl) LOADPRINT:FALSE$ (t2) DERIVABBREV:TRUE$ 1 (C3) LOADFILE(DUFFING,MAC)i cfJ3)
DONE
(C4) SETUPl(2)i (P4) (bS) Wi
DONE 2
(DS)
+ K E + 1 1
K E 2
(C6) Xi 2
A COS(T) + E
(D6) (C7) SETUP2(2)i (D7)
Y (T) + E Y (T) 2 1
DONE
(C8) EQ [1] ;
3
+ A
( D8 )/R/ Y (T)
3
COS (T) + (- F - 2 K A) COS(T)
1
1
T T + Y (T)
1 (C9) EQ[2];
- 2 K
(D9)/R/ Y (T)
2
332 2 A COS (T) + 3 A Y (T) COS (T)
1
1
T T 2
+ (2 K
F + (- 2 K + 3 K ) A) COS(T) + Y (T) - 2 K Y (T) 121 211
(CIO) STEPI(1)1 3
A (DIO) Y (T)
3
COS(3 T)
3 A
COS(T)
+ ----------- - F COS(T) + -----------
4
I
4
T T
A COS(T) + Y (T)
- 2 K
1
I
(Cll) STEP2(1) 1 3
A (Dll) Y (T)
3
COS(3 T)
(4 F - 3 A ) COS(T)
+ ----------- + -------------------
4
1
4
T T
3
3 A
COS(T)
- F COS(T) + ----------- + Y (T) 4 3 4 F -
(D12)
=
[K
3 A
----------]
1
8 A
(Cl3) STEP3(1)1 3 A
(Dl3)
Y (T) = 1
COS(3 T)
----------- +
SIN(T) + B
A
32
1
1
(C14) STEP4(1)1 3 A
(014)
- -
= 0, B
[ [A
1
1
--J]
32
COS(T)
1
(CIS) STEPS(l)i 3
3
A
COS(3 T)
A
COS(T)
Y (T) = ----------- -
1 (~16)
32
32
STEP1(2)i 2
S
3 A (D16) Y (T)
COS(S T)
F COS(3 T)
9 A
+ ------------- + 128
2
32
T T 2
S
cos{3
3 A
T)
2
COS(T)
F
F COS(T)
11 A
- --------- +
16
32
4 A
S
21 A -
COS(T)
------------ -
A COS(T) + Y (T)
2 K
128
2
2
(Cl7) STEP2(2) i
2
S
3 A (Dl7) Y (T)
COS(S T)
9 A
F COS(3 T)
+ ------------- + ---------------
128
2
32
T T 2
S
3 A
COS(3 T)
(32 F
3 -
44 A
6
F + 21 A ) COS(T)
+ --------------------------------
16
128 A
F
5
2
2
11 A
COS(T) 4 A
F COS(T)
21 A
COS(T) + Y (T)
+ -------------32
128
2
(CIS) F[2]) 2
32 F (DIS)
[K
--
6
3 -
F + 21 A
44 A
-----------------------]
2
2
256 A (CI9) STEP3(2»)
S 2 5 A COS(5 T) + (36 A F - 24 A ) COS(3 T) (DI9) Y (T) 2
= ---------------------------------------1024
+ A SIN(T) + B COS(T) 2 2 (C20) STEP4(2») 2
36 A (D20)
= 0, B
[[A
2
--
S
F - 23 A
---------------]]
2
1024
(C2l) STEPS(2); S A (D21) Y (T) 2
2 COS(S T) + (36 A
S F - 24 A ) COS(3 T)
= ---------------------------------------1024 2
(36 A
S
F - 23 A ) COS(T) 1024
134
Questions of Convergence
\
These three examples have demonstrated how computer
~lgebra
can be utilized to perform perturbation
calculations to orders of accuracy which are unthinkable by nand calculation.
Whereas perturbation presentations in 2
classical textbooks are taken out to order
£ or
c ,
we
may now generate results using exact arithmetic (i.e., 20 without roundoff error) to order
t.
or higher.
When only one or two terms of a perturbation series were known, it made little sense to talk about whether the series was convergent or not. convergence become relevant.
Now, however, questions of One asks oneself what good is
it to know 20 terms of a power series in of
t
f , if the value
you are interested in lies outside the radius of
convergence of the series ? The purpose of this section is to review a method for improving the convergence of perturbation series (Van Dyke, 1974).
Although this sUbject is not, strictlY speaking,
part of computer algebra (since it involves considerable number crunching), it is included here because of the ease I
with which MACSYMA can manipulate the associated mathematical tools (Euler transformations and Pade approximants). The idea of the method is, given a power series in z, to approximate the location of the nearest singularity to the origin, z via w
= f(z),
=
s, and then to transform the complex plane
in such a way that the singular point s is
sent to infinity, while infinity in the z-plane is sent to the point w = 1.
When the resulting series is summed in
the neighborhood of w = I, one often obtains results which are good approximations to values of the function represented by the original series at points z which lie outside its radius of convergence! 135
In order to find an approximation for the nearest singularity z = s, we write the truncated series in z as a ratio of two polynomials (a Pade approximant), and we examine the zeros of the denominator. In the case that the nearest" singulari ty z = s is a single real root, the transformqtion w = f(z)
(called an
Euler tansformation) takes the form:
z
w = ----z - s
which sends z=O to w=O, z=s to w=oO, and z=oO to w=l.
If
the nearest singularity consists of a pair of complex conjugates, then
z
w
= ---------------V(z - s)(z -
s)
(Andersen and Geer (1982) used a transformation of this form to study an asymptotic expression for the frequency in Van der Pol's equation.) As a simple example of this procedure, we will present a MACSYMA computation based on a six-term Taylor expansion about z = 0 of the function
"""l-+-z- . Although in this case we know the nearest singularity s is located at z = -1 (a branch point), we will ignore this fact, and treat the truncated Taylor series by the Pade approximant/ Euler transformation procedure.
We will
suppose that the value of the series is needed at the point z = 2, located well outside of its circle of convergence:
136
(C1) LOADPRINT:FALSE$ (C2) (l+Z)** (l/2); (lD2) (\C3 )
''1\/
SQRT (Z + 1)
,i
TAYLOR(%'Z'O'~";
3
2
Z
Z
(D3)/T/ 1 +
4
16
128
21 Z - ----- + • .
- ---- +
8
6
7 Z
5 Z
Z
-- + 2
5
256
•
1024
(C4) D2,Z:2,NUMER; (D4)
1.7320508
(C5) D3,Z:2,NUMER; (D5) (C6)
/'2.
. ..-'''- ("'..
PADE(D31};~);
0.9375
3
2
+ 56 Z
7 Z (D6)
+ 112 Z + 64
[-------------------------] 3 Z
2 + 24 Z
+ 80 Z + 64
(C7) DENOM(PART(%,I»;
3 (D7)
Z
2 + 24 Z
+ 80 Z + 64
(C8) ALLROOTS(%); (D8) [Z = - 1.23191379, Z = - 2.57241687, Z = - 20.1956694] (C9) S:RHS(PART(%,I»; (D9)
- 1.23191379
(CI0) W=Z/(Z-S)i Z
(DI0)
W= Z + 1.23191379
137
(C11) SOLVE(%,Z);
= 1.2319138
RAT replaced 1.23191379 by 2401/1949 2401 W (D11 )
[Z - - -------------] 1949 W - 1949
(C12) TAYLOR(EV(D3,D11),W,0,6); 2401 W (D12)/T/ 1 +
2
3
12953395 W
41862426613 W
+ ----------- +
3898
118455573584
30388808 4
5
567810141483459 W
1986716042420281599 W
+ ------------------ + ---------------------1846959303321728 7199447364348095744 6
14171541236398611827447 W + -------------------------- + .
.
56126891652457754420224 (Cl3) D10,Z:2; (Dl3)
W = 0.618828386
(C14) D12,%,NUMER; (014)
wtfK
/Yl-::4-
1.71246177
~_._...
---
--")
~ ~t1I-<'.-,- .. l,ti~g7{,G,\/~
Comments: (C4)-(D5) At the point Z = 2, the function (D2) which was expanded has the value 1.73 .•• , while the truncated Taylor series gives .93... •
_0
(C6) PADE(A,B,C) expresses the Taylor series A as a degree B polynomial divided by a degree C polynomial. (C8) ALLROOTS(A) returns the (generally complex) roots of the real polynomial A. 138
Here MACSYMA relaxes its penchant
for exact arithmetic and does a little number crunching for ius.
:( CI0) The Euler transformation.
:(Cll) While it would be easy enough to invert the Euler 1
•
•
•
transformat1on to obta1n Z as a funct10n of W, why not let
MACSYMA do it for us? The "RAT replaced" message informs us that a floating point number is being
~onverted
to a rational number.
To
suppress these messages, set RATPRINT:FALSE.
(CI2) Here we plug the inverted Euler transformation into the truncated Taylor series, and Taylor-expand the reSUlting function about W = O.
(CI3),(CI4) Now we wish to obtain a numerical value corresponding to Z
= 2.
So first we use the Euler
transformation to find the corresponding value of W, and then we substitute it into the transformed series. (DI4) The result of our labors is the value 1.71 ••• , a reasonable approximation to the exact value (D4) of 1.73 ••• We will now apply this procedure to one of the perturbation series derived earlier in this chapter.
In
particular we choose the N = 0 transition curve in
20 Mathieu' s equation, which we will generate to order
E
by
using the program IMPROVED.MAC presented at the end of example 3 in this chapter.
Since the reSUlting series has 2
only terms of even power in convenience.
E
I
we will set z =
E
for
(C1) LOADPRINT:FALSE$ (C2) RATPRINT:FALSE$ (C3) LOADFILE(IMPROVED,MAC);
DONE
(D3)
(C4) GO; (D4) This program uses an improved recursive function scheme involving arrays to more efficiently generate transition curves in Mathieu's equation. Call it by typing: TC( ) (C5) TC()$ ENTER TRANSITION CURVE NUMBER N
0; ENTER DEGREE OF TRUNCATION
20; 20
4011632808829219892175301 E DELTA= -----------------------------
1789497024366772224000000 18 63642189915976296887 E
16 7534554811777337 E
- ------------------------ + -------------------44737425609169305600 8182428094955520
14 286241141477 E
12 8022167579 E
10 123707 E
- ---------------- + -------------- - ---------468202291200 19110297600 409600 6
8
68687 E
29 E
2
4
7 E
E
32
2
+ -------- - ----- +
294912
144
(C6) TAYLOR(EV(DELTA,E:SQRT(Z»,Z,0,10)1 2
Z
7 Z
- +
2
3
4
29 Z
5
68687 Z
123707 Z
294912
409600
----- +
32
144
6 7 8 8022167579 Z 286441141477 Z 7534554811777337 Z + ------------- - --------------- + ------------------19110297600 468202291200 8182428094955520 9
63642189915976296887 Z
10 4011632808829219892175301 Z + -----------------------------
44737425609169305600
1789497024366772224000000
+ •
(C7) PADE(%,5,5)$ (C8) DENOM(PART(%,l»$ (C9) EXPAND(%/PART(%,l,l»$ (C10) %,NUMER1 5 4 3 2 (D10) Z + 35.1146765 Z + 180.33298 Z + 314.211308 Z .0
+ 219.114727 Z + 52.719221
(C11) ALLROOTS(%)1 (D11) [Z = - 0.586014464, Z - - 0.763283804, Z = - 1.26402457, Z - - 3.18006518, Z = - 29.3212886] (C12) S:RHS(PART(%,l»; (D12) - 0.586014464 (C13) W=Z/(Z-S)1 Z
(Dl3)
W =
Z + 0.586014464
(C14) SOLVE(%,Z); 8506 W (D14)
[Z - - ---------------] 14515 W - 14515
(C15) D6,%$ (C16) TAYLOR(%,W,0,10)$ (C17) %,NUMER; 10 9 8 (D17) - 0.107914398 W - 0.113099942 W - 0.119180193 W 7 6 5 - 0.126452167 W - 0.135373509 W - 0.146695398 W 4 3 2 - 0.16176071 W - 0.183292566 W - 0.217885653 W - 0.293007232 W (CI8) FOR Z:.1 THRU 1 STEP .1 DO PRINT("Z=",Z," DELTA=",EV(D6,NUMER»; Z= 0.1 DELTA= - 0.0479932516 Z= 0.2 DELTA= - 0.092564207 Z= 0.3 DELTA= - 0.134379392 Z= 0.400000002 Z= 0.5 DELTA= Z= 0.6 DELTA= Z= 0.7 DELTA= Z= 0.800000004 Z= 0.900000006 Z= 1.0 DELTA= (D18)
DELTA= - 0.173835762 - 0.210576976 - 0.240894021 - 0.248687673 DELTA= - 0.180584956 DELTA= 0.112428293 0.996684566 DONE
(C19) FOR Z:.l THRU 1 STEP .1 DO PRINT("Z=",Z," DELTA=",EV(D17,D13,NUMER»i Z* 0.1
DELTA= - 0.047993251
Z7 0.2
DELTA= - 0.092564224
Z~I 0.3
DELTA= - 0.134382648
Z~ 0.400000002
DELTA= - 0.173918912
Z~
0.5
DELTA= - 0.211510338
Z~
0.6
DELTA= - 0.247399954
Z= 0.7
DELTA= - 0.281763457
Z= 0.800000004
DELTA= - 0.31472943
Z= 0.900000006
DELTA= - 0.346394427
Z= 1.0
DELTA= - 0.37683379
(D19)
DONE
Comments: (C6) Here we replace E by the SQRT(Z), and we cause the series to be internally represented as a Taylor series, since the function PADE only works on Taylor series. (C7) Here and in succeeding~ommands we use the $ to suppress the display of long intermediate results. (C8),(C9) We isolate the denominator of the Pade approximant and divide the resulting quintic by its leading coefficient so that its new leading coefficient is simply 5
Z.
This apparently arbitrary step is aimed at preventing
an ARITHMETIC OVERFLOW error message, which occurs here if ALLROOTS is directly applied to the untreated DENOM. (C12) The approximate value of the nearest singularity S is found to be -.586 . • . •
More extensive computation reveals
that in fact it is -.539 •.• (Hunter and Guerrieri, 1980). An important advantage of the Euler transformation
procedure is its insensitivity to inaccuracies in the approximate value of S. (C13),(C14) The Euler transformation and its inverse. (C15)-(C17) We replace Z by W as independent variable, and expand in a Taylor series about the origin. (D17) The improved series. 143
(C18),(C19) In order to compare the original and treated series, we compute their values at a set of points lying both inside and outside the circle of convergence of the original series. The results of this computation are displayed in the figure.
Note how the untreated series runs amuck outside
its radius of convergence. Comparison with independently obtained results of numerical integration of Mathieu's equation shows that the method works well on this example.
Nevertheless there are
no general theorems guaranteeing us that this method will always work.
In the words of Van Dyke (1974), "Readers who
are persuaded of the utility of these techniques for their own research are cautioned against over-enthusiasm."
l 1
R
-.1
.1
Graphic representation of original (0) and improved (I) series. series.
144
R is the radius of convergence of the original
Exercise. The Two Variable Expansion Method
\
While Lindstedt's method offers an approximation for
tpe limit cycle in Van der Pal's equation, it cannot approximate the transient behavior associated with the approach to the limit cycle.
A more formidable tool which
is suitable for such a problem is the two variable expansion method, also called the method of multiple scales (Nayfeh, 1973). We summarize the method in what follows.
The exercise
is to show how this computation can be accomplished using MACSYMA. In Van der Pol's equation, 2
d x
dx
2
+
x
{;
= a
(1-x )
f
dt
2
dt 2
S = w(
we set
f. ) t,
w(
e. ) =
f
1 + k 2
~ = where
~
3
+
f
J~
+
...
3
f: t
is stretched time and
~ is slow time.
involves considering x to be a function of both
The method
J
and
and hence the appearance of partial derivatives in the
"1.'
chain rule:
= dt
4X
dX
dx
3 ~
w
+
a~
E-
145
2
2
d x
~
-a
2
x
=
+
a! 41'].
2
2
2 ~ x
x
-----
+ 2 w f
w
~~
dt
2
2
f
d'Z
2
2
=y
x
We expand
+ y
o
t
+ y
f 1
and
+
2
substitute into Van der Pol's equation.
After cOllecting
terms, we find:
+ y
y
=
°
°1! 1
2
=-
+ Y
Y
°
1
~!
+ (1 - Y
2 Y
°S~
Y
o~
0
Note that these are partial differential equations since the y
I
S
are functions of both ~
and
l '
and hence
i
the "constants" of integration are taken to be functions of
'1: y (
°
!>, '1.)
= A
('7 ) cos !
+ B ('1) sins
o
0
We choose the initial conditions dx
x(O) =
u,
--(0) = 0
dt The first condition implies that
y (0, 0) = U
° 146
and
y (0, 0)
i
= 0,
i = I, 2 , 3, ...
The second condition requires that ~x
'Ox w
d~ E~panding
+
Z.
=a
when
tJ'Y[
~ = '1- = o.
this equation yields a sequence of initial
conditions, the first two of which are:
'dY
a a
(0,0) =
'dY
I)Y
a
1
(0,0) +
(0,0) =
a
The initial conditions turn out to require that B (1t)
a be zero, so that Y
= A (17.) cos ~
a
After sUbstituting
a
this expression for Y sin ~
into the Y equation, removal of the a 1 resonant term gives: 3
A
a 2 A
a
I
A
a
+ ----- =
a ,
4
where the initial conditions require that A (0) = Solving for A
a
('1 ) completely
u.
a
(S ' ~ ).
specifies y
a 147
Integrating the y
equation gives y ( 1
5,' )
in terms
1 (~)
new unknown functions of integration A
(~).
and B
1
1
The initial conditions require that A ('l) be zero, and 1
tMt
qA
~y
1
o
----(0,0) - - ----(0) ~~ ~! The function B
(~)
and the stretching constant k
1
may be 2
found from the requirement that there be no resonant terms in the y
equation, and so on. 2
148
Exercise Solution. The Two Variable Expansion Method
('C2) DERIVABBREV:TRUE$ 0C3) W:l+K[2]*E**2; 2
K
(D3)
E
+ 1
2
(C4) X:Y[0](XI,ETA)+Y[I](XI,ETA)*E+Y[2](XI,ETA)*E**2; 2
(D4)
E
Y (XI, ETA) + E Y (XI, ETA) + Y (XI, ETA). 2
°
1
(C5) DIFF(X,XI,2)*W**2 +2*W*E*DIFF(X,XI,I,ETA,1) +E**2*DIFF(X,ETA,2) +X -E*(1-X**2)*(DIFF(X,XI)*W+DIFF(X,ETA)*E)$ (C6) TAYLOR(%,E,0,2)$ (C7) FOR 1:0 THRU 2 DO EQ[I]:COEFF(%,E,I)$ (C8) EQ[O]; + Y (XI, ETA)
Y (XI, ETA)
(D8)/R/
°
XI XI
°
(C9) EQ[I]; 2
+ (Y (XI, ETA) - 1) Y (XI, ETA)
(D9)/R/ Y (XI, ETA) 1
XI XI
°
°
+ 2 Y (XI, ETA)
°
XI
+ Y (XI, ETA) 1
ETA XI
l.dQ
( CI 0 ) EQ [ 2 ] J 2 + (Y (XI, ETA) - I) Y (XI, ETA)
(DIO)/R/ Y (XI, ETA) 2
0
I
XI XI + 2 Y (XI, ETA)
XI
+ 2 K
Y (XI, ETA)
2
1
0
ETA XI
XI XI
+ 2 Y (XI, ETA) Y (XI, ETA) Y (XI, ETA)
0
0
1
XI 2 + Y (XI, ETA)
+ (Y (XI, ETA) -
0
0
I) Y (XI, ETA) 0
ETA ETA
ETA
+ Y (XI, ETA)
2
(CII) ODE2(EQ[O],Y[O](XI,ETA),XI)J (Dll)
Y (XI, ETA) = %KI SIN(XI) + %K2 COS(XI)
o (CI2) %,%KI:O,%K2:A[O](ETA)J (DI2)
Y (XI, ETA)
o
=A
(ETA) COS(XI)
0
(CI3) EQ[I],%,DIFF$ (CI4) EXPAND(TRIGREDUCE(EXPAND(%»)i
3
A (ETA) SIN(3 XI)
a (P14) Y (XI, ETA)
- ----------------- + Y (XI, ETA) 4 1
1
XI XI 3
A (ETA) SIN(XI)
a - 2 A (ETA)
SIN(XI) - --------------- + A (ETA) SIN(XI)
a
a
4
ETA (CIS) COEFF(%,SIN(XI»i 3
A (ETA)
a (DIS)
- 2 A (ETA)
- ------- + A (ETA)
a
a
4
ETA (C16) ODE2(%,A[O](ETA),ETA); (D16) - LOG(A (ETA) + 2) + 2 LOG(A (ETA»
a
a LOG(A (ETA) - 2)
= ETA
+ %C
a (C1?) LOGCONTRACT(%); 2
A (ETA)
a (D1?)
LOG(-----------) = ETA + %C 2
A (ETA) - 4
o
151
(C18) %,ETA:O; 2 A (0)
o (D18)
LOG(---------) = %C 2 A (0) -
4
o (C19) SUBST(U,A[O](O),%)i 2 U
(D19)
= %C
LOG(------) 2 U
-
4
(C20) SOLVE(%,%C)i 2 U
[%C
(D20)
= LOG(------)] 2 U
-
4
(C21) D17,%i 2
A (ETA)
o (D21)
U
LOG(-----------) = LOG(------) + ETA 2 2 A (ETA) - 4 U - 4
o (C22) SOLVE(%,A[O](ETA»i
152
2
ETA/2 2 %E
,
U
-----------------------,
([)22) [A (ETA)
o
ETA
2 2 U - U + 4)
SQRT(%E
ETA/2 2 %E
U
A (ETA) = -----------------------] a ETA 2 2 SQRT(%E U - U + 4) (C23) PART(%,2); ETA/2
2 %E
U
ETA
2 2 U - U + 4)
A (ETA) =
(D23)
a SQRT(%E
(C24) D14-COEFF(D14,SIN(XI»*SIN(XI)$ (C25) RATSIMP(%)i (D25) 3
4 Y (XI, ETA)
- A (ETA) SIN(3 XI) + 4 Y (XI, ETA)
o
1
1
XI XI 4
(C26) ODE2(%,Y[1](XI,ETA),XI); 3
A (ETA) SIN(3 XI)
a (D26) Y (XI, ETA) - - ----------------- + r~1 SIN(XI) 1 32 + %1<2 COS (XI)
153
(C27) %,%K1:B[1](ETA),%K2:0~ 3
A (ETA) SIN(3 XI)
o (D27) Y (XI, ETA) = B (ETA) SIN(XI) - ----------------1 32 1 (C28) EQ[2],D12,%,DIFF$ (C29)
EXPAND(TRIGREDUCE(EXPAND(%»)~
5
5 A (ETA) COS(5 XI)
o (D29) Y (XI, ETA) 128
2
XI XI 2
5 A (ETA) A (ETA)
o
COS(3 XI)
0
ETA 16 2
5
3 A (ETA) B (ETA) COS(3 XI)
o
1
+ --------------------------4
3 A (ETA) COS(3 XI)
o 64
3
3 A (ETA) COS(3 XI)
o COS (XI)
+ ------------------- + Y (XI, ETA) + 2 B (ETA) 32 2 1 ETA
1')4
2
3 A (ETA) A (ETA)
o
COS(XI)
0 ETA
+A ,
(ETA)
COS (XI) + ---------------------------4
0 ETA ETA
2
A (ETA) B (ETA) COS (XI) - 0
- A (ETA)
1
COS (XI) + ----------------------4
o ETA
5 A (ETA) COS(XI)
o -
- B (ETA) COS (XI) -
128
1 (C30)
A (ETA) COS (XI)
2 K
2
o
COEFF(%,COS(XI))1 2
3 A (ETA) A (ETA)
o
0 ETA
+ A (ETA)
(D30) 2 B (ETA)
+ -------------------4
o
1
ETA ETA
ETA
5
2
A (ETA) B (ETA)
o - A (ETA)
A (ETA)
o
1
+ --------------- - B (ETA) - -------
o
4
1
128
ETA -
2 K
A (ETA) 2
o 155
(C31) SOLVE(D15, 'DIFF(A[O](ETA),ETA»; 3
A (ETA) - 4 A (ETA)
a (D31)
[A (ETA)
0
- - -------------------] 8
a ETA (C32) DIFF(%,ETA);
(D32) [A (ETA)
=
a ETA ETA 2
- 4 A (ETA)
3 A (ETA) A (ETA)
a
a
a
ETA
ETA
-----------------------------------]
-
8
(C33) D30,%$ (C34) %,D31$ (C35) EXPAND(%); 5
2
A (ETA) B (ETA)
a (D35) 2 B (ETA)
7 A (ETA)
a
1
+ --------------- - B (ETA)
4
1
1
128
ETA 3
A (ETA)
A (ETA)
a
a
+ ------4
156
-
A (ETA) - -------
2 K 2
a
4
(C36) SOLVE(%,DIFF(B[l](ETA),ETA»i 2 (~36)
[B (ETA) 1
128) B (ETA)
- - «32 A (ETA)
o
5
1
3
- 7 A (ETA) + 32 A (ETA) + (- 256 K
o
0
- 32) A (ETA»/256]
2
0
(C37) RHS(PART(%,l»/RHS(PART(D31,l»i 2 5 3 (D37) «32 A (ETA) - 128) B (ETA) - 7 A (ETA) + 32 A (ETA)
o
1 0 0 3
+ (- 256 K
- 32) A (ETA»/(32 (A (ETA) - 4 A (ETA») 2 0 0 0
(C38) SUBST(A,A(O](ETA),%)$ (C39) SUBST(B,B(l](ETA),%)i 2 5 3 (32 A
+ 32 A
- 128) B - 7 A
+ (- 256 K
- 32) A
2
(D39) -------------------------------------------------3
32 (A (C40) ODE2('DIFF(B,A)=%,B,A)i
- 4 A) 2
(16 K(2) + 1) LOG(A
- 4)
(D40) B = A (- ------------------------16
(8 K(2) + 1) LOG(A)
2 7 A
+ ------------------- - ---- + %c) 4 64
157
(C41) %/K(2):-1/16i 2 7 A
LOG(A) (D41)
B
=A
-
(------
----
+ %C)
64
8
(C42) SUBST(B[l](ETA),B,%)$ (C43) SUBST(A[O](ETA),A,%)i 4-
7 A (ETA) 0
LOG(A (ETA» 0
(------------
(D43) B (ETA) = A (ETA) 1 0
-
--------- + %C)
64
8
(C44) DIFF(D27 ,XI); 3 3 A (ETA) COS(3 XI) 0 (D44)
y
(XI, ETA)
= B (ETA) COS(XI)
1
-
-------------------
1
32
XI (C45) RHS(%) ,D43$ (C46) o/~-RHS(PART(D31,1))$ (C47) %,XI:O,ETA:Oi LOG(A (0) ) 0
(D47) A (0) (%C + ---------0 8
-
2 7 A (0)
3 3 A (0)
0
0
-------) -
64
------- = 32 3
A (0) - 4 A (0)
o
0 8
158
(C48) SUBST(U,A[O](O),%)i 2 LOG(U) ~D48)
U (------
\
8
3
7 U
- ----
3
3 U
+ %C) -
64
-
U =
4 U
--------
32
8
(C49) SOLVE(%,%C) i 2 8 LOG(U) - 21 U (D49)
+ 32
---------------------]
[%C
64 (C50) D43,%i 2
8 LOG(U) - 21 U
+ 32
(D50) B (ETA) = A (ETA) (1
o
64 2
LOG(A (ETA»
7 A (ETA)
o
o + ------------ -
---------)
8
64
Comments: (C4) The functions Y[I] are declared to be functions of both XI and ETA. (C5) The Van der Pol equation, transformed to include two time variables, XI and ETA. (C6)-(ClO) The usual process of equating to zero the coefficient of powers of E. (Cll) ODE2 is used to solve the YEO] equation. (C12) One constant of integration is declared to be a function of ETA, while the other is set to zero to satisfy the initial conditions. (C13),(C14) The expression for yEO] is substituted into the Y[l] equation, and the result is simplified. (CIS) The resonant term is removed from the Y[l] equation. (C16) ODE2 is used to integrate the A[O] equation. 159
(CI7) LOGCONTRACT is used to put the integral in a more convenient form. (CI8)-(C20) The initial condition A[O](O)
=U
is used to
evaluate the arbitrary constant of integration %C. (C21) The value of the constant %C is substituted into the integral of the A[O] equation. (C22),(C23) SOLVE is used to extract an expression for A[O] itself. (D23) This expression for A[O] together with the expression for Y[O] given in (DI2) represent the zeroth order sOlution.
We now proceed to continue the calculation to
the first order in E. (C24),(C2S) The resonant terms in the Y[l] equation, already taken care of in (C1S), are removed. (C26) ODE2 is used to solve for Y[1]. (C27) The initial condition is used to set one constant of integration to zero, the other being declared a function of ~~.
(C28),(C29) In order to find the function B[I](ETA) we plug the derived expressions for yeO] and Y[1] into EQ[2]. (C30) The resonant term is removed from the Y[2] equation. (D30) This equation on B[l] is solved in steps (C31)-(C43) by the following strategy:
Instead. of sUbstituting
A[O](ETA) into (D30), we use the original differential equation on A[O], (DIS), to obtain expressions for the first and second derivatives of A[O] in terms of A[O] itself.
Then we consider B[l] to be a function of A[O] and
use the chain rule to obtain a differential equation which does not contain ETA.
The details are as follows:
(C31) SOLVE is used to obtain an expression for the first derivative of A[O] in terms of A[O] itself. (C32),(C33) DIFFerentiating (D31) gives an expression for the second derivative of A[O], which is substituted back into the B[l] equation (D30). (C34) All references to derivatives of A[O] are removed from the B[l] equation by replacing the remaining first .qerivatives by (D31). 160
(D35) The new B[l] equation. (~36)
,
SOLVE isolates the derivative of B[l].
(C37)-(C40) Using the chain rule in the form dB[l]
B[l]'
----- = -----dA[O]
where
I
MO]
I
represents differentiation with respect to ETA, we
divide the RHS's of (D36) by (D31).
In order not to
confuse ODE2 we tempornrily replace A[O] by A and B[l] by B in steps (C38) and (C39). (D40) The resulting expression for B as a function of A. (C41) Since A[O] approaches 2 as ETA goes to infinity (representing the stable limit cycle), the term 2
LOG(A
- 4)
becomes singular as ETA grows large.
For a bounded
solution we choose K(2) to eliminate this term. (C42)-(C43) We recast equation (D41) into the original variables A[O](ETA) and B[l](ETA). (C44)-(C49) We use the initial condition to solve for the arbitrary constant %C in (D43). (D50) The completed expression for B[l](ETA). together with the expression for Y[l],
This,
(D27), represents
the solution to first order in E.
161
On Bugs
Epilogue
Any computer system as large and complicated as MACSYMA is bound to have some bugs in it.
Consider this
example discovered by Professor C.Y. Hui: (C1) LOADPRINT:FALSE$ (C2) INTEGRATE(X*SIN(X)/(X**2+1),X,O,INF)i (D2)
0
Now it turns out that (Gradshteyn and Ryzhik, 1965) 00
x sin x
J
o
x
dx =
11' 2 e
2
+ 1
(See the plot of the integrand at the end of the appendix.) So here we have a bug in MACSYMA. In future versions of MACSYMA it is likely that this bug and others will be fixed.
Nevertheless there may be
other bugs, as yet undiscovered, lying deep within MACSYMA's bits. program.
Moreover, there may bugs in one's own
In either case the user is always faced with the
question of how can one be sure that a given computation is correct? One strategy is to attempt to achieve the same result by as diverse a route as possible.
As an example, consider
the three different methods for finding the transition curves in Mathieu's equation presented in chapter 4. Alternately, checking an algebraically-obtained analytical answer by a numerical technique offers some reassurance. But the fact is, you can never be completely confident about a result obtained from a complicated computer 162
program.
On the other hand, the same can be said of
computations and proofs done by hand (or rather, by brain). i
P~rhaps
the moral of the story is to resist the temptation
t9 think of the results of a computer algebra program (like ~CSYMA)
as being exact, but rather to view them with the
same skepticism which one would have about a very long hand c~lculation
or a complicated proof.
1h?
Sample BATCH Program
Appendix
An alternative procedure for programming in MACSYMA
entails writing the program with a word processor (outside of MACSYMA), and then loading the program into MACSYMA using the BATCH command.
A program so written consists of
a sequence of commands which could have been manually entered from the keyboard, as well as any comments one
A
wishes to add.
comment is a statement of the form
/* ... */ where the
represents the message.
Here is a sample program which was written to be BATCHed:
/* An interactive demonstration of MACSYMA */ LOADPRINT:FALSE$
/* Enter a FUNCTION of X to be differentiated */ FO:READ( ); DIFF(FO,X) ;
/* Enter a FUNCTION of X to be integrated */ Fl : READ ( ); INTEGRATE(F1,X) ;
/* Enter an ALGEBRAIC EQUATION to be solved for X */ F2:READ() ; SOLVE ( F2,X) ;
/* Enter a TRIG EXPRESSION to be simplified */ F3:READ() ; EXPAND(TRIGREDUCE(EXPAND(F3»);
/* Enter a COMPLEX FUNCTION of Z whose real part is to be found */ Z:X+%I*Yi F4:READ() i REALPART(F4) ; 164
/* Enter a 3 X 3 MATRIX to be inverted */ F5:ENTERMATRIX(3,3); INVERT(F5),DETOUT;
/* ~nter a FUNCTION of X to be Taylor-expanded about X=O */ F6:READ(); TAYLOR(F6,X,O,8);
/*
~nter
a FUNCTION of X to be plotted
from X=O to 10 */ F7:READ() ; XAXIS:YAXIS:TRUE$ PLOT(F7,X,O,10)i This program was created with a word processor and placed in a disk file named DEMO. TXT.
This is what a
typical run looks like:
(Cl) BATCH(DEMO,TXT); (C2) /* An interactive demonstration of MACSYMA */ LOADPRINT:FALSE$ (C3) /* Enter a FUNCTION of X to be differentiated */ FO:READ( ) ; SIN(X) ; (D3)
SIN(X)
(C4) DIFF(FO,X); (D4)
COS (X)
(C5) /* Enter a FUNCTION of X to be integrated */ Fl : READ ( ) ; SIN(X) ; (D5)
SIN(X)
(C6) INTEGRATE(Fl,X); (D6)
- COS (X)
165
(C7) /* Enter an ALGEBRAIC EQUATION to be solved for X */ F2:READ(); X**2+X+A; 2
(D7) (C8) SOLVE(F2,X);
X
+ X + A
SQRT(l - 4 A) + 1 (D8) [X
SQRT(l - 4 A) - 1
= - -----------------,
X
= -----------------]
2
2
(C9) /* Enter a TRIG EXPRESSION to be simplified */ F3 :READ( ) ; SIN(X)**S; S
(D9)
SIN (X)
(ClG) EXPAND(TRIGREDUCE(EXPAND(F3»); SIN(S X) (DIG)
S SIN(3 X)
S SIN(X)
---------- +
16
16
(C1l) /* Enter a COMPLEX FUNCTION of Z whose real part is to be found */ Z:X+%I*Yi (Dl1 )
%1 Y + X
(C12) F4:READ(); SIN(Z); (D12)
SIN(%I Y + X)
(C13) REALPART(F4); (Dl3)
166
SIN(X) COSH(Y)
8
.(CI4) /* Enter a 3 X 3 MATRIX to be inverted */ £5:ENTERMATRIX(3,3); ~s the matrix
4. General I Answer 1, 2,
1. Diagonal
2. symmetric
3. Antisymmetric
3 or 4
I
2; \
Row 1 Column 1:
1;
ROw 1 Column 2:
0;
Row 1 Column 3:
2;
Row 2 Column 2:
A;
Row 2 Column 3:
1;
Row 3 Column 3:
1;
Matrix entered. [ 1
0
2 ]
[
]
a
[
(DI4)
A
1 ]
[
]
[ 2
1
1 ]
(CI5) INVERT(F5),DETOUTi [ A-I
2
- 2 A ]
[
]
[
~
- 3
- 1
[
] ]
[ -
2 A
A
- 1
]
(DI5) - 3 A-I
(CI6) /* Enter a FUNCTION of X to be Taylor-expanded about X=O */ F6:READ(); SIN(X) ; (DI6)
SIN(X)
(CI7) TAYLOR(F6,X,0,S); 357 X (D17)/T/
X 6
X
X
+ --- + • • 120 5040 167
(C18) /* Enter a FUNCTION of X to be plotted from X=O to 10 */ E'7:READ() ; K*SIN(X)/(X**2+1); X SIN(X) (DI8) 2
X
+ 1
(CI9) XAXIS:YAXIS:TRUE$ (C20) PLOT(F7,X,0,10); **
8[ •
[•
*
*
6[. [
.
4[.
*
[•
* * * *
*
2[. [•
*
0[.
[. * 8[.
*
****
*
**
[.
6[.* [*
** *
*
**
*
4[. [
.
2[ • [
.
0[.
o
*
*
*** ** **
**
.
*
*
*
** **
* *****
2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 468 024
XORG=O.O YORG=-0.206810959 XDELTA=0.190000001 YDELTA=O.038 XMAX=9.8800005 YMAX=0.466476705 (D20) (D21) 168
DONE BATCH DONE
Glossary of MACSYMA Functions The purpose of this glossary is to offer the reader a sonvenient summary of the MACSYMA func"tions referred to in ~his
book.
These functions represent but a small fraction
of all the functions listed in the MACSYMA Reference Manual, and this list is not intended to replace the Manual. ADDCOL(A,Bl,B2, ••• ,Bn) appends the column vectors Bl, ••• ,Bn to the matrix A. ALLROOTS(A) finds all the (generally complex) roots of the polynomial equation A, and lists them in NUMERical format (i.e. to 9 significant figures). ATAN2(A,B) represents the arctangent of
AlB
BATCH(A,B) loads and runs a BATCH program with filenames A,B.
If the program resides in a directory which is
different from the current default directory, then the arguments DSK,C must be added, where C is the directory which holds the program.
See chapter 2 on file
specification. CLOSEFILE(A,B) creates a disk file with filenames A,B consisting of a display of all MACSYMA interactions which have occurred since the WRITEFILE command was given.
The
file is in text format, and is not reloadable into MACSYMA. COEFF(A,B,C) gives the coefficient of B raised to the power C in expression A.
C may be omitted if it is unity.
DENOM(A) gives the denominator of A. 169
DETERMINANT (A) gives the determinant of the square matrix A. DIFF(A,B1,C1,B2,C2, ••• ,Bn,Cn) gives the mixed partial derivative of A with respect to each Bi, ci times.
For
brevity, DIFF(A,B,l) may be represented by DIFF(A,B). 'DIFF( ••• ) represents the unevaluated derivative, useful in specifying a differential equation. DISPFUN(A,B,C, .•. ) displays the definitions of user defined functions A,B,C, •.••
DISPFUN(ALL) displays the
definitions of all user defined functions. EIGENVALUES (A) returns two lists, the first being the eigenvalues of the square matrix A, and the second being their respective mUltiplicities.
In order to use this
function you must have loaded the EIGEN package with LOADFILE(EIGEN,FASL,DSK,SHARE). EIGENVECTORS (A) does everything that EIGENVALUES does, and adds a list of the eigenvectors of A.
The EIGEN package
must be loaded to use this function. ENTERMATRIX(A,B) cues the user to enter an AxB matrix, element by element. EV(A,B1,B2, ••• ,Bn) evaluates A sUbject to the conditions Bi.
In particular the Bi may be equations, lists of
equations (such as that returned by SOLVE), or assignments, in which cases EV "plugs" the Bi into A.
The Bi may also
be words such as NUMER (in which case the result is returned in numerical format), DETOUT (in which case any matrix inverses in A are performed with the determinant factored out), or DIFF (in which case all differentiations in A are evaluated, i.e.
'DIFF in A is replaced by DIFF).
For brevity in a manual command (i.e., not inside a user defined function), the EV may be dropped, shortening the syntax to A,B1,B2, ..• ,Bn. 170
EXPAND(A) algebraically expands A. ~ultiplication
In particular
is distributed over addition.
~ACTOR(A) factors A.
,I IDENT(A) returns an AxA identity matrix. ILT(A,S,T) attempts to invert the Laplace transform A considered as a function of S.
The result is stated in
terms of the variable T. IMAGPART(A) returns the imaginary part of A. INTEGRATE (A, B) attempts to find the indefinite integral of A with respect to B.
INTEGRATE(A,B,C,D) attempts to find
the definite integral of A with respect to B taken from B=C to B=D.
The limits of integration C and D may be taken as
INF (positive infinity) or MINF (negative infinity). INVERT (A) computes the inverse of the square matrix A. LAPLACE(A,T,S) takes the Laplace transform of A considered as a function of T.
The result is stated in terms of the
parameter S. LHS(A) gives the left hand side of the equation A. LOADFILE(A,B,DSK,C) loads a disk file with filenames A,B from directory C.
If C is the current default directory,
then the last two arguments may be omitted.
The disk file
must be in the proper format (e.g. created by a SAVE command). LOGCONTRACT (A) converts expressions like LOG(U) + LOG(V) into LOG(UV).
171
MAKELIST(A,B,C,D) creates a list of A's (each of which presumably depends on B), concatenated from B=C to B=D. MATRIX(Al,A2, •.• ,An) creates a matrix consisting of the rows Ai, where each row Ai is a list of m elements, [Bl,B2, ••• ,Bm]. NUM(A) gives the numerator of A. ODE2(A,B,C) attempts to solve the first or second order ordinary differential equation A for B as a function of C. PADE(A,B,C) returns a list of Pade approximants of the Taylor series A.
The numerator of the result will have
degree bounded by B, and the denominator will have degree bounded by C.
Band/or C may be taken as INF, in which
case no bounds will be placed on the associated degrees, and all possible appropriate Pade approximants will be returned. PART(A,Bl,B2, •.. ,Bn) first takes the Blth part of A, then the B2th part of that, and so on. PLAYBACK (A) displays the last A (an integer) labels and their associated expressions. are played-back.
If A is omitted, all lines
See the Manual for other options.
PLOT(A,B,C,D) plots A as a function of B from B=C to B=D. See Volume 2 of the Manual for various options. PRINT(Al,A2, ••• ,An) displays the Ai's one after the other on the same line.
If it is desired to display a string of
alphanumeric characters, enclose them in double quotes, and treat the string as one of the Ai's. RATS IMP (A) simplifies A and returns a quotient of two polynomials. 172
READ(A1,A2, .•• ,An) displays the Ai's as in PRINT, and then reads in and evaluates a single expression from the , keyboard.
i I
REALPART(A) returns the real part of A.
, R~MARRAY(Al,A2,
.•. ,An) removes the arrays Ai from memory.
RHS(A) gives the right hand side of the equation A. SAVE([A1,A2,DSK,A3],B1,B2, ... ,Bn) creates a disk file with filenames Al,A2 in directory A3, of variables, functions, or arrays Bi.
The format of the file permits it to be
reloaded into MACSYMA using the LOADFILE command. Everything (including labels) may be SAVE'd by taking Bl equal to ALL. If A3 is the current default directory then DSK and A3 may be omitted. SOLVE(A,B) attempts to solve the algebraic equation A for the unknown B.
A list of solution equations is returned.
For brevity, if A is an equation of the form C = 0 it may be abbreviated simply by the expression C. SOLVE([A1,A2, .•• ,An],[Bl,B2, ••. ,Bn]) attempts to solve the system of n polynomial equations Ai for the n unknowns Bi. A list of sOlution equations is returned. STRINGOUT([Al,A2,DSK,A3],Bl,B2, .•• ,Bn) creates a disk file with filenames Al,A2 in directory A3, of variables (e.g. labels) Ri.
The file is in a text format and is not
reloadable into MACSYMA.
However the strungout expressions
can incorporated into a FORTRAN or BASIC program with a minimum of editing.
If A3 is the current default directory
then DSK and A3 may be omitted. SUBST(A,B,C) substitutes A for B in C.
173
TAYLOR(A,B,C,D) expands A in a Taylor series in B about B=C, up to and including the term (B-C)**D.
MACSYMA also
supports Taylor expansions in more than one independent variable; see the Manual for details. TRANSPOSE (A) gives the transpose of the matrix A. TRIGEXPAND(A) is a trig simplification function which use, the sum-of-angles formulas to simplify the arguments of individual SIN or COS's. E.g. TRIGEXPAND(SIN(X+Y)) gives COS(X) SIN(Y) + SIN(X) COS(Y). TRIGREDUCE(A) is a trig simplification function which use trig identities to convert products and powers of SIN and COS into a sum of terms, each of which contains only a single SIN or COS.
E.g., TRIGREDUCE(SIN(X) **2) gives
(1 - COS(2X))/2. TRIGSIMP(A) is a trig simplification function which replaces TAN, SEC, etc., by their SIN and COS equivalents It also uses the identity SIN()**2 + COS()**2 = 1. WRITEFILE(DSK,A) begins to make a record of all MACSYMA displays.
The record can be saved to disk in .a text form
which is not reloadable into MACSYMA using the CLOSEFILE command.
Here A is the directory into which the disk fil
is to be dumped.
If A is the current default directory,
all arguments may be omitted and one may simply type WRITEFILE().
174
References II Andersen, C.M. and Geer, J.F. Power series Expansions for the Frequency and Period of the Limtt Cycle of the Van der Pol Equation SIAM J. Appl. Math. 42:678-693 (1982) Bender, C.M. and Orszag, S.A. Advanced Mathematical Methods for Scientists and Engineers McGraw-Hill Book Company (1978) Golden, V.E. and the Mathlab Group Introductory MACSYMA Documentation: A Collection of Papers Laboratory for Computer Science, MIT 545 Technology Square, Cambridge, MA 02139 (1982) Goldstein, H. Classical Mechanics Second edition Addison-Wesley Publishing Company (1980) Gradshteyn, I.S. and Ryzhik, I.M. Table of Integrals, series, and Products Fourth edition Academic Press (1965) Hunter, C. and Guerrier, B. Deducing the Properties of Singularities of Functions from their Taylor Series Coefficients SIAM J. Appl. Math. 39:248-263 (1980) also see erratum in SIAM J. Appl. Math. 41:203 (1981)
175
Levy, D.M. and Keller, J.B. Instability Intervals of Hill's Equation Corom. Pure Appl. Math. 16:469-476 (1963) MACSYMA Reference Manual, 2 vols. The Mathlab Group Laboratory for Computer Science, MIT 545 Technology Square, Cambridge, MA 02139 Version 10 (1983) Nayfeh, A. Perturbation Methods John Wiley & Sons (1973) Stoker, J.J. Nonlinear Vibrations Interscience Publishers (1950) Van der Pol, B. On "Relaxation Oscillations" Phil. Mag. 7:978-992 (1926) Van Dyke, M. Analysis and Improvement of Perturbation Series Q. J. Mech. Appl. Math. 27:423-450 (1974)
176
Index $, 2,39,143 %, 4,6 %C, 9 %E, 2 %1, 2 %1.<1, 9,81,93 %K2, 9,81,93 %PI, 2 ',9,107
,"
11
0' 22
0' 2 :=, 7 ;, 2
=,
2
??, 36,39 ADDCOL, 24,169 ALLROOTS, 13R,ln9 Andersen & Geer (1982), 71,136 ARITHMETIC OVERFLOW, 143 Array associated
functio~,
Arrays, 22,119,123 ATAN2, 2,169 BASIC, 8,31,35,41,118 BATCH, 36,41,164,169 Bender & Orszag (1978), 71 BLOCK, 87 /
Boundary value problem, 25 BUffer, 38 Bugs, 162 Calculus of variations, 3 Chain rule, 4,42,46,51,161
123
CHANGE_FILEDEFAULTS, 34 characteristic equation, 30 CLOSEFILE, 31,34,35,169 COEFF, 14,169 Complex variables, 1 Conformal mapping, 1 Conservation of energy, 9,57 CONTROL G, 36 CONTROL K, 36,39 CONTROL Y, 37 Convergence, 135 Cyclic coordinates, 57 DENOM, 143,169 DERIVABBREV, 4 DETERMINANT, 20,98,170 DETOUT, 24,170 DIFF, 3,81,170 Difference equations, 98-100 Differential equations, see O.D.E. 's DISPFUN, 41,170 Double pendulum, 55 DUffing's equation, 124 Editor, 38 EIGEN, 23,33,34,170 EIGENVALUES, 170 EIGENVECTORS, 24,170 Electric circuit, 18 Elliptic integral, 11 ENTERMATRIX, 22,170 Euler transformation, 135,136 Euler's equation, 3 Euler-Lagrange equation, 3 EV, 23,81,170 EXPAND, 7,171 FACTOR, 24,171 Fibonacci sequence, 118,119 file specification, 33 178
FOR-DO loop, 22,83 FORTRAN, 31,35 Fou~ier
series, 13,96,97,112,113
Fun~tion, 7,41,93
FUNc\TIONS, 41 I
.
Generallzed momentum, 57 Global variables, 93 GO'i 41 ,87 Goldstein (1980), 57 Gradshteyn & Ryzhik (1965), 162 Gyroscopic effects, 61 Hamilton's equations, 57 Hamilton's principle, 3 Harmonic balance, 13 Hill's determinants, 97 Hunter & Guerrier (1980), 143 IDENT, 22,171 ILT, 18,171 IMAGPART, 2,171 Initial conditions, 9,15,42,64,72,89,99,126,,147 INTEGRATE, 5,11,171 INVERT, 24,171 Lagrange's equations, 3,52,57 Lagrangian, 3,52,57 Laplace expansion, 99 Laplace transform, 15,64 Laplace's equation, 1 LAPLACE, 18,171 Legendre pOlynomial, 49 Legendre's equation, 49 Levy & Keller (1963), 112 LHS, 15,171 Limit cycle, 71,145 Lindstedt's method, 71,145 LISP, 32,41,118 LOADFILE, 31,33,171 LOADPRINT, 2
Local variables, 93 LOGCONTRACT, 160,171 MAKELIST, 51,63,86,172 Mathieu's equation, 87,125 MATRIX, 22,172 Memory allocation, 83 Nayfeh (1973), 145 NUM, 172 NUMBERP, 119 NUMER, 13,170 O.D.E. 'S, 3,5,9,15,19,25,42,52,57,64,71,87,124,145 ODE2, 8,9,80,172 Oscillator, 9,71,87,124 PADE, 138,172 Pade approximant, 135,136,172 PART, 14,18,172 Particle in the plane, 61 Perturbation methods, 71 Plane pendulum with variable length, 3,53,59 PLAYBACK, 35,172 PLOT, 165,168,172 PRINT, 8,172 RATPRINT, 139 RATSIMP, 2,49,172 READ, 8,173 REALPART, 2,173 Recursive functions, 118 REMARRAY, 56,119,123,173 Resonant terms, 73,80,90,96,126,159-161 RHS, 15,173 Robotics, 52 RUN A, 74 SAVE, 31-34,45,173 Secular terms, see Resonant terms SHARE, 23,33,34 Similarity transformation, 24 Slow time, 145 180
SOLVE, 14,18,21,63,173 Stoker (1950), 71,72,87,90,97,124,126 , Storage management, 83 Stretched time, 71,125,145 I
STRtNGOUT, 31,35,173 SUB~T,
4,173
Tay~or
series, 12,42,136
TAYLOR, 12,13,137,174 Transition curves, 87 TRANSPOSE, 24,174 TRIGEXPAND, 14,174 TRIGREDUCE, 14,174 TRIGSIMP, 6,174 Two variable expansion method, 145 Van der Pol (1926), 71 Van der Pol's equation, 71,124,145 Van Dyke (1974), 135,144 WRITEFILE, 31,34,35,174