# Lab 4

(Difference between revisions)
 Revision as of 22:49, 21 February 2011 (view source)Awk1 (Talk | contribs) (→Solving Systems of Equations with Matrices)← Older edit Latest revision as of 01:29, 22 February 2011 (view source)Awk1 (Talk | contribs) (→Surface Plotting) Line 69: Line 69: What are X1 and Y1? What are X1 and Y1? -
• Compute a new Z called Z_hypo from the matrices X and Y.
• +
• Compute a new Z called Z_hypo from the matrices X and Y. Important!! - Do not use x1 y1 from above. Use x and y from the given data.
• Plot your new surface given in Z_hypo and compare to the surface from the data (overlaying the two plots in the same figure).
• Plot your new surface given in Z_hypo and compare to the surface from the data (overlaying the two plots in the same figure).
• Compute the error in your model by computing the difference between Z and Z_hypo, square each element of the difference, and sum over the entire matrix.
• Compute the error in your model by computing the difference between Z and Z_hypo, square each element of the difference, and sum over the entire matrix.

## Surface Plotting

In lab 3, we plotted a quadratic function with the following code:

>> x = -10:10;
>> y = x.^2;
>> plot(x, y)

In this case, we created the vector x, which is the domain of the quadratic function, and then squared each one of the elements to create the range vector y. If we just wanted to study the quadratic function, we have another option. Type ezplot('x^2'), and observe the resulting plot. Note that we are using a symbolic expression. Read the help on ezplot.

 Q1: Plot three different functions with ezplot and save the figures to your working directory. Figures can be saved by going to File-->Save. Note that figures can be saved in many formats. For this lab, use the default "fig" format.

For plotting 3D surfaces, MATLAB offers an equivalent to ezplot called ezsurf. Try ezsurf('x^2+y^2').

 Q2: Plot 1 function (different than the example) with ezsurf and the same function with ezcontour and ezsurfc, then save the figures to your working directory.

When plotting a surface, the domain is 2D (the X-Y plane) and the range is 1D (the Z axis). The ezplot family of functions work for symbolic expressions. What if we only have grid samples? To plot grid samples, we use the surf function. The simplest way to use surf is to give it a matrix that indicates the height of every point on a grid. For example, run this code:

>> Z = rand(10,10)
>> surf(Z)

Like plot, the surf function assumes a default domain starting at 1 so the domain for this example is {x=1:10 | y=1:10}. To change or shift the domain, we can provide vectors for x and y, for example, surf(18:27,33:42,Z). Note that the lengths of x and y have to match the correspondent dimensions of Z.

 lab4_1.m: You've sampled a system at various 2D points but the points contain some noise. You are trying to find a mathematical model that best fits the data. Load the experimental data located here. The matrix Z contains the magnitude of each measurement over the square grid defined by the vectors x and y. Use surf to plot the data. Be sure to specify the domain with x and y. What mathematical function seems to describe the data? To test your hypothesis, you need to generate a surface from your model. In order to generate your surface, you need an X matrix and Y matrix that define the grid on which you can calculate Z. To generate X and Y, use the function meshgrid. To get a feel for meshgrid execute the following code: >> x1 = [1:4]; >> y1 = [3:9]; >> [X1, Y1] = meshgrid(x1, y1) What are X1 and Y1? Compute a new Z called Z_hypo from the matrices X and Y. Important!! - Do not use x1 y1 from above. Use x and y from the given data. Plot your new surface given in Z_hypo and compare to the surface from the data (overlaying the two plots in the same figure). Compute the error in your model by computing the difference between Z and Z_hypo, square each element of the difference, and sum over the entire matrix.

Try the following:

>> [X, Y, Z] = sphere(30)
>> surf(X, Y, Z)

 lab4_2.m: Generate an ellipsoid centered at (2, 3, -1) with x radius 5, y radius 2, and z radius 3. (Hint: See the help for sphere.) Use an appropriate number of surface points. Use the axis command to set the aspect ratio of the x-,y- and z-axis to show the true shape of the ellipse. Again, using the axis command, set the axes to all be in the range (-7, 10).

Select the "Demo" tab in MATLAB help. Check out all the demos under the "MATLAB" section. Make a mental note of the topics, because you may want to come back at some later date to learn something new.

Find the demo under "MATLAB / Graphics / 3-D Surface Plots" (NOT 3-D Plots, though it would be a good idea to check that one out too). Read the description and click on "Run this demo". Play with the different options, and look at the code used to generate each plot (in the mini command window as part of the demo).

 Q3: Plot the Z matrix from lab4_1.m using your favorite style.

## Solving Systems of Equations with Matrices

In this section, we will use matrices to solve a system of equations. Our example problem is that of a triathlon, which is a race that involves swimming, bicycling, and running.

A triathlete has performed different combinations of the events. The athlete would like to know the average time for each event.

Here's the data we'll use.  There were 3 races:

• The first race involved 1 segment of swimming, and 1 segment of running, and it took 1.5h.
• The second involved 2 swimming segments and 1 bicycling, and took 2h.
• The third had 1 bicycling and 1 running segment, and took 1.2h.

Assuming we have variables swimmingTime, bicyclingTime, and runningTime, the first race above can be expressed as:

swimmingTime + runningTime = 1.5 hrs

This can also be written as:

1*swimmingTime + 0*bicyclingTime + 1*runningTime = 1.5 hrs

We can represent the three events using matrices:

$\left[ \begin{array}{ccc} 1 & 0 & 1 \\ 2 & 1 & 0 \\ 0 & 1 & 1 \end{array} \right] \times \left[ \begin{array}{c} swimmingTime \\ bicyclingTime \\ runningTime \end{array} \right] = \left[ \begin{array}{c} 1.5\\ 2\\ 1.2\end{array} \right]$

Let's rewrite these as Mx = y where x represents the time variables. To solve this equation using linear algebra, we must multiply both sides by the inverse of M. That is, M-1 M x = M-1 y and after we simplify, x = M-1 y.

To compute M-1 we can use either:

>> M^-1

or

>> inv(M)

 lab4_3.m: Define M and y in your script. Compute the inverse of M. Compute and show swimmingTime, bicyclingTime, and runningTime. Show that the values you've calculated satisfy the equations by multiplying M and x and comparing to y.

Now, let's suppose we add another data point, in which she did 1 segment from each sports, and it took 1.7h.

This new data can be captured by the following matrix operation:

$\left[ \begin{array}{ccc} 1 & 0 & 1 \\ 2 & 1 & 0 \\ 0 & 1 & 1 \\ 1 & 1 & 1 \end{array} \right] \times \left[ \begin{array}{c} swimmingTime \\ bicyclingTime \\ runningTime \end{array} \right] = \left[ \begin{array}{c} 1.5\\ 2\\ 1.2\\ 1.7\end{array} \right]$

 lab4_4.m: Solve this system. To do so, you will need to compute the pseudo-inverse since the matrix is not square. See the help for inv to find an appropriate function. In which events did the athlete get better? get worse?

Note: A better way, from both an execution time and numerical accuracy standpoint, is to use the matrix division operator x = A\b. This produces the solution using Gaussian elimination, without forming the inverse.

 Demonstrate your answers to Q1-Q3 and your scripts for the lab problems (1-4) to the TA or instructor and answer some questions to checkout.