Lecture 17 Practice problems on ifelse construct loop
Lecture 17 • Practice problems on if-else construct loop construct • Midterm-solution
Practice problems (if-else construct) In each of the following questions, evaluate the given MATLAB code fragments for each of the cases indicated. Use MATLAB to check the answers.
Write brief scripts to evaluate the following functions. If you start each script with a request for input (using input), you will be able to test your code provides the correct answer.
for loop construct Though MATLAB has a number of built-in functions that are equivalent to some of the following exercises, use a loop construct to carry out the indicated computations. 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. Create an M-by-N array of random numbers (use rand). Move through the array, element by element, and set any value that is less than 0. 2 to 0 and any value that is greater than (or equal to) 0. 2 to 1.
3. Given x = [4 1 6] and y = [6 2 7], compute the following arrays a. aij = xiyj b. bij = xi/yj c. ci = xiyi, then add up the elements of c. d. dij = xi/(2 + xi + yj) e. eij = reciprocal of the lesser of xi and yj. 4. Write a script that will use the random-number generator rand to determine the following: a. The number of random numbers it takes to add up to 20 (or more). b. The number of random numbers it takes before a number between 0. and 0. 85 occurs. c. The number of random numbers it takes before the mean of those numbers is within 0. 01 of 0. 5 (the mean of this random-number generator). It will be worthwhile to run your script several times because you are dealing with random numbers. Can you predict any of the results that are described above?
5. Write a script that asks for a temperature (in degrees Fahrenheit) and computes the equivalent temperature in degrees Celcius. The script should keep running until no number is provided to convert. [NB. the function isempty will be useful here. ]
Midterm solution >> % Question 1 >> A=[2 7 9 7; 3 1 5 6; 8 1 2 5] >> B 1=A(1: 2, : ) >> Column. Sum=sum(A) >> Row. Sum=sum(A') % The transpose of A is taken. A= 2 3 8 7 1 1 9 5 2 7 6 5 B 1 = 2 7 3 1 9 5 7 6 Column. Sum = 13 9 16 18 Row. Sum = 25 15 16
>> % Question 2 >> B 2=A(: , 2: 2: end) >> C 2=A(1: 2: end, : ) >> reciprocal. Of. A= 1. /A >> squareroot. Of. A=sqrt(A) B 2 = 7 7 1 6 1 5 C 2 = 2 8 7 1 9 2 7 5 reciprocal. Of. A = 0. 5000 0. 1429 0. 1111 0. 1429 0. 3333 1. 0000 0. 2000 0. 1667 0. 1250 1. 0000 0. 5000 0. 2000 squareroot. Of. A = 1. 4142 2. 6458 3. 0000 2. 6458 1. 7321 1. 0000 2. 2361 2. 4495 2. 8284 1. 0000 1. 4142 2. 2361
>> % Question 3 >>a 3=2: 2: 10 >>b 3=10: -2: -10 a 3 = 2 4 6 8 10 b 3 = 10 8 6 4 2 0 -2 -4 -6 -8 -10
>> % Question 4 >> t=rand(1, 5) >> f 4= cos(t). ^2 -sin(t). ^2 >> s 4=t. ^2+3*t+1 t= 0. 8687 0. 0844 0. 3998 0. 2599 0. 8001 f 4 = -0. 1658 0. 9858 0. 6970 0. 8679 -0. 0293 s 4 = 4. 3607 1. 2604 2. 3592 1. 8471 4. 0403
>> % Question 5 >> x 5=0. 01: 0. 0001: 0. 1; % 1000 points >> y 5=sin(1. /x 5); >> plot(x 5, y 5, 'r')
>> x 51=0. 01: 0. 001: 0. 1; % 100 points >> y 51=sin(1. /x 51); >> plot(x 51, y 51, 'r')
>> x 52=0. 01: 0. 1; % 10 points >> y 52=sin(1. /x 52); >> plot(x 52, y 52, 'r')
>> % Question 6 >> % Script >> x 6=input('Enter the elements of the vector '); >> [y 6, average]=threshold(x 6); >> %disp('x 6') >> x 6 >> %disp('average') >> average >> %disp('y 6') >> y 6 Enter the elements of the vector [2 3 -4 0 9 -4 -3] x 6 = 2 3 -4 0 9 -4 -3 average = 2. 5000 y 6 = 3 9
Threshold function >> function [vector. Returned, av]=threshold(x) >> m 1=max(x); >> m 2=min(x); >> av=(m 1+m 2)/2; >> vector. Returned=x(find(x>av)); Script function >> x 6=input('Enter the elements of the vector'); >> [y 6, average]=threshold(x 6); >> % disp('x 6') x 6 >> % disp('average') average >> % disp('y') y 6
- Slides: 17