Computer Algebra in Applied Mathematics: An Introduction to MACSYMA
RHRand i
Cornell University
Computer Algebra in ...
38 downloads
571 Views
3MB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
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
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
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
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