Fundamentals of Computer Science I (CS151.02 2007S)

Homework 2: Quadratic Roots

This homework assignment is also available in PDF.

Assigned: Wednesday, January 24, 2007
Due: Friday, January 26, 2007
No extensions!

Summary: In this assignment, you will explore the use of Scheme to compute the square roots of a quadratic equation.

Purposes: To give you exprience writing expressions in Scheme form (prefix notation and parenthesized). To get you used to doing daily homework assignments, particularly submitting the assignments. To demonstrate the utility of Scheme as an extended calculator.

Expected Time: One hour.

Collaboration: You should work in groups of two or three. You may not work alone. You may not work in groups of four or more. You may discuss the assignment with anyone you wish. You may obtain help from anyone you wish, but you should clearly document that help.

Submitting: Email me your work. More details below.

Warning: So that this exercise is a learning assignment for everyone, I may spend class time publicly critiquing your work.

Background: Roots of Polynomials

One advantage of learning a programming language is that you can automate the more tedious computations you encounter. We begin your work in Scheme by considering one such computation.

One of the more painful computations students are often asked to do in high-school mathematics courses is to compute the roots of a polynomial. As you may recall, a root is a value for which the value of the polynomial is 0. For example, the roots of 3x2-5x+2 are 2/3 and 1. (See the end of this assignment for a proof.)

There is, of course, a formula for computing the roots of a quadratic polynomial of the form ax2+bx+c. In a narrative style, it's often expressed

Negative b plus or minus the square root of b squared minus four a c all over two a.

In more traditional mathematical notation, we might write

(-b +/- sqrt(b2 - 4ac))/2a

Assignment

Our goal, of course, is to convert all of these ideas to a Scheme program.

We can express the coefficients of a particular polynomial by using define expressions.

(define a 3)
(define b -5)
(define c 1)

If we also define x, we can evaluate the polynomial.

(define x 5)
(define value-of-polynomial (+ (* a x x) (* b x) c))

Of course, since we've defined a, b, and c, we can also compute the roots. Here is a bit of incorrect code to compute roots.

(define root1 (+ (- b) (sqrt b)))
(define root2 (- (- b) (sqrt b)))

Your goal, of course, is to correct the code for root1 and root2 so that we correctly compute the roots of the polynomial.

You can test the correctness of your solution by trying something like

(define value-of-polynomial-at-root1 (+ (* a root1 root1) (* b root1) c))
(define value-of-polynomial-at-root2 (+ (* a root2 root2) (* b root2) c))

If your solution is correct, each of these values should be 0 (or close to 0).

Testing Your Work

In the past, we've seen students test their quadratic-root formula by trying essentially arbitrary values for a, b, and c. Unfortunately, many arbitrary quadratic polynomials have no real roots. Hence, we suggest that you test your work by building polynomials for which you know the roots. How? Create your polynomials by multiplying two linear polynomials, (px+q)*(rx+s). You know that the roots of this polynomial will be -q/p and -s/r.

Important Evaluation Criteria

The primary evaluation criterion for this assignment is, of course, correctness. That is, I will check to make sure that you expressed the quadratic formula correctly in Scheme.

Particularly elegant solutions may earn a modicum grade boost. Conciseness is one aspect of elegance. Formatting of your code for clarity, using horizontal and vertical whitespace is another. You may discover others.

Submitting Your Homework

Once you have ensured that all of the definitions in the definitions window are correct, please submit your definitions in the body of an email message.

In particular, select all your definitions and then copy them (typically, using Copy from the File item). Open a mail composition window, either in Icedove or Outlook Express, and paste the definitions into that window. Add your names at the top of the window. Make the subject of the email CSC151 Homework 2. Send the mail.

Sample Roots

In the narrative above, we claimed that the roots of 3x2-5x+2 are 2/3 and 1. Let's see if that's true.

x=2/3

3*(2/3)*(2/3) - 5*2/3 + 2 
= 12/9 - 10/3 + 2
= 4/3 - 10/3 + 2
= (4-10)/3 + 2
= -6/3 + 2
= -2 + 2
= 0

x=1

3*1*1 - 5*1 + 2
= 3 - 5 + 2
= 0

 

History

Monday, 28 August 2006 [Samuel A. Rebelsky]

Wednesday, 30 August 2006 [Samuel A. Rebelsky]

Wednesday, 24 January 2007 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Thu Sep 13 20:54:07 2007.
The source to the document was last modified on Wed Jan 24 08:28:45 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Homework/hw.02.html.

You may wish to validate this document's HTML ; Valid CSS! ; Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2007 Samuel A. Rebelsky. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.