CS368 Homework 2
Lec 1, Spring 2016
Due by 9 AM on Thursday, February 18 (not accepted late)

Announcements

Check here periodically.

2/10/2016  Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: http://www.piazza.com/wisc/spring2016/cs368 and sign-in using your wisc.edu account.

Problems

To get started, download this MATLAB script:

homework2.m

Save the script (e.g., in a MATLAB/homework2 folder - if you don't have a homework2 folder, you can create one when you save the file).

Launch MATLAB and open the homework2.m script. The file is a text-only file that opens in the Editor window in MATLAB. Edit your homework2.m file with your name and answers to the problems. The header and some of the format is given to you in this template to ensure that your published results meet our expectations for this homeworks. Please do not reformat until you understand how your script headers and overall format affect the published results.

Problem 1: (4 points): Creating a tip table

For this problem you will be creating a "tip table" that could be used to look up an amount to leave as a tip given the restaurant bill. In addition to the column for the bill, the table will contain columns for 15%, 18%, and 20% tips.

Create and display a matrix with four columns:

  • column 1 contains bill totals from $5 to $100 in increments of $5
  • column 2 contains the tip amount if the tip is 15% of the bill
  • column 3 contains the tip amount if the tip is 18% of the bill
  • column 4 contains the tip amount if the tip is 20% of the bill

If you've constructed your matrix correctly, when it is displayed, the first few lines will look like:

          5.00          0.75          0.90          1.00
         10.00          1.50          1.80          2.00
         15.00          2.25          2.70          3.00
         20.00          3.00          3.60          4.00

To receive full credit, you must make use of MATLAB's matrix and vector creation and manipulation abilities, e.g., you cannot enter the first column in element by element, columns 2 through 4 should be calculated using column 1 (i.e., if you decided to modify the tip table to go from bill totals of $2 to $50 in increments of $3, exactly one line of the code you wrote would need to be changed).

Problem 2: (5 points) Density of freshwater

The density of freshwater can be computed as a function of temperature using the following equation:

ρ = 5.5289×10-8 TC3 - 8.5016×10-6 TC2 + 6.5622×10-5 TC + 0.99987

where ρ = density (g/cm3) and TC = temperature (°C).

  1. Determine the density of freshwater at 40 °F, 68 °F, and 100 °F. Recall that TC= 5/9 (TF - 32).

  2. For each of the temperatures from part a, display the results in the form:

    Freshwater density is xxxxx at yyy F

    where xxxxx is density of freshwater as determined in part a and yyy is the temperature. To receive full credit, your output must be exactly in the form given and must make appropriate use of the num2str command.

Problem 3: (8 points) Distance to horizon

The distance to the horizon from a location on a hill depends on both the height of the location on the hill and the radius of the planet and is given by the formula:

d=sqrt(2rh + h^2)

where d = the distance to the horizon, r = the radius of the planet, and h = the height of the hill (all in the same unit of measurement).

Plot the distance to the horizon vs the height of a hill for hill heights from 0 to 10,000 feet on Earth and Mars on one plot figure. Note: the diameter of the Earth is 7926 miles and the diameter of Mars is 4217 miles. To receive full credit:

  • Plot the curve for the Earth as a dashed blue line and the curve for Mars as a solid red line.
  • Use an appropriate increment for your hill heights so that the curves are smooth.
  • Add appropriate axis labels, a title, and legend to your plot.

Style and Technique (3 points)

Part of your score on this (and, really, every) assignment is based on your style and technique. Included in this are the following:

  • The main script must be published (to HTML or PDF). The document must be well-formatted and easy to read. (See the solutions for the Team Labs on the CS 310 Syllabus page for examples of published HTML documents and the MATLAB scripts that they were created from.)
  • Your published script must include output that demonstrates that your commands are doing the correct thing. This means you should display the results of your commands as specified in the write-up. (Note: (1) you may always suppress the output of commands that create result vectors that are then plotted; (2) commands inside the body of a function should have their output suppressed.)
  • File and variable names must be exactly as specified (including capitalization).
  • Variables names must be descriptive and follow naming conventions.
  • Variables must be used to access previously computed results; you should never retype a computed value.

Handing in

  • Do all of your work in your homework2.m file.

  • Publish your homework2 (to either HTML or PDF) and print the published results. Problems 4 - 8 of CS 310 Team Lab 1 describes various aspects of publishing step by step (see also the instructions on how to publish your work in the MATLAB Reference module).

  • Make sure to look at your printed work to see that it is formatted the way you want and that everything got printed. Please staple your work.

  • Turn in your work by 9 am on Thursday, February 18. You can turn in your work at lecture on Wednesday, February 18, or turn it in to Beck's office (5375 CS) by 9 am .
Last Updated: 2/9/2016     © 2016 Beck Hasti, hasti@cs.wisc.edu