Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction of Matlab Excercises and Solutions, Exercises of Mathematics

Matlab Excercises with Solutions Notes Prepared By Markus Kuhn.

Typology: Exercises

2021/2022

Uploaded on 02/11/2022

shokha
shokha 🇮🇳

4.5

(13)

234 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to MATLAB
exercises and solution notes
Markus Kuhn
Michaelmas 2015 Part II
Exercise 1: Find a short MATLAB expression to build the matrix
B=
1 2 3 4 5 6 7
9 7 5 3 1 13
4 8 16 32 64 128 256
Answer: b = [1:7; 9:-2:-3; 2.^(2:8)]
Exercise 2: Give a MATLAB expression that uses only a single matrix multiplication
with Bto obtain
(a) the sum of columns 5 and 7 of B
Answer: b*[0000101]'
(b) the last row of B
Answer: [001]*b
(c) a version of Bwith rows 2 and 3 swapped
Answer: [100;001;010]*b
Exercise 3: Give a MATLAB expression that multiplies two vectors to obtain
(a) the matrix
12345
12345
12345
Answer: [1 1 1]' * (1:5)
(b) the matrix
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
Answer: (0:4)' * [1 1 1]
1
pf3
pf4

Partial preview of the text

Download Introduction of Matlab Excercises and Solutions and more Exercises Mathematics in PDF only on Docsity!

Introduction to MATLAB

– exercises and solution notes

Markus Kuhn

Michaelmas 2015 – Part II

Exercise 1: Find a short MATLAB expression to build the matrix

B =

Answer: b = [1:7; 9:-2:-3; 2.^(2:8)]

Exercise 2: Give a MATLAB expression that uses only a single matrix multiplication with B to obtain

(a) the sum of columns 5 and 7 of B

Answer: b * [0 0 0 0 1 0 1]'

(b) the last row of B

Answer: [0 0 1] * b

(c) a version of B with rows 2 and 3 swapped

Answer: [1 0 0; 0 0 1; 0 1 0] * b

Exercise 3: Give a MATLAB expression that multiplies two vectors to obtain

(a) the matrix

Answer: [1 1 1]' * (1:5)

(b) the matrix

Answer: (0:4)' * [1 1 1]

Exercise 4: Modify slide 30 to produce tones of falling frequency instead.

Answer: Replace

f = fmin * (fmax/fmin) .^ l;

with

f = fmax * (fmin/fmax) .^ l;

Exercise 5:

(a) Write down the function g(t) that has the shape of a sine wave that increases linearly in frequency from 0 Hz at t = 0 s to 5 Hz at t = 10 s.

Answer: The instantaneous frequency of function g(t) at time t is

f (t) = t · 5 Hz 10 s = t 2 s^2 and since the phase of a sine wave is 2π times the integrated frequency so far, we get

g(t) = sin

( 2 π

∫ (^) t

0

f (t′) dt′

) = sin

( 2 π t

2 4 s^2

) = sin

( πt^2 2 s^2

)

(b) Plot the graph of this function using MATLAB’s plot command. (c) Add to the same figure (this can be achieved using the hold command) in a different colour a graph of the same function sampled at 5 Hz, using the stem command.

Answer: for (b) and (c)

f = [659 622 659 622 659 494 587 523 440]; fs = 8000; % sampling frequency d = 0.5; % duration per tone t = 0:1/fs:d-1/fs; w = sin(2 * pi * f' * t)/2; w = w'; w = w(:)'; w = [w, w .* (mod((1:length(w)), 2) * 2 - 1)]; audiowrite('matlab_answer-2.wav', w, fs); specgram(w, [], fs);

Time

1 2 3 4 5 6 7 8

Frequency

0

500

1000

1500

2000

2500

3000

3500

4000