def plot_function(self, interval= [-1,1]): function = self.f domain = np.arange(interval[0],interval[1],.1) ra = [eval(function) for x in domain] self.f = derivative.expand(function) plt.plot(domain,ra) plt.show()
def modified_newton(func,p0,tol = 10e-16,max_iter = 100): func = derivative.expand(func) df = derivative.deriv(func) for i in range(1,max_iter+1): x = p0 - 5*evaluate(func, p0)/float(evaluate(df, p0)) if abs(x - p0)/abs(p0) < tol : return x,i p0 = x
def __init__(self,n=2,number=4): self.max_iter = 1000 self.tol = 10e-16 self.tiny = 10e-10 self.n = n self.number = number function = "x^"+str(n)+str(number*(-1)) self.f = derivative.expand(function) self.function = function+ " = 0" self.native_value = self.number**(1./self.n)
def secant(func,p0 ,p1 ,tol=10e-16 ,max_iter=100 ): func = derivative.expand(func) q0 = evaluate(func, p0) q1 = evaluate(func, p1) for i in range(1,max_iter+1): x = p1 - (q1*(p1-p0))/float((q1 - q0)) if abs(x - p1) < tol: return x p0 = p1 q0 = q1 p1 = x q1 = evaluate(func, x)
def newton_user_function(self,function,guess = 2,iters = False): self.function = function + " = 0" self.native_value = None self.__check_valid(function) self.f = derivative.expand(function) self.n = 1 if iters is False: ans = self.__apply_newton(guess, 0) else: ans = self.__apply_newton(guess, 1) if ans < self.tiny and ans > 0.0: ans = 0.0 return ans
def bisection(start,end,maxIter,tol,mon,function): function = derivative.expand(function) fa = evaluate(start,function) fb = evaluate(end,function) if sign(fa)*sign(fb) > 0: return "f(start) and f(end) are same sign, no root exists!" for i in range(maxIter): x = (start+end) /2 if mon == 1: print "Iteration "+ str(i+1)+": x = "+ str(x) if evaluate(x,function) == 0.0 or abs(evaluate(x,function)) <= tol: return x if evaluate(start,function)*evaluate(x,function) <0: end = x else: start = x return "Failed to find root, here's my guess: "+ str(x)
def falsePos(func,a ,b ,tol=10e-16 ,max_iter=100 ): func = derivative.expand(func) q0 = evaluate(func,a) q1 = evaluate(func,b) p0 = a p1 = b for i in range(1,max_iter+1): x = p1 - (q1*(p1 - p0))/float((q1-q0)) new_point = evaluate(func,x) if abs(x - p1)/abs(x) < tol: return x if new_point*q1 < 0: p0 = p1 q0 = q1 p1 = x q1 = new_point
def bisection(func,a ,b ,tol=10e-16 ,max_iter=100 ): func = derivative.expand(func) start = evaluate(func,a) end = evaluate(func,b) if start*end > 0: return "Bad Interval!" for i in range(1,max_iter+1): x = (a + b)/2. new_point = evaluate(func,x) if abs(new_point) < tol: return x if start*new_point > 0: a = x start= new_point else: b=x
def solve(guess =1,maxIter = 1, tolerance = 10e-16, mon = 0, function = "x"): function = derivative.expand(function) #print function f = evaluate(guess, function) if abs(f) <=tolerance: return guess df = d(guess, function) #print "Derivative: "+df[0] for i in range(maxIter): if np.isnan(guess): return guess try: p = guess - (evaluate(guess, function)/float(d(guess, function)[1])) except: return float('nan') if abs(p - guess) <= tolerance: return guess guess = p if mon ==1: print "Iter "+ str(i+1) + ": guess ="+ str(guess) #print "Prev: "+str(prev) return guess