Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me debug my code main.py # -*- coding: utf-8 -*- import scipy as sp import matplotlib.pyplot as plt import module_functions as fn from

Please help me debug my code

main.py

# -*- coding: utf-8 -*- import scipy as sp import matplotlib.pyplot as plt import module_functions as fn from module_finiteDiff import finiteDiff as fd from module_newton import newton from module_binChop import binChop

# Finite difference

example = fd() example.set_function(fn.myfunc1) f0 = example.deriv(0.37,0) f1 = example.deriv(0.37,1) f2 = example.deriv(0.37,2)

print(" 0th derivative: {}".format(f0)) print("1st derivative: {}".format(f1)) print("2nd derivative: {} ".format(f2))

# Newton's method

solNewton = newton() solNewton.set_step(0.01) solNewton.set_function(fn.myfunc1) solNewton.set_tolerance(1.0e-8) solNewton.set_stop(100) solNewton.set_guess(0.5) x = solNewton.solution() print("Newton's method Solution: {}".format(x)) print("Check solution: f(x) = {} ".format(fn.myfunc1(x)))

solNewton.set_function(fn.myfunc2) x = solNewton.solution() print("Newton's method Solution: {}".format(x)) print("Check solution: f(x) = {} ".format(fn.myfunc2(x)))

# Binary chop

solBinChop = binChop() solBinChop.set_function(fn.myfunc1) solBinChop.set_tolerance(1.0e-8) solBinChop.set_stop(100) solBinChop.set_guess(0.5) x = solBinChop.solution() print("Bindary chop Solution: {}".format(x)) print("Check solution: f(x) = {} ".format(fn.myfunc1(x)))

# Calculate function values

dx = 0.01 xMin = -2.0 xMax = 2.0 n = round((xMax - xMin) / dx) x = sp.linspace(xMin,xMax,n+1) f = sp.empty(n+1) xline = [xMin, xMax] yline = [0, 0] for i in range(0,n+1): f[i] = fn.myfunc1(x[i]) # print("i = {:3d} x = {:5.2f} f = {:9.4f}".format(i,x[i],f[i]))

# Plot function

plt.plot(x, f, color = 'blue') plt.plot(xline,yline, color = 'black') plt.xlabel('x') plt.ylabel('f(x)') plt.xlim(-2,2) plt.ylim(-1.5,1.5) plt.title('Nonlinear Function') plt.show()

module basicSolver.py

# -*- coding: utf-8 -*- import scipy as sp import matplotlib.pyplot as plt import module_functions as fn from module_finiteDiff import finiteDiff as fd from module_newton import newton from module_binChop import binChop

# Finite difference

example = fd() example.set_function(fn.myfunc1) f0 = example.deriv(0.37,0) f1 = example.deriv(0.37,1) f2 = example.deriv(0.37,2)

print(" 0th derivative: {}".format(f0)) print("1st derivative: {}".format(f1)) print("2nd derivative: {} ".format(f2))

# Newton's method

solNewton = newton() solNewton.set_step(0.01) solNewton.set_function(fn.myfunc1) solNewton.set_tolerance(1.0e-8) solNewton.set_stop(100) solNewton.set_guess(0.5) x = solNewton.solution() print("Newton's method Solution: {}".format(x)) print("Check solution: f(x) = {} ".format(fn.myfunc1(x)))

solNewton.set_function(fn.myfunc2) x = solNewton.solution() print("Newton's method Solution: {}".format(x)) print("Check solution: f(x) = {} ".format(fn.myfunc2(x)))

# Binary chop

solBinChop = binChop() solBinChop.set_function(fn.myfunc1) solBinChop.set_tolerance(1.0e-8) solBinChop.set_stop(100) solBinChop.set_guess(0.5) x = solBinChop.solution() print("Bindary chop Solution: {}".format(x)) print("Check solution: f(x) = {} ".format(fn.myfunc1(x)))

# Calculate function values

dx = 0.01 xMin = -2.0 xMax = 2.0 n = round((xMax - xMin) / dx) x = sp.linspace(xMin,xMax,n+1) f = sp.empty(n+1) xline = [xMin, xMax] yline = [0, 0] for i in range(0,n+1): f[i] = fn.myfunc1(x[i]) # print("i = {:3d} x = {:5.2f} f = {:9.4f}".format(i,x[i],f[i]))

# Plot function

plt.plot(x, f, color = 'blue') plt.plot(xline,yline, color = 'black') plt.xlabel('x') plt.ylabel('f(x)') plt.xlim(-2,2) plt.ylim(-1.5,1.5) plt.title('Nonlinear Function') plt.show()

module finiteDiff.py

# -*- coding: utf-8 -*- #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ class finiteDiff: #============================================================================== # Class Attributes __default_h = 0.01 #------------------------------------------------------------------------------ def __default_func(x): return x #============================================================================== # Initializer / Instance Attributes def __init__(self): self.__h = finiteDiff.__default_h self.__func = finiteDiff.__default_func #============================================================================== # Instance Set/get methods #------------------------------------------------------------------------------ def set_function(self,arg_func): self.__func = arg_func #------------------------------------------------------------------------------ def get_function(self): return self.__func #------------------------------------------------------------------------------ def set_step(self,arg_h): self.__h = arg_h #------------------------------------------------------------------------------ def get_step(self): return self.__h #============================================================================== # Instance methods #------------------------------------------------------------------------------ def deriv(self, arg_x, arg_order): x = arg_x h = self.__h f = self.__func if arg_order == 0: value = f(x) elif arg_order == 1: value = (f(x + h) - f(x - h)) / (2.0 * h) elif arg_order == 2: value = (f(x + h) - 2.0 * f(x) + f(x - h)) / h**2 else: string = "Error in : [arg_order] = {} out of range" print(string.format(arg_order)) value = 0.0 return value #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

module functions.py

# -*- coding: utf-8 -*- """

# -*- coding: utf-8 -*- import math #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def myfunc1(x): pi = math.pi if x < -1.5: z = -1.5 elif x < 1.5: z = x else: z = 1.5 f = math.sin((pi * z)**2) return f #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def myfunc2(x): return (x - 1.0) * (x - 2.0) * (x - 3.0) #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

module newton.py

# -*- coding: utf-8 -*- """ Created on Wed Mar 3 15:07:21 2021

@author: user """

# -*- coding: utf-8 -*- from module_basicSolver import basicSolver #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ class newton(basicSolver): #============================================================================== # Initializer / Instance Attributes def __init__(self): basicSolver.__init__(self) #============================================================================== # Instance methods #------------------------------------------------------------------------------ def solution(self): N = self.get_stop() tol = self.get_tolerance() xOld = self.get_guess() # xNew = xOld # Default line just so this function has a retunr value # # ..........Start code snippet # for count in range(1,N+1): fx = self.deriv(xOld,0) fpx = self.deriv(xOld,1) xNew = xOld - fx / fpx if abs(xNew - xOld) < tol: break else: xOld = xNew

# # ..........End code snippet # return xNew #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

module binChop.py

# -*- coding: utf-8 -*- """ Created on Wed Mar 3 15:20:36 2021

@author: user """

# -*- coding: utf-8 -*- from module_basicSolver import basicSolver #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ class binChop(basicSolver): #============================================================================== # Class Attributes __default_step = 0.1 #============================================================================== # Initializer / Instance Attributes def __init__(self): basicSolver.__init__(self) #============================================================================== # Instance methods #------------------------------------------------------------------------------ def solution(self): a = self.get_guess() N = self.get_stop() f = self.deriv(a,0) step = binChop.__default_step

# Bracket root # ..........Fill in code

given1 = 0.1 given2 = 0.1 for i in range(1,N+1): given1 = given1 * 2 given2 = given2 / 2 if self.deriv( a + given1,0) * self.deriv(a,0) < 0: b = a + given1 break elif self.deriv( a + given1,0) * self.deriv(a,0) < 0: b = a - given1 break elif self.deriv( a + given2,0) * self.deriv(a,0) < 0: b = a + given2 break elif value(a - given2) * value(a) < 0: b = a - given2 break

# Binary chop # ..........Fill in code #

f = 0.5 return f #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions

Question

Describe the factors influencing of performance appraisal.

Answered: 1 week ago

Question

Identify conflict triggers in yourself and others

Answered: 1 week ago