You are on page 1of 56

Draft

DRAFT

Lecture Notes in:

COMPUTING LITERACY
For Undergraduate Engineering Students

! $%& b & P b B C ) ! ) 9) @ 9" 1%! 0'  $ ) ) s r q i y u tr x s rq i "p y u tr x s rq ip y u tr x s rq ip p y u tr x x 3 55 U3 w 5 4 5 U 3 3v s u tr q s rq i i s u tr q s q ip i ` XY W V r h f 6 g7 5 5 4 3 3 ! %& $ " H "b $%B ! ) C ) %11 e"@ 9) bc & a  B ) d ) $

` XY W V 5 4 5 U 3 3 6 5 43  # 1 $ ) ) (! $%& "!  9) @ 9" 1%! 0'  T54 4 54 3 3 S T5 54 3 3 S5 54 3 3 6 5 4 3 T54 4 54 3 54 3 23 S T 5 5 4 3 23 S 5 23 6 5 4 3 R Q ! &$ P $) "! $%& B  H G# "! ED C B %@ 9) @ 9" 1%! 0'  I F % & A $ ) ) 5 43 87 6 5 4 3 2 5 43 87 6 5 4 3 2 ! $%& " 1%! 0(! $%& # "   % )' !
1 2

   
r_, _ : r r_, _ : r
1 1

F1 F2

g1 r_, _ : D

r,

, r, r

r,

,r

r D

r,

, ,

r2 r2

g2 r_, _ : D g1 r,

, r, r

D g1 r,

,r

r D g1 r,

, ,

F1 _
m

Exp m

eqdiff Simplify Factor g2 r,


m

r3

% Dimension of elements A=[ft,Sigma2,(fc-Sigma2)]; if (sigma(1)<=ft && sigma(1)>0) w_nod=C1; elseif (sigma(1)<=0 && sigma(1)>Sigma2) for h=1:16 w_nod(h)=C1(h)+M1(h)*sigma(1); end elseif (sigma(1)<=Sigma2 && sigma(1)>=fc) for h=1:16 w_nod(h)=C2(h)+M2(h)*(sigma(1)-Sigma2); end else w_nod=zeros(1,14); end

Solve eqdiff Exp m


m 1

0, m

, m

, m

, m

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

Draft
Contents
1 MATLAB: Basic Introduction 1.1 Background . . . . . . . . . . . . . . . . . . . 1.1.1 What is MATLAB? . . . . . . . . . . 1.1.2 Availability . . . . . . . . . . . . . . . 1.1.3 Accessing MATLAB . . . . . . . . . . 1.1.4 Help . . . . . . . . . . . . . . . . . . . 1.1.5 Basic Features . . . . . . . . . . . . . 1.1.6 Simple Math . . . . . . . . . . . . . . 1.1.7 Common Mathematical Functions . . 1.1.8 Statements, Expressions and Variables 1.1.9 Display Formats . . . . . . . . . . . . 1.1.10 Printing Text and Matrices . . . . . . 1.1.11 Variables . . . . . . . . . . . . . . . . 1.1.12 MATLAB Workspace . . . . . . . . . 1.1.13 Saving and Retrieving Data . . . . . . 1.1.14 Arrays . . . . . . . . . . . . . . . . . . 1.1.15 Graphics . . . . . . . . . . . . . . . . 1.1.16 Graphics hardcopy . . . . . . . . . . . 1.2 Assignment . . . . . . . . . . . . . . . . . . . 1.2.1 Practice . . . . . . . . . . . . . . . . . 1.2.2 Work . . . . . . . . . . . . . . . . . . 1.2.3 Stresses . . . . . . . . . . . . . . . . . 1.2.4 Measurement Errors, [?] . . . . . . . . 1.2.5 Statistical Analysis; Part I . . . . . . . 1.2.5.1 Elements of Statistics . . . . 1.2.5.2 Assignment . . . . . . . . . . 2 MATLAB: Matrix Algebra 2.1 Syntax . . . . . . . . . . . . . . . . 2.1.1 Matrix Operations . . . . . 2.1.1.1 Matrix Denition 2.1.1.2 Matrix Operations 2.1.1.3 Matrix functions . 2.1.2 Graphics Revisited . . . . . 2.1.2.1 Polar Plots . . . . 2.1.2.2 3-D mesh plots. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 17 17 17 17 19 19 19 22 22 24 25 26 26 27 27 30 32 32 32 33 33 34 34 34 38 39 39 39 39 41 42 43 43 44

Draft
CONTENTS 4.1.2

5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 83 83 84 84 84 84 85 85 85 85 86 86 86 86 87 88 88 88 89 90 90 90 90 90 91 91 91 92 92 92 92 92 93 93 93 94 94 94 94 94 94 94 95 95 95 95 96

4.1.3

Front End and Kernel . . . . . . . . . . . Accessing Mathematica . . . . . . . . . . Help . . . . . . . . . . . . . . . . . . . . . Notebook Front End . . . . . . . . . . . . 4.1.1.6.1 Pointers . . . . . . . . . . . . . . 4.1.1.6.2 Cell Brackets . . . . . . . . . . . 4.1.1.7 Brackets, Parentheses, and Braces . . . . Examples . . . . . . . . . . . . . . . . . . . . . . . 4.1.2.1 Basic Arithmetic Operation . . . . . . . . 4.1.2.2 Approximate Numerical Values . . . . . . 4.1.2.3 Complex Numbers . . . . . . . . . . . . . 4.1.2.4 Derivatives . . . . . . . . . . . . . . . . . 4.1.2.5 Integration . . . . . . . . . . . . . . . . . 4.1.2.6 Algebraic Formulae . . . . . . . . . . . . 4.1.2.7 Solving equations . . . . . . . . . . . . . 4.1.2.8 Matrices . . . . . . . . . . . . . . . . . . 4.1.2.9 Graphics and Three-Dimensional Plots . 4.1.2.10 Interfacing with Mathematica . . . . . . . 4.1.2.10.1 Input . . . . . . . . . . . . . . . 4.1.2.10.2 Output . . . . . . . . . . . . . . 4.1.2.11 Packages . . . . . . . . . . . . . . . . . . 4.1.2.12 Graphics hardcopy . . . . . . . . . . . . . 4.1.2.13 Input le . . . . . . . . . . . . . . . . . . Some Mathematica Commands . . . . . . . . . . . 4.1.3.1 Basic Operations . . . . . . . . . . . . . . 4.1.3.2 Mathematical Functions . . . . . . . . . . 4.1.3.3 Some Mathematica Constants . . . . . . . 4.1.3.4 Complex Numbers . . . . . . . . . . . . . 4.1.3.5 Recall of Previous Expressions . . . . . . 4.1.3.6 Assignment of Variables . . . . . . . . . . 4.1.3.7 Brackets . . . . . . . . . . . . . . . . . . 4.1.3.8 Help . . . . . . . . . . . . . . . . . . . . . 4.1.3.9 Interrupting Mathematica . . . . . . . . . 4.1.3.10 Transformation of Algebraic Expressions 4.1.3.11 Dierentiation . . . . . . . . . . . . . . . 4.1.3.12 Integration . . . . . . . . . . . . . . . . . 4.1.3.13 Summation & Products . . . . . . . . . . 4.1.3.14 Equations . . . . . . . . . . . . . . . . . . 4.1.3.14.1 Preliminaries . . . . . . . . . . . 4.1.3.14.2 Solution . . . . . . . . . . . . . . 4.1.3.14.3 Dierential Equations . . . . . . 4.1.3.14.4 Numerical Values . . . . . . . . 4.1.3.15 Functions . . . . . . . . . . . . . . . . . . 4.1.3.16 Vectors & Matrices . . . . . . . . . . . . 4.1.3.17 Graphics . . . . . . . . . . . . . . . . . . 4.1.3.17.1 Preliminaries . . . . . . . . . . . 4.1.3.17.2 Plot Options . . . . . . . . . . . 4.1.3.17.3 3 Dimensional Plots . . . . . . .

4.1.1.3 4.1.1.4 4.1.1.5 4.1.1.6

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
CONTENTS 5.8.2

7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 135 136 136 136 136 137 137 138 138 139 140 140 140 141 141 142 144 144 145 147 148 150 153 153 153 153 155 156 157 157 157 157 158 158 158 158 161 161 161 161 161 161 162 162 163

Listing . . . . . . . . . . 5.8.2.1 inltration.m . 5.9 montecarlo . . . . . . . . . . . 5.9.1 Description . . . . . . . 5.9.2 Listing . . . . . . . . . . 5.9.2.1 montecarlo.m 5.10 3dplot . . . . . . . . . . . . . . 5.10.1 Description . . . . . . . 5.10.2 Listing . . . . . . . . . . 5.10.2.1 cont.m . . . . 5.10.2.2 pix.m . . . . . 5.10.2.3 smooth.m . . . 5.11 zec . . . . . . . . . . . . . . . . 5.11.1 Description . . . . . . . 5.11.2 Listing . . . . . . . . . . 5.11.2.1 linregress.m 5.11.2.2 zec.m . . . . . 5.12 Stress/Strain Programs . . . . 5.12.1 Description . . . . . . . 5.12.2 Sample Output: . . . . 5.12.2.1 Example 1: . 5.12.2.2 Example 2: . 5.12.2.3 Example 3: .

6 Fortran 90: INTRODUCTION 6.1 Introduction . . . . . . . . . . . . . . . . . . . . 6.1.1 Computer Architecture and Software . . 6.1.1.1 Computer Hardware . . . . . . 6.1.1.2 Computer Software . . . . . . 6.1.2 Fortran Past, Present, and Future . . . 6.2 From Source to Binary . . . . . . . . . . . . . . 6.3 Sample Example . . . . . . . . . . . . . . . . . 6.4 Fortran Compilers . . . . . . . . . . . . . . . . 6.4.1 Compilers . . . . . . . . . . . . . . . . . 6.4.2 File Extensions . . . . . . . . . . . . . . 6.4.3 Workstation Compilers . . . . . . . . . . 6.4.4 Compiling with Microsoft Power Station 6.5 Flow Charts, Macro-Code . . . . . . . . . . . . 7 Fortran 90: Basic Operations 7.1 Background . . . . . . . . . . . . 7.1.1 Preliminaries . . . . . . . 7.1.1.1 Program Layout 7.1.1.2 Characters . . . 7.1.1.3 Variable Types . 7.1.1.4 Labels . . . . . . 7.1.2 Data Declaration . . . . . 7.1.3 Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
CONTENTS

9 189 189 189 189 189 191 191 192 193 193 193 193 193 195 195 196 196 196 197 199 199 200 200 201 203 203 203 206 206 209 209 209 209 210 210 210 210 210 211 211 211 211 211 211 211 213

9 Fortran 90; PROGRAM UNITS; POINTERS 9.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1.1 Program Units . . . . . . . . . . . . . . . . . . . . 9.1.1.1 Main Program . . . . . . . . . . . . . . . 9.1.1.2 Functions . . . . . . . . . . . . . . . . . . 9.1.1.3 Recursive Subprograms . . . . . . . . . . 9.1.1.4 Subroutines . . . . . . . . . . . . . . . . . 9.1.1.5 Modules . . . . . . . . . . . . . . . . . . 9.1.1.6 Interface . . . . . . . . . . . . . . . . . 9.1.2 Derived Data Type . . . . . . . . . . . . . . . . . . 9.1.3 Pointers . . . . . . . . . . . . . . . . . . . . . . . . 9.1.3.1 Pointer Variable . . . . . . . . . . . . . . 9.1.3.2 Pointer Assignment . . . . . . . . . . . . 9.1.3.3 Dynamic Memory Allocation for Pointers 9.1.3.4 Pointers to Arrays . . . . . . . . . . . . . 9.1.3.5 Pointers as a Tool for Linked Lists . . . . 9.2 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . 9.3 Mendeleev . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.4 Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Fortran 90: MAKE, DEBUGGER 10.1 Debugging options . . . . . . . . . 10.2 FORTRAN Preprocessor . . . . . . 10.2.1 System Calls . . . . . . . . 10.3 Make . . . . . . . . . . . . . . . . . 10.4 Debugger . . . . . . . . . . . . . . 10.5 Introduction to Debugging . . . . . 10.5.1 The debugger . . . . . . . . 10.5.2 Running the debugger . . . 10.5.3 Debugger Commands . . . 11 INTRODUCTION TO UNIX 11.1 logging In . . . . . . . . . . . . . 11.2 Changing Your Password . . . . 11.3 Logging Out . . . . . . . . . . . . 11.4 On-Line Help . . . . . . . . . . . 11.4.1 man . . . . . . . . . . . . 11.4.2 Apropos . . . . . . . . . . 11.5 UNIX File System . . . . . . . . 11.5.1 Directory Structure . . . 11.5.2 Your User Account . . . . 11.5.3 Absolute Pathnames . . . 11.5.4 Relative Pathnames . . . 11.6 Shell . . . . . . . . . . . . . . . . 11.7 File Management . . . . . . . . . 11.7.1 Changing Directories . . . 11.7.2 Changing File Protection 11.7.3 Copying Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
CONTENTS

11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 228 228 229 231

11.17.3 Basic Commands . 11.18Standard Unix Editors . 11.18.1 vi Editor . . . . . 11.18.2 Emacs . . . . . . . 11.19Other Editors . . . . . . .

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
List of Figures
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 3.1 3.2 3.3 3.4 3.5 3.6 3.7 4.1 4.2 4.3 4.4 5.1 5.2 5.3 5.4 Matlabs Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . Set Path in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Selecting path Directory in Matlab . . . . . . . . . . . . . . . . . . . . Directory Containing Source Code in Matlab . . . . . . . . . . . . . . Matlabs Operating Environment . . . . . . . . . . . . . . . . . . . . . Simple Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multiple Plots in a Single Graph . . . . . . . . . . . . . . . . . . . . . Normalized Gauss Distribution, and Cumulative Distribution Function Polar Plot of a Cardioid . . . . . . . . . . . . . . . . . . . . . Sample of Surface and Contour Plots . . . . . . . . . . . . . . Picture Used for Animation . . . . . . . . . . . . . . . . . . . Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Regression Analysis . . . . . . . . . . . . . . . . . . . . . . . Bonded Strain Gage . . . . . . . . . . . . . . . . . . . . . . . Strain Rosette . . . . . . . . . . . . . . . . . . . . . . . . . . . Steel Truss . . . . . . . . . . . . . . . . . . . . . . . . . . . . Single Degree of Freedom Oscillator . . . . . . . . . . . . . . Simply Supported Beam Column; Dierential Segment; Eect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . of Axial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 19 20 21 21 30 32 37 44 45 45 47 48 52 53 56 59 61 66 68 71 71 72 76 77 82 83 88 89 104 104 106 108

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Force P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Newton-Cotes Numerical integration . . . . . . . . . . . . . . . . . . . . Center of Gravity and Moments of Inertia of A Triangular Cross-Section Runges Midpoint Method . . . . . . . . . . . . . . . . . . . . . . . . . . Runges Trapezoid Method . . . . . . . . . . . . . . . . . . . . . . . . . 4th Order Runge-Kutta Method . . . . . . . . . . . . . . . . . . . . . . Runge-Kutta Solution for the Mass-Spring-Damper System . . . . . . . Equivalent and Exact Stress Distribution in Reinforced Concrete Beams Notebook Front End for Mathematica . . . . . . . . Notebook Front End Help for Mathematica . . . . . Two-dimensional Plot generated from Mathematica . Three-dimensional Plot generated from Mathematica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Draft
List of Tables
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 3.1 3.2 3.3 3.4 4.1 6.1 6.2 6.3 7.1 7.2 7.3 7.4 Basic Commands . . . . . . . . . . Special Characters . . . . . . . . . Common Mathematical Functions Display Options . . . . . . . . . . . Text and String Operations . . . . Special Variables . . . . . . . . . . Disk Files Commands . . . . . . . Array Operations . . . . . . . . . . 2D Graphics Commands . . . . . . Printer Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 22 23 24 26 26 27 29 31 32 39 41 42 46 49 49 50 51 55 58 66 67 69 74

Built In Matrix Denition Functions Matrix Operations . . . . . . . . . . Matrix Functions . . . . . . . . . . . Statistical Analysis Functions . . . . Signal Processing Functions . . . . . Relations and Logical Operators . . Relational and Logical Functions . . Control Flow . . . . . . . . . . . . . Strain Rosette Problems . . . . . . . Result of Truss Design . . . . . . . .

Weights for Newton-Cotes Quadrature Formulas Numerical Integration and Dierentiation . . . . Nonlinear Equations and Optimization . . . . . . Dierential Equations . . . . . . . . . . . . . . .

Mathematica Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 PC Vs Workstation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Fortran Compilers in the Bechtel Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Fortran Compiler Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Characters . . . . . . . . . . . . . . . . . Basic Arithmetic Operations . . . . . . Truth Table for Logical Operators . . Common Arithmetic Intrinsic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 164 164 165

Draft
Chapter 1

MATLAB: Basic Introduction


1.1
1.1.1

Background
What is MATLAB?

1 MATLAB is an interactive, matrix-based system for scientic and engineering calculations. Contrarily to programming languages, such as Fortran, C, or Basic you can solve complex numerical problems without actually writing a program.

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
4

Availability

MATLAB is available in the CAD lab (PC/Windows version), as well as in the Bechtel Laboratory.

A relatively inexpensive Student Edition is available from Prentice Hall and can be purchased from the Bualo Chip.

1.1.3
6

Accessing MATLAB

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. Fig. 1.1.3 illustrates the content of the default directory containing your source code.

Finally, Fig. ?? illustrates a typical working envirnoment, where a le 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.
8 9

1 Other

similar products include IDL, and MATHEMATICA for symbolic computations.

Draft
Chapter 2

MATLAB: Matrix Algebra


2.1
2.1.1
2.1.1.1
1

Syntax
Matrix Operations
Matrix Denition

Matrices can be entered in several dierent 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 zeros ones diag triu tril rand hilb magic toeplitz identity matrix matrix of zeros matrix of ones see below upper triangular part of a matrix lower triangular part of a matrix randomly generated matrix Hilbert matrix magic square see help toeplitz

Table 2.1: Built In Matrix Denition 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,:) 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 diag(v) ans = 0.2000 0 0

41

2.6000

0 1.4000 0

0 0 2.6000

2.1.1.2
4

Matrix Operations

Matrix operations are listed in Table 2.2. + \ / addition subtraction multiplication power transpose left division right division

Table 2.2: Matrix Operations 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

43
-6.2917

-0.9167

EDU>> triu(lux) ans = 4.0000 2.0000 0 6.0000 0 0 EDU>> tril(lux) ans = 4.0000 0 -0.2500 6.0000 0.5000 -0.9167 EDU>> tril(lux,-1) ans = 0 0 -0.2500 0 0.5000 -0.9167 EDU>> diag(lux) ans = 4.0000 6.0000 -6.2917 EDU>> eig(x) ans = 4.4123 12.9438 2.6440

3.0000 11.5000 -6.2917

0 0 -6.2917

0 0 0

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
2.1.2.1

Graphics Revisited
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.8 0.6 0.4 0.2 0 1 0.5 0 0.5 1 1 0.5 0.5 0 1

45
1 0.8 0.6 0.4 0.4 0.2 0 0.2 0.4 0.3 0.6 0.8 1 1 0.5 0.2 0.8 0.6 0.4 0.2 0 0.2 0.4 0.6 0.8 1 0.7 0.6 0.9 0.8

0.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 0 0.5 1 1 0.5 0.5 0 1

Figure 2.3: Picture Used for Animation 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.7
2.2.7.1

2.2 Assignment

61

Nonlinear Equation
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
dx

y,u

M P
i

V
v x

V+
dx

V x dx
P

M+ M dx x

i j

P
j
P

dx

Figure 2.10: Simply Supported Beam Column; Dierential Segment; Eect of Axial Force P the horizontal axis is small, leads to M M+ (dx)2 dV dM dx + w + V + dx 2 dx dx P dv dx dx = 0 (2.25)

neglecting the terms in dx2 which are small, and then dierentiating each term with respect to x, we obtain dV d2 v d2 M P 2 =0 (2.26) 2 dx dx dx However, considering equilibrium in the y direction gives dV = w dx From beam theory, neglecting axial and shear deformations, we have M = EI d2 v dx2 (2.28) (2.27)

Substituting Eq. 2.27 and 2.28 into 2.26, and assuming a beam of uniform cross section, we obtain EI d2 v d4 v P 2 =w 4 dx dx (2.29)

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft

2.2 Assignment (e) Tolerance

63

2. Plot the equation for the specied range 3. Solve the equation within the specied 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 ops (oating point operations) with the preceding solution.

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
Chapter 3

MATLAB: Advanced Topics


3.1
3.1.1
1

Background
Numerical Integration and Dierentiation
b a b a

The integration of of F (x) and then use

F (x)dx is essentially based on passing a polynomial P (x) through given values P (x)dx as an approximation.
b a b

F (x)dx

P (x)dx
a

(3.1)

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 )


n

(3.2-a) (3.2-b)

li (x)F (xi )
i=1

3.1.1.1
3

Newton-Cotes Method

In Newton-Cotes integration, it is assumed that the sampling points are equally spaced, Fig. 3.6, thus we dene
b b n i b a n i n i=1 b

P (x)dx =
a a

li (x)dxF (xi ) =
a

li (x)dxF (xi )

(3.3)

or Approximation Weights
(n)

P (x)dx (n) Wi

= =

Wi F (xi ) b (n) li (x)dx = (b a)Ci a

(n)

(3.4)

where Ci are the weights of the Newton-Cotes quadrature for numerical integration with n equally spaced sampling points.

Draft
1 1

3.1 Background P (x)dx

67

P (x) = l1 (x) = l2 (x) l3 (x)


(3) W1 (3) W2 (3) W3

= = = = =

1 1

F (x)dx

3 i=1 li (x)F (xi ) (xx2 )(xx3 ) 1 (x1 x2 )(x1 x3 ) = 2 x(x 1) (xx1 )(xx3 ) (x2 x1 )(x2 x3 ) = (1 + x)(x 1) (xx1 )(xx2 ) 1 (x3 x1 )(x3 x2 ) = 2 x(1 + x) 1 1 1 1 l (x)dx = 2 1 x(x 1)dx = 3 1 1 1 1 l (x)dx = 1 (1 + x)(x 1)dx = 4 3 1 2 1 1 l3 (x)dx = 1 1 x(1 + x)dx = 1 2 3 1 1 (3) 3 P (x)dx = i=1 Wi F (xi ) = 1 [F (1) 3 1

+ 4F (0) + F (1)]

which is Simpsons rule 3.1.1.2


8

MATLAB Examples

MATLABs functions for numerical (denite) integration and dierentiation are shown in Table 3.2. Numerical Integration trapezoidal numerical integration numerical function integration; Simpsons rule Newton-Cotes 8 panel rule approximate derivatives

trapz quad quad8 di

Table 3.2: Numerical Integration and Dierentiation


9

For example, let us recall the expression for the center of gravity ydA y= A x2 dA
A cg Iyy +

(3.5)

and moment of inertia Ixx =


A cg Ixx = Ixx + Ad2 y

y 2 dA

Iyy = Iyy =

(3.6-a) (3.6-b)

Ad2 x

and moment of inertia for a given cross section for a rectangular cross (width b, height h) section we would have ydA y = b = =
Victor E. Saouma
0

A
h

(3.7-a) ydy

bh h 2

(3.7-b) (3.7-c)

Computing Literacy for Undergraduate Engineering Students

Draft
Chapter 4

MATHEMATICA
4.1
4.1.1
4.1.1.1
1

Background
Introduction
What is Mathematica ?

Mathematica is a program for symbolic mathematics.

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

4.1.1.2
5

Mathematica is available in the Bechtel Laboratory.

A relatively inexpensive Student Edition can be purchased from the Bualo Chip, and the University used to have a site license for it. Front End and Kernel

4.1.1.3
7

Mathematica 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
9

4.1 Background In both cases Mathematica is used interactively Accessing Mathematica

83

4.1.1.4

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).

If you want to run the program dierently, 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.
11 12

Alternatively, you may simply type mathematica for the Notebook Front End. Help

4.1.1.5
13

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

14

15

Figure 4.2: Notebook Front End Help for Mathematica

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
4.3 References

101

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

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. 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, AddisonWesley, 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 Beginners 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
5.1.1

arches
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. C (+ ') Mz = 0; (+ E ) Fx = 0; ) (+ T Fy = 0; B (+ ') Mz = 0; (RAy )(140) + (80)(3.75) (30)(80) (20)(40) + RAx (26.25) = 0 140RAy + 26.25RAx = 2.900 80 RAx RCx = 0 RAy + RCy 30 20 = 0

(Rax )(60) (80)(30) (30)(20) + (RAy )(80) = 0 80RAy + 60RAx = 3, 000

(5.1-a)

Draft
5.1 arches ' (+ ) MB = 0; HC

105 =
wR 2 [

sin ] |= = =0

wR 2 [( = wR 2

sin ) (0 sin 0)]

(5.4-a)

Next we determine the horizontal reaction


= 2

(HC )(R) + (Vc )(R) = =


2 wR 2 wR

wRd
=0 wR 2 = dP = 2

R cos
moment arm

=0

cos d
=0 2 wR

2 wR[sin ] |=0 =

1 wR

wR( 0) 2

(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 derivation: 1. Starting with CY (+ ') MA = 0;
=

(CY )(2R) + = =
wR 2 [ wR 2

wRd R(1 + cos ) = 0


=0 = dP

moment arm

CY

(1 + cos )d sin ) (0 sin 0)]

sin ]

=0 |= = wR [( =0 2 = wR 2

(5.6-a)

2. Next we determine the horizontal reaction ' (+ ) MB = 0; Cx =


2 wR = 2

(Cx )(R) + (Cy )(R) =


2 wR

wRd
=0 wR 2 = dP = 2

R cos
moment arm

=0

cos d
=0 2 wR

2 wR(sin ] |=0 =

1 wR

wR( 0) 2

(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): (
Victor E. Saouma

1)wR cos + wR sin 2 2


Cx Cy

wRd sin + V = 0
=0

(5.8)

V = wR ( 1) cos + ( ) sin 2 2

(5.9)

Computing Literacy for Undergraduate Engineering Students

Draft
5.1 arches

107

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

( 1)wR sin + wR cos 2 2 N = wR ( ) cos 2

wRd cos + N = 0
=0 ( 2

(5.10) (5.11)

1) sin

Moment: Now we can consider the third equation of equilibrium (M = 0): ( 1)wR R sin wR2 (1 cos ) + 2 2
=0

wRd R(cos cos ) + M = 0


2 (1

(5.12) (5.13)

M = wR2

sin ) + ( ) cos 2

We seek to determine the vertical deection of the crown of the three hinged statically determined semi-circular arch, Fig. 5.4 under its own dead weight w, Fig. ??. 1. We rst seek to determine the analytical expression of the moment diagram. From statics, it can be shown that the vertical and horizontal reactions are Rv = wR and Rh = ( 1)wR. 2 2 2. Next considering the free body diagram of the arch, and summing the forces in the radial direction (FR = 0):

( 1)wR cos + wR sin 2 2

wRd sin + V = 0
=0

(5.14-a) (5.14-b)

V = wR ( 1) cos + ( ) sin 2 2 3. Similarly, if we consider the summation of forces in the axial direction (F T = 0):

( 1)wR sin + wR cos 2 2 N = wR ( ) cos 2

wRd cos + N = 0
=0 ( 2

(5.15-a) (5.15-b)

1) sin

4. Now we can consider the third equation of equilibrium (M = 0): ( 1)wR R sin wR2 (1 cos ) + 2 2
=0

wRd R(cos cos ) + M = 0


2 (1

(5.16-a) (5.16-b)

M = wR2

sin ) + ( ) cos 2

5. The real curvature is obtained by dividing the moment by EI = M wR2 (1 sin ) + ( ) cos EI EI 2 2 (5.17)

6. The virtual force P will be a unit vertical point in the direction of the desired deection, causing a virtual internal moment M =
Victor E. Saouma

R [1 cos sin ] 2

(5.18)

Computing Literacy for Undergraduate Engineering Students

Draft
5.1 arches
P

109

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


2

2
=0

wR2 R (1 sin ) + ( ) cos [1 cos sin ] Rd EI 2 2 2


dx M

= =

wR4 7 2 18 12 16EI .0337 wR EI


4

(5.19-a)

5.1.2
5.1.2.1

Listing
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 N M arches.m le starts here:

1) cos + ( ) sin ] 2 2 = R[( ) cos ( 1) sin ] 2 2 2 = R [ (1 sin ) + ( ) cos ] 2 2 R[(

%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
Shear Force 200 90 120 60 150 180 210 240 270 300 0 200 30 0 330 Axial Force 400 90 120 60 200 0 150 200 400 180 210 240 270 300 30 0 330

111

Bending Moment 3000 90 120 60 1000 150 180 210 240 270 300 1000 3000 30 0 330

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

5.2
5.2.1

beam1
Description

SUBDIRECTORY : beam1 SYNOPSIS: shear and bending moment diagram for a simple beam (Structures) DESCRIPTION: BMdemo.m gets the bending moment and deection for a simply supported beam subjected to either a point load or a distributed load. CONTENTS: BMdemo.m M2deflection.m P2M.m P2V.m w2M.m w2V.m
Victor E. Saouma

the main program converts bending moments to deections by integration converts point loads to moment using a formula converts point loads to shear using a formula converts uniform loads to moment using a formula converts uniform loads to shear using a formula
Computing Literacy for Undergraduate Engineering Students

Draft
5.2 beam1

113

Continuous Load R1 = R2 = b (2a + b) 2L b (2c + b) 2L for (x < a) (x a) 2 for (a x a + b) for (x a + b) for (x < a) for (a x a + b) for (x a + b)

M = R1 x = R1 x

= R2 (L - x) V = R1 = (x a)(R2 R1 ) + R1 b

= R2 v = BMdemo.m le 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);

v(i 1) 2v(i) + (i + 1) M (i) = EI dx2

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
5.2 beam1
Bending Moment
6 4 2

115
x 10
4

0 0 4 x 10 1

10

12

Shear

1 0 0

10

12

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
% % % % % |---> x %get b b=L-a;

117

/\ /\ |----A----| |------------L------------| |-------B-------|

%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

x 10

2*Theta = 22.46 degrees Tmax

0.5

P1 2*Theta C

Tau xy

S2

S1 Sigma x

0.5

P2

1 Tmin 5 0 5 10 15 x 10
7

Figure 5.28: Example 2(e): Mohrs circle of 2-D stress.

150

Tmax2

100

Tmax1 Tmax3

50
Tau xy

S3

S2

S1 Sigma x

50

100

150

150

100

50

50

100

150

200

Figure 5.29: Example 3(c): Mohrs 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 nally 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
6.1.1
6.1.1.1
3

Introduction
Computer Architecture and Software
Computer Hardware

Digital computers are concerned with the processing of data1 abd their basic architecture has seen little if any fundamental changes since the conceptual model rst 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). A group of eight bits form a byte. A byte can have 28 = 256 dierent values, and is usually used to store a character (extended ASCII). Early PCs 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 buer between the CPU and the core memory. 2. Core memory, very fast, usually around 8 Mb (106 ) bytes for PCs and at least 32 Mb for workstations. In most present day computers, four bytes form a word. The most elementary unit of data is the bit.

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 PCs are computationally more powerful than the immediate past generation of workstations. Major distinction between PC and Workstation is highlighted by table 6.1. Feature Processor Memory Operating System RISC Monitor Resolution Bit Planes Graphics Accelerators Networking Editors Virtual Memory Primary Application Source of Software Graphics PC Intel 8-16 Mb Windows 95/NT N 15-17 1024x1024 8 Y Novell Word N Everything Microsoft Open GL Workstation Motorola 32-64 Mb Unix/Motif Y 19-21 1024x1024 8-24 Y Ehternet Vi, Emacs, Crisp Y Scientic Computing Public-Domain, GNU, Vendors 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 PCs for scientic computing (through Linux).
3 Unix

is a trademark of AT&T. Computing Literacy for Undergraduate Engineering Students

Victor E. Saouma

Draft
6.2
6

6.2 From Source to Binary

157

From Source to Binary

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 les 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
7

Sample Example

To begin with, use your favourite editor to create the following le ello.f5

program Hello print *, Hello, world end program Hello


8

Next save the le, and type

f90 hello.f a.out

6.4
6.4.1
9

Fortran Compilers
Compilers

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 rst internally translated into C and then compiled with a C compiler. This was

5 This classic example was rst 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
32

7.1 Background
ndata=ndata+1 end do count mean=sum/real(ndata)

169

Finally, do loops can be nested.

7.1.6
7.1.6.1

Input/Output
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 specied les. Thoose les must rst be dened through the open statement which takes the following form open (unit=unit number,file=le name, status=status) where unit number is an integer number assigned to the device, le name is a character string giving the le 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 specied through the open statement, and format label is the format label as dened below.

The format for the write statement is as follows write(unit number,format label)expression list where unit number is the device number specied through the open statement, and format label is the format label as dened below.
34 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 Denition A format can be dened in anyone of the following ways: Statement Label: referring to a format statement
900 write(6,900)A format(f10.2)

Character Variable provides a exible way to dynamically dene the format:

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
7.1.8 Style
do do

7.1 Background

171

1. Nested loops should be arranged such that the inner most loop should reference the rst subscript, and the outer most loop, the last subscript.
k=1,50 l=1,20 a(l,k)=b(l,k)+c(l,k) end do end do

2. All common blocks and sub-system control variables dened via parameter statements are dened in include les. The naming convention for include les is based on their contents; include les containing common blocks have the extension .cmn and include les containing parameters have the extension .par. The dierent extensions allow the developer to determine the contents of an include le 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 dened 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 dierent prexes depending on its usage in the program: (a) Prex i,j, or l should be used to indicate a DO loop variable (b) Prex k will indicate a counter (c) Prex m will indicate a maximum value (d) Prex n will indicate a given number For example ipoin, npoin, mpoin will indicate a particular point, number of points, and maximum 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 dicult to follow the logic of the subroutine. It can be especially dicult to debug a subroutine with multiple return statements and for
Victor E. Saouma Computing Literacy for Undergraduate Engineering Students

Draft
7.2
7.2.1

7.2 Assignment

173

Assignment
Beam on Elastic FOundation Deection

The defelection of a beam, length L, and exural rigidity EI on elastic foundation with stiness k by a central point load P is given by v= where =
k EI

2P [sinh(L) cos(x) cosh((L x)) sin(L) cosh(x) cos((L x))] k sinh2 (L) sin2 (L)

(7.1)

Find the deection 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 coeceints of the quadratic equation ax2 + bx + c = 0 (7.2) and calculate and print the roots. Account for the three possibilities 1. Two dierent 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 fc for two dierent ready mix companies. Determine the Mean, Standard Deviation, Coecient 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
8.1.1
1

Background
Denition

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

8.1.2
2

Declaration

Whereas 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 rst subscript of a two-dimensional array represents the row, and the second the column.

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.6

8.1 Background

177

Dynamic Memory Allocation

10 Dynamic allocation is useful when reading a datale of a size that is not known until run-time. The array would need to be dened as allocatable. This is done by simply adding allocatable to the declaration line, with the dimensions specied 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
11

do Loops & Vector Operations

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
9.1.1
1

Background
Program Units

A fortran 90 code is a modular hierarchical object dened 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.

Program and procedure can share data through the module unit. Main Program

9.1.1.1
4

Always assign a header, i.e. program stress. End the program similarly: i.e. end program stress.

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.1.3
17

9.1 Background
return end function average

191

Recursive Subprograms

Fortran 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 functions 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 predened.

21

The general format to reference a subroutine is through the following executable statement call subroutine-name (argument list)

22

In turn, the rst 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. Declaration of all parameters should include the intent attribute. The subroutine should include a return and end subroutine It is advisable to have input arguments preceding output ones. Following is a main program
Computing Literacy for Undergraduate Engineering Students

25

26

27

28

Victor E. Saouma

Draft
10.3 Make

201

10.3

Make

When developing a program, the executable le must be continuously recreated as the source code is modied and recompiled. This can become a hassle, especially if your program is lengthy. If your modications 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 les, each le containing one or more subroutines. Make will only recompile those les that have been modied since the last compile and will relink all the object les and create a new executable. To use make you need a description le that contains the information make needs to create your program. This information includes the names of the source les, the compiler you wish to use, the compiler options, and so on. This le is called the makele. While this section will not go into the million and one uses for make it will show you how to build a makele. Basically, a makele consists of a number of denitions and a number of targets. The denitions are used to specify le names, which compiler and linker to use, ags to use when compiling, etc. The targets is what tells make what to do when invoked. A target species 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 les in $(OBJECTS), and the command to make the program from the object les is specied 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 le from a source le, for example a .o le from a .f le. Thus when it sees that $(PROGRAM) is dependent on a number of object (.o) les, it will nd the source (.f) les in the directory and create the .o les automatically. The following is an example of a simple makele. 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 > make target

203

This looks for a le called makele in whatever directory you are in. If you want to call your description le something other than makele 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:

Usual targets are all (usually default), the program name, both of which makes the executable le, and clean, with cleans up in the directory. If you need more information with creating your makele the on-line man pages may help. Also the book Managing Programs with make by Andrew Oram and Steve Talbott, OReilly 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 specied places (by setting breakpoints), continue execution after a stop, see the contents of variables, either at specied 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 programs variables are at dierent points during the execution. When a program has a serious error, such as a segmentation fault, the operation system will create a le called core in the current working directory. This le can be used with a debugger to nd 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 ve subwindows. Menu Bar There are ve menu options each with their corresponding submenus. Each menu and submenu are briey described below:
Victor E. Saouma Computing Literacy for Undergraduate Engineering Students

Draft

10.5 Introduction to Debugging

205

Display Subwindow. This area shows the values of variables specied with the display command. 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 les. 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 rst 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 rst entry that the specied 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 specic 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 o when you have nished.

Draft
11.6 Shell

211

11.5.2

Your User Account

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
11.7.1

File Management
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 le. There are three groups to which protection may be separately assigned: uuser (yourself),

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
r w x X s

11.7 File Management Read. Write. Execute. Give execute permission if the le is a directory or if there is execute permission for one of the other user classes. For example: Set owner or group ID. This is only useful with u or g. Also, the set group ID bit of a directory may only be modied with + or -. Set the sticky bit to save program text between processes.

213

> chmod ugo=rw junk.tex will set the permission such that the user, group, and others can read from and write to the le junk.tex.

11.7.3

Copying Files

Usage: cp le1 le2 This command copies le1 to le2. If le2 already exists, it is replaced with le1. If le1 is in you current working directory but le2 is not, you must include the pathname for le2. The following example copies le1 to le2, which is in a dierent directory. > cp file1 ~username/directory/file2 If you have many les you wished to copy, and they all have similar names i.e. le1, le2, le3, le4, ..., le10, you can use whats called a wildcard to copy all the les at once. This example puts all the les named le1 through le10 into the subdirectory called directory. > cp file[1-10] ~username/directory If the les are not named in such a way that permitts you to use the wildcard option, you can list out the names of the les you wish to copy. Here the le sample and exmpl are put into the subdirectory temp. If a destination for the copy is omitted the les 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 les in your current directory. For example if you would like a listing of all les with the extension .f in your current directory you could type: > ls *.f

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
11.7.9

11.8 Printing Files

215

example either name lename or otherle can be used to call up the le, they are both valid names for the same le.

Removing Directories

Usage: rmdir directory Rmdir removes (deletes) directories. But rst the directory must be empty of all les. If you are certain you want to delete the directory rst delete all les 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 les that match the pattern typed and replaces the pattern with a list of those les 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 les beginning with a. You could type ls a* into the shell. All les of any length starting with a would be listed, including the le a if it existed. It would also match the le a.b since . is not a special character in Unix as it is in DOS, for instance. Or, if you wanted to list all les 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 les match before using rm.

11.8

Printing Files

For printing ASCII, or post-script les.

11.8.1

Enscript

Usage: enscript -2r -Gle When you are printing large source les 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 le and hit return the system prompt almost immediately returns. This does not mean that your le has been printed, all it means is that it has been spooled to a print que. If you would like to nd 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

11.9 Compressing and Archiving Files

217

Compressing and Archiving Files

Often users will be asked to limit their disk usage to ten megabytes, and will be notied when their usage exceeds this amount. If you nd yourself nearing this limit you can compress les that you are no longer using, or wont be using for a while. A text le can be compressed by up to 90%, executables, slightly less, this is a considerable space saving tool. Also, you can archive les directly to tape, freeing up diskspace, you can also use the archive tool to do backups of your les. 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 oppy drive of each machine.

11.9.1

Zipping Files

Usage: gzip lename The gzip command will generate a le named lename but will tack on the extension .gz. For example: > gzip filename the le lename.gz will be generated. > gzip file1.f the le le1.f.gz will be generated. To unzip les 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 le1.f to its normal size and strip o the .gz extension.

11.9.2

Compressing Files

Usage: compress lename Note that this utility is not as ecient as gzip but is supported on all Unix machines. The compress command will generate a le named lename but will tack on the extension .Z. For example: > compress filename the le lename.Z will be generated. > compress file1.f the le le1.f.Z will be generated. To uncompress les 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 le1.f to its normal size and strip o the .Z extension.

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
11.10
11.10.1
printcolor fn

11.10 Spooling Files

219

Spooling Files
Color Laser Printer

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

where fn is your lename

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 le, this is refered to as a lter. A lter 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 le size would could do this as follows: > ls -l | sort +4n The pipe consists of the use of both commands ls and sort. The lter 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 nshed. 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 o, and the background process will continue to run to completetion. Note: it is unethical the submitt batch jobs on a dierent 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 dontt 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

221

foreground, but when the console is idle, the machine will devote more time to the background process. 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. Processs set with 0 will run fast, processs set with a nice number of 19 will only run when nothing else wants to. If you do not use the nice command, a processs 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 processs 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 immediatley.

11.13
11.13.1

Utilities
du

Usage: du options lename The du command displays your disk usage . It gives the number of kilobytes contained in all les and, recursively, subdirectories within the specied directory. If the lename is omitted, the current directory is used. The -s option will display the grand total for each of the specied lenames. The -a option will generate an entry for each le 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: nd [pathname-list] [-options] expression Used to nd les by name, or by other characteristics. Find recursively descends the directory hierarchy for each pathname under the pathname-list, seeking les that match a given le name or other logical expression dened by any of the aviailable operators. For example, the following command will search all directories below the current directory for the les having the name misc.f. The le path will be printed each time the le is found. > find . -name misc.f -print ./Isds/Isds2/misc.f The nd command has several operators that can be used for dening search parameters. For instance, les can be searched for by, name of le, type of le, date accessed, date modied, les newer than arguement le, etc. For more information on the nd command, type man nd from a shell tool prompt.

11.13.3

Script

Usage: script lename The command script will create a typescript le of a loggin session. Everything that is printed on your
Victor E. Saouma Computing Literacy for Undergraduate Engineering Students

Draft
11.13.7

11.14 Shells and dotles

223

Whereis

Usage: whereis [-options] lename ... Whereis locates source, binary and manual page les 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 le is said to be unusual if it does not have one entry of each requested type. Thus whereis -m -u * asks for those les 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 dotles

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 eciently with the command lines, and some things absolutely require the use of the command line.

11.14.1

.cshrc & .login

When a user rst logs in, a shell called csh (C SHell) is started. This shell rst reads the contents of the le .cshrc (C SHell Run Commands) in the users home directory and performs the commands in that le. This le is very similar to the autoexec.bat le in MS-DOS. After that is done, the shell reads the contents of the le .login in the home directory and performs the commands in that le. After the shell is done with the commands in the .login le, 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 le and performs the commands in it. These other shells do not read the .login le, only the rst (login) shell does that. You can put commands you want every shell to perform in the .cshrc le. 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 le 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 le 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 dierent login names on dierent machines.

11.14.4

.signature

This le 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 le is appended to your post like a signature. Generally people will put their real name and e-mail address into the .signature le 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, dont make the .signature le too long. Generally, four lines or less is always acceptable. As you increase the size of the le, you increase the risk of people sending you nasty e-mail saying they dont like waiting for your .signature to be sent over their slow modem. A page long .signature, no matter how cute it may be, will denitely bring about such mail.

11.14.5

.mailrc

This le will contain various setups for your mail. One usefull application is the denition of aliases to shorten the length of e-mail addresses. For example, by having the following inside your .mailrc le: 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 nd 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 Software Series. Learning the UNIX Operating System by Grace Todino and John Strang. OReilly and Associates, Inc. And any of the numerous reference guides and users 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 rst 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.1

11.17 File Transfer Protocol (ftp)

227

Denitions

FTP: The Internet standard le 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 les. FTP Server: An Internet host which allows clients to connect and transfer les to or from the hosts. Anonymous FTP: Use of ftp to transfer les 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 les are les that you can display on your screen and/or pull into a text editor. For example, AUTOEXEC.BAT and CONFIG.SYS are text les in DOS. Files you would usually transfer in ASCII mode include: readme les, source code, uuencoded, binhex, PostScript and HTML les. 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 dierently on dierent types of computers. PC text is represented dierently than Mac text. Neither PC nor Mac text is represented the same as Unix text. Those les are usually ASCII les: Text (.txt, tex, .asc); Source code (.c, .f, .for); e-mail messages; uuencoded les; PostScript (.ps); Hypertext (.htm, .html). Binary File: Binary les are not text les. This includes many le types such as: executables, images, sound, movies, spreadsheets, databases, word processing, and compressed les. Files transferred in binary mode are copied exactly, bit-for-bit. Those les are usually binary: Spreadsheets (.xls, ...); Databases (.dbf, .dbt); Word Processing (.doc, wp); Compressed les (.Z, .zip); Images (.gif, .tif, .ti, .jpg, .jpeg, bmp, pcx); Sounds (.au); Movies (.mpg, .mpeg, .mov). Download: An FTP client receiving a le from an FTP server using the get command is downloading the le. Upload: An FTP client sending a le to an FTP server using the put command is uploading the le.

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

229

VI modes: VI has two modes of operation, INPUT mode, and EDIT mode. To get from INPUT mode 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 le changes applied to the actual le, 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 le, type: :q!. To save the current le to a new le name, type: :w filename. VI references: There are many sources of information on the VI editor. The rst place you should look are the users manuals located in the Lab. Most book stores that carry computer books will also have books specic 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 innitely 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 les. 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 buer, in emacs terms, is where editing takes place. It can contain a le, or hold new input from the user that is not yet attached to a le. Each buer can have a dierent le in it, and each is treated seperately. The name of the buer is displayed at the bottom of the window in what is called the status line. The name of the buer will generally be the same as the le in the buer, if any. The special buers *scratch* and *help* are exceptions as they do not have les associated with them. Also, each buer can also be in a dierent mode. The mode of a buer describes how emacs will act when editing in that buer. For instance, if a buer 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 doesnt try to treat the text in any special way. The scratch buer starts in Lisp Interaction mode. In this mode, emacs will try to interpret the text in the same way it does the .Emacs le when it rst starts up. Dont worry about how it works now. The mode the buer is in is also displayed in the status line. The status line gives some other information too. If the le in the buer has been modied, two *s will be displayed at the left of the status line. When you save the buer back into the le, the *s will be removed. Go ahead and experiment in the scratch buer. Dont worry about the text already there. If you have read it, its 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

11.19 Other Editors

231

The keys on the numeric keypad (Home, PgUp, arrows) should do what the keys say. Go ahead and try them on the scratch buer, although there is not enough text initially for PgUp and PgDn to do anything. Try typing several short lines to ll a couple of pages, then try these keys. You can get numbers by holding down the shift key.

Other Editors

Victor E. Saouma

Computing Literacy for Undergraduate Engineering Students

Draft
Bibliography

You might also like