Professional Documents
Culture Documents
ECE673 - Week1 - Lecture - With Figures
ECE673 - Week1 - Lecture - With Figures
ECE673 – Week 2
Computer Simulation
Learning Objectives
– Create vectors and matrices in the widely-used computer program
called Matlab, and perform numerical operations on them
– Become familiar with basic Matlab functions
– Plot functions and data in Matlab
– Understand how to simulate random events in Matlab
– Estimate characteristics such as average values and histograms in
Matlab
Slide #HOME
Week 1 – Lessons
Lesson 1.1Introduction to Matlab Lesson 1.2
Computer Simulation of Random Phenomen
a
Slide #1.1
Lesson 1.1
Introduction to Matlab
Introduction to Matlab
• Overview of Matlab
– In MATLAB we can perform operations on scalars,
vectors and matrices.
– Examples of making scalars, vectors and matrices in
Matlab:
>> x=-2 >> x=[1;0;-3] >> x=[4,2,3;1,-1,0]
x= x= x=
-2 1 4 2 3
0 1 -1 0
-3
Slide #1.3
Overview of Matlab
y= z= ans =
-2 9 -13 25 3 8
7 2 -16 72 -1 0
Slide #1.4
Overview of Matlab
• Using colon “:” to specify intervening values
>> x=[3:6] >> a=[4:0.5:6]' >> b=[2:7;6:-2:-4]'
x= a= b=
3 4 5 6 4.0000 2 6
4.5000 3 4
>> y=[3:6]' 5.0000 4 2
5.5000 5 0
y= 6.0000 6 -2
7 -4
3
4
5
6
Slide #1.5
Overview of Matlab
• Element-by-element operation using the dot symbol
“.” before the operation
>> b=[-1:3] >> x=[3,-1;8,0] >> z=x./y
b= x= z=
-1 0 1 2 3 3 -1 -3.0000 -0.2500
8 0 4.0000 0
>> a=b.^2
>> y=[-1,4;2,5] >> t=x.*y
a=
y= t=
1 0 1 4 9
-1 4 -3 -4
2 5 16 0
Slide #1.6
Overview of Matlab
• Making a “for” loop:
– Using for loop to create a column vector where the i-th element is
equal to i 2
1
4
9
16
25
Slide #1.7
Overview of Matlab
• Use of the “if” statement:
– Randomly simulate the digits 3 and -3 using the if statement and store
in a variable called x :
a=rand;
if a>0.5
x=3;
else
x=-3;
end
– In Matlab “rand” generates randomly a number between 0 and 1.
– The above code simulates the random variable x such that x is either
3 or -3, with a probability of 0.5.
Slide #1.8
Overview of Matlab
Slide #1.9
Plotting in Matlab
Slide #1.11
Plotting in Matlab
• Example:
9
clear all 8
x=[-3:0.5:3]; 7
y=x.^2; 6
plot(x,y) 5
2
y=x
xlabel('x') 4
ylabel('y=x^2') 3
grid 2
0
-3 -2 -1 0 1 2 3
x
Slide #End of Lesson
End of Lesson
Click here to return to the main menu.
Slide #2.1
Lesson 1.1
– Average Value
Slide #2.5
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
-4 -3 -2 -1 0 1 2 3 4
Slide #2.7
• Average Value
1 6
average xi
6 i 1
Slide #2.9
End of Lesson
Click here to return to the main menu.
Reference