2
Matlab Sample Problems 1. Create a vector v = [1, 2, 3, …, 35] with 35 elements. Then, use the reshape and other built-in functions or operators to create a 5x7 matrix in which the first row is the numbers: 1 to 7, the second row is the numbers 8 to 14, the third row is the numbers 15 through 21, and so on. 2. How do you calculate in MATLAB? Note that . 3. Create a column vector that has the following elements: 25.5, , 6!, 2.7 4 , 0.0375, and /5. 4. Create the following matrix using vector notation for creating vector with constant spacing and/or linspace command. 5. Create the following matrix: . a) Create a 2x5 matrix C from the second and fourth rows, and the third through the seventh column of matrix B. b) Create a 4x3 matrix D from all rows and the third through fifth columns of matrix B. 6. Problem 40 from Ch2. 7. Given problems from Ch3, Ch4, Ch5, Ch6, and Ch7. 8. The following two MATLAB blocks of codes are to show that the sum of the infinite series converges to ln2 % element-by-element implementation: n = 0:1000; s = 1/(2*n+1)./(2*n+2); est_val = sum(s); act_val = log(2); e = abs(est_val-act_val)/act_val*100; % while loop implementation:

Mat Lab Sample Problems

Embed Size (px)

DESCRIPTION

asdf

Citation preview

Page 1: Mat Lab Sample Problems

Matlab Sample Problems

1. Create a vector v = [1, 2, 3, …, 35] with 35 elements. Then, use the reshape and other built-in functions or operators to create a 5x7 matrix in which the first row is the numbers: 1 to 7, the second row is the numbers 8 to 14, the third row is the numbers 15 through 21, and so on.

2. How do you calculate in MATLAB? Note that

.

3. Create a column vector that has the following elements: 25.5,

, 6!, 2.74,

0.0375, and /5.

4. Create the following matrix using vector notation for creating vector with constant spacing and/or linspace command.

5. Create the following matrix:

.

a) Create a 2x5 matrix C from the second and fourth rows, and the third through the seventh column of matrix B.

b) Create a 4x3 matrix D from all rows and the third through fifth columns of matrix B.

6. Problem 40 from Ch2.

7. Given problems from Ch3, Ch4, Ch5, Ch6, and Ch7.

8. The following two MATLAB blocks of codes are to show that the sum of the infinite

series

converges to ln2

% element-by-element implementation:

n = 0:1000;

s = 1/(2*n+1)./(2*n+2);

est_val = sum(s);

act_val = log(2);

e = abs(est_val-act_val)/act_val*100;

% while loop implementation:

Page 2: Mat Lab Sample Problems

s = 0; n = 0; eps = 1;

while eps <= 0.0001 eps = 1/(2*n+1)*(2*n+2); s = s + eps; n = n + 1; end est_val = s; act_val = log(2);

e = abs(est_val-act_val)/act_val*100;

a) Do these implementations work properly? If not, correct them.

b) How can you increase the accuracy of the estimated value (est_val) in the loop implementation?

c) Which one of these implementations is more efficient (i.e., runs faster)?

9. The function f(x) = ax3 + bx2 + cx + d passes through the points (1,2), (2,0), (3,-2), and (4,0). Write a system of four equations to determine the constants a, b, c, d. How can you solve this system of linear equations in MATLAB?

10. The following user-defined function function, minmax, calculates local minimum and

maximum of a given function (func) within a given interval ([a b]) through a semi-

exhaustive search. Assume that we pass function handle to minmax. Correct

minmax.

function [xmin xmax] = minmax(func, a, b)

x = linspace(a,b,1000); y = feval(‘func’, x); ymin = min(y); ymax = max(y); xmin = x(find(y == ymin)); xmax = x(find(y == ymax));