0% found this document useful (0 votes)
109 views

Lab 1-Introduction To Matlab

This document provides an introduction to Matlab for students in a biomedical engineering course. It covers basic vector operations and functions, flow control and loops, writing scripts and functions, and plotting simple functions. The exercises demonstrate how to generate and manipulate vectors, operate on vectors using basic arithmetic and logical operators, use Matlab's help system and flow control structures like for loops and if/else statements, write scripts and functions, and create basic plots of functions.

Uploaded by

Batool Herzallah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Lab 1-Introduction To Matlab

This document provides an introduction to Matlab for students in a biomedical engineering course. It covers basic vector operations and functions, flow control and loops, writing scripts and functions, and plotting simple functions. The exercises demonstrate how to generate and manipulate vectors, operate on vectors using basic arithmetic and logical operators, use Matlab's help system and flow control structures like for loops and if/else statements, write scripts and functions, and create basic plots of functions.

Uploaded by

Batool Herzallah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

School of Applied Medical Science

Department of Biomedical Engineering


Medical Signal Processing Lab (BM323)

Experiment 1: Introduction to Matlab


1- Creating vectors

(a) Generate the following vectors:


A = [1 0 4 5 3 9 0 2]
a = [4 5 0 2 0 0 7 1]
Be aware that Matlab are case sensitive. Vector A and a have different values.

(b) Generate the following vectors:


B = [A a]
C = [a,A]
Concatenation is the process of joining small matrices to make bigger ones. In fact, you
made your first matrix by concatenating its individual elements. The pair of square
brackets, [], is the concatenation operator.

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

(d) Generate the following vectors using the colon operator


F = [1 2 3 4 ….. 30]
G = [25 22 19 16 13 10 7 4 1]
H = [0 0.2 0.4 0.6 …. 2.0]
The colon ‘: ‘ is one of Matlab's most important operators.

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

(e) What are the results of:


9-V1
V1*5
V1+V2
V1-V3
V1.*V2
V1*V2
V1.^2
V1.^V3
V1^V3
V1 == V3

2
V1>6
V1>V3
V3-(V1>2)
(V1>2) & (V1<6)
(V1>2) | (V1<6)
any(V1)
all(V1)

3. Using Matlab help system


Click on Help -> MATLAB help
Or type helpdesk to open the help _les. For description of a single function or command
type:
help command_name
on the command line, or use 'search' in the help window.
For example, type:
help plot
on the command line.

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

5. Compare a script and a function

(a) Write a script: In the main menu of Matlab, select


file -> new -> M-file
A new window will pop up. Input the following commands:
x = 1:5;
y = 6:10;
g = x+y;
and then save the file as myscript.m under the default path matlab/work.

(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

(c) Compare their usage


(1) run the commands one by one:
myscript
x
y
g
z = myscript (error?)
(2) Run command clear to remove all variables from memory
(3) Run the commands one by one:
x = 1:5;
y = 6:10;

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:

function [out1,out2] = multi(in1,in2,in3)


out1 = in1 + max(in2,in3);
out2 = (in1 + in2 + in3)/3;
which should be saved as multi.m. This gives us a function called multi that takes three
inputs in1, in2 and in3 and returns two outputs out1 and out2. We can call this function
in the following way:

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

function [output] = sumsq(x)


output = sum(x.ˆ2);

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.

6. Plotting Simple Functions


One of the most powerful elements of MATLAB is its excellent plotting facilities, which
allow us to easily and rapidly visualize the results of calculations. “MATLAB is capable
of producing very intricate and clear plots, as the following example illustrates.”[1]

Example 1.3: Consider the following example:


>> x = 0:pi/20:pi;
>> n = length(x);
>> r = 1:n/7:n;
>> y = x.ˆ2+3;
>> plot(x,y,’b’,x(r),y(r),’r*’)
>> axis([-pi/3 pi+pi/3 -1 15])
>> xlabel(’x values’)
>> ylabel(’Function values’)
>>title(’Demonstration plot’)
>> text(pi/10,0,’\alpha=\betaˆ2’)
Multiple (x; y) pairs arguments create multiple graphs with a single call to plot. For
example, these statements plot three related functions:

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

You might also like