You are on page 1of 2

Matlab exercises, 4th session

Programming
1. Solve the system of equations Ax = b when a) A = rand(100), b = rand(100,1) b) A = rand(100) + 5 * eye(100), b = rand(100,1) c) A = diag(rand(100,1)), b = rand(100,1) Use functions tic and toc to check how much time Matlab needs to solve the system. Use commands inv(A)*b and A\b. You will get same results but the calculations take different times. Why? Hint: In order to get reasonable results, put each command into a for loop and repeat the loop e.g. for 1000 times and measure the time it takes to execute the loop. 2. Write a function that can be used for creating random numbers as follows: a) if no parameters are given to the function, it returns a random number from the Normal distribution with zero mean and unit variance b) if one parameter is given to the function, the parameter is interpreted as the mean of the Normal distribution. The variance is still one. c) if two parameters are given to the function, the first one is interpreted as the mean and the second as the variance of the Normal distribution Hint: Use the switch-case structure. The number of parameters given to a function can be checked with nargin. Function randn returns numbers from the N(0,1) Normal distribution i.e. the Normal distribution with zero mean and unit variance. The command number = randn gives the variable number a value from the N(0,1) distribution. In item b) you can take the mean into account by adding the mean to the number returned by randn. The variance can be changed by multiplying the number returned by randn with the square root of the desired variance. For example, to get a number from the N(x,y) distribution in Matlab write number = randn*sqrt(y)+x. You can test your function (called distribution) with the following script.
n = 10000; numbers01 = zeros(n,1); numbers31 = zeros(n,1); numbers52 = zeros(n,1); for i = 1:n numbers01(i) = distribution; numbers31(i) = distribution(3); numbers52(i) = distribution(5,2); end disp(['mean(numbers01): ' num2str(mean(numbers01)) ', ... var(numbers01): ' num2str(var(numbers01))]); disp(['mean(numbers31): ' num2str(mean(numbers31)) ', ... var(numbers31): ' num2str(var(numbers31))]); disp(['mean(numbers52): ' num2str(mean(numbers52)) ', ... var(numbers52): ' num2str(var(numbers52))]);

3. Program a simple matrix calculator that can be used for addition, subtraction and multiplication of matrices. Hint: Use the switch-case structure for branching and the try-catch structure for checking the compatibility of the sizes of the matrices. Example run:
>> matcal Welcome to the Matrix Calculator! Choose: 1 addition, 2 subtraction, 3 multiplication, 0 exit 1 Enter the first matrix [1 2; 3 4] Enter the second matrix [2 6; 8 3] Result: 3 8 11 7 Choose: 1 addition, 2 subtraction, 3 multiplication, 0 exit 1 Enter the first matrix [1 2; 3 4] Enter the second matrix [1 2] The sizes of the matrices are not compatible Choose: 1 addition, 2 subtraction, 3 multiplication, 0 exit 0

4. Download the file session4_ex4.txt from the timetable page of the course and save it as an m-file to your working directory. What errors have been done from the point of view of efficiency? Fix the errors. Hint: The script needs the file processData.mat which can also be found from the home page of the course. Use Matlabs Profiler tool the measure the time that is spent. Aim: In the assistants computer (Intel Core 2 Duo 1.66 GHz, 1GB) execution of the original file takes 12.3 s. With small modifications this is reduced to 4.7 s (30 % of the original time).

You might also like