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

CHOOSE ONLY 1! 4A or 4B, up to you. We are programming in python. We are able to

ID: 3872690 • Letter: C

Question

CHOOSE ONLY 1! 4A or 4B, up to you.

We are programming in python. We are able to use numpy and scipy

CHOOSE ONLY ONE 4a) Consider f(x)-(1-x)/x, which has a root at x-1. Write a code to find the root using the secant method with xo=01 and X1-10. Comment on the convergence. Try some other starting intervals. Consider the function glx)-(x-a)/(bx-c), where a,b, and c are free parameters. Use your 3 data points (x i,f(x_i) to construct an approximation for f and use it to numerically determine the root 4b) Let a root finding method be given by: Xn+1-Xn +d , where d is a positive integer and fíx) is a smooth function, and (g)d) means d derivatives of the function g. Give a proof of the convergence rate of the iteration near a simple root r (f(r) 0). Code the method up for d-2 efficiently and demonstrate the convergence rate for a given test function(you pick, no trivial cases).

Explanation / Answer

from math import *

def scrub(f):

nf=''

nums='0123456789'

  for i in range(len(f)):

nf += f[i]

  if f[i] in nums:

  if i+1 < len(f):

  if f[i-1] != '.' and f[i+1] != '.' and f[i-1] not in nums and f[i+1] not in nums:

nf += '.0'

  elif i+1 == len(f):

nf +='.0'

  return nf

def funct(f):

f=scrub(f)

  def aval(x):

  try:

x=float(x)

  return eval(f)

  except:

  return f

  return aval

def newton(f,d,n,guess,exact=None):

F=funct(f)

D=funct(d)

p=[guess]

  for i in range(1,n+1):

prv = p[i-1]

p.append(prv-F(prv)/D(prv))

  if not exact==None: exact=(len(p)-1)*[exact]

  else: exact=(len(p)-1)*[p[len(p)-1]]

error = [abs(p_i-exact_i) for p_i,exact_i in zip(p,exact)]

  return [p,exact,error]

#==============================================================================

f='x**2-329'

d='2*x'

n=16

guess=2

exact=sqrt(329)

[p,exact,error]=newton(f,d,n,guess,exact)

import matplotlib

matplotlib.use("TKAgg")

import matplotlib.pyplot as plt

import numpy as np

plt.ion()

plt.plot(p,'r')  

plt.plot(exact,'y')

#plt.plot(np.log(error),'b--')

#