For this homework, we want to write a C++ progam called croots.cpp that will (partly) duplicate...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
For this homework, we want to write a C++ progam called croots.cpp that will (partly) duplicate the functionality of the myroots function we wrote for the last Matlab homework (H5M - you might want to review the solutions for this homework in preparation for this assignment). The difference is that our C++ program will be specialized for cubic polyno- mials, of the form p(x) = ax + bx + cx+d. A sample run of the program should look exactly like Enter cubic coefficients starting with x^3: 2 10 22 30 The roots are x1 = -3, x2 = 1+2j, x3 = 1-2j NOTES: 1. While there are direct formulae for finding the roots of a cubic, they are pretty ugly, and moreover can generate complex valued intermediate results even if the final roots are real. Since C++ doesn't know how to handle complex numbers, this would be a problem. Instead we will use Newton's method to find a real root, like in H5M. (A third-order polynomial is guaranteed to have at least one real root.) However, unlike that homework, we will only use Newton to find one root. After we divide that root out of the original polynomial, we will be left with a quadratic, and can then simply use the quadratic formula to find the remaining two roots. 2. C++ has no native polynomial handling functions equivalent to polyval, polyder, or deconv. The first two should not present a major hurdle: it is easy enough to "manually" evaluate the third order polynomial, and you should not have difficulty determining the derivative of this polynomial and using that calculation in your code (recall d/dx(ax) = anx-1). For deconv, recall we used this to divide the (x-2) factor, where z is a root, out of the polynomial. We can do this step manually as well: if z is a root of the cubic, then ax + bx + c + d) x-2 = x + 3x + where a = a, =b+az, and y = c+z(b+az). 3. For the Newton loop, you can use an initial guess z = 1 and a tolerance of 10-6. 4. Newton can get "stuck" finding the first root in some cases, which would result in the while loop never terminating. This can sometimes (but not always!) be fixed by using higher precision - double-calculations. For this reason, using double variable types instead of float is advised. Your code will not be tested with a polynomial that would cause Newton to get intractably stuck using this level of precision. 5. Please remember to place a comment at the top of your code with your name, section, and our honor statement. For this homework, we want to write a C++ progam called croots.cpp that will (partly) duplicate the functionality of the myroots function we wrote for the last Matlab homework (H5M - you might want to review the solutions for this homework in preparation for this assignment). The difference is that our C++ program will be specialized for cubic polyno- mials, of the form p(x) = ax + bx + cx+d. A sample run of the program should look exactly like Enter cubic coefficients starting with x^3: 2 10 22 30 The roots are x1 = -3, x2 = 1+2j, x3 = 1-2j NOTES: 1. While there are direct formulae for finding the roots of a cubic, they are pretty ugly, and moreover can generate complex valued intermediate results even if the final roots are real. Since C++ doesn't know how to handle complex numbers, this would be a problem. Instead we will use Newton's method to find a real root, like in H5M. (A third-order polynomial is guaranteed to have at least one real root.) However, unlike that homework, we will only use Newton to find one root. After we divide that root out of the original polynomial, we will be left with a quadratic, and can then simply use the quadratic formula to find the remaining two roots. 2. C++ has no native polynomial handling functions equivalent to polyval, polyder, or deconv. The first two should not present a major hurdle: it is easy enough to "manually" evaluate the third order polynomial, and you should not have difficulty determining the derivative of this polynomial and using that calculation in your code (recall d/dx(ax) = anx-1). For deconv, recall we used this to divide the (x-2) factor, where z is a root, out of the polynomial. We can do this step manually as well: if z is a root of the cubic, then ax + bx + c + d) x-2 = x + 3x + where a = a, =b+az, and y = c+z(b+az). 3. For the Newton loop, you can use an initial guess z = 1 and a tolerance of 10-6. 4. Newton can get "stuck" finding the first root in some cases, which would result in the while loop never terminating. This can sometimes (but not always!) be fixed by using higher precision - double-calculations. For this reason, using double variable types instead of float is advised. Your code will not be tested with a polynomial that would cause Newton to get intractably stuck using this level of precision. 5. Please remember to place a comment at the top of your code with your name, section, and our honor statement.
Expert Answer:
Related Book For
Computer Architecture A Quantitative Approach
ISBN: 978-0123704900
4th edition
Authors: John L. Hennessy, David A. Patterson
Posted Date:
Students also viewed these mechanical engineering questions
-
Consider the expression ($200)(1.03) 3 + ($200)(1.03) 2 + ($200)(1.03) 1 + ($200)(1.03) 0 If interest is compounded annually, what financial question involving an annuity is this expression the...
-
answer the question clearly You are building a flight-control system for which a convincing safety case must be made. Would you assign the tasks of safety requirements engineering, test case...
-
What is the difference between MouseListener and MouseAdapter? [3 marks] (b) Via suitable HTML, the compiled version of the following Java code is presented to the appletviewer application: import...
-
(a) A dice is thrown n times where n is a sufficiently large enough number to provide a uniformly distributed set of results. What would be the conditions for the arrangement of the result set that...
-
Define the following terms: a. The law of one price. b. Absolute purchasing power parity. c. Relative purchasing power parity.
-
On May 4, 2006, Argentina brought a complaint before the ICJ against Uruguay. Argentina alleged that Uruguay had breached obligations to provide notice to and consult with the Administrative...
-
Suppose you're evaluating a stock that just paid a quarterly dividend of $1.25, you expect future dividends on this stock to increase at a rate of 4 percent per quarter, and you think the appropriate...
-
Follow the instructions in the preceding the case in 10.59. Write the audit approach section like the cases in the chapter. In Plane View Whiz Corporation owned 160,000 shares of Wing Company stock,...
-
Which is NOT a consumer power developed by French and Raven? Influential power Coercive power Expert power Reward power
-
K A nutritionist wants to determine how much time nationally people spend eating and drinking. Suppose for a random sample of 925 people age 15 or older, the mean amount of time spent eating or...
-
What are the general environmental factors that Mayo Clinic and other hospital systems face? How about specific environmental issues? Circling back to planning, what else, if anything, could have...
-
Identify six environmental factors that affect international strategies. Discuss the kinds of things that each environmental factor includes. Give an example of how and why one industry is affected...
-
Strategy is not just about countering environmental threats and taking advantage of environmental opportunities. It is also about matching organizational resource to those threats and opportunities....
-
what have been the greatest strengths of environmental assesment law in Canada so far, and what have been the greatest weaknesses amd continuing deficiencies of environmental assessment in Canada?
-
What is a societal or environmental issue you personally are passionate about? It could be social inequality, water access and sanitation, environmental conservation, climate change, health...
-
The final project should include the following items: Find a business problem and define an analytical quesiton that can be tested and analyzed. Ideally, the analytical question should be predictive...
-
A container holds 2.0 mol of gas. The total average kinetic energy of the gas molecules in the container is equal to the kinetic energy of an 8.0 10-3-kg bullet with a speed of 770 m/s. What is the...
-
Suppose you are the controller of Ryerson University. The university president is preparing for her annual fundraising campaign for 2008-09. To set an appropriate target, she has asked you to prepare...
-
Grace Hospital provides a wide range of health services in its community. The board of directors has authorized the following capital expenditures: The expenditures are planned for October 1, 2008,...
-
Study the appendix beginning on page 551. The Speedy-Mart Store has the following budgeted sales, which are uniform throughout the month: Cost of goods sold averages 70 percent of sales and is...
Study smarter with the SolutionInn App