ELEC2146 L2 SimProg V03 PDF
ELEC2146 L2 SimProg V03 PDF
Dr Julien Epps
S2, 2011
CheckTimetable(date,timetable);
DoINeedToGo(date,timetable); CheckSocialSchedule(date);
CheckGearNeeded(date,timetable);
WhatDoINeed(date,timetable); Raining(windowCam);
HotCold(temperature);
DrivingHassle(date,trafficCond);
GetThere(homeGPS,UNSWGPS);
NextBus(date,time);
NextTrain(date,time);
Procedural language
Complex problems can be decomposed into a hierarchy of
functions
Interpreted language
– Each line of code is decoded as the compiler
reaches it
Slower than compiled languages. MATLAB now has a compiler
Command window
Workspace browser Note that variables defined here
can be seen inside MATLAB
scripts
22
20
18
16
Voltage (V)
14
12
10
4
1 1.5 2 2.5 3 3.5 4 4.5 5
Current (A)
20
15
Note:
10
axes
5
have 0
1 1.5 2 2.5 3 3.5 4 4.5 5
different
scaling: 50
difficult to 40
interpret 30
20
10
1 1.5 2 2.5 3 3.5 4 4.5 5
40
Voltage (V1)
30
20
10
0
1 1.5 2 2.5 3 3.5 4 4.5 5
50
40
Voltage (V2)
30
20
10
0
1 1.5 2 2.5 3 3.5 4 4.5 5
Current (A)
Dr Julien Epps ELEC2146 Electrical Engineering Modelling and Simulation 23
Getting to know MATLAB
Plots
I = 5:-0.1:1; % now in decreasing steps of 0.1 A
R1 = 4.7;
V1 = I*R1;
R2 = 10;
V2 = I*R2;
plot(I,V1,I,V2); % plots on same axes
legend(‘R1’,’R2’);
45 R1
R2
40
35
30
Voltage (V)
25
20
15
10
0
1 1.5 2 2.5 3 3.5 4 4.5 5
Current (A)
Dr Julien Epps ELEC2146 Electrical Engineering Modelling and Simulation 25
Getting to know MATLAB
Plots
I = 5:-0.1:1; % now in decreasing steps of 0.1 A
R1 = 4.7;
V1 = I*R1;
R2 = 10;
V2 = I*R2;
plot(I,V1);
hold;
plot(I,V2); % plots on same axes (alternative)
x = [4; 7; 2; 1];
x(3) % 3rd element of x (=2)
x = [1 3; 4 6; 7 9];
x(2,1) % 2nd row, 1st column (=4)
x(2,:) % all of 2nd row (= [4 6])
x(:,1) % all of 1st column (= [1; 4; 7])
A = [1 3; 4 6; 7 9; 2 5];
x*A % matrix multiplication
% dimensions must agree
% (= [48 77])
x = malloc(sizeof(int));
// Allocate an int pointee,
// and set x to point to it
// can use “new” (C++) instead
source: http://cslibrary.stanford.edu/106/
Dr Julien Epps ELEC2146 Electrical Engineering Modelling and Simulation 34
C : A Refresher
Pointers
*x = 42; // Dereference x to store
42 in its pointee
source: http://cslibrary.stanford.edu/106/
Dr Julien Epps ELEC2146 Electrical Engineering Modelling and Simulation 35
C : A Refresher
Pointers
y = x; // Pointer assignment
sets y to point to x's pointee
source: http://cslibrary.stanford.edu/106/
Dr Julien Epps ELEC2146 Electrical Engineering Modelling and Simulation 36
C : A Refresher
Functions
– Don’t forget to
Declare prototype
Give types for all arguments and outputs
– e.g.
float dist(float x, float y, int d);
float a[5], b[5]; int k = 5;
void main {
dist(a,b,k);
}
float dist(float x, float y, int d) {
// code implementing ‘dist’ function
}
% Define filter
NumCoeff = [1 -2 1] % numerator coefficients
DenCoeff = 1; % denominator coefficients
Good:
for k = 1:N,
if dist(x(k,:),y) < min,
d(k) = dist(x(k,:),z);
end
end New function dist created by the programmer
see e.g.
http://www.cs.ubc.ca/~mdunham/tutorial/
Dr Julien Epps objectOriented.html
ELEC2146 Electrical Engineering Modelling and Simulation 52