1. Other
  2. Other
  3. in question 2 you will write code to find a...

Question: in question 2 you will write code to find a...

Question details

In Question 2 you will write code to find a quartic (i.e. degree-4) polynomial p(x)=ax4+bx3+cx2+dx+ewith a prescribed behaviour. To be more specific, your code will need to determine the coefficients a, b, c, d, and e, using 5 pairs of the form (p(x),x). Your input: The scaffold code is set up to ask you to input a number, specifying which file to open (1, or 2, representing either matrix1.in or matrix2.in). It will read the contents of this file into a 2×5 matrix M=[p(x1)x1p(x2)x2p(x3)x3p(x4)x4p(x5)x5].You may assume that the entries in the second row of M (i.e. the given values of x) are all distinct, so there is a unique polynomial to be found. (See the end of these instructions for a specific example of the input, involving actual numbers.) Your task: Use the information contained in the matrix M to find a, b, c, d, and e. You must delete lines 20 to 24 of the scaffold code (where each of these variables is set to 0) and replace them with your own code. Expected output: The bottom portion of the scaffold code is set up to print the polynomial p(x)=ax4+bx3+cx2+dx+e.In order for the output of your program to be correct, you must correctly set each of the variables a, b, c, d, and e. You must set a to be the coefficient of x4, rather than setting it to be the constant term or any other coefficient. Similarly, you must use the correct variable names for each of the other coefficients, otherwise the scaffold code will not print the correct output. Please note that you must not write code that displays any output other than the output produced by the scaffold code, as described above. Recall that a semicolon (;) at the end of a line suppresses any output from that line. Testing your code: You should test your code using both of the input files matrix1.in and matrix2.in (by entering 1 and 2, respectively, when running your program). When the polynomial is printed, you should examine the contents of the input files and check that substituting each of the five values of x into the polynomial gives the correct corresponding value of p(x). Submitting your code: When you submit your code, it will be tested on the (default contents of the) input files matrix1.in and matrix2.in, and then on one additional non-hidden test input, and then on two hidden test inputs. Note that if you fail one of the tests, the testing process will stop and your code will not be tested on the remaining tests. You have completed the question when all of the tests have passed. Example of the input: The matrix M given in the matrix1.in file is M=[1−20−1−102152].Since the second row of M represents values of x, and the first row represents the corresponding values of p(x), we deduce that p(−2)=1, p(−1)=0, p(0)=−1, p(1)=2, and p(2)=5. We now must find a,b,c,d,e∈R such that the polynomial p(x)=ax4+bx3+cx2+dx+e satisfies all 5 of these polynomial evaluations obtained from the matrix M. Hint: If you are finding it difficult to come up with a valid algorithm to use to solve this problem, you should first try solving a simpler problem by hand (involving, say, a cubic or quadratic equation), and then try to generalise the algorithm. For example, suppose that p(x)=ax2+bx+c and we know that p(−1)=0, p(0)=−1, and p(1)=2. Try to show that a=2, b=1, and c=−1. Now think about how can you generalise this process so that it works for a degree-4 polynomial

Matrix 1

1 0 -1 2 5

-2 -1 0 1 2

Matrix 2




Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution