You are on page 1of 56

Draft DRAFT

Lecture Notes in:

COMPUTING LITERACY
For Undergraduate Engineering Students

       
   
             % Dimension of elements
1 ! r_, " _# :$ r% & F1 ! " #
1
A=[ft,Sigma2,(fc-Sigma2)];
2 ! r_, " _# :$ r% & F2 ! " #
1 if (sigma(1)<=ft && sigma(1)>0)
w_nod=C1;
    ' (')  (* + ,-  /.0 *    1  2   3 4 elseif (sigma(1)<=0 && sigma(1)>Sigma2)
g1 ! r_, " _# :$ D ! 1 ! r, " # , r, r# 5 D ! 1 ! r, " # , r# 6 r 5 D ! 1 ! r, " # , " , " # 6 r2 for h=1:16
g2 ! r_, " _# :$ D ! g1 ! r, " # , r, r# 5 D ! g1 ! r, " # , r# 6 r 5 D ! g1 ! r, " # , " , " # 6 r2 w_nod(h)=C1(h)+M1(h)*sigma(1);
end
    ' ('      elseif (sigma(1)<=Sigma2 && sigma(1)>=fc)
F1 ! " _# $ Exp ! m ! 7 # " # for h=1:16
8 9 m: ; < w_nod(h)=C2(h)+M2(h)*(sigma(1)-Sigma2);
end
 =  > ? ' (A@     +    * > 0 *     else
eqdiff $ Simplify B Factor ! g2 ! r, " # # r3C % D w_nod=zeros(1,14);
8 9 m : ; < E E F 1 GH I 2 G m J H K 2 I E E 1 G H I 2 G m J H K 2I end
Solve ! eqdiff Exp ! L m ! 7 # " # M 0, m ! 7 # #
N N m J H K O F P E F 1 G H I Q , N m J H K O P E F 1 G H I Q , N m J H K O FP E 1 G H I Q , N m J H K O P E 1 G H I Q Q
    ' ('R   + * >2  >     

Victor E. Saouma
Dept. of Civil Environmental and Architectural Engineering
University of Colorado, Boulder, CO 80309-0428
Draft

Contents

1 MATLAB: Basic Introduction 17


1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.1.1 What is MATLAB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.1.2 Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.1.3 Accessing MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.1.4 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.1.5 Basic Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.1.6 Simple Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.1.7 Common Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.1.8 Statements, Expressions and Variables . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.1.9 Display Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.1.10 Printing Text and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.1.11 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.1.12 MATLAB Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.1.13 Saving and Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.1.14 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.1.15 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
1.1.16 Graphics hardcopy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.2 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.2.1 Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.2.2 Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.2.3 Stresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.2.4 Measurement Errors, [?] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.2.5 Statistical Analysis; Part I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.2.5.1 Elements of Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.2.5.2 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

2 MATLAB: Matrix Algebra 39


2.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.1.1 Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.1.1.1 Matrix Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.1.1.2 Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.1.1.3 Matrix functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.1.2 Graphics Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.1.2.1 Polar Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.1.2.2 3-D mesh plots. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Draft
CONTENTS

4.1.1.3 Front End and Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


5

81
4.1.1.4 Accessing Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.1.1.5 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.1.1.6 Notebook Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.1.1.6.1 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.1.1.6.2 Cell Brackets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.1.1.7 Brackets, Parentheses, and Braces . . . . . . . . . . . . . . . . . . . . . . 84
4.1.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.1.2.1 Basic Arithmetic Operation . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.1.2.2 Approximate Numerical Values . . . . . . . . . . . . . . . . . . . . . . . . 85
4.1.2.3 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.1.2.4 Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
4.1.2.5 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
4.1.2.6 Algebraic Formulae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
4.1.2.7 Solving equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
4.1.2.8 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.1.2.9 Graphics and Three-Dimensional Plots . . . . . . . . . . . . . . . . . . . 88
4.1.2.10 Interfacing with Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.1.2.10.1 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.1.2.10.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.1.2.11 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.1.2.12 Graphics hardcopy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.1.2.13 Input file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.1.3 Some Mathematica Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.1.3.1 Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.1.3.2 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.1.3.3 Some Mathematica Constants . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.1.3.4 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.1.3.5 Recall of Previous Expressions . . . . . . . . . . . . . . . . . . . . . . . . 92
4.1.3.6 Assignment of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.1.3.7 Brackets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.1.3.8 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.1.3.9 Interrupting Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4.1.3.10 Transformation of Algebraic Expressions . . . . . . . . . . . . . . . . . . 93
4.1.3.11 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.1.3.12 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.1.3.13 Summation & Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
4.1.3.14 Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
4.1.3.14.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
4.1.3.14.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
4.1.3.14.3 Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . 94
4.1.3.14.4 Numerical Values . . . . . . . . . . . . . . . . . . . . . . . . . . 94
4.1.3.15 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
4.1.3.16 Vectors & Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
4.1.3.17 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
4.1.3.17.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
4.1.3.17.2 Plot Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
4.1.3.17.3 3 Dimensional Plots . . . . . . . . . . . . . . . . . . . . . . . . . 96

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
CONTENTS

5.8.2 Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7

135
5.8.2.1 infiltration.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
5.9 montecarlo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.9.1 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.9.2 Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.9.2.1 montecarlo.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.10 3dplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.10.1 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.10.2 Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
5.10.2.1 cont.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
5.10.2.2 pix.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
5.10.2.3 smooth.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.11 zec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.11.1 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.11.2 Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
5.11.2.1 linregress.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
5.11.2.2 zec.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
5.12 Stress/Strain Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
5.12.1 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
5.12.2 Sample Output: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
5.12.2.1 Example 1: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
5.12.2.2 Example 2: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
5.12.2.3 Example 3: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

6 Fortran 90: INTRODUCTION 153


6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
6.1.1 Computer Architecture and Software . . . . . . . . . . . . . . . . . . . . . . . . . . 153
6.1.1.1 Computer Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
6.1.1.2 Computer Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
6.1.2 Fortran Past, Present, and Future . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
6.2 From Source to Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.3 Sample Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.4 Fortran Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.4.1 Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.4.2 File Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
6.4.3 Workstation Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
6.4.4 Compiling with Microsoft Power Station . . . . . . . . . . . . . . . . . . . . . . . . 158
6.5 Flow Charts, Macro-Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

7 Fortran 90: Basic Operations 161


7.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
7.1.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
7.1.1.1 Program Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
7.1.1.2 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
7.1.1.3 Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
7.1.1.4 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
7.1.2 Data Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
7.1.3 Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
CONTENTS

9 Fortran 90; PROGRAM UNITS; POINTERS


9

189
9.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
9.1.1 Program Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
9.1.1.1 Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
9.1.1.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
9.1.1.3 Recursive Subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
9.1.1.4 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
9.1.1.5 †Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
9.1.1.6 † Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
9.1.2 Derived Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
9.1.3 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
9.1.3.1 Pointer Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
9.1.3.2 Pointer Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
9.1.3.3 Dynamic Memory Allocation for Pointers . . . . . . . . . . . . . . . . . . 195
9.1.3.4 Pointers to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
9.1.3.5 Pointers as a Tool for Linked Lists . . . . . . . . . . . . . . . . . . . . . . 196
9.2 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
9.3 Mendeleev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
9.4 Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

10 Fortran 90: MAKE, DEBUGGER 199


10.1 Debugging options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
10.2 FORTRAN Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
10.2.1 System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
10.3 Make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
10.4 Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
10.5 Introduction to Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
10.5.1 The debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
10.5.2 Running the debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
10.5.3 Debugger Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

11 INTRODUCTION TO UNIX 209


11.1 logging In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
11.2 Changing Your Password . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
11.3 Logging Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
11.4 On-Line Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.4.1 man . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.4.2 Apropos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.5 UNIX File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.5.1 Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.5.2 Your User Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.5.3 Absolute Pathnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.5.4 Relative Pathnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.6 Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.7 File Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.7.1 Changing Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.7.2 Changing File Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.7.3 Copying Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
CONTENTS

11.17.3 Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


11

227
11.18“Standard” Unix Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
11.18.1 vi Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
11.18.2 Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
11.19Other Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

List of Figures

1.1 Matlab’s Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18


1.2 Set Path in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.3 Selecting path Directory in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.4 Directory Containing Source Code in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.5 Matlab’s Operating Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.6 Simple Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
1.7 Multiple Plots in a Single Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.8 Normalized Gauss Distribution, and Cumulative Distribution Function . . . . . . . . . . . 37

2.1 Polar Plot of a Cardioid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44


2.2 Sample of Surface and Contour Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.3 Picture Used for Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.4 Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.5 Regression Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.6 Bonded Strain Gage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.7 Strain Rosette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.8 Steel Truss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.9 Single Degree of Freedom Oscillator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.10 Simply Supported Beam Column; Differential Segment; Effect of Axial Force P . . . . . . 61

3.1 Newton-Cotes Numerical integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66


3.2 Center of Gravity and Moments of Inertia of A Triangular Cross-Section . . . . . . . . . . 68
3.3 Runge’s Midpoint Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.4 Runge’s Trapezoid Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.5 4th Order Runge-Kutta Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
3.6 Runge-Kutta Solution for the Mass-Spring-Damper System . . . . . . . . . . . . . . . . . 76
3.7 Equivalent and Exact Stress Distribution in Reinforced Concrete Beams . . . . . . . . . . 77

4.1 Notebook Front End for Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82


4.2 Notebook Front End Help for Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.3 Two-dimensional Plot generated from Mathematica . . . . . . . . . . . . . . . . . . . . . . 88
4.4 Three-dimensional Plot generated from Mathematica . . . . . . . . . . . . . . . . . . . . . 89

5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Draft

List of Tables

1.1 Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20


1.2 Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.3 Common Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.4 Display Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.5 Text and String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.6 Special Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.7 Disk Files Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.8 Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.9 2D Graphics Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.10 Printer Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2.1 Built In Matrix Definition Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39


2.2 Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.3 Matrix Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.4 Statistical Analysis Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.5 Signal Processing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.6 Relations and Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.7 Relational and Logical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.8 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.9 Strain Rosette Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.10 Result of Truss Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

3.1 Weights for Newton-Cotes Quadrature Formulas . . . . . . . . . . . . . . . . . . . . . . . 66


3.2 Numerical Integration and Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.3 Nonlinear Equations and Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.4 Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

4.1 Mathematica Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

6.1 PC Vs Workstation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155


6.2 Fortran Compilers in the Bechtel Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.3 Fortran Compiler Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

7.1 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161


7.2 Basic Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
7.3 “Truth Table” for Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
7.4 Common Arithmetic Intrinsic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Draft

Chapter 1

MATLAB: Basic Introduction

1.1 Background
1.1.1 What is MATLAB?

1 MATLAB is an interactive, matrix-based system for scientific and engineering calculations. Contrarily

to programming languages, such as Fortran, C, or Basic you can solve complex numerical problems
without actually writing a program.
2 MATLAB is an abbreviation for MATrix LABoratory.
3 MATLAB is the most widely used software package for interactive numeric computation, graphics,

data analysis1 .

1.1.2 Availability

4 MATLAB is available in the CAD lab (PC/Windows version), as well as in the Bechtel Laboratory.
5 A relatively inexpensive Student Edition is available from Prentice Hall and can be purchased from
the Buffalo Chip.

1.1.3 Accessing MATLAB

6 Once you clicked on the MATLAB icon, the menu shown in Fig. 1.1 will be displayed which may be
accomplished through browsing, Fig. 1.1.3.
7 Fig. 1.1.3 illustrates the content of the default directory containing your source code.
8 Finally, Fig. ?? illustrates a typical working envirnoment, where a file has been selected for editing in
one window, and program is executed in the other. Note that upon completion of the program, you can
always querry values in the MATLAB main window.
9

1 Other similar products include IDL, and MATHEMATICA for symbolic computations.
Draft

Chapter 2

MATLAB: Matrix Algebra

2.1 Syntax
2.1.1 Matrix Operations
2.1.1.1 Matrix Definition

1 Matrices can be entered in several different ways:


• Entered by an explicit list of elements, or built from blocks. For example,

A=[1 4 6; 2 1 8; -2 7 -9];
B = [A, ones(3,2); zeros(2,3), eye(2)]}
will build a 5-by-5 matrix.

• Generated by built-in statements and functions, Table 2.1. For example, ones(m,n) produces an

eye identity matrix


zeros matrix of zeros
ones matrix of ones
diag see below
triu upper triangular part of a matrix
tril lower triangular part of a matrix
rand randomly generated matrix
hilb Hilbert matrix
magic magic square
toeplitz see help toeplitz

Table 2.1: Built In Matrix Definition Functions

m-by-n matrix of ones; if A is a matrix, then ones(A) produces a matrix of ones of the same size
as A.
If x is a vector, diag(x) is the diagonal matrix with x down the diagonal; if A is a square matrix,
then diag(A) is a vector consisting of the diagonal of A.
Draft
2.1 Syntax

x(1,:)
41

ans =
3 -1 5

z=[y(1,:);y(3,:)]
z =
3 -1 5
5 -3 9

v=[0.2 1.4 2.6]


v =
0.2000 1.4000 2.6000

diag(v)
ans =
0.2000 0 0
0 1.4000 0
0 0 2.6000

2.1.1.2 Matrix Operations

4 Matrix operations are listed in Table 2.2.

+ addition
− subtraction
∗ multiplication
b power
0
transpose
\ left division
/ right division

Table 2.2: Matrix Operations

5 The “matrix division” operations deserve special comment. If A is an invertible square matrix and b
is a compatible column, vector, then x = A\b is the solution of A ∗ x = b

>> A=[2 3;1 4];b=[8;9];AInv=inv(A);


>> x=AInv*b
x =
1
2

>> x=A\b
x =

1
2

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
2.1 Syntax

0.5000 -0.9167 -6.2917


43

EDU>> triu(lux)
ans =
4.0000 2.0000 3.0000
0 6.0000 11.5000
0 0 -6.2917

EDU>> tril(lux)
ans =
4.0000 0 0
-0.2500 6.0000 0
0.5000 -0.9167 -6.2917

EDU>> tril(lux,-1)
ans =
0 0 0
-0.2500 0 0
0.5000 -0.9167 0

EDU>> diag(lux)
ans =
4.0000
6.0000
-6.2917

EDU>> eig(x)
ans =
4.4123
12.9438
2.6440

EDU>> x=[0.5 0.25;0.25 0.5]


x =
0.5000 0.2500
0.2500 0.5000

EDU>> [V,D]=eig(x)
V =
0.7071 0.7071
-0.7071 0.7071
D =
0.2500 0
0 0.7500

EDU>> x*V-V*D
ans =
0 0
0 0

2.1.2 Graphics Revisited


2.1.2.1 Polar Plots
theta=0:pi/60:2*pi;r=2*(1-cos(theta));polar(theta,r);axis square
print -deps2 polar.eps

resulting in Fig 2.1

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
2.1 Syntax

1
0.1
45

0.8
1
0.6
0.4
0.8
0.4 0.8
0.9
0.6 0.2

0.7
0.4 0

−0.2 0.6
0.2

−0.4
0
1 0.3
−0.6 0.5
0.5 1
0 0.5
−0.8
0 0.2
−0.5
−0.5
−1
−1 −1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

Figure 2.2: Sample of Surface and Contour Plots

for fac=-5:1:5 % loop


k=k+1;
z=fac*N8;
surf(X,Y,z);
%axis off;
axis([-1 1 -1 1 -5 5]) % freeze the axis to user specified values
M(:,k)=getframe; % grab the frame into M
end
movie(M,10) % play back 10 times

will generate the animation of Fig. 2.3

−5
1
0.5 1
0 0.5
0
−0.5
−0.5
−1 −1

Figure 2.3: Picture Used for Animation

9 Note that alternatively you may generate the image within a loop, and insert a pause after each one.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
2.2 Assignment

2.2.7 Nonlinear Equation


61

2.2.7.1 Theory
Considering a beam-column subjected to axial and shear forces as well as a moment, Fig. 2.10, taking
dv
the moment about i for the beam segment and assuming the angle dx between the axis of the beam and

w(x)
P P
x

dx
y,u

w
M
δV
P i V+ δx dx
V
δv P
δx
dx
M+ δM
δx dx

θi i P
P
j
P
θj
dx

Figure 2.10: Simply Supported Beam Column; Differential Segment; Effect of Axial Force P

the horizontal axis is small, leads to


     
dM (dx)2 dV dv
M− M+ dx + w + V + dx − P dx = 0 (2.25)
dx 2 dx dx
neglecting the terms in dx2 which are small, and then differentiating each term with respect to x, we
obtain
d2 M dV d2 v
2
− −P 2 =0 (2.26)
dx dx dx
However, considering equilibrium in the y direction gives
dV
= −w (2.27)
dx
From beam theory, neglecting axial and shear deformations, we have
d2 v
M = −EI (2.28)
dx2
Substituting Eq. 2.27 and 2.28 into 2.26, and assuming a beam of uniform cross section, we obtain

d4 v d2 v
EI −P 2 =w (2.29)
dx 4 dx

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
2.2 Assignment

(e) Tolerance
63

2. Plot the equation for the specified range

3. Solve the equation within the specified tolerance

4. Display the root of the equation and the number of iterations.

Test your program by solving Eq. 2.33 and compare the number of flops (floating point operations) with
the preceding solution.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

Chapter 3

MATLAB: “Advanced” Topics

3.1 Background
3.1.1 Numerical Integration and Differentiation
Rb
1 The integration of a F (x)dx is essentially based on passing a polynomial P (x) through given values
Rb
of F (x) and then use a P (x)dx as an approximation.

Z b Z b
F (x)dx ≈ P (x)dx (3.1)
a a

2 Using P (x) = F (x) at n points, and recalling the properties of Lagrangian interpolation functions, we

obtain
P (x) = l1 (x)F (x1 ) + l2 (x)F (x2 ) + · · · + ln (x)F (xn ) (3.2-a)
Xn
= li (x)F (xi ) (3.2-b)
i=1

3.1.1.1 Newton-Cotes Method

3In Newton-Cotes integration, it is assumed that the sampling points are equally spaced, Fig. 3.6, thus
we define Z b Z bXn n Z b
X
P (x)dx = li (x)dxF (xi ) = li (x)dxF (xi ) (3.3)
a a i i a
or
Rb Pn (n)
Approximation P (x)dx = W F (xi ) (3.4)
a
(n) R b i=1 i (n)
Weights Wi = a i
l (x)dx = (b − a)Ci
(n)
where Ci are the “weights” of the Newton-Cotes quadrature for numerical integration with n equally
spaced sampling points.
Draft
3.1 Background

R1
−1
P (x)dx
67

P3
P (x) = i=1 li (x)F (xi )
(x−x2 )(x−x3 ) 1
l1 (x) = (x1 −x2 )(x1 −x3 ) = 2 x(x − 1)
(x−x1 )(x−x3 )
l2 (x) = (x2 −x1 )(x2 −x3 ) = −(1 + x)(x − 1)
(x−x1 )(x−x2 )
l3 (x) = = 1 x(1 + x)
(3) R(x13 −x1 )(x3 −x2 ) 1 R 21
W1 = l1 (x)dx = 2 −1 x(x − 1)dx = 31
(3) R−11 R1
W2 = l2 (x)dx = −1 −(1 + x)(x − 1)dx = 43
(3) R1−1 R1
W3 = l3 (x)dx = 12 −1 x(1 + x)dx = 13
R1 R1−1
P3 (3)
−1
F (x)dx ≈ −1
P (x)dx = i=1 Wi F (xi ) = 13 [F (−1) + 4F (0) + F (1)]
which is Simpson’s rule

3.1.1.2 MATLAB Examples

8 MATLAB’s functions for numerical (definite) integration and differentiation are shown in Table 3.2.

Numerical Integration
trapz trapezoidal numerical integration
quad numerical function integration; Simpson’s rule
quad8 Newton-Cotes 8 panel rule
diff approximate derivatives

Table 3.2: Numerical Integration and Differentiation

9 For example, let us recall the expression for the center of gravity
Z
ydA
y= (3.5)
A
and moment of inertia
Z Z
Ixx = y 2 dA Iyy = x2 dA (3.6-a)
A A
cg
Ixx = Ixx + Ad2y Iyy = cg
Iyy + Ad2x (3.6-b)
and moment of inertia for a given cross section
for a rectangular cross (width b, height h) section we would have
Z
ydA
y = (3.7-a)
A
Z h
b ydy
0
= (3.7-b)
bh
h
= (3.7-c)
2

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

Chapter 4

MATHEMATICA

4.1 Background
4.1.1 Introduction
4.1.1.1 What is Mathematica ?

1 Mathematica is a program for symbolic mathematics.


2 With Mathematica, you can perform algebraic operations of various complexities. Hence, rather than
numerically solving an Engineering problem, you can solve it algebraically.
3 Because of its power, Mathematica will enable you to easily address problem of such complexities that
it would have been impractical to solve otherwise.
4 Finally, Mathematica has a very powerful set of graphics capabilities.

4.1.1.2 Availability

5 Mathematica is available in the Bechtel Laboratory.


6 A relatively inexpensive Student Edition can be purchased from the Buffalo Chip, and the University
used to have a site license for it.

4.1.1.3 Front End and Kernel

7Mathematica consists of two parts: the Kernel which is the computation engine, and the Front End
which is the user interface. The Kernel is identical on all computers, however the Kernel may vary.
8 There are two Front Ends supported in the Bechtel Lab:

Command-Line Interface: where the current input line is displayed on the last line of the screen
(it can be edited by using the arrow, insert and delete keys). This Front End does not support
graphics, and is invoked by the math command. It is a convenient (and only available one) to use if
you connect to the server via a remote terminal which does not support graphics (such as a home
Draft
4.1 Background

9 In both cases Mathematica is used interactively


83

4.1.1.4 Accessing Mathematica

10 In the Bechtel Lab, Mathematica is accessible through by clicking the left mouse button and then

selecting Mathematica. Note that the program is licensed to run only on the server (bechtel).
11If you want to run the program differently, you need to
xhost + on your console
rlogin bechtel To connect you to the server
setenv DISPLAY yourmachine:0.0 To have the graphics displayed on your workstation
Type math for the Command Line Front End. To exit you simply type Exit.
12 Alternatively, you may simply type mathematica for the Notebook Front End.

4.1.1.5 Help

13 For help, simply type ?. For a specific function, you can type ?Sqrt or ?P*.
14 Note that after each command, you should hit the following two keys simultaneously <Shift> <return>
15 The Notebook Front End also provides extensive help, Fig. 4.2.

Figure 4.2: Notebook Front End Help for Mathematica

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
4.3 References

(c) Label the x axis “x”, and the y axis “y”.


101

5. Plotsin(π sin x + y) for −3 < x < 3 and −3 < y < 3 using 33 grid points, no display of the axes.

6. Generate a contour plot of the previous equation.


p
7. Write a function which takes a pair x, y and returns x2 + y 2 .

4.3 References
1. Wolfram, S. “Mathematica: A System for Doing Mathematics by Computer second edition, Addison-
Wesley, 1991.

2. Maeder, R., Programming in Mathematica, Addison Wesley, 1991

3. Blachman, N., Mathematica a Practical Approach, Prentice Hall, 1991.


4. Gray, T., and Glynn J., The Beginner’s Guide to Mathematica, Addison-Wesley, 1992.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

Chapter 5

SAMPLES of MATLAB
PROGRAMS

This chapter contains the description and listing of a number of simple (and not so simple) MATLAB
codes.
The source codes can be freely copied from
/pub/Structures/cven4837

5.1 arches
5.1.1 Description
SUBDIRECTORY: arches

SYNOPSIS: axial, shear, and normal force plots for an arch (Structures)

DESCRIPTION: arches.m plots the Bending Moment, Shear Force, and Axial Force for a three pin
semi circular arch under uniform load.
CONTENTS;
arches.m main program

Written by: Brian Rose

Determine the reactions of the three-hinged arch shown in Fig. 5.1 Solution:

Four unknowns, three equations of equilibrium, one equation of condition ⇒ statically determinate.

(+ )
 ΣMzC = 0; (RAy )(140) + (80)(3.75) − (30)(80) − (20)(40) + RAx (26.25) = 0
⇒ 140RAy + 26.25RAx = 2.900
(+ - ) ΣFx = 0; 80 − RAx − RCx = 0
(+ 6) ΣFy = 0; RAy + RCy − 30 − 20 = 0
(+ )
 ΣMzB = 0; (Rax )(60) − (80)(30) − (30)(20) + (RAy )(80) = 0
⇒ 80RAy + 60RAx = 3, 000 (5.1-a)
Draft
5.1 arches

= wR
2 [θ − sin θ] |θ=π
θ=0 =
wR
2 [(π − sin π) − (0 − sin 0)]
105

= π2 wR (5.4-a)
Next we determine the horizontal reaction
Z θ= π
 2
(+ ) ΣMB = 0; −(HC )(R) + (Vc )(R) − |wRdθ
{z } R
| cos
{z θ} =0
θ=0
dP moment arm
Z θ= π
2
π wR
⇒ HC = 2 wR − 2 cos θdθ
θ=0
π θ= π
= 2 wR − wR[sin θ] |θ=02 = π2 wR − wR( π2 − 0)

= π2 − 1 wR (5.5-a)
By symmetry the reactions at A are equal to those at C
Draw the shear and moment diagram for the three hinged statically determined semi-circular arch
under its own dead weight w, Fig. 5.3. Solution:

Reactions: Those were determined earlier, Example ??. For the sake of clarity, we repeat their deriva-
tion:
1. Starting with CY
Z θ=π
(+ )
 ΣMA = 0; −(CY )(2R) + |wRdθ
{z } |R(1 +{zcos θ)} = 0
θ=0
dP
moment arm
Z θ=π
wR
⇒ CY = 2 (1 + cos θ)dθ
θ=0
wR wR
= 2 [θ − sin θ] |θ=π
θ=0 = 2 [(π − sin π) − (0 − sin 0)]
= π2 wR (5.6-a)
2. Next we determine the horizontal reaction
Z θ= π
 2
(+ ) ΣMB = 0; −(Cx )(R) + (Cy )(R) − |wRdθ
{z } R
| cos
{z θ} =0
θ=0
dP moment arm
Z θ= π
2
π wR
⇒ Cx = 2 wR − 2 cos θdθ
θ=0
π θ= π
= 2 wR − wR(sin θ] |θ=02 = π2 wR − wR( π2 − 0)

= π2 − 1 wR (5.7-a)
3. By symmetry the reactions at A are equal to those at C
Shear Forces: Considering the free body diagram of the arch, and summing the forces in the radial
direction (ΣFR = 0):
Z θ
π π
−( − 1)wR cos θ + wR sin θ − wRdα sin θ + V = 0 (5.8)
| 2 {z } |2 {z } α=0
Cx Cy
h π π i
⇒ V = wR ( − 1) cos θ + (θ − ) sin θ (5.9)
2 2

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
5.1 arches

Axial Forces: Similarly, if we consider the summation of forces in the axial direction (ΣF N = 0):
107

Z θ
( π2 − 1)wR sin θ + π2 wR cos θ − wRdα cos θ + N = 0 (5.10)
α=0
 
N = wR (θ − π2 ) cos θ − ( π2 − 1) sin θ (5.11)

Moment: Now we can consider the third equation of equilibrium (ΣMθ = 0):

( π2 − 1)wR · R sin θ − π2 wR2 (1 − cos θ) +


Z θ
wRdα · R(cos α − cos θ) + M = 0 (5.12)
α=0
 
M = wR2 π2 (1 − sin θ) + (θ − π2 ) cos θ (5.13)

We seek to determine the vertical deflection of the crown of the three hinged statically determined
semi-circular arch, Fig. 5.4 under its own dead weight w, Fig. ??.
1. We first seek to determine the analytical expression of the moment diagram. From statics, it can
be shown that the vertical and horizontal reactions are Rv = π2 wR and Rh = ( π2 − 1)wR.
2. Next considering the free body diagram of the arch, and summing the forces in the radial direction
(ΣFR = 0):
Z θ
−( π2 − 1)wR cos θ + π2 wR sin θ − wRdα sin θ + V = 0 (5.14-a)
α=0
 π 
V = wR ( 2 − 1) cos θ + (θ − π2 ) sin θ (5.14-b)

3. Similarly, if we consider the summation of forces in the axial direction (ΣF T = 0):
Z θ
( π2 − 1)wR sin θ + π2 wR cos θ − wRdα cos θ + N = 0 (5.15-a)
α=0
 
N = wR (θ − π2 ) cos θ − ( π2 − 1) sin θ (5.15-b)

4. Now we can consider the third equation of equilibrium (ΣMθ = 0):

( π2 − 1)wR · R sin θ − π2 wR2 (1 − cos θ) +


Z θ
wRdα · R(cos α − cos θ) + M = 0 (5.16-a)
α=0
 
M = wR2 π2 (1 − sin θ) + (θ − π2 ) cos θ (5.16-b)

5. The real curvature φ is obtained by dividing the moment by EI


M wR2 h π π i
φ= (1 − sin θ) + (θ − ) cos θ (5.17)
EI EI 2 2

6. The virtual force δP will be a unit vertical point in the direction of the desired deflection, causing
a virtual internal moment
R π
δM = [1 − cos θ − sin θ] 0≤θ≤ (5.18)
2 2

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
5.1 arches

7. Hence, application of the virtual work equation yields:


109

Z
wR2 h π i R
π
2 π
1 ·∆
|{z} = 2 (1 − sin θ) + (θ − ) cos θ · · [1 − cos θ − sin θ] |{z}
Rdθ
θ=0 |EI 2 {z 2 } |2 {z }
δP dx
φ δM
wR4  2 
= 7π − 18π − 12
16EI
4
= .0337 wR
EI (5.19-a)

5.1.2 Listing
5.1.2.1 arches.m

Figure 5.5: Simply Supported Arch with Uniform Load

Equations used for calculation of shear force, axial force, and bending moment are as follows:

π π
V = ωR[( − 1) cos θ + (θ − ) sin θ]
2 2
π π
N = ωR[(θ − ) cos θ − ( − 1) sin θ]
2 2
2 π π
M = ωR [ (1 − sin θ) + (θ − ) cos θ]
2 2
arches.m file starts here:
%Arches is a script that plots the Bending Moment, Shear Force,
%and Axial Force for a three pin semi circular arch under uniform

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
5.2 beam1 111

Shear Force Axial Force


200
90 90400
120 60 120 60
200
150 0 30 150 0 30
−200
−200 −400
180 0 180 0

210 330 210 330

240 300 240 300


270 270

Bending Moment
3000
90
120 60
1000
150 30
−1000
−3000
180 0

210 330

240 300
270

Figure 5.6: Sample output for arches.m using r = 100 and ω= 2

5.2 beam1
5.2.1 Description
SUBDIRECTORY : beam1
SYNOPSIS: shear and bending moment diagram for a simple beam (Structures)
DESCRIPTION: BMdemo.m gets the bending moment and deflection for a simply supported beam
subjected to either a point load or a distributed load.
CONTENTS:
BMdemo.m the main program
M2deflection.m converts bending moments to deflections by integration
P2M.m converts point loads to moment using a formula
P2V.m converts point loads to shear using a formula
w2M.m converts uniform loads to moment using a formula
w2V.m converts uniform loads to shear using a formula

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
5.2 beam1

• Continuous Load
113

ωb
R1 = (2a + b)
2L
ωb
R2 = (2c + b)
2L
M = R1 x for (x < a)

ω
= R1 x − (x − a) for (a ≤ x ≤ a + b)
2
= R2 (L - x) for (x ≥ a + b)

V = R1 for (x < a)

(x − a)(−R2 − R1 )
= + R1 for (a ≤ x ≤ a + b)
b
= −R2 for (x ≥ a + b)

00 M (i) v(i − 1) − 2v(i) + (i + 1)


v = =
EI dx2
BMdemo.m file starts here:
%BMdemo
%this script gets the bending moment and
%deflection for a simply supported beam subjected to either
%a point load or a distributed load

%prompt the user for the load type


choice=input(’Analyze for point load (1) or a uniform load (2) ? ’);

%clear the screen


clc

%do the point load case


if choice==1

%prompt the user for some parameters


P=input(’Enter P: ’);
a=input(’Enter a: ’);
L=input(’Enter L: ’);
E=input(’Enter E: ’);
I=input(’Enter I: ’);
number_points=input(’Enter the number of discretization points: ’);

%calculate the mesh size


dx=L/(number_points-1);

%get the BM
M=P2M(P,a,L,dx);

%get the shear


V=P2V(P,a,L,dx);

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
5.2 beam1

4
x 10
115

Bending Moment
4

0
0 4
2 4 6 8 10 12
x 10
1
Shear

−1
0 2 4 6 8 10 12
0
Displacement

−0.5

0 2 4 6 8 10 12

Figure 5.8: Sample output of BMdemo.m with a pointload using P = 20 kN, a = 6 m, L = 12 m,


E = 200 GPa, I = 6e6 mm4 , and 12 discretization points.

%
%This algorithm integrates the bending moment twice
%using the central difference method.
%The second derivative is defined as
%
% v(i-1) - 2v(i) + (i+1)
%v’’= ----------------------
% dx^2
%
%
%Using the central difference method we can w%The central difference method will take on the form Av=b.rite
%
% v(i-1) - 2v(i) + (i+1)
%M(i)/EI = ----------------------
% dx^2
%
%If you write the above equation for every mesh point
%you get the following matrix equation.
%
% ---- -- -- -- --
% | 1 -2 1 | | v(-1) | | M(0) |
% | 1 -2 1 | | v(0) | | M(1) |
% | 1 -2 1 | | v(1) | | M(2) |
% | . | | v(2) | | . |
%1/dx^2 | . | | . | = 1/EI | . |

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
5.2 beam1

% /\ /\
117

% |----A----|
% |------------L------------|

% |-------B-------|
% |---> x

%get b
b=L-a;

%get the reactions


R1=P*b/L;
R2=P*a/L;

%make a vector x
x=(0:dx:L);

%get the moment


M=R1*(x<a).*x + R2*(x>=a).*(L-x);

%the above statement is a shorthand way of saying

%for i=1:length(x)
% if x(i) < a
% M(i)=R1*x(i);
% elseif x(i) >= a
% M(i)=R2*(L-x(i));
% end
%end

%Matlab is very fast at evaluating matrix operations


%like the shorthand command for M. Matlab is not so fast
%at doing if statements and for loops.

5.2.2.4 P2V.m
function V=P2V(P,a,L,dx)
%FUNCTION V=P2V(P,A,L,dx) returns the Shear diagram for a
%point load on a simply supported beam
%
% |P
% |
% \|/
% __________________________
% /\ /\
% |----A----|
% |------------L------------|

% |-------B-------|
% |---> x

%get b
b=L-a;

%get the reactions


R1=P*b/L;
R2=P*a/L;

%make a vector x

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
5.12 Stress/Strain Programs 151

8
x 10 2*Theta = 22.46 degrees

Tmax

0.5
P1

Tau xy 2*Theta
S2 S1
0
C Sigma x

P2
−0.5

−1

Tmin

−5 0 5 10 15
7
x 10

Figure 5.28: Example 2(e): Mohr’s circle of 2-D stress.

Tmax2
150

100 Tmax1
Tmax3

50
Tau xy

0 S3 S2 S1
Sigma x

−50

−100

−150

−150 −100 −50 0 50 100 150 200

Figure 5.29: Example 3(c): Mohr’s circle of 3-D stress.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

Chapter 6

Fortran 90: INTRODUCTION

1 For a better understanding of fortran, this chapter will begin with a brief overview of the architecture
of a computer, role of its software, and finally status of fortran.
2 The next section will be devoted to a brief review of fortran (with which the reader is asumed to have a

certain familiarity), highlight those features which may not have been covered in an introductory course,
and terminate with recommendations for programming style and documentation.

6.1 Introduction
6.1.1 Computer Architecture and Software
6.1.1.1 Computer Hardware

3 Digital computers are concerned with the processing of data1 abd their basic architecture has seen little
if any fundamental changes since the conceptual model first proposed by Von Neumann in a landmark
paper in 1946:

Storage: of data and commands:


• Data as well as commands are internally stored in binary form (0 or 1).
• The most elementary unit of data is the bit.
• A group of eight bits form a byte. A byte can have 28 = 256 different values, and is usually
used to store a character (extended ASCII).
• In most present day computers, four bytes form a word.
• Early PC’s had 16 bit word, most other computers have 32 bits, and supercomputers have 64
bits.
• Storage is accomplished on any one of the following:
1. cache memory, extremely fast, it acts as a buffer between the CPU and the core memory.
2. Core memory, very fast, usually around 8 Mb (106 ) bytes for PC’s and at least 32 Mb
for workstations.
1 Early on Engineers used extensively analog computers.
Draft
6.1 Introduction 155

1. a 1,012 by 1,012 8 bit graphical display (i.e a resolution of 1, 012 × 1, 012 pixels, each one
capable of assuming up to 28 = 256 colors).
2. A keyboard, and a mouse
3. Unix Operating system
4. At least 12 Mb of core memory
5. 200 Mb of disk space
6. Access to a local area network (usually ethernet)
7. X based window management
and access to laser printer, plotter, CD-ROM, tape drive, digitizer, scanner, speaker, modem.
• Computers are often networked together, a commonly used standard is ethernet.

4 Increasingly the distinction between PC and workstation is being blurred. Currently, most new PC’s
are computationally more powerful than the immediate past generation of workstations.
5 Major distinction between PC and Workstation is highlighted by table 6.1.

Feature PC Workstation
Processor Intel Motorola
Memory 8-16 Mb 32-64 Mb
Operating System Windows 95/NT Unix/Motif
RISC N Y
Monitor 15”-17” 19”-21”
Resolution 1024x1024 1024x1024
Bit Planes 8 8-24
Graphics Accelerators Y Y
Networking Novell Ehternet
Editors Word Vi, Emacs, Crisp
Virtual Memory N Y
Primary Application Everything Scientific Computing
Source of Software Microsoft Public-Domain, GNU, Vendors
Graphics Open GL Open GL, X

Table 6.1: PC Vs Workstation

6.1.1.2 Computer Software


For the computer to operate, it must also have:
Operating System: which is a collection of programs to control the operation of the hardware and
application programs.
• Early on each computer vendor had its own operating system. Most recently, Unix 3 has
emerged as the uncontested de facto standard operating system for workstations, mainframes,
and supercomputers. It is also increasingly used on PC’s for scientific computing (through
Linux).
3 Unix is a trademark of AT&T.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
6.2 From Source to Binary

6.2 From Source to Binary


157

6 There are two major steps in creating an executable code:


Compiling: which takes as input the source code(s) written (in ASCII) by the developped and converts
it into an object code. Usually, the former has extensions such as .f, .F or .for, and the later
.obj.
Linking: Which takes the object files written by the user, and link them not only among themselves bu
also with standard (fortran) and non-standard (such as graphics) libraries. This process results in
an executable which on Unix is by default called a.out, or given an extension .exe in Windows.

6.3 Sample Example

7 To begin with, use your favourite editor to create the following file ello.f5
program Hello
print *, ’Hello, world’
end program Hello

8 Next save the file, and type


f90 hello.f
a.out

6.4 Fortran Compilers


6.4.1 Compilers

9 In the Bechtel Lab, you have access to the fortran compilers listed in Table 6.2.

Compiler Debugger Description


Sun Workstations
f77 dbx Fortran 77
f90 None Nag Fortran 90
IBM Workstations
f77 xldbx Fortran 77
xlf90 xldbx Fortran 90
PC
MicroSoft Power Station Fortran Compiler

Table 6.2: Fortran Compilers in the Bechtel Lab

10 It should be noted that the Fortran-90 compiler f90 on the Sun is only a “pseudo-compiler” in the
sense that the code is first internally translated into C and then compiled with a C compiler. This was
5 This classic example was first introduced by XX & xx in their introductory book on C, and has since then become the

opening example in most introductory computer language tutorials.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
7.1 Background

ndata=ndata+1
169

end do count
mean=sum/real(ndata)

32 Finally, do loops can be nested.

7.1.6 Input/Output
7.1.6.1 Devices
Default Devices/Read/Write Units must be assigned to input and output devices. On most systems,
they are 5 for the keyboard, and 6 for the screen.

33 Hence, by default, the basic format of read write formats becomes

read*, a,b,c
print*, a,b,c

Selected Input/Output Devices In the most general case, input data will be read from, and output
writen into user specified files. Thoose files must first be defined through the open statement which
takes the following form
open (unit=unit number,file=’file name’, status=status) where unit number is an integer num-
ber assigned to the device, ’file name’ is a character string giving the file name, and status is a
character string which can be ’old’, ’write’, or ’read’.

7.1.6.2 Formated I/O

33 The format for the read statement is as follows

read(unit number,format label)variable list where unit number is the device number specified through
the open statement, and format label is the format label as defined below.
34 The format for the write statement is as follows
write(unit number,format label)expression list where unit number is the device number specified through
the open statement, and format label is the format label as defined below.
35 Note that we can use the default devices for both read and write
open(unit=1,file=’input.dat’)
open(unit=2,file=’output.dat’)
write(*,*)’ About to start reading from unit 1’ ! May be placed for debugging purposes
read(1,800)a,b,c
write(2,900)a,b,c

Format Definition A format can be defined in anyone of the following ways:


Statement Label: referring to a format statement
write(6,900)A
900 format(f10.2)
Character Variable provides a flexible way to dynamically define the format:

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
7.1 Background

7.1.8 Style
171

1. Nested loops should be arranged such that the inner most loop should reference the first subscript,
and the outer most loop, the last subscript.

do k=1,50
dol=1,20
a(l,k)=b(l,k)+c(l,k)
end do
end do

2. All common blocks and sub-system control variables defined via parameter statements are defined
in include files. The naming convention for include files is based on their contents; include files
containing common blocks have the extension .cmn and include files containing parameters have
the extension .par. The different extensions allow the developer to determine the contents of an
include file without using a text editor.
3. Fortran is not a case sensitive language. However, lower case code is allowed. All source code
should be in lower case, this is much easier in a Unix environment which is case sensitive, with the
exception of the following exceptions:
• Global or common variables begin with an upper case letter; all other characters are lower
case.
• Variables defined via parameter statements are all upper case.
This allows the developer to quickly determine where a particular variable comes from.
4. All variables should be declared; and the FORTRAN standard for variable types should be ignored.
To insure that all variables are declared the -u option should be used when the source code is
compiled on the Sun. Explicit declaration of all variables eliminates typographical errors and the
omission of function subroutine type declarations, both of which are fairly tricky bugs to locate.
5. Variable names should be selected in a logical manner by choosing descriptive names. All variables
should have no more than 6 characters. Also a common ”syntax rule” should be adopted where
a single basic variable name is employed with different prefixes depending on its usage in the
program:
(a) Prefix i,j, or l should be used to indicate a DO loop variable
(b) Prefix k will indicate a counter
(c) Prefix m will indicate a maximum value
(d) Prefix n will indicate a given number
For example ipoin, npoin, mpoin will indicate a particular point, number of points, and maxi-
mum number of points respectively.
6. All do loops should end on separate continue statements and the code between a do statement and
the corresponding continue statement should be indented. This is more a matter of readability
than anything else, but often comes in very handy when examining a particularly long do loop.
7. Ideally, a subroutine should have only one return statement. This is particularly true with long
subroutines where multiple return statements make it difficult to follow the logic of the subrou-
tine. It can be especially difficult to debug a subroutine with multiple return statements and for

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
7.2 Assignment

7.2 Assignment
173

7.2.1 Beam on Elastic FOundation Deflection


The defelection of a beam, length L, and flexural rigidity EI on elastic foundation with stiffness k by a
central point load P is given by

2P β [sinh(βL) cos(βx) cosh(β(L − x)) − sin(βL) cosh(βx) cos(β(L − x))]


v=   (7.1)
k sinh2 (βL) − sin2 (βL)
q
k
where β = 4
EI
Find the deflection for x = 0, 2, and 5 m for P = 100 kN, k = 4 × 103 kN/m2 and EI = 106 kN/m2

7.2.2 Quadratic Equation


Write a program to read in three numbers a, b and c, assumed to be the coefficeints of the quadratic
equation
ax2 + bx + c = 0 (7.2)
and calculate and print the roots. Account for the three possibilities

1. Two different real roots

2. One (double) root

3. A pair of complex conjugate roots

7.2.3 Statistical Analysis


Retrieve (through anonymous ftp to bechtel) the following set ftp/pub/Matlab/set1.dat. It contains
results of concrete compressive strength fc0 for two different ready mix companies.
Determine the Mean, Standard Deviation, Coefficient of variation, Skewness, and Kurtosis. (Refer
to MATLAB lecture notes).

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

Chapter 8

Fortran 90: ARRAYS

8.1 Background
8.1.1 Definition

1 An array is defined to have a shape given by its number of dimensions, called rank 1 and extent for
each one of these.

8.1.2 Declaration
2Whereas the declaration of scalar quantities is optional (unless we use implicit none0, all arrays
must be declared. For example
real, dimension(20):: force
real, dimension(20,20):: k

Alternatively we can use the following format


real :: force(20)
real :: k(20,20)

3 It is conventially assumed that the first subscript of a two-dimensional array represents the row, and

the second the column.


4 In fortran, arrays are stored column-wise, i.e. internally all arrays (no matter what their rank is) must
be stored sequentially as a vector, hence stiffness will be stored as
k(1,1),k(2,1),...,k(20,1),k(1,2),k(2,2),...k(20,2),k(1,3),...k(20,20)
5 Array subscript can be negative, zero, and/or positive:
{\tt real:: vector(-10:22)}

is an example of one dimensional array where the subscript may range from -10 to +22.
{\tt real dimension(-10:25, 20:40):: grid}

is a two dimensional array.


1 Note that the rank of an array is the number of dimensions and has nothing to do with the mathematical rank of a

matrix.
Draft
8.1 Background

8.1.6 Dynamic Memory Allocation


177

10 Dynamic allocation is useful when reading a datafile of a size that is not known until run-time. The
array would need to be defined as allocatable. This is done by simply adding allocatable to the
declaration line, with the dimensions specified as “:”. Next, the array must be allocated once the
dimensions are known, but before anything is read into the array. A second argument can be used with
the allocate routine, in which the error status is set according to the success of the call. This should
always be checked, to be sure there was enough memory for the allocation.
real, dimension(:,3), allocatable :: nodal_coordinates
integer, dimension(:,2), allocatable :: element_connectivity
integer :: error
read*, number_nodes, number_elements ! Get the matrix dimensions
allocate(nodal_coordinates(number_nodes, 3), stat = error)
allocate(element_connectivity(number_elements, 2), stat = error)
if (error .ne. 0) then
print*, "Allocation failed"
stop
endif
read*, nodal_coordinates
read*, element_connectivity
...
! Use arrays
...
deallocate(nodal_coordinates, element_connectivity) ! Release memory when finished

8.1.7 do Loops & Vector Operations

11 The do loops is often unnecessary for most array operations


1. Rows, columns, matrices, etc. may be assigned to each other as long as the dimensions are the
same. All of the following assignments are valid:

! Initialize A and B to dummy arrays of the same dimension


integer, dimension(5) :: a = (/ 1, 2, 3, 4, 5 /)
integer, dimension(8) :: b = (/ 1, 2, 3, 4, 5, 6, 7, 8 /)
integer, dimension(8) :: c
...
c = b ! set vector c equal to vector b
a = b(4:8) ! Assign to a the 4th through 8th elements of b
b(3:7) = c(2:6) + a

2. For example, if we have an array (any dimensions) and want to initialize it to any scalar, say 0.0,
rather than “walking” through each element with nested do loops, we can simply say “matrix =
scalar”, and every element of “matrix” will then be assigned the value of “scalar”.
3. Some do loops can be replaced with a call to the dot product intrinsic function.

real, dimension(4) :: a, b
c = dot_product(a,b)

4. Arrays or matrices can be accessed in non-ascending order, and portions can be extracted using
appropriate subscripts. To repeat the dot product of the previous example with array b reversed,
the following could be done:

c = dot_product(a,b(n:1:-1))

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

Chapter 9

Fortran 90; PROGRAM UNITS;


POINTERS

9.1 Background
9.1.1 Program Units

1 A fortran 90 code is a modular hierarchical object defined by fundamental blocks.


2 Each program unit may have one or more external procedures. Those procedures (function and/or

subroutine) are nearly independent pieces segments of the code. They can be written separately, make
it easier to debugg the code, can be reused in other programs, and can be reused in the same code.
3 Program and procedure can share data through the module unit.

9.1.1.1 Main Program

4 Always assign a header, i.e. program stress.


5 End the program similarly: i.e. end program stress.
6 Always return control to the main program upon termination (good programming practice), and hence
there should be no more than one stop statement in your code.
7 For abnormal completion, you may append a character string after the stop
stop ’Incomplete data, abnormal completion’
8 There should be few executable statements in the main program. Calls to subroutines should be used

instead.

9.1.1.2 Functions

9 Often times an application program needs to have a relatively simple operation to be performed. If

that operation is to be repeated, and returns a single variable as a result of an operation on one or more
Draft
9.1 Background

return
191

end function average

9.1.1.3 Recursive Subprograms

17Fortran 90 supports recursion. If a subprogram calls itself, directly or indirectly, the keyword
recursive must appear in the subprogram statement.
18 Recursive functions must also declare a result variable to avoid ambiguity with array-valued functions
that are directly recursive. The result variable is used to hold the function result for each function
invocation; the function name is used to invoke the function itself. Consequently, the recursive function’s
name should never appear on the left side of an assignment statement. An example of a recursive factorial
function is shown below.
recursive function factorial( n ) result( f )
integer f
integer, intent( in ) :: n

if ( n <= 0 ) then
f = 1
else
f = n * factorial( n-1 )
end if
end function factorial

9.1.1.4 Subroutines

19 Subroutines are procedures used to perform operations which are too complex to be performed by

functions (if several values need to be returned, or values are simply to be read and passed back).
20 Just as we have intrinsic functions, fortran 90 has intrinsic subroutines which are included in the

compiler. You should consult your compiler manual to determine which intrinsic subroutines are pre-
defined.
21 The general format to reference a subroutine is through the following executable statement

call subroutine-name (argument list)

22 In turn, the first line in a subroutine should look like

subroutine subroutine-name(argument list)

23 Contrarily to functions, a subroutine use the argument list to identify both input and output values

to a subroutine.
24 A subroutine may return zero, one or many values.
25 Declaration of all parameters should include the intent attribute.
26 The subroutine should include a return and end subroutine
27 It is advisable to have input arguments preceding output ones.
28 Following is a main program

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
10.3 Make

10.3 Make
201

When developing a program, the executable file must be continuously recreated as the source code is
modified and recompiled. This can become a hassle, especially if your program is lengthy. If your
modifications have only been in a few of the subroutines making up your program, it is a considerable
waste of time to recompile the entire program. This is where the make command is useful.
A program can be created from several source files, each file containing one or more subroutines.
Make will only recompile those files that have been modified since the last compile and will relink all
the object files and create a new executable.
To use make you need a description file that contains the information make needs to create your
program. This information includes the names of the source files, the compiler you wish to use, the
compiler options, and so on. This file is called the makefile. While this section will not go into the
million and one uses for make it will show you how to build a makefile.
Basically, a makefile consists of a number of definitions and a number of targets. The definitions are
used to specify file names, which compiler and linker to use, flags to use when compiling, etc. The targets
is what tells make what to do when invoked. A target specifies a dependency and a list of command.
The dependencies are used to tell make when to remake something, and the commands are used to tell
make what to do to remake the target. In the following example, the target $(PROGRAM) is dependent
on the files in $(OBJECTS), and the command to make the program from the object files is specified
on the next line (note that the command lines must start with a tab character).
Make is partially intelligent, it understands how to make an object file from a source file, for example
a .o file from a .f file. Thus when it sees that $(PROGRAM) is dependent on a number of object (.o)
files, it will find the source (.f) files in the directory and create the .o files automatically.
The following is an example of a simple makefile. The lines starting with # are comments, the
numbers in parentheses refers to the more detailed explanations following the example.

# (1) Name of the program.


PROGRAM = sample

# (2) List of source files making up the program.


SOURCES.f = \
file1.f \
file2.f \
file3.f \
file4.f

# (3) Linker to use.


LINK = $(LINK.f)

# (4) Flags to the compiler and linker.


FFLAGS += -g

# (5)Source and object files.


SOURCES = $(SOURCES.f)
OBJECTS = $(SOURCES.f:%.f=%.o)

# (6) Default target.


all: $(PROGRAM)

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
10.4 Debugger 203

This looks for a file called makefile in whatever directory you are in. If you want to call your description
file something other than makefile you would use the -f option as follows
> make -f filename
To make another target than the default one, specify the target on the command line to make:
> make target
Usual targets are all (usually default), the program name, both of which makes the executable file, and
clean, with cleans up in the directory.
If you need more information with creating your makefile the on-line man pages may help. Also the
book
Managing Programs with make by Andrew Oram and Steve Talbott, O’Reilly and Associates, Inc.
may prove very helpful.

10.4 Debugger
Written by Johannes Hermanrud

10.5 Introduction to Debugging


A debugger is a program that makes it easy to debug programs. Using a debugger, one can:
• run the program in a normal fashion,
• step through the program line by line,
• run the program, but stop the execution at specified places (by setting breakpoints),
• continue execution after a stop,
• see the contents of variables, either at specified times or continuously, and
• see the call stack, i.e. see which functions has been called to reach the current point of execution.
This information is valuable in deciding which parts of the program are actually executed, and what the
values of the program’s variables are at different points during the execution.
When a program has a serious error, such as a segmentation fault, the operation system will create a
file called core in the current working directory. This file can be used with a debugger to find out where
in the program the error occurred. See the command where for details.

10.5.1 The debugger


Debugger is a tool that can be used with C, FORTIN, Pascal, Modula-2 and assembler (See section 10.5.2
for how to run the debugger). Debugger opens a window on the screen containing a menu bar and five
subwindows.

Menu Bar There are five menu options each with their corresponding submenus. Each menu and
submenu are briefly described below:

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
10.5 Introduction to Debugging

Display Subwindow. This area shows the values of variables specified with the display command.
205

The text can be scrolled using the scollbar. The display command needs to be used only once
for a single variable, as the display of the variable will be updated throughout the execution of
the program. To get the display window to pop-up either type display variable name or select
”Display Window...” from the data menu.

Figure 10.1: Debugging Tool

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
10.5 Introduction to Debugging

clear [n] Clear breakpoints on line n, if n is not given, clear the breakpoint last stopped at.
207

change directory - cd path When the debugger is started from the menu system, the current directory
is your home directory. Generally it is best to run the debugger in the directory in which your
program resides, particularly if your program must read any data files. To change from your home
directory to a subdirectory called homework/CVEN5555 type :

(debugger) cd homework/CVEN5555

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

Chapter 11

INTRODUCTION TO UNIX

UNIX is a multi-user operating system that was invented by AT&T in 1969 and has grown to be the
operating system of choice for universities and research organizations. This section will introduce the
novice to some basic commands and also provide more advanced commands or tricks of the trade for
those who are interested.

11.1 logging In
You may login to a Unix system in a number of way; rlogin, telnet from one computer (including pc)
into the (Unix) computer.
In the Bechtel Lab., simply press return and the login prompt should appear. At the prompt, type
in the username which was given to you, and then press return. Then the passwprd prompt will
appear and you must carefully enter the password given to you. Note that as you type the password the
characters will not appear on the screen. Note that Unix is case sensitive.
If this is the first time you are loggin in, then it may bee safe to change your password.

11.2 Changing Your Password


Usage: passwd
After you typed passwd, the system will prompt you for your old password (for security reason),
and then will prompt twice (to make sure that there was no “typo error”) for the new one.
The system will check after the first entry that the specified password can not be easily guessed by
intruders (no dictionary name or permutations on your name allowed), that it has at least six characters,
and that it contains at least one upper case or numeral or special character.

11.3 Logging Out


In general logging out is from a specific device.
If you are remotely connected to a computer, then simply type exit.
To logg out from the Bechtel Lab., then click the right button of the mouse on the screen and select
quit. Once you are out, type contrl D. If you have followed properly the procedure, then a new login
prompt should appear.
Note: If you are using a workstation, never turn it off when you have finished.
Draft
11.6 Shell

11.5.2 Your User Account


211

When you log into your user account you log into your own directory. The name of this directory will
typically be you last name. This directory is called your home directory. Your working directory is
whatever directory you are currently in, this will change many times during the average loggin session.

11.5.3 Absolute Pathnames


The absolute pathname tells you the path you must travel to get from the root directory to where you
want to go. For example if Smith had a subdirectory called temp, the absolute pathname would be
/bechtel/users1/undergraduate/smith/temp.

11.5.4 Relative Pathnames


A relative pathname points to a particular subdirectory, relative to your current directory. The shell
assumes you are using relative pathnames unless you explicitly use an absolute pathname. If the user
Smith were in his home directory the path to temp would be temp.

11.6 Shell
In Unix a shell is a command-line interpreter. You can choose the shell you want to use. The most
popular ones being /bin/csh and /bin/tcsh or better known as C shell and turbo C shell.

11.7 File Management


11.7.1 Changing Directories
Usage: cd or cd directory

This command is used to change your working directory. If the directory you wish to change to is
the next level down you would type cd directoryname. If the directory you wish to change to is several
levels down you would type cd pathname. The pathname includes:

/bechtel/users1/undergraduate/username/...

A useful shortcut is to type:

~username/...

Where username is your user account name. When you type cd without a directory name, you will
be placed in your home directory. Also typing cd (space) .. will move you up one directory, not to be
confused with the MS DOS command cd.., you must remember to put a space between the cd and the
..

11.7.2 Changing File Protection


Usage: chmod
Since UNIX is intended for multiple users, protection rights for read, write and execute are assigned
to each file. There are three groups to which protection may be separately assigned: uuser (yourself),

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.7 File Management

r Read.
213

w Write.
x Execute.
X Give execute permission if the file is a direc-
tory or if there is execute permission for one
of the other user classes. For example:
s Set owner or group ID. This is only useful with
u or g. Also, the set group ID bit of a direc-
tory may only be modified with ‘+’ or ‘-’.
t Set the sticky bit to save program text between
processes.

> chmod ugo=rw junk.tex

will set the permission such that the user, group, and others can read from and write to the file
“junk.tex”.

11.7.3 Copying Files


Usage: cp file1 file2
This command copies file1 to file2. If file2 already exists, it is replaced with file1. If file1 is in you current
working directory but file2 is not, you must include the pathname for file2. The following example copies
file1 to file2, which is in a different directory.

> cp file1 ~username/directory/file2

If you have many files you wished to copy, and they all have similar names i.e. file1, file2, file3, file4, ...,
file10, you can use what’s called a wildcard to copy all the files at once. This example puts all the files
named file1 through file10 into the subdirectory called directory.

> cp file[1-10] ~username/directory

If the files are not named in such a way that permitts you to use the wildcard option, you can list out
the names of the files you wish to copy. Here the file sample and exmpl are put into the subdirectory
temp. If a destination for the copy is omitted the files will be put in your current working directory.

> cp ~username/direc/sample ~username/direc/exmpl ~username/temp

If you wish to copy the contents of one directory to another directory this can be done as follows:

> cp ~username/directory1 ~username/directory2

11.7.4 Directory Listing


Usage: ls
Lists all files in your current directory. For example if you would like a listing of all files with the
extension .f in your current directory you could type:

> ls *.f

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.8 Printing Files

example either name filename or otherfile can be used to call up the file, they are both valid names for
215

the same file.

11.7.9 Removing Directories


Usage: rmdir directory
Rmdir removes (deletes) directories. But first the directory must be empty of all files. If you are certain
you want to delete the directory first delete all files in the directory using rm, then delete the directory
using the rmdir command.

> rm ~username/directory/temp/*
> rmdir ~username/directory/temp

11.7.10 Wildcards
Another function the shell provides is wildcard expansion. UNIX shells provide two special characters,
* and ? as wildcards. The * will match any number of arbitrary characters, including none. The ?
will match any single character. The shell looks for files that match the pattern typed and replaces the
pattern with a list of those files that matched. It sends this list to the program. This process is called
expanding the wildcards.
As an example, say you want to list all the files beginning with “a”. You could type

ls a*

into the shell. All files of any length starting with “a” would be listed, including the file “a” if it existed.
It would also match the ‘file a.b since “.” is not a special character in Unix as it is in DOS, for instance.
Or, if you wanted to list all files of length two that start with “a” you would type

ls a?

You should be especially careful with wildcards when using rm until you are familiar with how they
match. Use ls to see what files match before using rm.

11.8 Printing Files


For printing ASCII, or post-script files.

11.8.1 Enscript
Usage: enscript -2r -Gfile
When you are printing large source files on the laser printer, this command will rotate the output and
place it in two columns. Also see Print.

11.8.2 Checking the Print Que


Usage: lpq
When you type the command to print a file and hit return the system prompt almost immediately
returns. This does not mean that your file has been printed, all it means is that it has been spooled to a
print que. If you would like to find out how busy the printer is, or how long the print que is, type lpq.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.9 Compressing and Archiving Files

11.9 Compressing and Archiving Files


217

Often users will be asked to limit their disk usage to ten megabytes, and will be notified when their
usage exceeds this amount. If you find yourself nearing this limit you can compress files that you are
no longer using, or won’t be using for a while. A text file can be compressed by up to 90%, executables,
slightly less, this is a considerable space saving tool. Also, you can archive files directly to tape, freeing
up diskspace, you can also use the archive tool to do backups of your files. Users will be allowed to use
the cartrigde and 8mm tape drives on Bechtel, with permission from the system administrator, or they
can do backups directly to a diskette in the floppy drive of each machine.

11.9.1 Zipping Files


Usage: gzip filename
The gzip command will generate a file named filename but will tack on the extension .gz. For example:

> gzip filename

the file filename.gz will be generated.

> gzip file1.f

the file file1.f.gz will be generated. To unzip files use the command gunzip in the same manner you use
gzip. If we were to uncompress the FORTRAN source code we compressed in the previous example:

> gunzip file1.f.gz

this will restore file1.f to it’s normal size and strip off the .gz extension.

11.9.2 Compressing Files


Usage: compress filename

Note that this utility is not as efficient as gzip but is supported on all Unix machines.
The compress command will generate a file named filename but will tack on the extension .Z. For
example:

> compress filename


the file filename.Z will be generated.

> compress file1.f

the file file1.f.Z will be generated. To uncompress files use the command uncompress in the same
manner you use compress. If we were to uncompress the FORTRAN source code we compressed in the
previous example:

> uncompress file1.f.Z

this will restore file1.f to it’s normal size and strip off the .Z extension.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.10 Spooling Files

11.10 Spooling Files


219

11.10.1 Color Laser Printer


Color post-script files can be printed on the color laser printer on the NAC printer on the second floor
of the Engineering building by typing

printcolor fn

where fn is your filename

11.11 Pipe and Filters


You may wish the output from one command, be the input for another. Connecting commands together
in this fashion is known as a pipe. A pipe is designated by a vertical bar (|) on the command line between
two commands. When a pipe is set up between two commands, the output from the command on the
left becomes the input for the command on the right. Pipes can also be set up between programs.
When a program takes the output from another program as its input and does some sort of operation
on it and writes out a new output file, this is refered to as a filter. A filter can be thought of as some
sort of post-processor. For example, if we wished to take a listing of a directory and sort it by file size
would could do this as follows:

> ls -l | sort +4n

The pipe consists of the use of both commands ls and sort. The filter is sort, since it is rearranging the
output of ls. For more information on ls and sort please see Utilities.

11.12 Multi-Tasking
Suppose you have a large job to run, for example, you might be running a statistical package of some
sort that will probably take a long time. If you were using an MS-DOS machine, like a pc, you would
start the job running and come back later when it finshed. This is called a single process system. UNIX,
on the other hand, is a multi-process system, you can run jobs in the “background” while still doing
things in the “foreground”. You may even log off, and the background process will continue to run to
completetion.
Note: it is unethical the submitt batch jobs on a different workstation than the one you are logged
in on, this practice will not be tolerated. Under no circumstances are you to submitt a batch job to
Bechtel.

11.12.1 Job Control


To start a job running in the background, or a batch job, use the & after a command. After you hit
return the system will show you the process ID number, PID. The prompt will then return immediately.
The PID is useful for keeping track of how long the job has been running, and you will also need it if
you wish to kill the job. You dont’t need to remember your process ID, however, since there is a simple
UNIX command to check on what processes you have running, (see Checking on a Process). You can
also bring the program back as the foreground job by using the fg function. Here is a sample session:

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.13 Utilities

foreground, but when the console is idle, the machine will devote more time to the background process.
221

So, as you sit talking to a friend, or looking something up, the background process will get more of the
cpu than it does when you are working.
You can set the nice value to anything between 0 and 19. The default value, if no number is given
with the nice command, is 10. Process’s set with 0 will run fast, process’s set with a nice number of 19
will only run when nothing else wants to. If you do not use the nice command, a process’s nice number
will be 0. A user can only prioritize those process he has started, and can only increase the nice value
i.e decrease a process’s priority. In order to change the priorty of a process you must use the renice
command. See the man page on renice for instructions on how to do this.
> nice -19 ps -l
this will set the nice number of a process to 19, then list all the process running on the machine (see ps
for more information) and the -l option displays the nice numbers for the processes.
Note: if someone has started a process on your machine, notify the system administrator immedi-
atley.

11.13 Utilities
11.13.1 du
Usage: du options filename
The du command displays your disk usage . It gives the number of kilobytes contained in all files and,
recursively, subdirectories within the specified directory. If the filename is omitted, the current directory
is used. The -s option will display the grand total for each of the specified filenames. The -a option
will generate an entry for each file in the listed directory, and recursively go through the subdirectories,
of the directory. So doing du in your home directory will list all the subdirectories and the total space
they are using.

11.13.2 Find
Usage: find [pathname-list] [-options] expression
Used to find files by name, or by other characteristics. Find recursively descends the directory hierarchy
for each pathname under the pathname-list, seeking files that match a given file name or other logical
expression defined by any of the aviailable operators. For example, the following command will search
all directories below the current directory for the files having the name “misc.f”. The file path will be
printed each time the file is found.
> find . -name misc.f -print
./Isds/Isds2/misc.f
The find command has several operators that can be used for defining search parameters. For instance,
files can be searched for by, name of file, type of file, date accessed, date modified, files newer than
arguement file, etc. For more information on the find command, type “man find” from a shell tool
prompt.

11.13.3 Script
Usage: script filename
The command script will create a typescript file of a loggin session. Everything that is printed on your

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.14 Shells and “dotfiles”

11.13.7 Whereis
223

Usage: whereis [-options] filename ...


Whereis locates source, binary and manual page files for a command. This command searches for
programs in a list of standard places. The available options are:
-b Search only for binaries.
-m Search only for manual sections.
-s Search only for sources.
-u Search for unusual entries. A file is said to be
unusual if it does not have one entry of each requested
type. Thus ‘whereis -m -u *’ asks for those files
in the current directory which have no documentation.
Other options are also available for changing or limiting directories in which to search. For more
information type “man whereis” from a shell tool prompt.

11.13.8 Who
Usage: who
This function will show the login names of all users on the computer you are currently logged in on.

11.14 Shells and “dotfiles”


A shell is a program that serves as an interface between the user and the operating system. The shell is
a command line interface as opposed to a graphical interface like the windows. That is, the user types a
command line into the shell for execution. Although much work can be done solely through the graphical
interface, some things can be done more efficiently with the command lines, and some things absolutely
require the use of the command line.

11.14.1 .cshrc & .login


When a user first logs in, a shell called csh (C SHell) is started. This shell first reads the contents of the
file .cshrc (C SHell Run Commands) in the users home directory and performs the commands in that
file. This file is very similar to the autoexec.bat file in MS-DOS. After that is done, the shell reads the
contents of the file .login in the home directory and performs the commands in that file. After the shell
is done with the commands in the .login file, it is ready to read commands from the user. This is when
the user generally starts openwindows.
Other shells are started for every cmdtool window that is started under openwindows. Each of these
shells also reads the .cshrc file and performs the commands in it. These other shells do not read the
.login file, only the first (login) shell does that. You can put commands you want every shell to perform
in the .cshrc file. Such commands might include setting the prompt to something other than the default.
One of the abilities of the shell is to alias a command to another. For instance, say you normally run
the command verylongprogramname and you get tired of typing it. You could alias it to something else
like myprog. Then, you would just have to type myprog to start verlongprogramname. The format of
the alias command is:
alias substitute_name ’original_program_name’
So for verlongprogramname, the alias would be
alias myprog ’verlongprogramname’

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.15 Further Reference 225

Assuming your login is “mylogin”, this file allows the login “mylogin” to login to any machines in the lab
from grieg, beethoven, and spot without giving a password. Generally, you would list all the machines
in the bechtel lab. There is a program called rhosts.setup which will create a .rhosts file for you and put
each of the lab machines in it. If you want to add other machines like spot or tramp, you will have to
edit .rhosts by hand.
The example above also allows “buddy” to log into any of the machines in the lab using your account
from spot. buddy would log in with the command rlogin -l mylogin bechtel. The -l means that
“buddy” wants to be “mylogin” on bechtel instead of “buddy”. This format is also useful if you have
different login names on different machines.

11.14.4 .signature
This file is used when you post news (see the section on news for information on how to get started with
news). When you post an article on news, this file is appended to your post like a signature. Generally
people will put their real name and e-mail address into the .signature file so others reading news will
know who they really are and how to reach them. Some people al from spotso include a humorous or
thoughful saying that they identify with. People reading news will often remember a saying better than a
name. However, don’t make the .signature file too long. Generally, four lines or less is always acceptable.
As you increase the size of the file, you increase the risk of people sending you nasty e-mail saying they
don’t like waiting for your .signature to be sent over their slow modem. A page long .signature, no
matter how cute it may be, will definitely bring about such mail.

11.14.5 .mailrc
This file will contain various setups for your mail. One usefull application is the definition of aliases to
shorten the length of e-mail addresses. For example, by having the following inside your .mailrc file:

alias bicanic bicanic@cvvaxa.swan.ac.uk

you simply have to address your e-mail to bicanic rather than to: bicanic@cvvaxa.swan.ac.uk

11.15 Further Reference


You can find more information on any of the topics discussed in this section in the following:

The UNIX Programming Environment by Brian W. Kernighan and Rob Pike. Prentice Hall Soft-
ware Series.

Learning the UNIX Operating System by Grace Todino and John Strang. O’Reilly and Asso-
ciates, Inc.

And any of the numerous reference guides and user’s manuals available in the lab.

11.16 Remote Loggin; Telnet


Telnet is your key to accessing applications on Internet host computers located around the world. In
a TCP/IP environment telnet is used to login to any computer on the network. Your first connection
to a UNIX computer is made using telnet unless you login via a directly connected serial terminal. So
what is telnet? It is terminal emulation program with a limited set of commands for you to remember.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.17 File Transfer Protocol (ftp)

11.17.1 Definitions
227

FTP: The Internet standard file transfer protocol. An ftp program is a user interface to this protocol
which allows the transfer to and from a remote network host.

FTP Client: The local program which allows connection to an FTP server for the purpose of sending
or receiving files.

FTP Server: An Internet host which allows clients to connect and transfer files to or from the hosts.

Anonymous FTP: Use of ftp to transfer files to or from a server which allows anyone to connect. The
FTP server allows a user login ID of “anonymous” or “guest” and accepts any password input.
The accepted courtesy on the Internet is for the use to provide his or her Internet email address.

ASCII File: Text files are files that you can display on your screen and/or pull into a text editor.
For example, AUTOEXEC.BAT and CONFIG.SYS are text files in DOS. Files you would usually
transfer in ASCII mode include: “readme” files, source code, uuencoded, binhex, PostScript and
HTML files. Files transferred in ASCII mode are “translated” so that they appear to be the same
on the local machine as the remote. Text is stored differently on different types of computers. PC
text is represented differently than Mac text. Neither PC nor Mac text is represented the same
as Unix text. Those files are usually ASCII files: Text (.txt, tex, .asc); Source code (.c, .f, .for);
e-mail messages; uuencoded files; PostScript (.ps); Hypertext (.htm, .html).

Binary File: Binary files are not text files. This includes many file types such as: executables, images,
sound, movies, spreadsheets, databases, word processing, and compressed files. Files transferred
in binary mode are copied exactly, bit-for-bit.
Those files are usually binary: Spreadsheets (.xls, ...); Databases (.dbf, .dbt); Word Processing
(.doc, wp); Compressed files (.Z, .zip); Images (.gif, .tif, .tiff, .jpg, .jpeg, bmp, pcx); Sounds (.au);
Movies (.mpg, .mpeg, .mov).

Download: An FTP client receiving a file from an FTP server using the “get” command is downloading
the file.

Upload: An FTP client sending a file to an FTP server using the “put” command is uploading the file.

11.17.2 Connecting to an FTP Server


To establish a connection with a remote host (FTP server) from the command line, simply type “ftp
<host name>”. Once the connection is made, the remote host will require the user enter a valid username
and password. To establish an anonymous ftp session, the username is anonymous and the password is
your full internet email address.

11.17.3 Basic Commands


The words with the “less-than” (<) and “greater-than” (>) signs are not literal, but indicate a real,
situtationally-dependent word should be substituted. The information contained within the brackets
(“[” and “]”) are not required.
cd <directory>: Change current directory to the indicated directory. Similar to DOS, there are several
unique inputs for <directory>. They are “/” (go to the top most directory) and “..” (go to the
next higher directory).

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.18 “Standard” Unix Editors

VI modes: VI has two modes of operation, INPUT mode, and EDIT mode. To get from INPUT mode
229

to EDIT mode hit the ¡ESC¿ key. Then to get back to EDIT mode, hit the <ESC> key.

Exiting the Editor: You must be in EDIT mode to be able to exit VI, so always make sure by hitting
the <ESC> key. To exit from VI and have the file changes applied to the actual file, need to be
in the editing mode of the editor and type: :wq and hit RETURN. It is possible to exit the editor
without applying any changes. To exit without saving the file, type: :q!. To save the current file
to a new file name, type: :w filename.

VI references: There are many sources of information on the VI editor. The first place you should
look are the users manuals located in the Lab. Most book stores that carry computer books will
also have books specific to the VI editor.

Some useful vi commands are summarized in Table 11.1.

11.18.2 Emacs
Emacs is a freely available editor which has been enhanced by thousands of enterprising contributors.
It is distributed by the Free Software Foundation and is available in both source code and precompiled
forms. (You can ftp a copy from the /pub/gnu directory at prep.ai.mit.edu or one of numerous mirror
ftp sites. ) It supports both character-based and X Window environments and runs under a wide variety
of operating systems, including Unix, DOS, and Windows. Emacs has been written by programmers
for programmers. Its single best feature is that it is infinitely extensible by means of a built-in LISP
interpreter. Almost every command in Emacs is actually a tiny little LISP program that acts upon your
text files. To extend Emacs, you simply modify the existing programs or add a few of your own.
Emacs is known as a ”modeless” editor, meaning that it is always ready to both accept text as input
and act upon that text via user commands. You never switch between ”command” and ”input” modes
in Emacs.
We need to start with some terminology. A buffer, in emacs terms, is where editing takes place. It
can contain a file, or hold new input from the user that is not yet attached to a file. Each buffer can have
a different file in it, and each is treated seperately. The name of the buffer is displayed at the bottom of
the window in what is called the status line. The name of the buffer will generally be the same as the
file in the buffer, if any. The special buffers *scratch* and *help* are exceptions as they do not have
files associated with them.
Also, each buffer can also be in a different mode. The mode of a buffer describes how emacs will act
when editing in that buffer. For instance, if a buffer is in C mode, emacs will try and treat the text as C
source code. In doing so, it will perform certain formating of the code as the user types it in. Likewise, if
it is in Fortran mode, it treats the text as Fortran source. Fundamental mode is the basic mode, which
doesn’t try to treat the text in any special way. The scratch buffer starts in Lisp Interaction mode.
In this mode, emacs will try to interpret the text in the same way it does the .Emacs file when it first
starts up. Don’t worry about how it works now. The mode the buffer is in is also displayed in the status
line.
The status line gives some other information too. If the file in the buffer has been modified, two *’s
will be displayed at the left of the status line. When you save the buffer back into the file, the *’s will
be removed.
Go ahead and experiment in the scratch buffer. Don’t worry about the text already there. If you
have read it, it’s purpose has been completed and it can now be discarded or altered. Some of the more
commonly used command keys are listed below.

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft
11.19 Other Editors

The keys on the numeric keypad (Home, PgUp, arrows) should do what the keys say. Go ahead
231

and try them on the scratch buffer, although there is not enough text initially for PgUp and PgDn to
do anything. Try typing several short lines to fill a couple of pages, then try these keys. You can get
numbers by holding down the shift key.

11.19 Other Editors

Victor E. Saouma Computing Literacy for Undergraduate Engineering Students


Draft

Bibliography

You might also like