Programming Project #2: Solving Quadratic Equations Date Due: Monday 15 August 2016

Similar documents
0.8 Rational Expressions and Equations

Zero: If P is a polynomial and if c is a number such that P (c) = 0 then c is a zero of P.

3.2. Solving quadratic equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

Vieta s Formulas and the Identity Theorem

JUST THE MATHS UNIT NUMBER 1.8. ALGEBRA 8 (Polynomials) A.J.Hobson

The Method of Partial Fractions Math 121 Calculus II Spring 2015

1.3 Algebraic Expressions

Factoring Polynomials and Solving Quadratic Equations

Numerical Matrix Analysis

3.2 The Factor Theorem and The Remainder Theorem

Finding Rates and the Geometric Mean

3.1. Solving linear equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

MATH Fundamental Mathematics IV

CHAPTER 5 Round-off errors

CP Lab 2: Writing programs for simple arithmetic problems

2.3. Finding polynomial functions. An Introduction:

Determinants can be used to solve a linear system of equations using Cramer s Rule.

Answer Key for California State Standards: Algebra I

MULTIPLICATION AND DIVISION OF REAL NUMBERS In this section we will complete the study of the four basic operations with real numbers.

1.6 The Order of Operations

Equations, Inequalities & Partial Fractions

Grade Level Year Total Points Core Points % At Standard %

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

Summary of important mathematical operations and formulas (from first tutorial):

Solving Quadratic Equations by Factoring

Definition 8.1 Two inequalities are equivalent if they have the same solution set. Add or Subtract the same value on both sides of the inequality.

Review of Fundamental Mathematics

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

SOLVING QUADRATIC EQUATIONS - COMPARE THE FACTORING ac METHOD AND THE NEW DIAGONAL SUM METHOD By Nghi H. Nguyen

Solving Rational Equations

Copy in your notebook: Add an example of each term with the symbols used in algebra 2 if there are any.

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

This document has been written by Michaël Baudin from the Scilab Consortium. December 2010 The Scilab Consortium Digiteo. All rights reserved.

Solving Quadratic Equations

Click on the links below to jump directly to the relevant section

Partial Fractions. Combining fractions over a common denominator is a familiar operation from algebra:

Solutions of Linear Equations in One Variable

Tom wants to find two real numbers, a and b, that have a sum of 10 and have a product of 10. He makes this table.

Chapter 11 Number Theory

HIBBING COMMUNITY COLLEGE COURSE OUTLINE

Section 1.5 Exponents, Square Roots, and the Order of Operations

Revised Version of Chapter 23. We learned long ago how to solve linear congruences. ax c (mod m)

The Mathematics 11 Competency Test Percent Increase or Decrease

A Systematic Approach to Factoring

NSM100 Introduction to Algebra Chapter 5 Notes Factoring

TI-83/84 Plus Graphing Calculator Worksheet #2

8 Primes and Modular Arithmetic

The Factor Theorem and a corollary of the Fundamental Theorem of Algebra

7.6 Approximation Errors and Simpson's Rule

Rational Exponents. Squaring both sides of the equation yields. and to be consistent, we must have

Lies My Calculator and Computer Told Me

Sequences. A sequence is a list of numbers, or a pattern, which obeys a rule.

COLLEGE ALGEBRA. Paul Dawkins

POLYNOMIALS and FACTORING

MATH 143 Pre-calculus Algebra and Analytic Geometry

Wentzville School District Algebra 1: Unit 8 Stage 1 Desired Results

1 Lecture: Integration of rational functions by decomposition

Notes on Orthogonal and Symmetric Matrices MENU, Winter 2013

Zeros of Polynomial Functions

MBA Jump Start Program

Pearson Algebra 1 Common Core 2015

1.7. Partial Fractions Rational Functions and Partial Fractions. A rational function is a quotient of two polynomials: R(x) = P (x) Q(x).

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

What are the place values to the left of the decimal point and their associated powers of ten?

Linear Equations and Inequalities

expression is written horizontally. The Last terms ((2)( 4)) because they are the last terms of the two polynomials. This is called the FOIL method.

3.3. Solving Polynomial Equations. Introduction. Prerequisites. Learning Outcomes

Academic Success Centre

This is Factoring and Solving by Factoring, chapter 6 from the book Beginning Algebra (index.html) (v. 1.0).

5.3 SOLVING TRIGONOMETRIC EQUATIONS. Copyright Cengage Learning. All rights reserved.

Factoring Quadratic Expressions

The Deadly Sins of Algebra

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

x 2 + y 2 = 1 y 1 = x 2 + 2x y = x 2 + 2x + 1

No Solution Equations Let s look at the following equation: 2 +3=2 +7

2.5 Zeros of a Polynomial Functions

Properties of Real Numbers

BEST METHODS FOR SOLVING QUADRATIC INEQUALITIES.

1 Shapes of Cubic Functions

Vector and Matrix Norms

MAT 096, ELEMENTARY ALGEBRA 6 PERIODS, 5 LECTURES, 1 LAB, 0 CREDITS

5. Factoring by the QF method

MATH10040 Chapter 2: Prime and relatively prime numbers

SOLVING EQUATIONS WITH RADICALS AND EXPONENTS 9.5. section ( )( ). The Odd-Root Property

3.6. Partial Fractions. Introduction. Prerequisites. Learning Outcomes

Algebra Practice Problems for Precalculus and Calculus

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

is identically equal to x 2 +3x +2

2.4 Real Zeros of Polynomial Functions

Basic Use of the TI-84 Plus

SECTION 0.6: POLYNOMIAL, RATIONAL, AND ALGEBRAIC EXPRESSIONS

Math 4310 Handout - Quotient Vector Spaces

Completing the square

MATH 108 REVIEW TOPIC 10 Quadratic Equations. B. Solving Quadratics by Completing the Square

Operations with positive and negative numbers - see first chapter below. Rules related to working with fractions - see second chapter below

3.1. RATIONAL EXPRESSIONS

SOLVING QUADRATIC EQUATIONS BY THE DIAGONAL SUM METHOD

CAHSEE on Target UC Davis, School and University Partnerships

5.1 Radical Notation and Rational Exponents

Transcription:

CISC 5300 Programming in C++ Fall, 2016 Programming Project #2: Solving Quadratic Equations Date Due: Monday 15 August 2016 Write a program that prompts the user to enter the coefficients of a quadratic polynomial ax 2 + bx + c. It should then solve the polynomial equation ax 2 + bx + c = 0. You should use the quadratic formula to solve this equation; if you don t remember it, you ll find it later on in this handout. In what most people consider to be the normal case, there are two real roots, as in the case x 2 3x+2 = 0. However, there is the possibility of only one real root, 1 as in the case x 2 2x + 1 = 0, and no real roots, as with x 2 + 1 = 0. Since you can t control the input your users are giving you, think about what to do if a = 0. (Note that the equation is now a linear equation, provided that b 0.) A few considerations: 1. You are to do this using only the techniques found in Chapters 1 through 4 of the text. 2. You should do this project, in a subdirectory of your /private/programming-c++ directory. See the Project 1 handout for further discussion, and adjust accordingly. 3. I am providing you with the following goodies, to help you get started. An executable version of this program for you to try out, named -agw. An executable for a stable version of this program, named proj1-agw-stable. (I ll explain this later on.) If you d like to earn 10 points in extra credit, you can implement a stable quadratic solver. You should do this after you ve done the basic version of this assignment. A stub version of the C++-code, named -stub.cc. I ll also explain this further on. You should copy these files into your working directory, via the cp command. Assuming that you re logged into one of our machines (i.e., on erdos or a lab machine) and that the working directory described in (2) above is your current directory, the command cp agw/class/programming-c++/share//. will do this. The dot is part of the command; it means current working directory. Once you ve copied it over, please try it out, so you can see what I consider to be good input/output behavior, not to mention how I handle various strange data values (as mentioned above). 1 More precisely, it s a repeated real root of multiplicity two.

4. Of course, you ll need to compute the square root of the discriminant. The standard library has a function sqrt() that will be pretty helpful here. Its prototype (which you get for free) is double sqrt(double x); The return value of sqrt(blah) is (not surprisingly) the square root of blah, assuing that blah is non-negative. 2 5. As you know by now, it s a good idea to let a function carry out a well-encapsulated subtask. When done carefully, this can shorten the main() function; my main() is only 21 lines long. I used two subsidiary functions: The function having prototype void solve_linear(double b, double c); solves a linear equation bx + c = 0, distinguishing between the cases b 0 and b = 0. My version was 14 lines long. The function having prototype void solve_quadratic(double a, double b, double c); solves a genuine quadratic equation ax 2 + bx + c = 0, genuine meaning that a 0. Mine was 21 lines long. The main reason for using functions here (other than simply getting practice) is that each function (including the main() function) is small enough to understand easily; in particular, each function takes up less than a screenful of space. At this point, we can talk about -stub.cc. This file takes care of the overall logic flow, declares the two functions mentioned above, and gives stub implementations of these functions, i.e., minimal implementations for the program to compile. Your first step will be to make a copy of -stub.cc, which you ll call.cc. For fun, you should compile this file as is, and see if you understand why it works the way it does, in its limited way. If you now fill in the blanks (which means implementing the input section and completing the implementation of the two functions mentioned above), you ll now have a working program. Big hint: Do one thing at a time. First, take care of the input phase (this should be pretty easy). Run the program, to make sure that this piece works right. Now, implement one of the two functions, after which you should run the program and test that the program works right so far. Finally, implement the other function, and do yet more testing. 6. Here is a good collection of sample data to use. 2 More honestly, the sqrt() function doesn t really return the exact square root of its argument, but an approximation to same. But it s a very good approximation. 2

a b c Equation Explanation 1-3 2 x 2 3x + 2 = 0 Two real roots (x = 1 and x = 2) 2-6 4 2x 2 6x + 4 = 0 Two real roots (x = 1 and x = 2) 1-2 1 x 2 2x + 1 = 0 One double root (x = 1) 1-2 4 x 2 2x + 4 = 0 No real roots 0 2 4 2x + 4 = 0 Linear equation, root x = 2 0 0 0 0 = 0 An identity 0 0 1 1 = 0 A contradiction 7. Use the photo program so you can have something to turn in. Don t do this until the program is working! The commands you should issue are as follows: photo cat.cc g++ -std=c++14 -o.cc exit You ll be running once for each test dataset. Once again, see the Project 1 handout if you re still unclear about photo. Good luck! For 10 points extra credit: The classical quadratic formula is fine if one is doing exact arithmetic of real numbers. For our discussion, we ll think of a real number as being a decimal number an infinite number of places beyond the decimal point. Computers cannot represent such numbers with exact precision; they can only approximate them. The common standard is to approximate real numbers by floating-point numbers. For the purpose of our discussion, we can think of a floating-point number as being a number written in scientific notation (such as 0.6626068 10 34 or 0.6022142 10 24, with a fixed number of digits after the decimal point (in these examples, six places) and a limited range for the exponent (say, from -44 to 38). You ll also notice that there s a zero before the decimal point and that the first place after the decimal point is nonzero; such floating-point numbers are said to be normalized. The common standard is known as IEEE 754, which specifies both single-precision and double-precision floating-point arithmetic (which might be implemented via float and double, respectively, by a C++ compiler). What this all means is that floating-point arithmetic is inaccurate, compared to real arithmetic. For example, whereas the commutative laws (for addition and multiplication) hold in floating point, the associative law does not hold; neither does the distributive law. The inaccuracy is generally negligible, but it occasionally can cause bad things to happen. 3

One such example is catastrophic cancellation, which occurs when subtracting nearly equal quantities. Suppose we are using floating-point arithmetic having ten decimal digits worth of accuracy. Let s look at an example, taken from http://www.zipcon.net/ swhite/docs/math/loss of significance.html Let x = 0.1234567891234567890 0.1234567890, and suppose that we approximate x on a machine using floating-point arithmetic with 10-digit accuracy. Our best approximation would be x = 0.1234567891 0.1234567890 = 0.0000000001 = 0.100000000 10 9. But the real answer is 0.0000000001234567890, which is 0.1234567890 10 9 when represented in floating point. So we only have one digit of accuracy in our answer, even though the answer can be represented in floating point with ten digits worth of accuracy. Putting this another way, the relative error of our approximation is x x x =. 0.18999, which is about 19%. Now how does all this apply to our quadratic equation? sobolev@dsm:$ Enter the coefficients of a quadratic polynomial ax2 + bx +c: a? 1 b? -20000 c? 1.5e-9 Trying to solve the quadratic equation 1xx + -20000x + 1.5e-09 == 0 Two roots, x = 20000 and x = 0 Let s double-check this answer, using Mathematica R : sobolev@dsm:$ math Mathematica 9.0 for Linux x86 (64-bit) Copyright 1988-2013 Wolfram Research, Inc. In[1]:= xˆ2-20000 x + 1.510ˆ-9-9 2 Out[1]= 1.5 10-20000 x + x In[2]:= Solve[%==0, x] -14 Out[2]= {{x -> 7.5 10 }, {x -> 20000.}} In[3]:= Quit So one computed solution (x = 20000) is correct, whereas the relative error in the other computed solution (x = 0) has a relative error of 100%!! 4

The problem here is that the values of a, b, and c were carefully chosen so that although b 2 b 2 4ac in real arithmetic, the floating-point values of b 2 and b 2 4ac are the same (you should check this yourself). Hence the discriminant of this polynomial computes out as being b 2, and so x + = b + b 2 4ac 2a in floating point. What to do? Note that in this case, the other root. = b + b 2 2a x = b b 2 4ac 2a = 0 2 1 = 0 does not cause a cancellation error (since both b and b 2 4ac are negative). We can calculate the other root by using the fact that the product of the roots must equal c/a, which follows from some simple algebra. 3 Thus we can compute x + = c ax Of course, if b < 0, things will work in the opposite direction. So (finally!) for ten points worth of extra credit, implement this stable version of solve_quadratic(). Sample runs of the program look like the following: sobolev@dsm:$ -stable Enter the coefficients of a quadratic polynomial ax2 + bx +c: a? 1 b? -20000 c? 1.5e-9 Trying to solve the quadratic equation 1xx + -20000x + 1.5e-09 == 0 Using classical formula: Two roots, x = 20000 and x = 0 Using stable formula: Two roots, x = 20000 and x = 7.5e-14 sobolev@dsm:$ -stable Enter the coefficients of a quadratic polynomial ax2 + bx +c: a? 1 b? 20000 c? 1.5e-9 Trying to solve the quadratic equation 1xx + 20000x + 1.5e-09 == 0 Using classical formula: Two roots, x = 0 and x = -20000 Using stable formula: Two roots, x = -7.5e-14 and x = -20000 For your test data, run your program on the test data given above, along with these two additional data sets. Note: If you re going to do a stable version, you should wait until you ve finished the basic version. Having done so, you should call your C++ file -stable.cc, and it should start out as a copy of of.cc. 3 Indeed, since the two roots are x + and x (these are numbers, and not variables!), we have ax 2 + bx + c = a(x x + )(x x ) = a ( x 2 (x + + x )x + (x + x ) ) = ax 2 a(x + + x )x + (ax + x ). Now compare the constant terms to see that c = ax + x, and so x + x = c/a. 5

Here is a listing of -stub.cc: / Project 2: Quadratic Equation Solver Say something here. Author: Harry Q. Bovik, PhD Date: 11 August 2016 / #include <bjarne/std_lib_facilities.h> // function declarations void solve_linear(double b, double c); void solve_quadratic(double a, double b, double c); int main() { // input the coefficients of the polynomial double a, b, c; // coefficients of the polynomial // figure out the rest yourself!! } // handle degenerate case (linear equation) and quit if (a == 0) // linear equation, not quadratic solve_linear(b, c); else // genuine quadratic equation... forge ahead solve_quadratic(a, b, c); // solve the linear equation bx + c == 0 void solve_linear(double b, double c) { cout << "Trying to solve linear equation " << b << "x + " << c << " == 0\n"; // figure out the rest yourself! } // use classical quadratic formula to solve a genuine quadratic equation // axˆ2 + bx + c ==0, with a!= 0 void solve_quadratic(double a, double b, double c) { cout << "Trying to solve the quadratic equation " << a << "xx + " << b << "x + " << c << " == 0\n"; // figure out the rest yourself! } 6