0% found this document useful (0 votes)
10 views18 pages

Ifem ch05

Uploaded by

Umut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Ifem ch05

Uploaded by

Umut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

5

A Mathematica
Implementation
for the Example Truss

5–1
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–2

§5.1 COMPUTER ALGEBRA SYSTEMS


As their name indicates, symbolic computer algebra systems or CAS are programs designed to
perform symbolic manipulations following the rules of mathematics.1 The development of such
programs began in the late 1960s. The first widely available system — the “granddaddy” of them
all, called Macsyma — was developed using the programming language Lisp at MIT’s famous
Artificial Intelligence Laboratory.
The number and quality of symbolic-manipulation programs has expanded dramatically since the
availability of graphical workstations and personal computers has encouraged interactive and ex-
perimental programming. As of this writing the leading general-purpose contenders are the three
“big Ma” packages: Macsyma (now a commercial product)2 Maple and Mathematica.3 In addition
there are dozen of more specialized programs, some of which are available free or at very reasonable
cost. One such “glorified calculator” product is MathCAD.

§5.1.1 Why Mathematica?

In the present material Mathematica will be primarily exclusively for Chapters and Exercises that
develop symbolic and numerical computation for matrix structural analysis and FEM implementa-
tions. Mathematica is a commercial product developed by Wolfram Research. The Web site of this
company is located at http://www.wolfram.com. The main advantages of Mathematica are:
1. Campus license: CU’s faculty and staff (including TAs and GRAs in the latter) can get
Mathematica 3.0 for free from ITS. Typically this is installed on PCs and Macs from a CDROM.
The license expires after one year of use and must be renewed.4
2. Availability on a wide range of platforms that range from mainframes through PCs and Macs.
(But be sure your computer has plenty of memory; at least 64MB RAM.)
3. Up-to-date user interface with above average graphics. On all machines Mathematica offers
a graphics interface called the Notebook front-end. This is mandatory for serious work.
4. A powerful programming language.
5. Good documentation and abundance of application books at all levels.

1 Some vendors call that kind of activity “doing mathematics by computer.” It is more appropriate to regard such
programs as enabling tools that help humans with complicated and error-prone manipulations. As of now, only
humans can do mathematics.
2 As of July 1999, Macsyma is no longer distributed by Macsyma Inc, which has folded. That leaves only Maple and
Mathematica as head-on competitors for the general purpose market.
3 Another commonly used ‘Ma’: Matlab, does only numerical processing although an interface to Maple can be
purchased as a toolbox.
4 If you do not qualify for the campus license but want to have the program on your computer, and have some spare
cash, get a copy of Mathematica for Students from a campus bookstore [at CU Boulder: the Buffalo Chip at the
UMC Bookstore, or the Colorado Book Store]. You will need to show a student id. The PC and Mac versions are
reasonably priced (about $150 versus $1,495 list) and contain virtually all capabilities except inline help. For the
latter you will need to buy the hefty Mathematica Book (about $60) which can be inter alia used to improve your
upper body strength.

5–2
5–3 §5.1 COMPUTER ALGEBRA SYSTEMS

One word of caution. If you plan to be a heavy user of symbolic manipulation systems for research (as
opposed to instructional or recreational) work, having more than one general-purpose system at hand
is strongly advisable. The packages vary greatly in strength and reliability when diverse functional
aspects and areas of application are considered. For example, as noted above Mathematica is above
average in graphics, programming tools and documentation aspects. But several shortcomings are
evident, particularly the shoddy implementation of many capabilities, poor debugging facilities,
and multitude of irritating bugs.5
Macsyma has been around for over 20 years. Consequently it is battle tested: has a reliable engine,
excels at difficult tasks and comes endowed with an huge library. But procedural capabilities are
inflexible, the user interface is antiquated (1970s vintage) and its documentation is only digestible
to Lisp hackers. The writer has no direct experience with Maple, but has heard favorable comments
from users as regards its algebraic capabilities.
For the present course the computational demands will not be significant in terms of expression
complexity — after all, the course deals only with linear problems. But programming capabilities
are heavily used. Consequently the advantages of Mathematica in terms of availability, cost,
interfaces and documentation outweight the feeble power and erratic behavior of its mathematical
engine.

§5.1.2 Mathematica Versions: 4.0, 3.0, 2.2

As of this writing the latest commercial version of Mathematica is 4.0, which is being distributed
since August 1999.6 The previous major releases were 2.2 and 3.0. The main advantage of
3.0 over 2.2 is the superior typesetting capabilities of the Notebook front-end. Results can be
directly displayed in standard mathematical notation, which is obviously convenient for instructional
purposes. Another nice feature is the superior inline help facility: the entire Mathematica user
manual in not only inline, but is organized as a hyperlinked HTML document.
On the other hand, 3.0 is slower (much slower in some cases) and not as reliable as 2.2. 4.0 is
more reliable but even slower. The sparse but elegant Notebook interface of 2.2 has become a
“syntactic sugar” mudpit. The writer still uses 2.2 for research work while reserving 3.0 and 4.0 for
instructional material such as this course. Versions 3.0 and 4.0 are memory and disk hogs. Be sure
to have at least 64MB of RAM and generous disk space (the standard installation takes up about
200MB) to load these versions.
If you are new to Mathematica there is an excellent tutorial available for 3.0: The Beginner’s Guide
to Mathematica V3 by Jerry Glynn and Theodore W. Gray. (Gray was the original implementor of
the Notebook front end, so he understands the program first hand.) This can be ordered as a book
(ISBN 1-891289-00-4, $24.95 at amazon.com). Also available on CDROM from MathWare, Ltd,

5 Some shortcomings may be explained on the grounds that Mathematica is the latest general purpose system to be
developed, and the program be expected to improve as new versions are released. Some difficulties, however, are
inherent in the design and cannot be expected to be solved. As an example of shoddy implementation, try taking the
FortranForm of an array in version 3.0: the output is not legal Fortran of any vintage.
6 The educational version is supposed to upgrade to 4.0 by September 1999.

5–3
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–4

P. O. Box 3025, Urbana, IL 61208, e-mail: [email protected]. The CDROM can be installed
on the same directory as Mathematica, and is a hyperlinked version of the book.

§5.2 PROGRAMMING STYLE AND PREREQUISITES


The following material assumes that you are a moderately experienced user of Mathematica, or are
willing to learn to be one. If you are not a user, get a copy or secure access to the program at a lab,
and get the primer listed above. The Mathematica Book is just a reference manual and not good
for training.
Practice with the program until you reach the level of writing functions, modules and procedures
with relative ease. With the Notebook interface and a good primer it takes only a few hours.
When approaching that level you may notice that functions in Mathematica display many aspects
similar to C. You can exploit this similarity if you are proficient in that language. But Mathematica
functions do have some unique aspects, such as matching arguments by pattern, and the fact that
internal variables are global unless otherwise made local.
Although function arguments can be modified, in practice this should be avoided because it can
cause difficult to trace side effects.7 Since the programming style enforced here outlaws output
arguments, a function can only return its name. But since the name can be a list of arbitrary objects,
however, the restriction is not serious. Such restrictions on arguments and function returns are
closer in spirit to C than Fortran although you can of course modify C-function arguments using
pointers.
Our objective is to develop a symbolic program written in Mathematica that solves the example
plane truss as well as some symbolic versions thereof. The program will rely heavily on the
development and use of functions to perform specific tasks. Thus the style will be one of procedural
programming. The programs will not be particularly modular because Mathematica is not suitable
for that programming style.8
The code below uses a few language constructs that may be deemed as advanced, and these are
briefly noted in the text so that appropriate reference to the Mathematica reference manual by
Wolfram can be made.
All following program fragments were written in 1996 and tested on a Macintosh PowerPC computer
using the then standard version 2.2.2 and its front-end Notebook interface. They have not yet been
converted to 3.0 or 4.0 because of time constraints. Each fragment has its own Notebook cell, an
arrangement that facilitates in-cell testing.

§5.3 THE ELEMENT STIFFNESS FUNCTION


As our first code segment, Cell 5.1 shows a function that evaluates and returns the 4 × 4 stiffness

7 As an example of the unpleasantries of side effects consider the Fortran statement z = fun(x) + x where function
fun returns 1 while modifying its argument x to x+1. Is z set to 2*x or 2*x+1? That is compiler dependent.
8 And indeed none of the CAS packages in popular use is designed for strong modularity because of historical and
interactivity constraints.

5–4
5–5 §5.3 THE ELEMENT STIFFNESS FUNCTION

Cell 5.1 Element Stiffness Function for 2D Two-Node Bar

FormElemStiff2DTwoNodeBar[xyi_,xyj_,Em_,A_] := Module[
{c,s,dx,dy,L,Ke},
{dx,dy}=xyj-xyi; L=Sqrt[dx^2+dy^2]; c=dx/L; s=dy/L;
Ke=(Em*A/L)* {{ c^2, c*s,-c^2,-c*s},
{ c*s, s^2,-s*c,-s^2},
{-c^2,-s*c, c^2, s*c},
{-s*c,-s^2, s*c, s^2}};
Return[Ke]
];

Ke= FormElemStiff2DTwoNodeBar[{0,0},{10,10},100,2*Sqrt[2]];
Print["Numerical Elem Stiff Matrix: \n"]; Print[Ke//MatrixForm];
Ke= FormElemStiff2DTwoNodeBar[{0,0},{L,L},Em,A];
Ke=Simplify[Ke/. 1/Sqrt[L^2] -> 1/L];
Print["\nSymbolic Elem Stiff Matrix: \n"]; Print[Ke//MatrixForm];

matrix of a plane truss member (two-node bar) in global coordinates. This segment will be described
in detail below.

§5.3.1 Function Description


The function is called FormElemStiff2DTwoNodeBar. Such descriptive names are encouraged
by the language. The function takes 4 arguments:
xyi A 2-component array containing the {x, y} coordinates of node i.
xyj A 2-component array containing the {x, y} coordinates of node j.
Em The bar elastic modulus, E. See below as to why name E cannot be used.
A The member cross section area, A.
The use of the underscore after the arguments names when they are formally listed after the function
name is a requirement for pattern-matching in Mathematica. If, as recommended, you have learned
functions this aspect should not come as a surprise.
The function returns the 4 × 4 member stiffness matrix internally called Ke. The logic that leads
to the formation is straightforward and need not be explained in detail. Note, however, the elegant
direct declaration of the matrix Ke as a two-level list, which eliminates the fiddling around with
array indices typical of standard programming languages. The format in fact closely matches the
mathematical expression (3.4).

§5.3.2 Programming Comments


The function in Cell 5.1 uses several intermediate variables with short names such as dx, dy, s, c

5–5
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–6

Cell 5.2 Output from Program of Cell 5.1

Numerical Elem Stiff Matrix:

10 10 -10 -10
10 10 -10 -10
-10 -10 10 10
-10 -10 10 10

Symbolic Elem Stiff Matrix:

A Em A Em -(A Em) -(A Em)


----------- ----------- ----------- -----------
2 Sqrt[2] L 2 Sqrt[2] L 2 Sqrt[2] L 2 Sqrt[2] L

A Em A Em -(A Em) -(A Em)


----------- ----------- ----------- -----------
2 Sqrt[2] L 2 Sqrt[2] L 2 Sqrt[2] L 2 Sqrt[2] L

-(A Em) -(A Em) A Em A Em


----------- ----------- ----------- -----------
2 Sqrt[2] L 2 Sqrt[2] L 2 Sqrt[2] L 2 Sqrt[2] L

-(A Em) -(A Em) A Em A Em


----------- ----------- ----------- -----------
2 Sqrt[2] L 2 Sqrt[2] L 2 Sqrt[2] L 2 Sqrt[2] L

and L. It is strongly advisable to make these symbols local to the function to avoid names clashes
somewhere else. Mathematica provides the Module[ ...] construct9 to permit “hiding” those
names in the list after the function name as shown in Cell 5.1.10
The use of the Return statement fulfills the same purpose as in C or Fortran 90. Mathematica
guides and textbooks advise against the use of that and other C-like constructs. The writer strongly
disagrees: the Return statement makes clear what the function gives back to its invoker.

§5.3.3 Case Sensitivity

Mathematica, like most recent computer languages, is case sensitive so that for instance E is not the
same as e. This is fine. But the language designer decided that names of system-defined objects
such as built-in functions and constants must begin with a capital letter. Consequently the liberal

9 The name “Module” should not be taken too seriously: it is far away from the concept of modules in Ada, Modula or
Fortran 90. But such precise levels of interface control are rarely needed in symbolic languages.
10 The “global by default” choice is the worst one, but we must live with the rules of the language.

5–6
5–7 §5.4 MERGING A MEMBER INTO THE MASTER STIFFNESS

use of names beginning with a capital letter may run into clashes. If, for example, you cannot use
E because of its built-in meaning as the base of natural logariths.11
The decision appears harmless until one begins using the language. The use of lower and upper
case symbols is deeply ingrained in applications such as Structural Analysis. For example, after
two centuries E is universally used to denote the modulus of elasticity rather than e, which stands
for strain or (as superscript) element number. Similarly the use of capital (bold or italics) letters
for matrices is common in the scientific literature: K is a stiffness matrix whereas k is a spring
constant.
In the code fragments presented here, identifiers beginning with upper case are used for objects
such as the global stiffness matrix, modulus of elasticity, and bar area. When there is danger of
clashing with a protected system symbol, additional lower case letters are used. For example, Em
is used for the elastic modulus instead of E because the latter is a reserved symbol.

§5.3.4 Testing the Member Stiffness Function


The statements after the function definition test it for two cases. First, member (3) of the example
truss using all-numerical values. Next, some of the input arguments are given symbolic names so
they stand for variables; for example the elastic module is given as Em instead of 100 as in the first
test. The output of this test is shown in Cell 5.2.
The first test returns the member stiffness matrix (3.10) as may be expected. The second test returns
a symbolic form in which three symbols appear: L are coordinates of node j which is taken to be
located at (L , L) instead of (10, 10), A is the cross-section area and Em is the elastic modulus.12
The ability to carry along variables is of course a fundamental capability of computer algebra
systems and one of the major reasons for which such programs are used.

§5.4 MERGING A MEMBER INTO THE MASTER STIFFNESS


The next fragment of Mathematica code, listed in Cell 5.3, is used in the member-
assembly step. Function MergeElemIntoMasterStiff calls the member formation function
FormElemStiff2DNodeBar of Cell 5.1 and “merges” the member stiffness just formed into the
master stiffness matrix. The statements after the function provide a simple test.
Function MergeElemIntoMasterStiff takes three arguments:
Ke The 4 × 4 member stiffness matrix to be merged.
eftab The column of the Element Freedom Table, defined in §3.4.1, appropriate to the
member being merged; cf. (3.29). Recall that the EFT lists the global equation
numbers for the four member degrees of freedom.

11 In retrospect this appears to have been a highly questionable decision. All system defined names should have been
identified by a leading reserved character to avoid surprises, as done in Macsyma. The program, however, issues a
warning message if an attempt to redefine a “protected symbol” is made.
12 For this case a simplification construct had to be used so that Sqrt[L^2] was simplified to L in the Print statement.
Having to explicitly simplify patterns as in Cell 5.1 is one of the most annoying aspects of Mathematica, and one in
which it is behind Macsyma and Maple.

5–7
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–8

Cell 5.3 Merge-Element-Stiffness Function for 2D Two-Node Bar

MergeElemIntoMasterStiff[Ke_,eftab_,Kin_]:=Module[ {i,j,ii,jj,K=Kin},
For [i=1, i<=4, i++, ii=eftab[[i]];
For [j=i, j<=4, j++, jj=eftab[[j]];
K[[jj,ii]]=K[[ii,jj]]+=Ke[[i,j]]
]
]; Return[K]
];

K=Table[0,{6},{6}];
Print["Initialized Master Stiffness Matrix:\n"];
Print[K//MatrixForm]
Ke=FormElemStiff2DTwoNodeBar[{0,0},{10,10},100,2*Sqrt[2]];
Print["\nMember Stiffness Matrix:\n"];Print[Ke//MatrixForm]
K=MergeElemIntoMasterStiff[Ke,{1,2,5,6},K];
Print["\nMaster Stiffness after Member Merge:\n"];
Print[K//MatrixForm];

Kinp The incoming 6 × 6 master stiffness matrix.


The function returns the updated master stiffness matrix internally called K with the member stiffness
merged in. Thus we encounter here a novelty: an input-output argument. Because a formal argument
cannot be modified, the situation is handled by copying the incoming Kin into K on entry. It is the
copy which is updated and returned via the Return statement. The implementation has a strong
C flavor with two nested For loops. Because the iterators are very simple, nested Do loops could
have been used as well; the For version being used only for illustration purposes.
Before the first call to this function, the master stiffness matrix must be initialized to a zero 6 × 6
array. This is done in the first test statement using the Table function. The test member stiffness
matrix is that of member (3) of the example truss. Running the test statements yields the listing
given in Cell 5.4. The result is as expected.

§5.5 ASSEMBLING THE MASTER STIFFNESS

The function AssemMasterStiffOfExampleTruss, listed in Cell 5.5, makes use of the previously
described two functions to form the master stiffness matrix of the example truss. The listing of
AssemMasterStiffOfExampleTruss is self explanatory.
This function is similar in style to argument-less Fortran or C functions. It takes no arguments. All
the example truss data is “wired in.”
The output from the program in Cell 5.5 is shown in Cell 5.6. The output stiffness matches that in
Equation (3.20), as can be expected if all fragments used so far work correctly.

5–8
5–9 §5.6 MODIFYING THE MASTER SYSTEM

Cell 5.4 Output from the Program of Cell 5.3

Initialized Master Stiffness Matrix:

0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

Member Stiffness Matrix:

10 10 -10 -10
10 10 -10 -10
-10 -10 10 10
-10 -10 10 10

Master Stiffness after Member Merge:

10 10 0 0 -10 -10
10 10 0 0 -10 -10
0 0 0 0 0 0
0 0 0 0 0 0
-10 -10 0 0 10 10
-10 -10 0 0 10 10

§5.6 MODIFYING THE MASTER SYSTEM

Following the assembly process the master stiffness equations Ku = f must be modified to ac-
count for displacement boundary conditions. This is done through the computer-oriented equation
modification process described in §3.4.2.
Funtion ModifyMasterStiffForDBC carries out this process for the master stiffness matrix K,
whereas ModifyNodalForcesForDBC does this for the nodal force vector f. These two functions
are shown in Cell 5.7, along with test statements. The logic of both functions, but especially
that of ModifyNodalForcesForBC, is considerably simplified by assuming that all prescribed
displacements are zero, that is, the BCs are homogeneous. The more general case of nonzero
prescribed values is treated in Chapter 5.
Function ModifyMasterStiffnessForDBC has two arguments:
pdof A list of the prescribed degree of freedom identified by their global number. For the
example truss this list contains three entries: {1, 2, 4}.
K The master stiffness matrix K produced by the assembly process.

5–9
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–10

Cell 5.5 Function for Assembling the Master Stiffness of the Example Truss

AssembleMasterStiffOfExampleTruss[]:= Module[{Ke,K=Table[0,{6},{6}]},
Ke=FormElemStiff2DTwoNodeBar[{0,0},{10,0},100,1];
K= MergeElemIntoMasterStiff[Ke,{1,2,3,4},K];
Ke=FormElemStiff2DTwoNodeBar[{10,0},{10,10},100,1/2];
K= MergeElemIntoMasterStiff[Ke,{3,4,5,6},K];
Ke=FormElemStiff2DTwoNodeBar[{0,0},{10,10},100,2*Sqrt[2]];
K= MergeElemIntoMasterStiff[Ke,{1,2,5,6},K];
Return[K]
];

K=AssembleMasterStiffOfExampleTruss[];
Print["Master Stiffness of Example Truss:\n"];Print[K//MatrixForm];

Cell 5.6 Output from the Program of Cell 5.5

Master Stiffness of Example Truss:

20 10 -10 0 -10 -10


10 10 0 0 -10 -10
-10 0 10 0 0 0
0 0 0 5 0 -5
-10 -10 0 0 10 10
-10 -10 0 -5 10 15

The function clears appropriate rows and columns of K, places ones on the diagonal, and returns
the thus modified K as function value. The only slightly fancy thing in this function is the use
of the Mathematica function Length to extract the number of prescribed displacement compo-
nents: Length[pdof] here will return the value 3, which is the length of the list pdof. Similarly
nk=Length[K] assigns 6 to nk, which is the order of matrix K. Although for the example truss
these values are known a priori, the use of Length serves to illustrate a technique that is used
heavily in more general code.
Function ModifyNodeForcesForDBC has a very similar structure and logic and need not be de-
scribed in detail. It is important to note, however, that for homogeneous BCs the two functions are
independent of each other and may be called in any order. On the other hand, if there were nonzero
prescribed displacements the force modification must be done before the stiffness modification.

5–10
5–11 §5.7 RECOVERING INTERNAL FORCES

Cell 5.7 Modifying the Master Stiffness Equations of the Example Truss

ModifyMasterStiffForDBC[pdof_,K_] := Module[
{i,j,k,nk=Length[K],np=Length[pdof],Kmod}, Kmod=K;
Do [i=pdof[[k]]; Do [Kmod[[i,j]]=Kmod[[j,i]]=0, {j,1,nk}];
Kmod[[i,i]]=1,
{k,1,np}];
Return[Kmod]
];
ModifyNodeForcesForDBC[pdof_,f_] := Module[
{i,k,np=Length[pdof],fmod}, fmod=f;
Do [i=pdof[[k]]; fmod[[i]]=0, {k,1,np}];
Return[fmod]
];

K=Array[Kij,{6,6}];
Print["\nAssembled Master Stiffness:\n"];Print[K//MatrixForm];
K=ModifyMasterStiffForDBC[{1,2,4},K];
Print["\nMaster Stiffness Modified For Displacement B.C.:\n"];
Print[K//MatrixForm];
f=Array[fi,{6}];
Print["\nNode Force Vector:\n"];Print[f];
f=ModifyNodeForcesForDBC[{1,2,4},f];
Print["\nNode Force Vector Modified For Displacement B.C.:\n"];
Print[f];

This is because stiffness coefficients that are cleared in the latter are needed for modifying the force
vector.
The test statements are purposedly chosen to illustrate another feature of Mathematica: the use of
the Array function to generate subscripted symbolic arrays of one and two dimensions. The test
output is shown in Cell 5.8, which should be self explanatory. The force vector and its modified
form are printed as row vectors to save space.

§5.7 RECOVERING INTERNAL FORCES


Mathematica provides built-in matrix operations for solving a linear system of equations and mul-
tiplying matrices by vectors. Thus we do not need to write application functions for the solution
of the modified stiffness equations and for the recovery of nodal forces. Consequently, the last
application functions we need are those for internal force recovery.
Function GetIntForce2DTwoNodeBar listed in Cell 5.9 computes the internal force in an individual
member. It is somewhat similar in argument sequence and logic to FormElemStiff2DTwoNodeBar
(Cell 5.1) . The first four arguments are identical. Argument eft provides the Element Foreedom
Table array for the individual member. The last argument, vdis, is the vector of computed node

5–11
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–12

Cell 5.8 Output from the Program of Cell 5.7

Assembled Master Stiffness:

Kij[1, 1] Kij[1, 2] Kij[1, 3] Kij[1, 4] Kij[1, 5] Kij[1, 6]


Kij[2, 1] Kij[2, 2] Kij[2, 3] Kij[2, 4] Kij[2, 5] Kij[2, 6]
Kij[3, 1] Kij[3, 2] Kij[3, 3] Kij[3, 4] Kij[3, 5] Kij[3, 6]
Kij[4, 1] Kij[4, 2] Kij[4, 3] Kij[4, 4] Kij[4, 5] Kij[4, 6]
Kij[5, 1] Kij[5, 2] Kij[5, 3] Kij[5, 4] Kij[5, 5] Kij[5, 6]
Kij[6, 1] Kij[6, 2] Kij[6, 3] Kij[6, 4] Kij[6, 5] Kij[6, 6]

Master Stiffness Modified For Displacement B.C.:

1 0 0 0 0 0
0 1 0 0 0 0
0 0 Kij[3, 3] 0 Kij[3, 5] Kij[3, 6]
0 0 0 1 0 0
0 0 Kij[5, 3] 0 Kij[5, 5] Kij[5, 6]
0 0 Kij[6, 3] 0 Kij[6, 5] Kij[6, 6]

Node Force Vector:

{fi[1], fi[2], fi[3], fi[4], fi[5], fi[6]}

Node Force Vector Modified For Displacement B.C.:

{0, 0, fi[3], 0, fi[5], fi[6]}

displacements.
The logic of GetIntForce2DTwoNodeBar is straightforward and follows the method outlined in
§3.2. Member joint displacements ū(e) in local coordinates {x̄, ȳ} are recovered in array ubar, then
the longitudinal strain e = (ū x j − ū xi )/L and finally the internal force p = E Ae are computed.
The latter returns as function value. As coded the function contains redundant operations because
entries 2 and 4 of ubar (components ū yi and u y j ) are not actually needed to get p, but were kept
to illustrate the general backtransformation of global to local displacements.
Running this function with the test statements shown in Cell 5.9 produces the output in Cell 5.10.
The first test is for member (3) of the example truss using the actual nodal displacements (3.24).
It also illustrates the use of the Mathematica built in function N to produce output in floating-point
form (the 8 given as second argument of N requests 8 decimal places.) The second test does a
symbolic calculation in which several argument values are fed in variable form.
Cell 5.11 lists again GetIntForce2DTwoNodeBar as well as a higher-level function,
GetIntForcesOfExampleTruss. The latter has a single argument: u, which is the complete

5–12
5–13 §5.8 PUTTING THE PIECES TOGETHER

Cell 5.9 Function for Calculating Internal Force in an Individual Element

GetIntForce2DTwoNodeBar[xyi_,xyj_,Em_,A_,eft_,u_] := Module[ {c,s,


dx=xyj[[1]]-xyi[[1]],dy=xyj[[2]]-xyi[[2]],L,ix,iy,jx,jy,ubar,e},
L=Sqrt[dx^2+dy^2]; c=dx/L; s=dy/L; {ix,iy,jx,jy}=eft;
ubar={c*u[[ix]]+s*u[[iy]],-s*u[[ix]]+c*u[[iy]],
c*u[[jx]]+s*u[[jy]],-s*u[[jx]]+c*u[[jy]]};
e=(ubar[[3]]-ubar[[1]])/L;
Return[Em*A*e]
];

fe=GetIntForce2DTwoNodeBar[{0,0},{10,10},100,2*Sqrt[2],
{1,2,5,6},{0,0,0,0,0.4,-0.2}];
Print["Member Int Force (numerical): \n"]; Print[N[fe,8]];
fe=GetIntForce2DTwoNodeBar[{0,0},{L,L},Em,A,
{1,2,5,6},{0,0,0,0,ux3,uy3}];
Print["\nMember Int Force (symbolic): \n"]; Print[Simplify[fe]];

Cell 5.10 Output from the Program of Cell 5.9

Member Int Force (numerical):

2.8284271

Member Int Force (symbolic):

A Em (ux3 + uy3)
----------------
2 L

vector of joint displacements u. This function calls GetIntForce2DTwoNodeBar three times,


once for each member of the example truss, and returns the three member internal forces thus
computed as a 3-component list.
The test statements listed in Cell 5.11 feed the actual node displacements (3.24) to
GetIntForcesOfExampleTruss. Running the functions with these test statements yields the
output shown in Cell 5.12.

§5.8 PUTTING THE PIECES TOGETHER


After all this development and testing effort, we are ready to make use of all these bits and pieces

5–13
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–14

Cell 5.11 Calculation of Internal Forces of Example Truss

GetIntForcesOfExampleTruss[u_]:= Module[{f}, f=Table[0,{3}];


f[[1]]=GetIntForce2DTwoNodeBar[{0,0},{10,0},100,1,{1,2,3,4},u];
f[[2]]=GetIntForce2DTwoNodeBar[{10,0},{10,10},100,1/2,{3,4,5,6},u];
f[[3]]=GetIntForce2DTwoNodeBar[{0,0},{10,10},100,2*Sqrt[2],
{1,2,5,6},u];
Return[f]
];

f=GetIntForcesOfExampleTruss[{0,0,0,0,0.4,-0.2}];
Print["Internal Member Forces in Example Truss:\n"]; Print[N[f,8]];

Cell 5.12 Output from the Program of Cell 5.11

Internal Member Forces in Example Truss:

{0, -1., 2.8284271}

of code to analyze the example plane truss. This is actually done with the logic shown in Cell 5.13.
This “main program” uses the previously described seven functions

AssMasterStiffOfExampleTruss
FormElemStiff2DTwoNodeBar
GetIntForce2DTwoNodeTruss
GetIntForcesOfExampleTruss
MergeElemIntoMasterStiff
ModifyMasterStiffForDBC
ModifyNodeForcesForDBC

These functions must have been defined (”compiled”) at the time the main program is executed.
If, as recommended, you are using a Notebook interface, the simplest way to making sure that all
of them are defined is to put all these functions in the same Notebook file and to mark them as
initialization cells. These cells are then executed on request when the file is opened. (An even
simpler procedure is to group them all in one cell, but this inhibits placing separate test statements.)
The program listed in Cell 5.13 first assembles the master stiffness matrix through

5–14
5–15 §5.8 PUTTING THE PIECES TOGETHER

Cell 5.13 Program for Example Truss Analysis

f={0,0,0,0,2,1};
K=AssembleMasterStiffOfExampleTruss[];
Kmod=ModifyMasterStiffForDBC[{1,2,4},K];
fmod=ModifyNodeForcesForDBC[{1,2,4},f];
u=Simplify[Inverse[Kmod].fmod];
Print["\nComputed Nodal Displacements:\n"]; Print[u];
f=Simplify[K.u];
Print["\nExternal Node Forces Including Reactions:\n"]; Print[f];
p=Simplify[GetIntForcesOfExampleTruss[u]];
Print["\nInternal Member Forces:\n"]; Print[p];

(*Do[ListPlot[
{{0+t*u[[1]],0+t*u[[2]]},{10+t*u[[3]],0+t*u[[4]]},
{10+t*u[[5]],10+t*u[[6]]},{0+t*u[[1]],0+t*u[[2]]}},
Axes->False, PlotJoined->True, AspectRatio->Automatic],
{t,0,10,2}]; *)

Cell 5.14 Output from the Program of Cell 5.13

Computed Nodal Displacements:

2 1
{0, 0, 0, 0, -, -(-)}
5 5

External Node Forces Including Reactions:

{-2, -2, 0, 1, 2, 1}

Internal Member Forces:

{0, -1, 2 Sqrt[2]}

5–15
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–16

AssMasterStiffOfExampleTruss. Next, it applies the displacement boundary conditions


through ModifyMasterStiffForDBC and ModifyNodeForcesForDBC. Note that the modified
stiffness matrix is placed into Kmod rather than K to save the original form of the master stiffness
for the reaction force recovery later. The complete displacement vector is obtained by the matrix
calculation

u=Inverse[Kmod].fmod

which takes advantage of two built-in Mathematica functions. Inverse returns the inverse of its
matrix argument13 The dot operator signifies matrix multiply (here, matrix-vector multiply.) The
enclosing Simplify function is placed to simplify the expression of vector u in case of symbolic
calculations; it is actually redundant if all computations are numerical as in Cell 5.13.
The remaining calculations recover the node vector including reactions by the matrix-vector multiply
f = K.u (recall that K contains the unmodified master stiffness matrix) and the member internal
forces p through GetIntForcesOfExampleTruss. The program prints u, f and p as row vectors
to conserve space. The commented-out Do loop at the end is the topic of Exercise 5.5.
Running the program of Cell 5.13 produces the output shown in Cell 5.14. The results confirm the
hand calculations of Chapter 4.
At this point you may wonder whether all of this is worth the trouble. After all, the hand calculation
is certainly quicker in terms of flow time. Writing and debugging the Mathematica fragments
displayed here took the writer about 6 hours (although about two thirds of this was spent in editing
and getting the fragment listings into the Chapter.) And for bigger or more complex problems a
program in a conventional language, such as Fortran or C, is far more efficient in terms of computer
time.
The big payoff of symbolic tools appear when you need to parametrize the problem by leaving one
or more problem quantities as variables. For example suppose that the applied forces on node 3
are to be left as f x3 and f y3 . You replace the last two components of array p as shown in Cell 5.15,
press the Enter key and shortly get the symbolic answer shown in Cell 5.16. This is the answer to
an infinite number of numerical problems.
To a knowledgeable structural engineer this symbolic example would not look impressive. Because
the problem is linear, the same answer could be obtained numerically by solving the problem for two
unit load cases and combining the two answers multiplied by the force amplitudes. But suppose that
the symbolic variables affect the stiffness matrix in a nonlinear way; for example by parametrizing
one or more node locations. Then the inversion (or solve) process is likely to make its effect on
displacement solution highly complex. Although one may try to undertake such studies by hand,
the likelyhood of errors grows rapidly with the complexity of the system. Symbolic manipulation
systems can amplify human abilities in this regard. Examples of such nontrivial calculations will
appear throughout the following Chapters.

13 This is a highly inefficient way to solve Ku = f if this system becomes large. It is done here to keep simplicity.

5–16
5–17 §5.8 PUTTING THE PIECES TOGETHER

Cell 5.15 Program for Example Truss Analysis - Symbolic Calculation

f={0,0,0,0,fx3,fy3};
K=AssembleMasterStiffOfExampleTruss[];
Kmod=ModifyMasterStiffForDBC[{1,2,4},K];
fmod=ModifyNodeForcesForDBC[{1,2,4},f];
u=Simplify[Inverse[Kmod].fmod];
Print["\nComputed Nodal Displacements:\n"]; Print[u];
f=Simplify[K.u];
Print["\nExternal Node Forces Including Reactions:\n"]; Print[f];
p=Simplify[GetIntForcesOfExampleTruss[u]];
Print["\nInternal Member Forces:\n"]; Print[p];

Cell 5.16 Output from the Program of Cell 5.15

Computed Nodal Displacements:

3 fx3 fy3 -fx3 + fy3


{0, 0, 0, 0, ----- - ---, ----------}
10 5 5

External Node Forces Including Reactions:

{-fx3, -fx3, 0, fx3 - fy3, fx3, fy3}

Internal Member Forces:

{0, -fx3 + fy3, Sqrt[2] fx3}

5–17
Chapter 5: A MATHEMATICA IMPLEMENTATION FOR THE EXAMPLE TRUSS 5–18

Homework Exercises for Chapter 5


Symbolic Programming of Example Truss

EXERCISE 5.1
Enter the Mathematica functions and main programs listed in Cells 5.1, 5.3, 5.5, 5.7, 5.9, 5.11, 5.13 and 5.15
on your computer. Organizational details will vary according to whether you have a Notebook front end or
a standard “In-Out” command interface. If the former, functions in each input Cell listing may be place in
separate Notebook cells and the whole program can be kept in one file. If only a standard interface is available,
functions may be maintained as separate files that are loaded (using the Mathematica << operator) as needed.
A Notebook file may be sent by e-mail (will check in class as to the best distribution method).
To check, run the input boxes and verify that the outputs agree with those listed in the output boxes. As answer
for this Exercise, run the main program with f={0,0,px2,0,px3,py3} and return the output.

EXERCISE 5.2
Explain the rationale behind the use of Length in the functions of Cell 5.5. Why not simply set nk and np to
6 and 3, respectively?

EXERCISE 5.3
Explain the logic of the For loops in the merge function MergeElemIntoMasterStiff of Cell 5.3. What
does the operator += do?

EXERCISE 5.4
The code fragments listed here make use of built-in properties for the truss; for example bar moduli and
cross-section areas are directly specified through their numerical values. This is fine for expediency but causes
problems for generalization. To see this, try to run the example truss with a variable area called A3 for member
(3). There is no easy way to do this from the main program of Cell 5.13. You will need to go to functions
AssMasterStiffExTruss and GetIntForcesExTruss, guess that 2*Sqrt[2] therein has to be replaced
by A3, redefine the changed functions and finally run AnalyzeExTruss with them. This code-tweaking is an
error-prone process that will be corrected with the techniques explained in a later Chapter.
As solution, return the print output. After getting this one to work, note that the node displacements are
functions of A3 but that nodal forces and internal forces do not. Why do you think this happens?

EXERCISE 5.5
Enable the commented out statement at the end of the program in Cell 5.13, and execute. Explain the effect
(do not return output). If you have a Notebook interface try to “animate” the plot frame sequence by double
clicking on one of the pictures.

5–18

You might also like