Question: 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 this homework, we want to write a C++ progam called croots.cpp

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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mechanical Engineering Questions!