Lab 1-Introduction To Matlab
Lab 1-Introduction To Matlab
(c) Generate the following vectors using function zeros and ones:
D = [0 0 0 …. 0] with fifty 0's.
E = [1 1 1 …. 1] with a hundred 1's.
1
2- Operate with the vectors
V1 = [1 2 3 4 5 6 7 8 9 0]
V2 = [0.3 1.2 0.5 2.1 0.1 0.4 3.6 4.2 1.7 0.9]
V3 = [4 4 4 4 3 3 2 2 2 1]
(a) Calculate respectively the sum of all elements in the vectors V1, V2, and V3.
(b) How to get the value of the fifth element of each vector?
What happens if we execute the command V1 (0) and V1 (11)?
Remember if a vector has N elements, their subscripts are from 1 to N.
(c) Generate a new vector V4 from V2, which is composed of the first five elements of
V2.
Generate a new vector V5 from V2, which is composed of the last five elements of V2.
(d) Derive a new vector V6 from V2, with its 6th element omitted.
Derive a new vector V7 from V2, with its 7th element changed to 1.4.
Derive a new vector V8 from V2, whose elements are the 1st, 3rd, 5th, 7th, and 9th
elements of V2
2
V1>6
V1>V3
V3-(V1>2)
(V1>2) & (V1<6)
(V1>2) | (V1<6)
any(V1)
all(V1)
4. Flow control
What are the results of these sets of commands? Think them over and run them with
Matlab to see if you are right.
(a) A = zeros (1,5);
for n = 1:4
for m = 1:3
A = A + n*m;
end
end
A
3
(b) B = [1 0];
if (all(B))
B = B + 1;
elseif (any(B))
B = B + 2;
else
B = B + 3;
end
B
(c) C = 7:2:22
num = 0;
while (all( C>0))
C = C - 3;
num = num + 1;
end
C
num
(d) Situations under which loops can be avoided. Does the set of commands:
for i = 1:20
H(i) = i * 5;
end
have the same result as:
H = 1:20;
H = H*5;
Does the set of commands:
for n = 1:100000
x(n) = sin(n*pi/10);
end
have the same result as:
4
n = 1:100000;
x = sin(n*pi/10);
(b) Write a function: Create a new m file following the procedure of above. Type in the
commands:
function g = myfunction (x, y)
g = x + y;
and then save it as myfunction.m
5
myfunction (error?)
z = myfunction(x,y)
g (error?)
a = 1:10;
b = 2:11;
myfunction(a,b)
Can you explain what is going on here?
Example 1.1: As you might expect a function can have multiple inputs and outputs:
>> x1 = 2; x2 = 3; x3 = 5;
>> [y1,y2] = multi(x1,x2,x3);
>> y1, y2
The input and output of a function do not have to be the same size (although in most
cases they will be).
Example 1.2: Consider a code which returns a scalar result from a vector input. For
example:
6
Our function sumsq takes a vector (or potentially a scalar) as an input and returns the
sum of the squares of the elements of the vector. The MATLAB intrinsic function sum
calculates the sum of its vector argument. For instance:
>> x = [1 2 4 5 6];
>> y = sumsq(x)
sets y equal to the scalar 12 + 22 + 42 + 52 + 62 = 82.
Example 1.4: y1 = 2 *cos(x), y2 = cos(x), and y3 =0.5 *cos(x), in the interval 0 ≤ x ≤ 2π.
>> x = 0:pi/100:2*pi;
7
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,'--',x,y2,'-',x,y3,':')
>> xlabel('0 \leq x \leq 2\pi')
>> ylabel('Cosine functions')
>> legend('2*cos(x)','cos(x)','0.5*cos(x)')
>> title('Typical example of multiple plots')
>> axis([0 2*pi -3 3])
“By default, MATLAB uses line style and color to distinguish the data sets plotted in the
graph. However, you can change the appearance of these graphic components or add
annotations to the graph to help explain your data for presentation.”[2]
Sources
(1) An Introduction to Programming and Numerical Methods in MATLAB, S.R.
Otto, J.P. Denier, (2005) Springer eBooks.
(2) INTRODUCTION TO MATLAB FOR ENGINEERING STUDENTS, David
Houcque, 2005, Northwestern University.
(3) EE 3054: Signals, Systems, and Transforms Lab Manual