You are on page 1of 16

veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

Introduction to MATLAB & function manipulations

1. Please refer to notes.

2.

1 clear; close all; clc;


2

3 % (a)
4 v_even = 16:2:26;
5

6 % (b)
7 v_odd = 15:2:27;
8

9 % (c)
10 v_even_r = 26:-2:16;
11 v_odd_r = 27:-2:15;

3.

1 clear all; close all; clc;


2

3 x = [2, 5, 1, 6];
4

5 % (a)
6 ind_odd = [1,3];
7 v = x(ind_odd) + 3;
8

9 % (b)
10 ind_even = [2,4];
11 x(ind_even) = v;

4.

1 clear all; close all; clc;


2

3 % (a)
4 alpha_rev = 'z':-1:'a';
5

6 % (b)
7 s1 = 'Hello!!';
8 s2 = 'Goodbye';

1
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

10 s(1:2:13) = s1;
11 s(2:2:14) = s2;

5.

1 clear all; close all; clc;


2

3 % (a)
4 n = 1:100;
5 sum_n = sum(n);
6

7 sum_n_loop = 0;
8 for i = 1:length(n)
9 sum_n_loop = sum_n_loop + n(i);
10 end
11

12

13 % (b)
14 n = 1:10;
15 sum_sq = sum(n.^2);
16

17 sum_sq_loop = 0;
18 for i = 1:length(n)
19 sum_sq_loop = sum_sq_loop + n(i)^2;
20 end
21

22 % (c)
23 sum_pow = 0.5.^(n);
24

25 sum_pow_loop = 0;
26 for i = 1:length(n)
27 sum_pow_loop = sum_pow_loop + 0.5^n(i);
28 end

6.

1 clear; close all; clc;


2

3 x = [1, 5, 2, 8, 9, 0, 1];
4 y = [5, 2, 2, 6, 0, 0, 2];
5

2
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

7 % (a)
8 x > y
9

10 % ans =
11 %
12 % 1x7 logical array
13 %
14 % 0 1 0 1 1 0 0
15 %
16 % - Mask array with true for elements in x greater than elements in y
17

18

19 % (b)
20 y > x
21

22 % ans =
23 %
24 % 1x7 logical array
25 %
26 % 1 0 0 0 0 0 1
27 %
28 % - Mask array with true for elements in y greater than elements in x
29

30

31 % (c)
32 x == y
33

34 % ans =
35 %
36 % 1x7 logical array
37 %
38 % 0 0 1 0 0 1 0
39 %
40 % - Mask array with true for elements in x and y equal to each other
41

42

43 % (d)
44 (x > y) | (y > x)
45

46 % ans =
47 %
48 % 1x7 logical array
49 %
50 % 1 1 0 1 1 0 1
51 %

3
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

52 % - Mask array with elements true for elements in x greater than elements
53 % of y OR elements of y greater than elements of x
54

55

56 % (e)
57 x(y < 5)
58

59 % ans =
60 %
61 % 5 2 9 0 1
62 %
63 % - Produces the elements of x corresponding to the elements in y that are
64 % less than 5

7.

1 clear; close all; clc;


2

3 x = 1:10;
4 y = [3, 1, 5, 6, 8, 2, 9, 4, 7, 0];
5

7 % (a)
8 (x > 3) & (y < 5)
9

10 % ans =
11 %
12 % 1x10 logical array
13 %
14 % 0 0 0 0 0 1 0 1 0 1
15 %
16 % - Mask array set to true for elements corresponding to elements of x
17 % greater than 3 and elements of y less than 5
18

19

20 % (b)
21 x(x > 5)
22

23 % ans =
24 %
25 % 6 7 8 9 10
26 %
27 % - Outputs elements of x greater than 5
28

4
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

29

30 % (c)
31 x(y > 5)
32

33 % ans =
34 %
35 % 4 5 7 9
36 %
37 % - Outputs elements of x corresponding to elements of y that are greater
38 % than 5
39

40

41 % (d)
42 y(x <= 4)
43

44 % ans =
45 %
46 % 3 1 5 6
47 %
48 % - Outputs elements of y corresponding to elements of x that are less
49 % than or equal to 4
50

51

52 % (e)
53 x( (x < 2) | (x >= 8) )
54

55 % ans =
56 %
57 % 1 8 9 10
58 %
59 % - Outputs elements of x that are less than 2 and greater than or equal to
60 % 8
61

62

63 % (f)
64 y( (x < 2) | (x >= 8) )
65

66 % ans =
67 %
68 % 3 4 7 0
69 %
70 % - Outputs elements of y corresponding to elements of x that are less
71 % than 2 or greater than or equal to 8

5
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

8.

1 clear; close all; clc;


2

3 x = [3, 15, 9, 12, -1, 0, -12, 9, 6, 1];


4

5 % (a)
6 xa = x; % Make a copy before manipulating
7 mask = x > 0;
8 xa(mask) = 0;
9

10 % (b)
11 xb = x;
12 % if -3 < x(i) < 3, the mod function will still give 0, so filter them
13 mask = (abs(x) >= 3) & (mod(x,3) == 0);
14 xb(mask) = 3;
15

16 % (c)
17 xc = x;
18 % Negative numbers aren't even
19 mask = (mod(x,2) == 0) & (x >= 0);
20 xc(mask) = 5*x(mask);
21

22 % (d)
23 mask = x > 10;
24 y = x(mask);
25

26 % (e)
27 xe = x;
28 x_mean = mean(x);
29 mask = x < x_mean;
30 xe(mask) = 0;

9.

1 clear all; close all; clc;


2

3 % (a)
4 x = linspace(0, 2*pi, 1024);
5 y1 = sin(x);
6 y2 = 0.5*cos(5*x);
7

8 figure(1)
9 plot(x,y1,x,y2,'--')

6
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

10 legend('sin(x)','0.5cos(5x)')
11

12 % (b)
13 t = linspace(0, 2*pi, 1024);
14 x = sin(3*t);
15 y = sin(4*t);
16

17 figure(2)
18 plot(x,y)
19 axis equal % Equal scales on both axes
20 title('Lissajou curve')
21

22

23 % (c)
24 r1 = 1;
25 x1 = linspace(-r1,r1,1024);
26 y1 = sqrt(r1*r1 - x1.*x1);
27

28 r2 = 0.5;
29 x2 = linspace(-r2,r2,100);
30 y2 = sqrt(r2*r2 - x2.*x2);
31

32 figure(3)
33 plot(x1,y1,'b',x2,y2,'r',x1,-y1,'b',x2,-y2,'r')
34 axis equal
35 legend('r = 1', 'r = 0.5')
36 title('Circles')
37 xlabel('x')
38 ylabel('y')
39

40

41 % (d)
42 x = linspace(0.1,10);
43 y = log(x);
44

45 figure(4)
46 plot(x,y)
47 ylim([0 3])
48 xlim([0 10])
49 xlabel('x')
50 ylabel('log(x)')

10.

7
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

1 clear; close all; clc;


2

3 %% Q10(a)
4

5 N = 100;
6

7 Sn = 0;
8 for i = 1:N
9 Sn = Sn + (-1)^(i+1)/(2*i - 1);
10 end
11 disp('Sn = ')
12 disp(Sn)
13

14

15 %% Q10(b)
16

17 Sinf = pi/4;
18

19 % Change N, check the output from the disp() after the loop
20 % After a few runs, it was found that the following N provides the required
21 % error
22

23 N = 100000;
24

25 % Sn = 0;
26 % for i = 1:N
27 % Sn = Sn + (-1)^(i+1)/(2*i - 1);
28 % end
29 disp('err = ')
30 disp(abs(Sn - Sinf))
31

32 % This can be vectorised as


33 % n = 1:N;
34 % Sn = sum((-1).^(n+1)./(2*n - 1));
35

36 %% Q10 (c)
37

38 N_arr = logspace(0, 8, 10);


39 Sn_arr = zeros(size(N_arr));
40

41 for j = 1:length(N_arr)
42 % We are looping through multiple values of N, which are stored in the
43 % vector, N_arr. The rest of the code inside the loop remains the same
44 % as Part (a) and (b)

8
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

45

46 N = N_arr(j);
47 Sn = 0;
48

49 for i = 1:N
50 Sn = Sn + (-1)^(i+1)/(2*i - 1);
51 end
52

53 Sn_arr(j) = Sn;
54 end
55

56 eps_n = abs(Sn_arr - Sinf);


57

58 figure(1)
59 loglog(N_arr, eps_n, 'o')

11.

1 clear; close all; clc;


2

3 %% Q11(a)
4

5 x = 73;
6

7 isPrime = true;
8

9 % loop from 2 to sqrt(73)


10 ind_vec = 2:sqrt(x);
11

12 for i = ind_vec
13 if (mod(x,i) == 0)
14 % divisible by a number, so not a prime
15 % set to false and break from the loop, no need to continue
16 isPrime = false;
17 break;
18 end
19 end
20

21 if (isPrime == true)
22 disp('73 is a prime number')
23 end
24

25

26 %% Q11(b)

9
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

27

28 % Try a large number (1000 in this case) and collect all the primes between
29 % 1 and the large number in the vector prime_vec
30

31 upper_bnd = 1000;
32

33 prime_cnt = 0;
34 for j = 2:upper_bnd
35 x = j;
36

37 isPrime = true;
38

39 ind_vec = 2:sqrt(x);
40 for i = ind_vec
41 if (mod(x,i) == 0)
42 % divisible by a number, so not a prime
43 % set to false and break from the loop, no need to continue
44 isPrime = false;
45

46 break;
47 end
48 end
49

50 if (isPrime == true)
51 % It is a prime, so collect it inside the vector
52

53 prime_cnt = prime_cnt + 1;
54 prime_vec(prime_cnt) = x;
55 end
56 end
57

58 % Extract the first 20 from the vector by indexing


59 prime20 = prime_vec(1:20);
60

61 %% Q11(c)
62

63 % Check the fIsPrime.m printed below


64

65

66 %% Q11(d)
67

68 % Change the value upper_bnd until 1000 primes is reached


69

70 upper_bnd = 10000;
71

10
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

72 prime_cnt = 0;
73 for j = 2:upper_bnd
74 x = j;
75

76 isPrime = fIsPrime(x);
77

78 if (isPrime == true)
79 prime_cnt = prime_cnt + 1;
80 prime_vec(prime_cnt) = x;
81 end
82 end
83

84 prime1000 = prime_vec(1000);

1 function isPrime = fIsPrime(x)


2

3 isPrime = true;
4

5 % loop from 2 to sqrt(73)


6 ind_vec = 2:sqrt(x);
7

8 for i = ind_vec
9 if (mod(x,i) == 0)
10 % divisible by a number, so not a prime
11 % set to false and break from the loop, no need to continue
12 isPrime = false;
13 break;
14 end
15 end

12.

1 clear; close all; clc;


2

3 %% Input
4

5 % convert speed to ms^-1


6 speed = [16.1, 32.2, 48.3, 64.4, 80.5, 96.6, 112.7]*1000./3600; % [ms^{-1}]
7 react_t = [0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75]; % [s]
8 tot_stop_t = [1.57, 2.45, 3.25, 4.33, 5.88, 7.57, 9.61]; % [s]
9

11
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

10

11 %% Q12(a)
12

13 % define braking time


14 brake_t = tot_stop_t - react_t;
15

16 acc = speed./brake_t;
17

18

19 %% Q12(b)
20

21 react_dist = speed.*react_t;
22 brake_dist = 0.5*acc.*brake_t.^2;
23

24 %% Q12(c)
25

26 tot_stop_dist = react_dist + brake_dist;


27

28 plot(speed, tot_stop_dist,'o-')
29 hold on
30 xlabel('Speed [ms^{-1}]')
31 ylabel('Stopping distance [m]')
32

33 %% Q12(d)
34

35

36 Malik_speed = 60*1000/3600;
37

38 plot([Malik_speed Malik_speed], [0 60], 'k--')


39

40 % Looks like Malik would drive 40.35m before stopping. Poor Ms Aida!!
41

42 plot([0 Malik_speed+10], [40.35 40.35], 'r--')

13.

1 clear; close all; clc;


2

3 %% Q13(a)
4

5 A = 100; % [m^2]
6

7 a = 5:0.1:20;
8

12
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

10 %% Q13(b)
11

12 b = A./a;
13 F = 2*a + 2*b;
14

15

16 %% Q13(c)
17

18 plot(a,F,'.-')
19 xlabel('a [m]')
20 ylabel('Total cost, F [$]')
21

22 % From the graph, the minimum cost, F = $ 40 and this happens when a = 10 m
23

24

25 %% Q13(d)
26

27 amin = 10; % determined graphically


28 bmin = A./10;
29

30 % Since b = 10m when a = 10m, this means it will be a square that will
31 % minimize the cost

Given that A = 100 m2 , the area equation can be written as

100
b= .
a

This can be used to formulate the cost function in terms of a only as

200
F = 2a + .
a

For the minimum cost, the derivative of this function must be equal to 0. The a for which this occurs can
then be calculated as follows

dF
=0
dx
200
2− 2 =0
a
⇒ a = 10 m

This in turn gives b = 10 m from the area equation above. Since a = b, this proves that the minimum cost
is associated with a square.

14.

13
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

1 clear; close all; clc;


2

3 %% Q14 (a)
4

5 x = linspace(-pi,pi,1024);
6

7 fg = cos(x).*sin(x);
8 figure(1)
9 plot(x, fg)
10

11 %% Q14(b)
12

13 fog = cos(x)./sin(x);
14 figure(2)
15 plot(x, fog)
16 ylim([-1 1])
17

18 %% Q14(c)
19

20 gof = sin(x)./cos(x);
21 figure(3)
22 plot(x, gof)
23 ylim([-1 1])
24

25 %% Q14(d)
26

27 g2 = cos(x).*cos(x);
28 figure(4)
29 plot(x, g2)
30

31

32 %% Q14(e)
33

34 g2 = sin(x).*sin(x);
35 figure(5)
36 plot(x, g2)
37

38 %% Q14(f)
39

40 fgx = cos(sin(x));
41 figure(6)
42 plot(x, fgx)
43

44 %% Q14(g)

14
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

45

46 gfx = sin(cos(x));
47 figure(7)
48 plot(x, gfx)

15.

1 clear; close all; clc;


2

3 %% Q15 (a)
4

5 x = linspace(-1,1,1024);
6

7 y0 = x.^3;
8 y = (x+1).^3 - 1;
9 figure(1)
10 plot(x, y, x,y0)
11 legend('Left 1, down 1', 'Original', 'Location', 'Best')
12

13

14 %% Q15 (b)
15

16 x = linspace(-1,1,1024);
17

18 y0 = 2*x-7;
19 y = (2*x-7) + 7;
20 figure(2)
21 plot(x, y, x,y0)
22 legend('Up 7', 'Original', 'Location', 'Best')
23

24

25 %% Q15 (c)
26

27 x = linspace(0.1,0.9,1024);
28

29 y0 = 1./x;
30 y = (1./(x-1)) + 1;
31 figure(3)
32 plot(x, y, x,y0)
33 legend('Up 1, right 1', 'Original', 'Location', 'Best')
34

35 %% Q15 (d)
36

37 x = linspace(-1,1,1024);

15
veb1062/ml Introduction to matlab Tutorial Sheet 1: solutions

38

39 y0 = x.^2 - 1;
40 y = 3*(x.^2 - 1);
41 figure(4)
42 plot(x, y, x,y0)
43 legend('Vertically stretched', 'Original', 'Location', 'Best')
44

45

46 %% Q15 (e)
47

48 x = linspace(0.9,2,1024);
49

50 y0 = 1 + 1./(x.^2);
51 y = 1 + 1./((x./3).^2);
52 figure(4)
53 plot(x, y, x,y0)
54 legend('Horizontally stretched', 'Original', 'Location', 'Best')

16.

1 clear; close all; clc;


2

3 x = linspace(-2*pi,2*pi,1024);
4

5 y1 = sin(x+pi/2);
6 y2 = cos(x);
7

8 plot(x,y1,x,y2,'.')
9 legend('sin(x+pi/2)','cos(x)','Location','Best')

16

You might also like