Lab Activity 3

Objectives

In this lab, you will:

References


Instructions




    

        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 3: Writing your own function: finding the maximum distance to the origin


We will use the six step procedure found in lecture 2-16 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 'max_dist'. Use the variables x, y and z. The inputs will be x and y, while z is the return value. Don't write the entire function.

  3) Develop Algorithm

    1. For two vectors   x = [ x x2  ] and y = [ y y2  ], then x+y in Matlab gives the result [ x1+ y1     ,   x2 + y2  ]. Write a Matlab expression that creates a vector  w =  [ x12+ y12     x22 + y22  ]. (Use only x and y in your expression, and don't use subscripting.)
      Note that the expression above should work for any number of points, as long as vectors x and y have the same length. Don't include this Matlab code inside your function.

    2. The built-in 'sqrt' function computes the square-root, for example if w = [ 4  16  ] then sqrt(w) would return the vector [  2  4]. Write a Matlab expression which creates the vector u  =   sqrt using only the variables x and y.  Don't include this Matlab code inside your function.

    3. The built-in 'max' function computes the maximum value, for example  if u = [2 4] then max(u) would return the value 4. Write a Matlab expression which
      calculates z = max(vector square root)  using only the variables x and y.   Include this statement in your function.

  4)Write the Function (Code)

  5) Test and Debug the Code

        or

      >> quiver(0*x,0*y,x,y)

        or

       >> compass(x,y)

Part 4 : Absolute and Relative Error

1) Problem Definition

           We will write our own function to compute  ex  using the Taylor series approximation

 

truncated e^x

 
         at various values of x where N = 100 using polynomial function polyval.  The correct Taylor series for ex  continues forever, that is, N = infinity.
         By using only a partial series we are introducing what is called truncation error.


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

 3) Develop Algorithm

              To do this, first create the vector  [ 1   2!   3!  ...     98!   99!   100!] using the vector 1:100 and  Matlab function cumprod . Use the "help

              cumprod" Matlab command if you need to find more information on this function. Yes there is a missing 1 but we will add that later!
   

                   21. Write the code to fill in the blank to assign the values [ 1   2!   3!  ...     98!   99!   100!]  to the variable named temp.


                     temp = ____________________________________________________________

           Next use the values of the variable temp from the previous question and the Matlab function fliplr create a vector with the values

             [ 100!    99!     98!     ...      3!     2!    1]

 
               22. Write a single Matlab expression that creates the vector above. You must use the variable temp in your answer.


               temp2 = ____________________________________________________________

              Now add the missing 1 by typing,

             temp2 = [temp2 , 1];

             Finally, create our polynomial vector p = [ 1/100!    1/99!     1/98!     ...      1/3!     1/2!    1   1]    by dividing 1 by the variable temp2.


                    p =  1./ temp2;


                    y   = ____________________________________________________________          

4)Write the Function (Code)

        Type the above code into your function named my_exp .   

5) Test and Debug the Code


     Matlab already has a function for computing ex  named exp . At the Matlab prompt type,

     >> format long e
    
    >> exp(1)
       ans
             = 2.718281828459046e+000


     How good is our approximation by using polynomial p?


     >> my_exp(1)

         ans =   
                       ???

     How do we measure how accurate our results are?

    At the Matlab prompt compute the absolute error by typing

       >> abserror = abs(exp(1) - my_exp(1))

      The absolute error is definded as the absolute value of the difference between the correct value and the approximate value or

      absolute error = | correct value - approximate value |

    Here we have used the Matlab abs function to compute the absolute value.
        
      Let's try one more test, at the Matlab prompt type,

     >> abserror = abs(exp(50) - my_exp(50))

     Your answer is no longer small so our polynomial approximation is not accurrate in an absolute sense, however if you type

     >> exp(50)
 
     >> my_exp(50)

    you note that both numbers are huge and in a relative sense they are close.
    The relative error is defined as the absolute value of the absolute error divided  by the correct value or

       relative error =     |  absolute error /  correct value |

    At the Matlab prompt type,

    >>  relerror =  abs(  (exp(50) - my_exp(50))/exp(50)  )


    1.   Is the relative error small ( less than 10-6)?  Circle the correct answer:         TRUE                  FALSE