Computing For Engineers PDF
Computing For Engineers PDF
Ronald Ssejjuuko
Department of computing & IT
week 1
Our Guiding Principles
• Parallel Computing
• Math, Statistics, and Optimization
• Control System Design and Analysis
• Signal Processing and Communications
• Image Processing and Computer Vision
• Test and Measurement, Data Acquisition
• Computational Finance, Datafeeds
• Computational Biology
• Code Generation and Application Deployment
• Database Connectivity
SIMULINK Applications
1. MATLAB desktop
2. MATLAB editor
3. Getting help
week 1
4. Variables, built-in constants, keywords
5. Numbers and formats
6. Arrays and matrices
7. Operators and expressions
8. Functions – built-in and user-defined
9. Basic plotting week 2
10. Function maxima and minima
11. Strings, cell arrays, fprintf
Help
to array editor
doubleclick
to edit M-file
workspace
window
current
folder
switch to
command window
3. Getting Help
Several ways of getting help:
>> y = [4, 5, 6]
y =
4 5 6
>> A = [1 2 3; 4 5 6]
A =
1 2 3
4 5 6
What are your variables? How to clear them?
Use workspace window, or the commands:
who, whos, clear, clc, close
>> who
Your variables are:
A y z
>> whos
Name Size Bytes Class Attributes
A 2x3 48 double
y 1x3 24 double
z 3x1 24 double
Note: i,j are commonly used for array and matrix indices. If you’re
dealing with complex-valued data, avoid redefining both i,j.
Values of special constants:
>> eps % equal to 2^(-52)
ans =
2.2204e-016 % MATLAB’s floating-point accuracy
% i.e., 2.2204 * 10^(-16)
>> intmax % 2^(31)-1 for 32-bit integers
ans =
2147483647
>> iskeyword
ans =
'break' 'function'
'case' 'global'
'catch' 'if'
'classdef' 'otherwise'
'continue' 'parfor'
'else' 'persistent'
'elseif' 'return'
'end' 'switch'
'for' 'try'
'while'
'true' , 'false'
5. Numbers and Formats
We discuss briefly:
a) row and column vectors
b) transposition operator, '
c) colon operator, :
d) equally-spaced elements, linspace
e) accessing array elements
f) dynamic allocation & de-allocation
g) pre-allocation
The key to efficient MATLAB programming
can be summarized in three words:
x = [2,-3,4,1,5,8]; x = [2,-3,4,1,5,8];
y = zeros(size(x)); y = x.^2;
for n = 1:length(x)
y(n) = x(n)^2;
element-wise exponentiation .^
end
ordinary exponentiation ^
answer: y = [4,9,16,1,25,64]
>> x = [0 1 2 3 4 5] % row vector
x =
0 1 2 3 4 5
x = linspace(a,b,N+1);
is equivalent to:
x = a : (b-a)/N : b;
step
increment
6 points, 5 subintervals
step increment
>> x = 0 : 0.3 : 1
x = x = a : s : b;
0 0.3 0.6 0.9
the number of subintervals
within [a,b] is obtained by
>> x = 0 : 0.4 : 1 rounding (b-a)/s, down
x = to the nearest integer,
0 0.4 0.8 N = floor((b-a)/s);
length(x) is equal to N+1
>> x = 0 : 0.7 : 1 x(n) = a + s*(n-1),
x = n = 1,2,...,N+1
0 0.7
rule of thumb: M = C + 1
accessing array entries:
>> x = [2, 5, -6, 10, 3, 4]
x =
2 5 -6 10 3 4
>> x(end+3) = 8
x =
2 5 -6 10 3 4 0 0 8
>> clear x
>> x(3) = -6
x =
0 0 -6
>> x(6) = 4
x =
0 0 -6 0 0 4
>> clear x
>> x = zeros(1,6) % 1x6 array of zeros
x =
0 0 0 0 0 0
clear x;
for k=[3,7,10] % k runs successively through
x(k) = 3 + 0.1*k; % the values of [3,7,10]
disp(x); % diplay current vector x
end
0.0 0.0 3.3 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 3.3 0.0 0.0 0.0 3.7 0.0 0.0 0.0
0.0 0.0 3.3 0.0 0.0 0.0 3.7 0.0 0.0 4.0
Example: Octave Frequency Scales
• Tasks:
• generate and print the major notes (do, re, mi, fa, sol, la, si, do) of
the middle (4th) octave, and play them forward & backward on the
PC’s soundcard (need earphones in the DSV lab)
1 2 3 4 5 6 7
A0 A1 A2 A3 C A4 A5 A6 A7
27.5 Hz 55 Hz 110 Hz 220 Hz 440 Hz 880 Hz 1760 Hz 3520 Hz
20 21 22 23 24 25 26 27
f
octaves = log2 ( ) f0
.
f = f0 2
octaves
You can also “publish” your programs directly to PDF or DOC formats.
key, k=0 key, k=87
1 2 3 4 5 6 7
A0 A1 A2 A3 C A4 A5 A6 A7
27.5 Hz 55 Hz 110 Hz 220 Hz 440 Hz 880 Hz 1760 Hz 3520 Hz
20 21 22 23 24 25 26 27
f
octaves = log2 ( ) f0 f = f0 . 2
octaves
5000
4000
3000
2000
1000
0
0 20 40 60 80 100
frequ encies on a pia no keyboa r d
3960 a ll 88 keys
3520 'A' k eys on ly
3080
2640
f (H z)
2200
1760
1320
880
440
0
0 12 24 36 48 60 72 84
k , pia no k ey
ka = 0:12:87; red color, open circles
fa = f(ka+1);
figure; plot(k,f, ka,fa,'ro'); plot fa vs. ka,
i.e., the ‘A’ keys
% ka = [0, 12, 24, 36, 48, 60, 72, 84]
% fa = [27.5, 55, 110, 220, 440, 880, 1760, 3520]
0 1 2 3 4 5 6 7 8 9 10 11 12
B C D E F G A B C D E F G A B C
A3 A4
middle C is 3/12 of an 220 Hz 440 Hz
octave above A3, or, middle C
9/12 octaves below A4 261.63 Hz
. 3/12 . -9/12
261.63 = 220 2 = 440 2
cell arrays
% formatted printing of frequencies and key names
% define cell arrays of key names to facilitate printing
keys = {'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',...
'A', 'A#', 'B', 'C'};
empty string