Programming and Data Types
M-File ProgrammingIn 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.
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)'.
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.
h and K variables), write
out five equations for i = 1..
5 (N = 5) on the answer sheet for question #1.
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 .

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 .

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 .

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;
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( ______ ) - ______________ ;
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 = ___________________________________________ ;
Congratulations, you are done!