Engineering Computing M2 (KFMV) 2009/10: Programming (In Matlab)
Engineering Computing M2 (KFMV) 2009/10: Programming (In Matlab)
Basic principles
Printing
Introduction
» Intended Learning Outcomes
be familiar with the engineering computing environment,
» Assessment
» Lessons
including software important to engineering practice.
» Course outline
Basic principles
have a deeper understanding of framing engineering
A first program
problems in a computing context.
A simulation
Printing
have developed a logical top-down problem solving
approach to this process to use in the writing of
well-structured programming solutions.
Introduction
» Intended Learning Outcomes
be familiar with the engineering computing environment,
» Assessment
» Lessons
including software important to engineering practice.
» Course outline
Basic principles
have a deeper understanding of framing engineering
A first program
problems in a computing context.
A simulation
Printing
have developed a logical top-down problem solving
approach to this process to use in the writing of
well-structured programming solutions.
Introduction
» Intended Learning Outcomes
be familiar with the engineering computing environment,
» Assessment
» Lessons
including software important to engineering practice.
» Course outline
Basic principles
have a deeper understanding of framing engineering
A first program
problems in a computing context.
A simulation
Printing
have developed a logical top-down problem solving
approach to this process to use in the writing of
well-structured programming solutions.
Introduction
» Intended Learning Outcomes
be familiar with the engineering computing environment,
» Assessment
» Lessons
including software important to engineering practice.
» Course outline
Basic principles
have a deeper understanding of framing engineering
A first program
problems in a computing context.
A simulation
Printing
have developed a logical top-down problem solving
approach to this process to use in the writing of
well-structured programming solutions.
Introduction exam
» Intended Learning Outcomes
» Assessment
» Lessons
» Course outline
75%
Basic principles
1.5 hours
A first program
no choice of question
A simulation labs
Printing
25%
10 x 3 hours
complete all exercises
Moodle
Introduction
» Intended Learning Outcomes
lectures
» Assessment
» Lessons
labs
» Course outline
Moodle
Basic principles
A first program
Fridays 10am, St. Andrews 213
A simulation
Printing
Demonstration of general principles
concepts
pseudo-code examples
case studies
Introduction
» Intended Learning Outcomes
lectures
» Assessment
» Lessons
labs
» Course outline
Moodle
Basic principles
A first program
A simulation
Angus Jawad Gerlind
Printing
workbooks
one each, for self-teaching
worksheets
to be signed by demonstrator
retain as proof of completion
Introduction
» Intended Learning Outcomes
lectures
» Assessment
» Lessons
labs
» Course outline
Moodle
Basic principles
A first program
lecture notes
A simulation
Printing
revision material
workbooks
worksheets
forum & e-mail
Introduction
» Intended Learning Outcomes Topics Weeks
» Assessment
» Lessons
» Course outline
Basic principles
introduction 1
A first program basic principles 2–3
A simulation loops and iteration 4–5
Printing
conditional flow 6–7
functions 8–10
Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
» Digital Computer
» Programmable computers
» Bin and Hex Basic principles
» A simple program
» Executable
» Assembler language
» Higher level
» Languages
» Architecture
» Numbers
» Fixed point
» Floating point
» Floating point format
» f.p. representation
» IEEE double precision
A first program
A simulation
Printing
A first program
A simulation
Printing
A first program
A simulation
Printing
Printing
Introduction
Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
» Digital Computer
» Programmable computers
» Bin and Hex
» A simple program
» Executable
» Assembler language
» Higher level
» Languages
» Architecture
» Numbers
» Fixed point
» Floating point
» Floating point format
» f.p. representation
» IEEE double precision
A first program
A simulation
Printing
http://www.vaxman.de/my_machines/hitachi/240/240.html
A first program
A simulation
Printing
A first program
A simulation
Printing
http://en.wikipedia.org/wiki/File:Punch-card-blue.jpg
A first program
Instructions must be translated into machine code
A simulation
Printing
Introduction
Bin Hex Dec Bin Hex Dec
Basic principles
» Basic principles 0000 0 0 1000 8 8
» Analytical Engine
» Electronic Computer
» Digital Computer
0001 1 1 1001 9 9
» Programmable computers
» Bin and Hex
0010 2 2 1010 A 10
» A simple program
» Executable 0011 3 3 1011 B 11
» Assembler language
» Higher level 0100 4 4 1100 C 12
» Languages
» Architecture 0101 5 5 1101 D 13
» Numbers
» Fixed point
» Floating point
0110 6 6 1110 E 14
» Floating point format
» f.p. representation
0111 7 7 1111 F 15
» IEEE double precision
A first program
Hex numbers are often preceded with ’0x’, e.g 0x2A
A simulation
Printing
1 hex digit = 4 binary digits, e.g.: 0x2A = 0010
|{z} 1010
|{z}
x2 xA
A first program
Hitchhikers’ Guide to the Galaxy
A simulation
Printing
Introduction
.file " answer.c "
Basic principles
» Basic principles .section .rodata
» Analytical Engine
» Electronic Computer
.LC0 :
» Digital Computer . s t r i n g " 42\ n "
» Programmable computers
» Bin and Hex .text
» A simple program
» Executable
. g l o b l main
» Assembler language .type main , @function
» Higher level
» Languages main :
» Architecture
» Numbers
leal 4(%esp ) , %ecx
» Fixed point andl $ − 16, %esp
» Floating point
» Floating point format pushl −4(%ecx )
» f.p. representation
pushl %ebp
» IEEE double precision
movl %esp , %ebp
A first program
pushl %ecx
A simulation
subl $20 , %esp
Printing
movl s t d o u t , %eax
movl %eax , 12(%esp )
movl $3 , 8(%esp )
movl $1 , 4(%esp )
[email protected] Engineering Computing M2 (KFMV) 2009/10 - p. 16
movl $.LC0 , (%esp )
Higher level
Introduction C language
Basic principles
» Basic principles # include < s t d i o . h>
» Analytical Engine answer.c
» Electronic Computer
» Digital Computer
i n t main ( void ) {
» Programmable computers
» Bin and Hex f p r i n t f ( s t d o u t , " 42\ n " ) ;
» A simple program
» Executable r etu r n 0 ;
» Assembler language
}
» Higher level
» Languages
» Architecture
» Numbers
» Fixed point
» Floating point
» Floating point format
» f.p. representation
» IEEE double precision
A first program
A simulation
Printing
Introduction C language
Basic principles
» Basic principles # include < s t d i o . h>
» Analytical Engine answer.c
» Electronic Computer
» Digital Computer
i n t main ( void ) {
» Programmable computers
» Bin and Hex f p r i n t f ( s t d o u t , " 42\ n " ) ;
» A simple program
» Executable r etu r n 0 ;
» Assembler language
}
» Higher level
» Languages
» Architecture
» Numbers
» Fixed point
» Floating point
Matlab
» Floating point format
» f.p. representation f p r i n t f ( ’ 42\ n ’ ) ;
» IEEE double precision answer.m
A first program
A simulation
Printing
Introduction C language
Basic principles
» Basic principles # include < s t d i o . h>
» Analytical Engine answer.c
» Electronic Computer
» Digital Computer
i n t main ( void ) {
» Programmable computers
» Bin and Hex f p r i n t f ( s t d o u t , " 42\ n " ) ;
» A simple program
» Executable r etu r n 0 ;
» Assembler language
}
» Higher level
» Languages
» Architecture
» Numbers
» Fixed point
» Floating point
Matlab
» Floating point format
» f.p. representation f p r i n t f ( ’ 42\ n ’ ) ;
» IEEE double precision answer.m
A first program
A simulation
Printing
Matlab
disp ( 4 2 ) ;
(alternative)
[email protected] Engineering Computing M2 (KFMV) 2009/10 - p. 17
Languages
Introduction
Compiled: Interpreted (scripting):
Basic principles Ada Awk
» Basic principles
» Analytical Engine
» Electronic Computer
C / C++ HTML
» Digital Computer
» Programmable computers COBOL Lisp
» Bin and Hex
» A simple program Forth Matlab
» Executable
» Assembler language Fortran Perl
» Higher level
» Languages
» Architecture
Java PHP
» Numbers
» Fixed point Pascal Python
» Floating point
» Floating point format Visual basic Ruby
» f.p. representation
» IEEE double precision
A first program
Compiled languages require a compiler to convert the
A simulation
program to machine code (via assembly language) - faster
Printing
Interpreted languages run within an environment that calls a
limited set of pre-compiled functions - slower
A simulation
Machine code
Printing
A first program
A simulation
Printing
Introduction
The value of each digit depends on its position relative to the
Basic principles
» Basic principles
decimal point
» Analytical Engine
» Electronic Computer
» Digital Computer
| {z0101} in binary = 0x |{z}
e.g. 53 in base 10 (5310 ) = 0011 35
» Programmable computers
» Bin and Hex
8 bits 1 byte
» A simple program
» Executable
» Assembler language
» Higher level
Printing
A first program
A simulation
Printing
Introduction
Basic principles
±m × 10e
» Basic principles
» Analytical Engine
» Electronic Computer
» Digital Computer m Mantissa:
» Programmable computers
» Bin and Hex
» A simple program
» Executable
0 ≤ m < 10
» Assembler language
» Higher level
» Languages p digits, where p is the precision
» Architecture
» Numbers
» Fixed point
» Floating point
» Floating point format
e Exponent:
» f.p. representation
» IEEE double precision
an integer : emin < e < emax
A first program
A simulation
Printing
Introduction
To represent a real number x with a f.p. number, round x to the
Basic principles
» Basic principles
nearest real number (y ∈ R) with precision p
» Analytical Engine
» Electronic Computer
» Digital Computer if y ∈ F (set of floating point numbers) , then x is
» Programmable computers
» Bin and Hex representable
» A simple program
» Executable
» Assembler language
» Higher level
x is represented by y, if x = y
» Languages
» Architecture
» Numbers
» Fixed point
| x − y| is the round-off error
» Floating point
» Floating point format
» f.p. representation
» IEEE double precision
if y ∈
/ F, x is not representable
A first program
A simulation
overflow error, if x is too large
Printing
underflow error, if x is too small
Introduction
Matlab uses IEEE double precision
Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
» Digital Computer 53 bits in the mantissa
» Programmable computers
» Bin and Hex
» A simple program
» Executable 11 bits in the exponent
» Assembler language
» Higher level
» Languages
» Architecture 64 bits to represent a scalar number
» Numbers
» Fixed point
» Floating point
» Floating point format
» f.p. representation
10−308 < x < 10+308
» IEEE double precision
A first program
A simulation
Printing
Basic principles
A first program
» Making tea
A simulation
Printing
A first program
A first program
» Making tea
Use English as a language
A simulation
Think about
inputs
processes
outputs
Basic principles
A first program
A simulation
» A non-linear system
» Output
» Rates function
A simulation
» Euler integration
» Simulation
» Plotting
Printing
Introduction
Non-linear ordinary differential equations:
Basic principles
A first program
Printing
Euler integration:
χ(t + ∆t) = χ(t) + χ̇ × ∆t
χn+1 = χn + χ̇n × ∆t
Parameters:
x1 ( 0) x2 ( 0) u(t) a b c k
0 0 +1 +1 -1 -1 -0.1
Introduction
A non−linear system
2
Basic principles
x1
x2
A first program
A simulation
1.5
» A non-linear system
» Output
» Rates function
» Euler integration
» Simulation 1
» Plotting
states: x1, x2
Printing
0.5
−0.5
−1
0 2 4 6 8 10 12 14 16 18 20
time [s]
[email protected] Engineering Computing M2 (KFMV) 2009/10 - p. 30
Rates function
Introduction
The ODEs can be placed in a function file “rates.m”
Basic principles
function dx = r a t e s ( x , u , t )
A first program
% c a l c u l a t e s r a t e v e c t o r , dx
A simulation
» A non-linear system
% x i s t he s t a t e v e c t o r
» Output % u i s t he i n p u t v e c t o r
» Rates function
» Euler integration % t i s t he t i me ( s c a l a r )
» Simulation
» Plotting
% parameters
Printing
a = +1.0;
b = − 1.0;
c = − 1.0;
k = − 0.1;
% equations
dx1 = a∗x ( 1 ) ^ 2 + b∗x ( 1 ) ∗ x ( 2 ) + c∗x ( 2 ) ^ 2 ;
dx2 = u ( 1 ) − x ( 2 ) − x ( 1 ) ∗ exp ( k∗ t ) ;
Introduction
The Euler integration routine, in file “euler.m”
Basic principles
function x2 = e u l e r ( x1 , dx , d t )
A first program
% c a l c u l a t e s x ( t + d t ) usi ng f o r w a r d E u l e r i n t e g r a t i o n
A simulation
» A non-linear system
» Output % x1 i s t he s t a t e v e c t o r a t t i me t , x ( t )
» Rates function
» Euler integration % dx i s t he r a t e v e c t o r a t t i me t , dx ( t )
» Simulation
» Plotting
% d t i s t he s i m u l a t i o n t i me st ep ( not t oo b i g ! )
Printing
% x1 and dx must be v e c t o r s o f t he same s i z e !
x2 = x1 + dx ∗ d t ;
Introduction
% initialisation
Basic principles
x = [ 0 ; 0 ];
A first program
u = 1;
A simulation
» A non-linear system
» Output % s i m u l a t i o n parameters
» Rates function
» Euler integration dt = 0.01;
» Simulation
» Plotting
t i me = [ 0 : d t : 20.0 ] ;
Printing
% c r e a t e an a r r a y t o s t o r e t he r e s u l t s
data = [ ] ;
% main s i m u l a t i o n l oop
f o r t = t i me
dx = r a t e s ( x , u , t );
x = e u l e r ( x , dx , d t ) ;
data = [ data ; x ’ ];
end
Introduction
% p l o t t he r e s u l t s
Basic principles
x1 = data ( : , 1 ) ;
A first program
x2 = data ( : , 2 ) ;
A simulation
» A non-linear system
p l o t ( time , x1 , ...
» Output time , x2 ) ;
» Rates function
» Euler integration
» Simulation
» Plotting
% annot at e t he p l o t
x l a b e l ( ’ t i me [ s ] ’ ) ;
Printing
y l a b e l ( ’ s t a t e s : x1 , x2 ’ ) ;
legend ( { ’ x1 ’ , ’ x2 ’ } ) ;
g r i d on ;
t i t l e ( ’A non− l i n e a r system ’ ) ;
Basic principles
A first program
A simulation
Printing
» fprintf escape characters
Printing
» Investigating escape
characters
» Escape code effects
» fprintf numeric formats
» Investigating numeric formats
» Numeric formatting effects
Introduction
The fprintf function uses the following escape codes to affect
Basic principles
the whitespace that is printed. What do they all do?
A first program
A simulation
\n
Printing
» fprintf escape characters
» Investigating escape
\r
characters
» Escape code effects \t
» fprintf numeric formats
» Investigating numeric formats \b
» Numeric formatting effects
\f
Introduction
f o r token i n [ ’n ’ , ’ r ’ , ’ t ’ , ’b ’ , ’ f ’ ]
Basic principles
escape = [ ’ \ ’ , token ] ;
A first program
format = [ ’ p r i n t i n g token ’ , token , ...
A simulation
’ : H e l l o ’ , escape , ’ World \ n ’ ] ;
Printing f p r i n t f ( format ) ;
» fprintf escape characters
» Investigating escape end
characters
» Escape code effects
» fprintf numeric formats
» Investigating numeric formats
» Numeric formatting effects
Introduction
The fprintf function uses the following codes to affect the way
Basic principles
that numbers are printed. What do they all do?
A first program
A simulation
%c %d
Printing
» fprintf escape characters
» Investigating escape
%e %E
characters
» Escape code effects %f %g
» fprintf numeric formats
» Investigating numeric formats %G %i
» Numeric formatting effects
%o %u
%x %X
Introduction
f o r token i n [ ’ c ’ , ’ d ’ , ’ e ’ , ’E ’ , ’ f ’ , ’ g ’ , ’G ’ , . . .
Basic principles
’ i ’ , ’ o ’ , ’ s ’ , ’ u ’ , ’ x ’ , ’X ’ ]
A first program
escape = [ ’% ’ , token ] ;
A simulation
format = [ ’ 42 usi ng ’ , token , ’ is ’ , escape , ’ \n ’ ];
Printing f p r i n t f ( format , 4 2 ) ;
» fprintf escape characters
» Investigating escape end
characters
» Escape code effects
» fprintf numeric formats
» Investigating numeric formats
» Numeric formatting effects