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 Part 1 and Part 2(shows the equation Dy = wp) of
MP1. The ultimate 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:
D*y = wp
where y is a nx1 column vector, D is an nxn matrix and wp is an nx1 column vector.
Your code should perform the calculations necessary to solve the
matrix equation above to obtain the value of the vector y, which represents the
values of the heights of the cables. 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.
yi-1*1/di + yi*(-1/di
+ -1/di+1) + yi+1 * 1/di+1 =
Wi/ HF for i=1 .. n
These equations define a tri-diagonal system of linear equations for the heights y1 to yn 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 type '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 code for your Matlab function
until step 3.
Don't type this in your Matlab function yet as we need to
first create the three matrices D_main, D_upper and
D_lower.
D_main is a diagonal matrix whose only non-zero values lie
on the main diagonal and are -(1/di + 1/di+1)
i = 1,...n .
Answer the question #3 on the answer sheet.
D_upper is the upper diagonal matrix whose only non-zero values
lie above the main diagonal and are 1/di
i = 2,...n .

Answer the question #4 on the answer sheet.
D_lower is the lower diagonal matrix whose only non-zero values
lie below the main diagonal and are 1/di
i = 2,...n .
Answer the question #5 on the answer sheet.
Now we can type
b) We need to create the right-hand
vector wp.
This vector is created using the weight vector W, the HF value and the
values yL
and yR, all of
which are given to you. Except for the first and last elements
of wp, all the
others have the form:
wpi = Wi / HF;
Answer question #6.
The first element is W1
/ HF - yL/d1 .The last element
is Wn / HF -
yR/dn+1
Therefore all we need to do is subtract yL/d1
from the first element of wp and subtract yR/dn+1
from the nth element of wp.
Answer question #7.
Answer questions #8,#9 and #10.
d) Create a vector x to hold the corresponding x-coordinates of the towers and the cables, 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
Answer question #11. We’re finished with the function at this
point, so you can save it.
x =
Congratulations, you are done!