Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import numpy as np def polym(c, x): # this function evaluates c[0] + c[1]*x + ..

ID: 3603144 • Letter: I

Question

import numpy as np

def polym(c, x):
# this function evaluates c[0] + c[1]*x + ... c[m]*x**m by summing term by term,
# it computes x**i at the i-th iteration (the slowest of all of the three methods)
#
#make sure x is an np array so that it can be scaled (i.e., x*x will be x**2 elementwisely)

  
  
def polym2(c, x):
# this function evaluates c[0] + c[1]*x + ... c[m]*x**m by summing term by term,
# it should not compute x**i directly at each iteration, instead it should update
# x**i as x**(i-1)*x, reusing the x**(i-1) that is already computed at the previous iteration
#
#make sure x is an np array so that it can be scaled (i.e., x*x will be x**2 elementwisely)
#(also, be careful when copying an np array, if not done properly, it would be hard to debug)


  

  
  

def horner(c, x):
# this function evaluates c[0] + c[1]*x + ... c[m]*x**m by the Horner's method.   
#make sure x is an np array so that it can be scaled (i.e., x*x will be x**2 elementwisely)   


  


  
def compare_efficiency():

import time
print(' ==> Now compare the efficiency of the methods: ')
mmin=4000; mmax=20000; step=2000

#initialize lists to store the time for each method
time_poly=[]; time_poly2=[]; time_horner=[]
  
for m in range(mmin, mmax, step):
c = np.random.randn(m)
x = np.random.randn(round(m/4));  
x /= max(abs(x))

#using the above generated x and c,
#add code below to call the three functions above, find the excution time for each,
#and store the time in the corresponding lists


#add code to print out the timing info using the format specified in the project PDF file

  
x= range(mmin, mmax, step)   
#add code below to plot the figure using the format specified in the project PDF file

  
#add code save the plot into a file naed 'poly_eval_compare.png' for submission
  


Any help please.

Prob. 1 Polynomial evaluations: Evaluate the polynomials using three methods: (1) The straightforward summation adding term by term, starting from co That is, you compute at the i-th iteration and add to the sum. (2) Similar to (1), you add term by term, but at the i-th iteration you do not compute as a power term, instead, you compute r as z-1*x, reusing the r- that is already computed at the previous iteration. (3) The Horner's method. Complete the script pi-poly eval.py For the timing comparison, you may need to loop up the lecture slides on how to get timing info test code already did is saved, and print it out. Your output format should match the following 4000 , 6000 , 8000 , m=10000, sm=12000, Gm=14000 , 7m=16000 , m=18000 , len(x)-1000 , len(x)-1500 , len(x)-2000 , len(x)-2500 , len(x)-3000 , len(x)-3500 , len(x)-4000 , len(x)-4500 , polym-time= polym-time= polym-time= polym-time= polym-time= polym-time= polym-time= polym-time= 0.446, 0.992, 1.766 , 2.764, 3.972, 5.390, 7.054, 8.919, polym2-time= polym2-time= polym2-time= polym2-time= polym2-time= polym2-time= polyn2-time= polyn2-time= 0.035, 0.140, 0.185, 0.168, 0.428, 0.503, 0.580, 0.732, horner-time= horner-time= horner-time= horner-time= horner-time= horner-time= horner-time= horner-time= 0.014 0.027 0.036 0.053 0.068 0.089 0.111 0.134 im= 2 m= im= The values of your timing (in seconds) do not need to match the above values, because computers have different CPU speed. But the format, such as indentation and how many digits to print out, should match. Finally, plot the time data in one figure with two subplots. Your final plot (legend, labels, titles etc, and the shape of curves) should be similar to the following figure. poly evaluation time (seconds) term-by-term /horner poly 60 t horner 50 6 40 t poly It honer t_poly2 /t honer t poly/t_poly2 4 30 20 2 10 0 0 5000 5000 10 10000 degree m 15000 000 15000 degree m

Explanation / Answer

# this function evaluates c[0] + c[1]*x + ... c[m]*x**m by summing term by term,
# it computes x**i at the i-th iteration (the slowest of all of the three methods)
#
#make sure x is an np array so that it can be scaled (i.e., x*x will be x**2 elementwisely)

  
  
def polym2(c, x):
# this function evaluates c[0] + c[1]*x + ... c[m]*x**m by summing term by term,
# it should not compute x**i directly at each iteration, instead it should update
# x**i as x**(i-1)*x, reusing the x**(i-1) that is already computed at the previous iteration
#
#make sure x is an np array so that it can be scaled (i.e., x*x will be x**2 elementwisely)
#(also, be careful when copying an np array, if not done properly, it would be hard to debug)