CS 171 & 171L Introduction to Computer Science I & Lab Fall 2017

## Homework Assignment 9 - Arrays and Objects

In this lab you will continue working with java arrays and objects.

Complete the methods in below in Polynomial.java from our lab. Use HW9.java, as shown below to test your code. After you complete each method, test it by running the main method. Consider adding other interesting test cases.

Include your name in a comment at the top of the Java source file. Rename the file so that it reflects your initials, such as HW9_AB.java.

  // Your name here! public class HW9 { public static void main(String args[]){ Polynomial p[] = new Polynomial[10]; int n = 0; p[n++] = new Polynomial(new double[] {6.0, 5.0, 1.0}); // p(x) = x^2 + 5x + 6; p[n++] = new Polynomial(new double[] {1.0, 5.0, 7.0, 3.0}); // p(x) = 3x^3 + 7x^2 + 5x + 1 for (int i = 0; i < n; i++) { System.out.println("p(x) = " + p[i]); System.out.println("degree: " + p[i].degree()); // New items you can add to main in Polynomial.java double M = p[i].bound(); System.out.println("bound: " + M); System.out.println("Tangent line at origin: " + p[i].tangent(0.0)); double r = p[i].root(M); System.out.println("root: " + r); Polynomial q = p[i].divide(r); System.out.println("q(x) = " + q); // Extra credit testing double roots[] = p[i].roots(); for (int i = 0; i < roots.length; i++) { System.out.printf("%d %f\n", i, roots[i]); } System.out.println("--------------------------------------------------------"); } } } 

Given a polynomial, say $p(x) = a_0 + a_1 x + a_2 x^2 + . . . + a_{(n-1)} x^{(n-1)} + a_n x^n$.

1. (10 points) Create a method in Polynomial that returns the bound, $M$, of the roots of this polynomial.
• Divide the polynomial $p$ by the coefficient of the highest degree term ($a_n$), to get a new polynomial, say $q$.
$q(x) = b_0 + b_1 x + b_2 x^2 + . . . + b_{(n-1)} x^{(n-1)} + 1 x^n$, where $$b_i = \left| \frac{a_i}{a_n} \right|.$$
• Let $S$ be the sum of the absolute value of all the coefficients of the polynomial $q$, with the exception of the leading coefficient ($a_n/b_n$). $$S = b_0 + b_1 + \cdots + b_{(n-1)}.$$
• return $M = \max(1, S)$.

2. (5 points) Write a method in Polynomial that takes a double parameter, say $x_0$, and returns a Line that is the derivative of this polynomial at $x_0$. The tangent line for the polynomial $p$ at some position $x_0$ has equation $y = m x + b$, where $$m = p'(x_0) \qquad\text{and}\qquad b = \left(p(x_0) - x_0 p'(x_0)\right)$$
3. (5 points) Create a function named root that takes a single double parameter representing a starting value, say $x_0$, and returns the root $r$ of this polynomial as found using Newton's method: $$x_1 = x_0 - \frac{p(x_0)}{p'(x_0)}$$ $$x_2 = x_1 - \frac{p(x_1)}{p'(x_1)}$$ $$\cdots$$ $$x_n = x_{n-1} - \frac{p(x_{n-1})}{p'(x_{n-1})}$$ Here $p'(x)$ represents the derivative of the function. This procedure is repeated until $|x_n-x_{n-1}|$ is less than some tolerance, say $10^{-10}$. The final value of $x_n$ is an approximation of the root $r$. (Note that this technique may fail in some cases, but that is beyond the scope of this course). This is very similar to a previous homework problem.
4. (10 points) Create a method divide that takes a single parameter representing a root $r$ of this polynomial and returns a new polynomial $q(x)$, where $$q(x) = \frac{p(x)}{x-r}.$$ Use Horner's method for synthetic division of $p(x)$ by $(x-r)$ as follows: $$b_n = a_n$$ $$b_{n-1} = a_{n-1} + b_n r$$ $$b_{n-2} = a_{n-2} + b_{n-1} r$$ $$\cdots$$ $$b_{i} = a_{i} + b_{i+1} r$$ $$\cdots$$ $$b_{1} = a_{1} + b_{2} r$$ $$b_{0} = a_{0} + b_{1} r$$ When $r$ is a root, then $b_0$ will equal zero and $$q(x) = b_1 + b_2 x^1 + . . . + b_{n} x^{(n-1)} + 1 x^n$$ This new polynomial $q(x)$ will be returned. Note $q(x)$ has degree $n-1$, which is one less than the degree of $p(x)$.

Hint: Use and array to store the coeeficients, $b_i$, of the polynomial $q(x)$. Store $b_i$ at index $i-1$ in the array representing the coefficients.
5. (10 points extra credit) Create a method roots that returns an array of doubles that contains the roots of this polynomial.
1. Find the largest root, $r$, using the bound M as a starting value.
2. Compute the polynomial $q(x) = p(x)/(x-r)$.
3. Find the roots of this new polynomial by repeating the above two steps.
4. Repeat this procedure $n$ times, where $n$ is the degree of this polynomial.
Finally, return an array containing the roots!

### Email the code to your instructor.

Send me the code as an attachment to an email message. Send the .java file (NOT the .java~ or .class files). Use Homework 9 as the subject for the email.