lab 1

5
Basic Syntax and Command-Line Exercises Answer the following exercises are by a single MATLAB command or in script file. 1. Create a vector of the even whole numbers between 31 and 75. 2. Let x = [2 5 1 6]. a. Add 16 to each element b. Add 3 to just the odd-index elements c. Compute the square root of each element d. Compute the square of each element 3. Let x = [3 2 6 8]' and y = [4 1 3 5]' (NB. x and y should be column vectors). a. Add the sum of the elements in x to y b. Raise each element of x to the power specified by the corresponding element in y. c. Divide each element of y by the corresponding element in x c. Multiply each element in x by the corresponding element in y, calling the result "z". d. Add up the elements in z e. Compute x'*y - z and interpret the result 4. Evaluate the following MATLAB expressions by hand and use MATLAB to check the answers a. 2 / 2 * 3 b. 6 - 2 / 5 + 7 ^ 2 - 1 c. 10 / 2 \ 5 - 3 + 2 * 4 d. 3 ^ 2 / 4 e. 3 ^ 2 ^ 2 f. 2 + round(6 / 9 + 3 * 2) / 2 - 3 g. 2 + floor(6 / 9 + 3 * 2) / 2 - 3 h. 2 + ceil(6 / 9 + 3 * 2) / 2 - 3 5. Create a vector x with the elements ... up to N element. N can be any positive integer. a. 2, 4, 6, 8, ... b. 10, 8, 6, 4, 2, 0, -2, -4 c. 1, 1/2, 1/3, 1/4, 1/5, ... d. 0, 1/2, 2/3, 3/4, 4/5, ...

Upload: deepak-mishra

Post on 24-Nov-2014

65 views

Category:

Documents


1 download

DESCRIPTION

Basic problems for learning Matlab

TRANSCRIPT

Page 1: Lab 1

Basic Syntax and Command-Line Exercises

Answer the following exercises are by a single MATLAB command or in script file.

1. Create a vector of the even whole numbers between 31 and 75.

2. Let x = [2 5 1 6].

a. Add 16 to each element b. Add 3 to just the odd-index elements c. Compute the square root of each element d. Compute the square of each element 3. Let x = [3 2 6 8]' and y = [4 1 3 5]' (NB. x and y should be column vectors).

a. Add the sum of the elements in x to y b. Raise each element of x to the power specified by the corresponding element in y. c. Divide each element of y by the corresponding element in x c. Multiply each element in x by the corresponding element in y, calling the result "z". d. Add up the elements in z e. Compute x'*y - z and interpret the result

4. Evaluate the following MATLAB expressions by hand and use MATLAB to check the answers a. 2 / 2 * 3 b. 6 - 2 / 5 + 7 ^ 2 - 1 c. 10 / 2 \ 5 - 3 + 2 * 4 d. 3 ^ 2 / 4 e. 3 ^ 2 ^ 2 f. 2 + round(6 / 9 + 3 * 2) / 2 - 3 g. 2 + floor(6 / 9 + 3 * 2) / 2 - 3 h. 2 + ceil(6 / 9 + 3 * 2) / 2 - 3 5. Create a vector x with the elements ... up to N element. N can be any positive integer.

a. 2, 4, 6, 8, ... b. 10, 8, 6, 4, 2, 0, -2, -4 c. 1, 1/2, 1/3, 1/4, 1/5, ... d. 0, 1/2, 2/3, 3/4, 4/5, ...

6. Given a vector, t, of length n, write down the MATLAB expressions that will correctly compute the following:

a. ln(2 + t + t2) b. et(1 + cos(3t)) c. cos2(t) + sin2(t) d. tan-1(1) (this is the inverse tangent function) e. cot(t) f. sec2(t) + cot(t) - 1

Page 2: Lab 1

Test and plot your solution works for t = 1:0.2:2

7. Plot the functions x, x3, ex and ex2 over the interval 0 < x < 4 ...

a. on rectangular paper b. on semilog paper (logarithm on the y-axis) c. on log-log paper Be sure to use an appropriate mesh of x values to get a smooth set of curves. 8. Make a good plot (i.e., a non-choppy plot) of the function

f(x) = sin(1/x) for 0.01 < x < 0.1. How did you create x so that the plot looked good?

Basic Array Syntax and Manipulations

Answer the following exercises are by a single MATLAB command or in script file. Exercise will cover some basics of Array and Matrix manipulation.

1. Given x = [3 1 5 7 9 2 6], explain what the following commands "mean" by summarizing the net result of the command.

a. x(3) b. x(1:7) c. x(1:end) d. x(1:end-1) e. x(6:-2:1) f. x([1 6 2 1 1]) g. sum(x)

2. Given the arrays x = [1 4 8], y = [2 1 5] and A = [3 1 6 ; 5 2 7], determine which of the following statements will correctly execute and provide the result. If the command will not correctly execute, state why it will not. Using the command whos may be helpful here. a. x + y b. x + A c. x' + y d. A - [x' y'] e. [x ; y'] f. [x ; y] g. A - 3

3. Given the array A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5], explain the results of the following commands: a. A' b. A(:,[1 4]) c. A([2 3],[3 1])

Page 3: Lab 1

d. reshape(A,2,6) e. A(:) f. flipud(A) g. fliplr(A) h. [A A(end,:)] i. A(1:3,:) j. [A ; A(1:2,:)] k. sum(A) l. sum(A') m. sum(A,2) k. [ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ]

Exercises on Relational and Logical Operations

Answer the following exercises are by a single MATLAB command or in script file. This will cover basic Relational and Logical operations in MATLAB.

1. Given that x = [1 5 2 8 9 0 1] and y = [5 2 2 6 0 0 2], execute and explain the results of the following commands: a. x > y b. y < x c. x == y d. x <= y e. y >= x f. x | y g. x & y h. x & (~y) i. (x > y) | (y < x) j. (x > y) & (y < x)

Control of Flow: if-blocks

Answer the following exercises are by a single MATLAB command or in script file. This will cover basic Control of Flow: if-blocks in MATLAB.

1. Compute m in three cases.

if n > 1 a. n = 7 m = ? m = n+1 b. n = 0 m = ? else c. n = -10 m = ? m = n - 1 end

2. Compute w in three cases.

if z < 5 a. z = 1 w = ? w = 2*z b. z = 9 w = ? elseif z < 10 c. z = 60 w = ? w = 9 - z d. z = 200 w = ? elseif z < 100 w = sqrt(z) else

Page 4: Lab 1

w = z end

3. Compute h in three cases.

if T < 30 a. T = 50 h = ? h = 2*T + 1 b. T = 15 h = ? elseif T < 10 c. T = 0 h = ? h = T - 2 else h = 0 end

4. Compute y in three cases.

if 0 < x < 10 a. x = -1 y = ? y = 4*x b. x = 5 y = ? elseif 10 < x < 40 c. x = 30 y = ? y = 10*x d. x = 100 y = ? else y = 500 end

Loop Constructs

Answer the following exercises are by a single MATLAB command or in script file. This will cover basic Control of Flow: if-blocks in MATLAB.

1. Given the vector x = [1 8 3 9 0 1], create a short set of commands that will

a. Add up the values of the elements (Check with sum.) b. Computes the running sum (for element j, the running sum is the sum of the elements from 1 to j, inclusive. Check with cumsum.) c. computes the sine of the given x-values (should be a vector) 2. Given matrices of size (3x4) i.e. x = [1 2 3 4; 2 3 4 7; 1 2 3 5] and y = [5 2 6 4; 6 3 34 17; 1 52 3 6]. Use FOR loop to compute the summation and subtraction for the given matrices. Check your answer with direct summation command i.e. sum_ans = x+y sub_ans = x-y.