Lab Activity 4

Objectives

In this lab, you will:

References


Instructions


Part 1: Programming the find_xy function.

Instructions:

In this lab we will be writing MATLAB code to implement the central part of the solution to the suspension bridge problem from MP1. Take time now to read through the first three sections of Part 1 (Section 3. Math shows the equation A*h =  W) of MP1. The first goal of this lab is to get MATLAB to solve a system of equations represented by a matrix equation similar to the one in MP1. To solve the system, we need to solve the matrix equation:
A*h = W

where h is an unknown N x 1 column vector, A is an N x N matrix and W is an N x 1 column vector.

Your code should perform the calculations necessary to solve the matrix equation above to obtain the value of the vector h, which represents the values of the heights of the joint points. The data characterizing the system are provided in the file mp1_data.m. You can download this file here. (Right click and save it to your home directory.)

The file contains the following variables that describe the known quantities of the system.

The equations that relates these terms are:

      Ki+1*(hi+1 - hi)+ Ki*(hi-1 - hi) = Wi for i=1 .. N
 

These equations define a tri-diagonal system of linear equations for the heights h1 to hN corresponding to the weights W1 to WN.

For this lab, you will write a function containing MATLAB code called 'find_xy.m' that you can execute from within MATLAB by typing 'find_xy(HF)' at the MATLAB prompt. Of course you must assign a value for HF before you can use 'find_xy(HF)'.

  1. Open Matlab and edit a new file called 'find_xy.m' . Add the following lines to your find_xy.m file:

  2.  

    function[x,y] = find_xy(HF)
    function[x,y] = find_xy(HF)
    % Begin the file by writing a few lines of
    % comments (lines that start with '%').
    % Your comments should include your names, the date,
    % and a brief  description of the program you are about to write.

    mp1_data     % mp1_data.m is a script file.
    % You can run a script file from inside a function.
    % Running a script from inside the function
    % find_xy make the values of the
    % variables in mp1_data available to the function find_xy.

    % Begin typing your  code after this comment.


     

    For the next two steps you will go over the math behind the problem for MP1. Don't do any coding for your Matlab function until step 3.

  3. Using the equation given above (the one relating h and K variables), write out five equations for i = 1.. 5 (N = 5) on the answer sheet for question #1.

    #1. Write down the five equations.


    We will write the code that works for any value of N but we want to understand how we generate the matrix equation.
    HINTS: h0 = HL (the left tower), h6 = HR (the right tower). These aren't part of the vector h that you're solving for in  A*h = W; vector h has N joint points where the weights are attached. HL and HR are given to you as data characterizing the system, so they can be treated as constants. All constants should be moved to the right side of the equation.

  4.  
     
  5. Using the equations from the previous step , answer question #2 on the answer sheet by writing out the matrix equation A*h = W (don't use Matlab notation) on the answer sheet. This will make it a lot easier to understand the rest of the lab.  See MP1, at the end of Part 2 where it shows how you should write the matrix equation A*h= W . The example shown in MP1 is for any general N.
    To answer question #2 use N = 5.

           #2. Write down the equation that you are going to solve, in the matrix form [matrix]*[column vector] = [column vector]. (Hint: see the matrix        
                 equation in the MP 1 write-up immediately above equation (7) in the 3. Math section.)


    From this point on, you will be writing MATLAB code to work with vectors and matrices. Remember that all the code you write should go into your find_xy.m file. Although the mp1_data.m datafile sets N=9 and we wrote the equations for N=5 above, the Matlab code you now write should work for any N.
    a) Now you should start putting together the pieces of the matrices in the equation above. We'll start with the A matrix on the left-hand side of the equation mentioned above. We will do this by creating three matrices, A_main, A_upper and A_lower so that
                    A = A_main + A_upper + A_lower ;


      Don't type the above in your Matlab function yet as we need to first create the three matrices  A_main, A_upper and A_lower.
      A_main is a diagonal matrix whose only non-zero values lie on the main diagonal and are   -(Ki + Ki+1)  i = 1,...N  .
      A_main
    Answer questions #3 and #4 on the answer sheet.

    #3. Fill in the blank to create a row vector K from d and HF. See the MP 1 instructions, equation (4) in section 3. Math.

         K = _______________________________

    #4. Fill in the blanks to create the matrix A_main. Assume that the variable N has already been created.
         A_main = diag( - (K(______________) + K(_____________) ) , ________ ) ;

    A_upper is the upper diagonal matrix whose only non-zero values lie above the main diagonal and are  Ki  i = 2,...,N  .
      A_upper
    Answer question #5 on the answer sheet.
     #5. Fill in the blanks to create the matrix A_upper. Assume that the variable N has already been created.
           A_upper = diag( K( ____________ ) , _________ );

    A_lower is the lower diagonal matrix whose only non-zero values lie below the main diagonal and are  Ki  i = 2,...N  .
      A_lower
    Answer question #6 on the answer sheet.

    #6. Fill in the blanks to create the matrix A_lower. Assume that the variable N has already been created.
          A_lower = diag( K(_____________) , ________ ) ;
          Now add the line of Matlab code,

            A = A_main + A_upper + A_lower;

    in you find_xy.m file.

    b) We need to modify the right-hand vector W.  This vector is created using the weight vector W, the HF value and the values HL and HR, all of which are given to you.

    The first element is W(1). The last element is W(N).

    Answer question #7.
    #7. Fill in the blanks to modify the first and the last elements of the vector W:

                W(1) = W( ______ ) - ______________ ;

                W(N) = W( ______ ) - ______________ ;


    c) Since we know the left-hand side matrix A and the right-hand side vector W, we can solve the system of equations A*h = W.
    Store the solution in a column vector called h. The vector h shall be amended by first making it a row vector (by transposing it) and then inserting the values HL to its left and HR to its right. Then we shall assign h to the output variable y, which is supposed to be a row vector of length N+2 holding the vertical coordinates of the 2 towers and the N joint points.


    Answer questions #8,#9 and #10.
     
    #8. Fill in the blank to solve the system of equations using the backslash (\) operator, and store this result in a vector h.

            h = ___________________________ ;
     
    #9. Fill in the blank to change h from a column vector into a row vector.

             h = _____________________ ;

    #10. Fill in the blank to prepend h with HL and append h with HR. (Hint: example, if h = [1 2 3 4 5]  and HL = 0 , HR = 6 then we need to create
            [ 0 1 2 3 4 5 6])

            h = __________________________________________ ;
     
            %   assign h to y , that's one of our output variables

            y = h;


    d) Create a vector x to hold the corresponding x-coordinates of the towers and the joint points, so that you can plot all of these points. The distances are measured from the left tower. They are derived from the d vector that you are given (in mp1_data.m). Your first value in this x vector should be 0 (corresponding to the left tower), and the rest of them should cumulatively add up the distances between the left tower and the first joint point(weight), each pair of successive joint points(weights), and the last joint point(weight) and the right tower (think cumsum).


    Answer question #11. We’re finished with the function at this point, so you can save it.
    #11. Fill in the blank to create a row vector x of distances from the left tower. You should have N+2 (7 for our example) elements in this vector.     
            Assume the variable d (with N+1 elements) has already been created. (Hint: use 'cumsum' and prepend 0 )

           x = ___________________________________________ ;



Part 2 Testing the find_xy function

Now that you have solved the problem correctly, you should test your code.  After saving your code in 'find_xy.m' , then in the Matlab command window type:

>>[x,y] = find_xy(15)

Then you should get the following values for x and y:

x =

         0    7.5000   15.0000   22.5000   30.0000   37.5000   45.0000   52.5000   60.0000   67.5000   75.0000


y =


   70.0000   56.6500   45.8000   37.4500   31.6000   28.2500   27.4000   29.0500   33.2000   39.8500   50.0000


If you don't get the above values, you must "debug" your program.( Note: we are displaying only 4 digits to the right of the decimal. Rounding may occur, so if you display more digits you answers may vary.)  If you get an error message, read it carefully. Some messages are cryptic and therefore useless however most contain useful information like the line number in the file find_xy where the error occurred. When this doesn't work ask your TA for help.

 

Congratulations, you are done!