Lab Activity 3

Objectives

In this lab, you will:


References


Part 1: Standard MATLAB Functions

Section I. Random numbers

The MATLAB function 'rand' generates uniform random real numbers on the interval (0,1). The 'rand' function can generate a vector or matrix filled with random real numbers.
The command
>> rand(row,column)
creates a row x column array of reals on the interval (0,1).

  1. Complete the MATLAB command to create a row vector of 10,000 random integers with either the value 0 or 1 (like flipping a coin heads-1, tails-0). You must use the rand function.

    x = floor ( .5 + ___________________________________ );

  2. Write a single MATLAB command to compute the arithmetic average of 10,000 random integers generated by the 'rand' function in question #1 above. Use the MATLAB mean function and the vector x to compute the arithmetic average. Write the MATLAB command and not the answer (approx. .5) that your command returns. Use the MATLAB help facility (doc mean) if you have a question about using the mean function. If your mean isn't between .48 and .52 then probably your answer to #1 is probably incorrect.

    _________________________________________________


  3. Fill in the three blanks below to complete a MATLAB command to create a column vector of 500 random real numbers with a value between -2*pi and 2*pi.


    y = ______________ + _____________ * rand(_________________________________) ;

    (You can test your code by typing min(y) and max(y). These values should lie close to -2*pi and 2*pi respectively.)

        Section II. Polynomials

    Now you will learn how to represent polynomials in MATLAB. You will also use the roots and polyval functions that were mentioned in your course notes. See lecture 2 slides 32,33.

    MATLAB defines polynomials by a vector of the coefficients of the terms in descending order. For instance, f = 2x + 3 would be represented as f = [2, 3], where the first element of the vector (2) is the coefficient of the first degree term (2 x1) and the second element of the vector is the coefficient of the zero-th degree term (3 x0). Polynomials that do not have a term for each degree of x MUST have a 0 in the vector for that element (e.g., f = 3x3 + 2x is missing the coefficient of the second degree term and the zero-th term, so the vector representation would be f = [3, 0, 2, 0]).

    MATLAB provides several built-in functions that work with polynomials in this form. For example, to find the roots of a polynomial, you can simply call the roots function. (Remember, the roots of a polynomial are the values of x where the value of the polynomial is 0.)

  4. To find the roots of the polynomial 2x3 + 15x2 +6x - 1, we type the following into the command window,

    >> roots([2 15 6 -1])

    Write the result.

    ___________________________________________________________________

  5. Write the single MATLAB command to find all the roots of -x + x100 . (Don't write the roots, just write the MATLAB command that gives the roots.)

    >> p = __________________________________________________
    >> roots(p)

    Hint: x100 - x   = x100 + 0*x99 + 0*x98+ ... + 0*x3 + 0*x2 - 1*x + 0 . Create a vector, p [ 1  (followed by 98 0's)  -1  0]. Use linspace or zeros function to create the 98 0's.

  6. Use the polyval fucntion to compute the value of x3 + 2x2 + 3 at x=3 . Don't write the solution, just write the MATLAB command that gives you the solution.)


    ___________________________________________________________________

        Section III. Miscellaneous functions

  7. Fill in the blank with the result that MATLAB gives when you type the following commands.
    >> x = [ 5.9 5.1 -1 0 4.99 -3.99 ];
    >> floor(x)
    ans =

    _____________________________________________________

    >> ceil(x)
    ans =


    _____________________________________________________

    >> z = [1 3 5 7 9];
    >> cumsum(z) % note how this differs from the sum function
    ans =

    _____________________________________________________


    >> w = 1:2:9;
    >> cumprod(w) % note how this differs from the prod function
    ans =

    _____________________________________________________



Part 2: Scripts and Functions

Section A. Scripts are just lists of commands

1) Problem Definition

2) Refine , Generalize and Decompose the problem definition (identify sub-problems, I/O, ...)

3) Develop Algorithm

4) Write the function (the code)

5) Test and Debug the code

6) Run the Code


Part 4: Writing your own function: finding the minimum distance to a line Ax+By+C =0

We will use the six step procedure found in lecture 2-17 to write a computer program.

1) Problem Definition

2) Step 2 (Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.))

    1. Write the function definition line (i.e. the first line) for the function 'min_dist'. Use the variables x, y, A, B, C and z. The inputs will be x, y, A, B, C while z is the return value. Don't write the entire function, just the function definition.

      ____________________________________________________________________

3) Develop Algorithm

    1. For two vectors x and y write a single MATLAB expression that returns a vector of the distances from the points to the line Ax+By+C = 0.
      Hint: Pretend x and y are scalars (so we only have one point in the plane specified by (x,y)) and write a formula for the distance between this point and the line Ax+By+C = 0 using the math formula shown above. Does your formula work correctly when x and y are vectors of the same length? If not, then change the operators ( e.g. * to .* ).

      z = _____________________________________________________________________________________________________

      Note that the expression above should work for any number of points, as long as vectors x and y have the same length.

4) Write the Function (Code)

5) Test and Debug the Code

use the hold on command so we can add another plot in the same figure window

     >> hold on
    >> xvalues = linspace(min(x),max(x),20);
    >> yvalues = -(2/3)*xvalues +5/3;
    >> grid on
    >> plot(xvalues,yvalues) 
    >> axis([ min(xvalues)-1 max(xvalues)+1 min(yvalues)-1 max(yvalues)+1])

Part 5 : Multi-valued functions, if statement

1) Problem Definition

We will write our own function to find the maximum and minimum value of n variables. Our function find_max_min_and_make_even_odd will take a system generated random number n as input (a number between 1 and 10), generate n system generated numbers, and return the generated numbers, total number of elements generated, and the maximum and minimum values as maxNum and minNum respectively. To make it more interesting, if n has odd number of elements, we will return maxNum and minNum as odd numbers. If either maxNum or minNum is even, we will change it to the next odd number(s) and return those values instead. Similarly, if n has even number of elements, we will return maxNum and minNum as even numbers. If either maxNum or minNum is odd, we will change it to the next even number(s) and return those values instead. find_max_min_and_make_even_odd should work correctly for any integer values of n. For example, to call the function at the MATLAB prompt, we would type,

>> [numbers, num, maxNum, minNum] = find_max_min_and_make_even_odd(n)

2) Step 2 (Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.))

3) Develop Algorithm

19. Write the result MATLAB returns when you type the following:
>> t = [ 3, 7, 1, 9];
>> max(t)
>> min(t)

ans =
____________________________________________________________


How do we determine if a number is even or odd? We can use the MATLAB mod function which returns the modulus after division. Type help mod in the MATLAB command window to find out how it works.

    Write the result MATLAB returns when you type the following:
    >> m = 6;
    >> mod(m, 2)
    >> n = 7;

    >> mod(n,2)

    ans =
    ____________________________________________________________


    In English our algorithm can be written as follows:


    generate n number of random numbers between 1 and 10

    find how many elements n has and what those elements are
    find the max and min values of the generated numbers
    if number_of_elements generated is odd then make max and min odd
    if number_of_elements generated is even then make max and min even
    return generated_numbers, total number_of_elements, max, and min

    That's in pseudo-code, but in MATLAB you would write,


    numbers = ceil(10*rand(1,n));
    num = length(numbers);
    maxNum = max(numbers);
    minNum = min(numbers);

    if ((mod(num,2) == 1) && (mod(maxNum,2) == 0))
    maxNum = maxNum+1;
    elseif ((mod(num,2) == 0) && (mod(maxNum,2) == 1))
    maxNum = maxNum+1;

    end

    if ((mod(num,2) == 1) && (mod(minNum,2) == 0))
    minNum = minNum+1;
    elseif ((mod(num,2) == 0) && (mod(minNum,2) == 1))
    minNum = minNum+1;

    end

    end

    We are using the MATLAB if statement. Type help if in the MATLAB command window to find out how it works.

4)Write the Function (Code)

Type the above code into your function named find_max_min_and_make_even_odd and save your work. Remember MATLAB is case sensitive.

5) Test and Debug the Code

Test your code by typing the following in the MATLAB command window. This following example is showing the result when n = 5. For other values of n, the result will be different.


>> n = ceil(10*rand(1,1)); % Integer between 1 and 10 inclusive
>> [numbers, num, maxNum, minNum] = find_max_min_and_make_even_odd(n)

[numbers, num, maxNum, minNum] = find_max_min_and_make_even_odd(5)

numbers =

1 3 6 10 10

num =

5

maxNum =

11

minNum =

1


Now test your code by answering the following questions.