CE 502 SWI, Assignment # 4
Due by 5:00 p.m. on Monday, March 20, 1999





More Problems on Matlab with an emphasis on writing and debugging Matlab functions.

There are a number of laboratory problems that require you to use specific data in your demonstration that your programs give correct results. This data will automatically be defined in your workspace when you execute the program: data303 in your Matlab session.

In many cases you may wish to do some preliminary testing on simpler data than you will get from the program. Please do that, but include tests on the specified data when you see arrays marked with a asterisk.

Reading Assignment: Finish reading all except the last chapter in the Matlab notes:

Laboratory Problems

These problems may be completed with help from any of your fellow students (as well as the instructor). You may not copy anyone else's work, but you can get other users to give you suggestions and point out mistakes that should be corrected.

Problems should be completed using the tools suggested and the results stored in a file that the instructor can look at. One way to do that is to copy the commands you execute and the results you see into a text editor (nedit, xedit, emacs) and save the file as hw3, then e-mail it to the instructor.
 

1) One check on any program is to develop another function that gives its inverse and test the two against each other. To see an example of this, write two functions as follows:

Function 1 has as its first argument a vector of molecular weights and its second argument a vector of mass fractions for a given chemical mixture. This function should return the vector of mole fractions of that mixture or an empty vector if the dimensions of the two arguments do not agree.

Function 2 has as its first argument a vector of molecular weights and as its second argument a vector of mole fractions. It should return the vector of mass fractions or an empty vector.

Hint: if one has a 1 gram sample of a mixture with wk as the weight fraction of the kth compound, then it will contain wk grams of the kth compound. Further, if Mk is the molecular weight of the kth compound, the sample will contain wk/Mk mols of the kth compound. The mol fraction for the kth compound will then be: (wk/Mk)/(Total number of mols in the sample)

After you have written and checked out both functions, verify that they are genuine inverses by executing with the result of the first as the argument of the second, as well as demonstrating that each function returns a null vector under the conditions stated.
 

Problems 2 and 3 were particular to the Rice University course, and have been deleted.  I have replaced them with the following problem.

2)  Imagine that an astronaut, standing on the surface of the moon, throws a ball into the air.  The position of the ball is recorded with a video camera at a rate of 12 frames per second.  The vertical position of the ball, relative to the moon surface in each frame is given in this file.  By integrating Newton's law of motion for the ball, we would predict that the position of the ball, as a function of time, is given by
    x(t) = xo+ vot - 0.5 g t2, where xo is the initial position, vo is the initial velocity, and g is the acceleration of gravity.

(a) Use the matlab function leastsq to determine the values of xo, voand g that give the best fit to the data.
(b) Repeat this, but use the matlab function fmins to minimize the sum of the squares of the errors instead of using leastsq.
 
 

Test Problems

You may work these problems with help ONLY from the course instructor.

1) Write a Matlab function with vector arguments: X, Y, and the order of the polynomial to be used in the fit of the data. The program should print out:

Demonstrate this program on the following data for both a linear and a quadratic fit:
        Time sec.    0.0    1.0    2.0    3.0    4.0
      Distance m     0.0    3.1    5.1    6.3    7.1
Problems 2 and 3 were particular to the Rice University course, and have been deleted.  They have been replaced with

(2)  In laboratory problem (2), you did data fitting using the built-in Matlab functions leastsq and fmins.  These functions perform a non-linear least-squares fit and minimize a non-linear function of several variables, respectively.  However, the problem of interest is linear, and it would be more efficient to do the fitting by performing a linear least-squares analysis.  If we have data pairs (t,x), which are fit by a function of several parameters f(t;p), then the errors are given by
    ei = xi - f(ti;p)
and the sum of the squares of these is given by
    Q = S(ei2) = S(xi - f(ti;p))2
If f is linear in the parameters, then the derivatives of Q with respect to each parameter will be linear in the parameters as well.  To minimize Q, you need to take the derivative of Q with respect to each parameter and then solve for the parameter values that make all of the derivatives zero.  For the particular problem considered in laboratory problem 2, this will give three linear equations in three unknowns.  Use matlab to set up and solve these equations to find the values of xo, vo and g that give the best fit to the data.