Project 2 on CMS - Introduction to Computing using MATLAB | CS 1112, Study Guides, Projects, Research of Computer Science

Material Type: Project; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Fall 2008;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/30/2009

koofers-user-8cx
koofers-user-8cx 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Fall 2008 Project 2 due Monday 9/22 at 6pm
You must work either on your own or with one partner. You may discuss background issues and general solution
strategies with others, but the project you submit must be the work of just you (and your partner). If you work with
a partner, you and your partner must first register as a group in CMS and then submit your work as a group.
Objectives
Completing this project will help you learn about loops and invoking functions. Remember to practice
decomposing a problem into smaller (simpler) sub-problems.
Note: Do not use arrays. You will call several provided functions, but do not create your own functions in
this project. In order to call the provided functions, put them in the same directory/folder as the script that
calls these functions.
1 What time is it?
Have you ever wondered how an analog clock is displayed on a computer screen or other digital devices?
In this problem you will make only a still display, not a running clock! You will write a Matlab script to
display a time (e.g., 23:15:31) on a clock with hour and minute hands.
The clock is drawn in a figure window. Our clock’s hands have a continuous pivot, i.e., they are not restricted
to the 60 individual minute marks. The hour value may be any whole number in [0..23] and the minute
and second values are whole numbers in [0..59]. There are quite a few things that the solution will need, so
practice problem decomposition. Here are some “subproblems” that you need to solve in order to complete
the solution; work on them one at a time: convert numeric time values to angles, convert angles to x-y
coordinates for plotting, draw the clock with tick marks, and draw the hands to indicate the time correctly.
First, structure your solution by writing a comment for each task in the provided skeleton script drawClock
in a logical order. Then fill in the detail (add code) under each comment. Test each newly added section
of code simply by running the (partially completed) program. (Later in the course we will discuss a better
strategy for testing and program development.) Keep in mind that some tasks may appear in multiple parts
of the script (e.g., converting an angle to x-y coordinates for plotting). Below are additional specifications:
You must call the provided functions DrawDisk,DrawStar,orDrawRect to draw the clock, including
the hour and minute marks.
Use the plot function to draw the hands. For example, plot([x1 x2],[y1 y2],’r-’,’linewidth’,8)
draws a red line of “width 8” from (x1,y1) to (x2,y2). The hour hand in the figure above has a width
of 8.
The hour marks must be different from the minute marks.
1
pf2

Partial preview of the text

Download Project 2 on CMS - Introduction to Computing using MATLAB | CS 1112 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS1112 Fall 2008 Project 2 due Monday 9/22 at 6pm

You must work either on your own or with one partner. You may discuss background issues and general solution strategies with others, but the project you submit must be the work of just you (and your partner). If you work with a partner, you and your partner must first register as a group in CMS and then submit your work as a group.

Objectives

Completing this project will help you learn about loops and invoking functions. Remember to practice decomposing a problem into smaller (simpler) sub-problems.

Note: Do not use arrays. You will call several provided functions, but do not create your own functions in this project. In order to call the provided functions, put them in the same directory/folder as the script that calls these functions.

1 What time is it?

Have you ever wondered how an analog clock is displayed on a computer screen or other digital devices? In this problem you will make only a still display, not a running clock! You will write a Matlab script to display a time (e.g., 23:15:31) on a clock with hour and minute hands.

The clock is drawn in a figure window. Our clock’s hands have a continuous pivot, i.e., they are not restricted to the 60 individual minute marks. The hour value may be any whole number in [0..23] and the minute and second values are whole numbers in [0..59]. There are quite a few things that the solution will need, so practice problem decomposition. Here are some “subproblems” that you need to solve in order to complete the solution; work on them one at a time: convert numeric time values to angles, convert angles to x-y coordinates for plotting, draw the clock with tick marks, and draw the hands to indicate the time correctly. First, structure your solution by writing a comment for each task in the provided skeleton script drawClock in a logical order. Then fill in the detail (add code) under each comment. Test each newly added section of code simply by running the (partially completed) program. (Later in the course we will discuss a better strategy for testing and program development.) Keep in mind that some tasks may appear in multiple parts of the script (e.g., converting an angle to x-y coordinates for plotting). Below are additional specifications:

  • You must call the provided functions DrawDisk, DrawStar, or DrawRect to draw the clock, including the hour and minute marks.
  • Use the plot function to draw the hands. For example, plot([x1 x2],[y1 y2],’r-’,’linewidth’,8) draws a red line of “width 8” from (x1,y1) to (x2,y2). The hour hand in the figure above has a width of 8.
  • The hour marks must be different from the minute marks.
  • The hour marks for 3, 6, 9, and 12 o’clock must be different from the other hour marks.
  • The hour hand must be easily distinguishable from the minute hand.

Choose colors and shapes as you wish. Here’s a hint: hours, minutes, and seconds can be converted readily to an angle, so think in polar coordinates. A polar coordinate has two components: radial and angle. The radial component is like the radius of a circle; the angle is measured counter-clockwise from 0◦, which is the positive x-axis in the Cartesian system. In order to use Matlab’s plot function, you need to convert from polar coordinates (r, θ) to Cartesian coordinates (x, y):

x = r cos(θ) y = r sin(θ)

2 Compute the Pythagorean sum

The Pythagorean sum

a^2 + b^2 appears frequently in computation. For example, if I need a wheelchair ramp at my house to rise 8 inches across 9 feet, how much surfacing material do I need for the ramp? Answer: w

(8/12)^2 + 9^2 square feet where w is the width of the ramp.

Computationally speaking, the square root is much more expensive to calculate than elementary operations such as +, × and ÷. Therefore it is desirable to avoid the explicit square root operation. Moler and Morrison (1983) devised the following iteration to compute the Pythagorean sum. (This approach also avoids the numerical errors of overflowing and underflowing—to be discussed in Chapter 4—that may result from squaring a and b.)

Given x 0 > 0, y 0 > 0, and x 0 ≤ y 0 , one can compute p =

x^20 + y^20 as follows:

xn+1 = xn

y n^2 4 x^2 n + y n^2

yn+1 =

y^3 n 4 x^2 n + y^2 n

As n → ∞, xn converges to p from below while yn converges to 0 from above.

Write a script pythSum to solicit two values, a and b, and compute their Pythagorean sum as described above. The iteration should stop when xn − xn− 1 is less than 10−^8 and yn < 10 −^8. Note that it is not necessary to store all the values x 0 ,... , xn and y 0 ,... , yn. At any point in the algorithm, the current x value depends only on the previous x value and the previous y value. A user may enter negative values for a and/or b and a may be greater than b. Your code should set x 0 and y 0 appropriately. (We assume that |a| and |b| are “far enough” from zero, e.g., |a| > 10 −^8 and |b| > 10 −^8 .)

3 Detect a pattern

Complete exercise P3.2.7 in Chapter 3 of Foundations of Computational Science & Engineering by FVL. Save the file as pattern.m.