This homework assignment is also available in PDF.
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.
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
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 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
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
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).
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.
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.
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, usingfrom the 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.
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.
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
3*1*1 - 5*1 + 2 = 3 - 5 + 2 = 0
Monday, 28 August 2006 [Samuel A. Rebelsky]
Wednesday, 30 August 2006 [Samuel A. Rebelsky]
Wednesday, 24 January 2007 [Samuel A. Rebelsky]
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 Wed Aug 29 08:59:24 2007.
The source to the document was last modified on Sun Jul 29 16:12:56 2007.
This document may be found at
You may wish to validate this document's HTML ; ;Samuel A. Rebelsky, email@example.com
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.