Sei sulla pagina 1di 50

Engineering Computing M2 (KFMV) 2009/10

Programming (in Matlab)

Dr. Geraint Bevan


g.bevan@eng.gla.ac.uk
room J654

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 1


Introduction
» Intended Learning Outcomes
» Assessment
» Lessons
» Course outline

Basic principles

A first program Introduction


A simulation

Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 2


Intended Learning Outcomes

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.

 be able to exploit the skills developed by studying examples


of familiar engineering problems in a computing context.

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 3


Intended Learning Outcomes

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.

 be able to exploit the skills developed by studying examples


of familiar engineering problems in a computing context.

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 3


Intended Learning Outcomes

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.

 be able to exploit the skills developed by studying examples


of familiar engineering problems in a computing context.

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 3


Intended Learning Outcomes

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.

 be able to exploit the skills developed by studying examples


of familiar engineering problems in a computing context.

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 3


Assessment

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

 complete 100% quizzes


 multiple attempts allowed
 time-limited availability
 enrolment key: mechMatlab

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 4


Lessons

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 5


Lessons

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 5


Lessons

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 5


Course outline

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 6


Introduction

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 7


Basic principles

Introduction  a computer program


   
Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
» Digital Computer input - program -output
   
» 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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 8


Basic principles

Introduction  a computer program


   
Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
» Digital Computer input - program - output
   
» Programmable computers
» Bin and Hex
» A simple program
» Executable
» Assembler language
» Higher level
» Languages
 we can use words or mathematics
» Architecture
» Numbers
» Fixed point
» Floating point
 “Add one to the input to get the output”
» Floating point format  output - input = 1
» f.p. representation
» IEEE double precision

A first program

A simulation

Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 8


Basic principles

Introduction  a computer program


   
Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
» Digital Computer input - program - output
   
» Programmable computers
» Bin and Hex
» A simple program
» Executable
» Assembler language
» Higher level
» Languages
 we can use words or mathematics
» Architecture
» Numbers
» Fixed point
» Floating point
 “Add one to the input to get the output”
» Floating point format  output - input = 1
» f.p. representation
» IEEE double precision

A first program  computers act on physical mechanisms


A simulation

Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 8


Charles Babbage’s Analytical Engine

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

Science Museum / Science and Society Picture Library

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 9


Hitachi 240 Analog Computer

Introduction  uses patch cables to solve differential equations


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

http://www.vaxman.de/my_machines/hitachi/240/240.html
A first program

A simulation

Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 10


Hollerith punch card

Introduction  mechanical digital computers can read punched cards


Basic principles
» Basic principles
 a binary representation of states and operations
» 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://en.wikipedia.org/wiki/File:Punch-card-blue.jpg

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 11


Programmable computers

Introduction  Digital electronic computers manipulate voltages


Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
» Digital Computer
» Programmable computers
» Bin and Hex
» A simple program
» Executable Transistor: University of Arizona ECE
» Assembler language
» Higher level
» Languages
» Architecture
» Numbers
 States and operations are represented in binary
» Fixed point  1 = on
» Floating point
» Floating point format  0 = off
» f.p. representation
» IEEE double precision

A first program
 Instructions must be translated into machine code
A simulation

Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 12


Binary and hexadecimal codes

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 13


A simple program

Introduction  "What do you get if you multiply six by nine?"


Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
 "Six by nine. Forty two."
» Digital Computer
» Programmable computers
» Bin and Hex
» A simple program
 "That’s it. That’s all there is."
» Executable
» Assembler language
» Higher level
» Languages
 "I always thought something was fundamentally wrong with
» Architecture
» Numbers
the universe"
» Fixed point
» Floating point
» Floating point format
» f.p. representation
» IEEE double precision

A first program
Hitchhikers’ Guide to the Galaxy
A simulation

Printing

613 × 913 = 4213

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 14


Executable: answer.exe

Introduction Address Instruction


Basic principles 0000000: 457f 464c 0101 0001 0000 0000 0000 0000
» Basic principles
0000010: 0002 0003 0001 0000 82f0 0804 0034 0000
» Analytical Engine
» Electronic Computer 0000020: 0778 0000 0000 0000 0034 0020 0007 0028
» Digital Computer 0000030: 001b 001a 0006 0000 0034 0000 8034 0804
» Programmable computers
» Bin and Hex 0000040: 8034 0804 00e0 0000 00e0 0000 0005 0000
» A simple program 0000050: 0004 0000 0003 0000 0114 0000 8114 0804
» Executable
» Assembler language 0000060: 8114 0804 0013 0000 0013 0000 0004 0000
» Higher level 0000070: 0001 0000 0001 0000 0000 0000 8000 0804
» Languages
» Architecture
0000080: 8000 0804 0478 0000 0478 0000 0005 0000
» Numbers 0000090: 1000 0000 0001 0000 0478 0000 9478 0804
» Fixed point
» Floating point
00000a0: 9478 0804 010c 0000 0110 0000 0006 0000
» Floating point format 00000b0: 1000 0000 0002 0000 048c 0000 948c 0804
» f.p. representation
» IEEE double precision
00000c0: 948c 0804 00d0 0000 00d0 0000 0006 0000
00000d0: 0004 0000 0004 0000 0128 0000 8128 0804
A first program
00000e0: 8128 0804 0020 0000 0020 0000 0004 0000
A simulation 00000f0: 0400 0000 51e5 7464 0000 0000 0000 0000
Printing 0000100: 0000 0000 0000 0000 0000 0000 0600 0000
...

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 15


Assembler: answer.s

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 )
g.bevan@eng.gla.ac.uk 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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 17


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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 17


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
 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)
g.bevan@eng.gla.ac.uk 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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 18


Architecture

Introduction  Higher level languages:


Basic principles Interpreted language
» Basic principles
» Analytical Engine  provide abstractions
6
» Electronic Computer
» Digital Computer
» Programmable computers
» Bin and Hex  are portable
» A simple program
» Executable
High level language
» Assembler language
» Higher level
 are easier to learn
» Languages 6
» Architecture
» Numbers
» Fixed point
 are easier to maintain
» Floating point Assembly code
» Floating point format
» f.p. representation
» IEEE double precision
6
A first program

A simulation
Machine code
Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 19


Numbers

Introduction  computers store numbers as binary


Basic principles
» Basic principles
» Analytical Engine
» Electronic Computer
 same for all languages
» Digital Computer
» Programmable computers
» Bin and Hex
» A simple program
 (usually) a fixed number of bits
» Executable
» Assembler language
» Higher level
» Languages
 π is only stored to fixed # d.p.
» Architecture
» Numbers
» Fixed point
» Floating point  most numbers cannot be represented precisely
» Floating point format
» f.p. representation
» IEEE double precision

A first program

A simulation

Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 20


Fixed point

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

5310 = 5 × 101 + 3 × 100


» Languages
» Architecture
» Numbers
» Fixed point
» Floating point
» Floating point format
» f.p. representation
» IEEE double precision
0011 01012 = 0 × 27 + 0 × 26 + 1 × 25 + 1 × 24
A first program + 0 × 23 + 1 × 22 + 0 × 21 + 1 × 20
A simulation

Printing

0x35 = 3516 = 3 × 161 + 5 × 160

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 21


Floating point

Introduction  represent non-integer numbers


Basic principles
» Basic principles
 can only approximate the set of real numbers R
» Analytical Engine
» Electronic Computer  consists of:
» Digital Computer
» Programmable computers  zero (0)
» Bin and Hex
» A simple program
 a limited set of positive numbers (+ ve )
» Executable
 a limited set of negative numbers (− ve )
» Assembler language
» Higher level
» Languages
» Architecture
» Numbers
» Fixed point
» Floating point What Every Engineer Should Know About Floating-Point Arithmetic
» Floating point format
» f.p. representation
by David Goldberg, Computing Surveys, ACM. March, 1991
» IEEE double precision

A first program

A simulation

Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 22


Floating point format

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 23


Floating point representation

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 24


IEEE double precision

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 25


Introduction

Basic principles

A first program
» Making tea

A simulation

Printing
A first program

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 26


Making tea

Introduction  Write a program to make a cup of tea


Basic principles

A first program
» Making tea
 Use English as a language
A simulation

Printing  Set out the procedure

 Think about

 inputs

 processes

 outputs

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 27


Introduction

Basic principles

A first program

A simulation
» A non-linear system
» Output
» Rates function
A simulation
» Euler integration
» Simulation
» Plotting

Printing

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 28


A non-linear system

Introduction
Non-linear ordinary differential equations:
Basic principles

A first program

A simulation ẋ1 = ax12 + bx1 x2 + cx22


» A non-linear system
» Output
» Rates function
ẋ2 = u − x1 ekt − x2
» Euler integration
» Simulation
» Plotting

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 29


Output

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]
g.bevan@eng.gla.ac.uk 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 ) ;

% return rates vector


dx = [ dx1 ; dx2 ] ;
g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 31
Euler integration

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 ;

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 32


Simulation

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 33


Plotting

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 ’ ) ;

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 34


Introduction

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 35


fprintf escape characters

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 36


Investigating escape characters

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 37


Escape code effects

Introduction >> escapecodes


Basic principles printing token n: Hello
A first program World
A simulation Worlding token r: Hello
Printing printing token t: Hello World
» fprintf escape characters
» Investigating escape printing token b: HellWorld
characters
» Escape code effects printing token f: HelloˆLWorld
» fprintf numeric formats
» Investigating numeric formats >>
» Numeric formatting effects

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 38


fprintf numeric formats

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 39


Investigating numeric formats

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

g.bevan@eng.gla.ac.uk Engineering Computing M2 (KFMV) 2009/10 - p. 40


Numeric formatting effects

Introduction >> numbercodes


Basic principles 42 using c is *
A first program 42 using d is 42
A simulation 42 using e is 4.200000e+01
Printing 42 using E is 4.200000E+01
» fprintf escape characters
» Investigating escape 42 using f is 42.000000
characters
» Escape code effects 42 using g is 42
» fprintf numeric formats
» Investigating numeric formats 42 using G is 42
» Numeric formatting effects
42 using i is 42
42 using o is 52
42 using s is Warning: The argument for the %s format
ype char (a string).
> In numbercodes at 5
*
42 using u is 42
42 using x is 2a
42 using X is 2A
g.bevan@eng.gla.ac.uk >> Engineering Computing M2 (KFMV) 2009/10 - p. 41

Potrebbero piacerti anche