MATLAB Tutorial Exercises Solns
MATLAB Tutorial Exercises Solns
• A 1 × 100 array consisting of all zero elements (Hint: Use the command zeros)
Answer: a=zeros(1,100);
1
• x(t) = 1
Answer: x=ones(size(t));
• x(t) = 2t + 3
Answer: x=2*t+3;
• x(t) = 3t2 − 5t + 1
Answer: x=3*t.ˆ2-5*t+1;
2t2 −4t+1
• x(t) = 3t3 −2t2 +5t+2
Answer: x=(2*t.ˆ2-4*t+1)./(3*t.ˆ3-2*t.ˆ2+5*t+2);
• x(t) = 2 cos(2π5t + 1)
Answer: x=2*cos(2*pi*5*t+1);
Answer: x=3*sin(2*pi*(4*t+3)./(2*t.ˆ2+1))-4;
q
2|t|+1
2 cos
4t2 +1
• x(t) = 3 sin3 (5t−2)+4
Answer: x=(2*cos(((2*abs(t)+1)./(4*t.ˆ2+1)).ˆ0.5))./(3*sin(5*t-2).ˆ3+4);
• x(t) = ej2π10t
Answer: x=exp(j*2*pi*10*t);
2
• x(t) = ejπ3t
Answer: x=exp(j*pi*3*t.ˆ2);
t2
• x(t) = e− 2
Answer: x=exp(-t.ˆ2/2);
• x(t) = e−|t|
Answer: x=exp(-abs(t));
2
3 Extracting Parts of a Matrix or an Array
Let x= [x1 x2 x3 x4 . . . x98 x99 x100 ]. Prepare the following arrays using single-line commands:
To test your codes, you may take x= [1 2 3 . . . 98 99 100].
• y = [0 0 x1 0 0 0 x2 0 0 0 x3 0 . . . 0 0 x99 0 0 0 x100 0]
Answer: y=zeros(1,400); y(3:4:400)=x;
3
g=x*y
4.2
Suppose we have an image x[m, n] that is stored in a matrix x of size 512 × 512. Let y[m, n] =
x2 [m, n]. Now, we want to compute the matrix y which is again 512 × 512 and which contains
y[m, n]. The following code tries to do it but it contains a mistake. What is the mistake?
y=x ˆ 2
4.3
The following code tries to sum 100 complex sinusoids over a time array given by t. The
frequencies are contained within an array named omega and the amplitudes are contained
within A. However, it contains a bug. Find it.
MySum=zeros(size(t));
for j=1:100
MySum=MySum+A(j)*exp(j*omega(j)*t);
end
Answer: Note that the letter j is defined on the second line as the counter parameter of
the
√ for loop. On the third line, it is also used to represent the unit imaginary √ number, i.e.,
−1. However, since j is defined on line 2, MATLAB does not recognize it as −1 any more,
so MySum turns out to be quite different than intended. √
One practice of avoiding such bugs is to reserve the letter j for −1 and use different letters
or names for the counters of loops. A better way to write the above code is:
MySum=zeros(size(t));
4
for i=1:100
MySum=MySum+A(i)*exp(j*omega(i)*t);
end
4.4
Suppose we have a 1 × 1000 array named x which consists only of zeroes and ones. Our purpose
is to search the array from the beginning and set an alarm when the number of ones reach 10.
The following code tries to do this but it contains a mistake. In particular, one line of code
appears in a place where it should not be. Find that line and put it in the correct position so
that the code works.
alarm=0;
index=0;
while(alarm==0)
count=0;
index=index+1;
if x(index)==1 count=count+1; end
if count==10 alarm=1; end
end
Answer: The given code sets count = 0 in each iteration of the for loop, so it cannot
calculate the number of ones. To correct the mistake, the line “count=0;” must be moved
anywhere before the for loop so that it increases by one when the value 1 is encountered in the
array x.
4.5
The following code tries to form a periodic signal x(t) by adding the Fourier series components
for −10 ≤ k ≤ 10. Suppose the coefficients are given within a 1 × 21 array whose name is
X. However, the code contains a small programming mistake so that MATLAB gives an error
message. Find that mistake. What is the error message that MATLAB gives?
x=zeros(size(t));
for k=-10:1:10
x=x+X(k)*exp(j*2*pi*k*t/T);
end
Answer: Suppose the program recently enters the for loop, so that k=-10 as indicated on
line 2. On line 3, MATLAB tries to fetch the −10th value of X. The problem is, index values
must be positive integers in MATLAB. In other words, X(1), X(2),..., X(21) are all defined
but X(-10) is not defined. Therefore, MATLAB returns the error:
Index exceeds matrix dimensions
The correct way to write the code is as follows:
x=zeros(size(t));
for k=-10:1:10
x=x+X(k+11)*exp(j*2*pi*k*t/T);
end