import numpy as np import math import matplotlib.pyplot as plt # function definition def f(x): y = x**2 + 4*np.cos(x) return y # Ploting the function x^2+4cos(x) x = np.arange(1,2,0.01) y = [0]* len(x) # define an empty array for storing values of y for i in range(0,len(x)): y[i] = f(x[i]) plt.plot(x, y) # Golden section search function def golden_section_search(a,b,ro,r): ''' The number of iterations needed to reach the given range between two values,i.e|a-b|< epsilon (1-ro)^N <= 0.2(final range)/1(given initial range)''' N=math.ceil(np.log10(r/(b-a))/np.log10(1-ro)) arr1 = np.array(["Iteration k","a","b","f(a)","f(b)", "New Uncertainity interval"]) arr = np.empty((N, 6), dtype=object) arr = np.vstack((arr1,arr)) a1 = a + ro*(b-a) b1 = a + (1-ro)*(b-a) for i in range(1,len(arr)): j=0 f1= f(a1); f2 = f(b1) if f1<=f2: b=b1 b1=a1 a1 = a + ro*(b-a) else: a = a1 a1 = b1 b1 = a + (1-ro)*(b-a) arr[i,j] = i ; arr[i,j+1]=a1 ; arr[i,j+2]=b1; arr[i,j+3]=f1; arr[i,j+4]=f2;arr[i,j+5]= str([a,b]) if abs(a-b) <= r: for i in arr: print(*i) a = 1 b = 2 ro = 0.3819 r = 0.2 golden_section_search(a,b,ro,r) #Fibonacci Function def fibonacci(n): f0=0 f1=1 #Initialize an empty array arr = [0]*n arr[0]= 0 arr[1]= 1 for i in range(2,n): arr[i]=arr[i-1]+arr[i-2] return arr[1:] # Fibonacci search Method def fibonacci_search(a,b,r,eps): ''' The number of iterations needed to reach the given range between two values,i.e|a-b|< epsilon (1+2*epsilon)^/F_(n+1) <= 0.2(final range)/1(given initial range)''' N = math.ceil((1+2*eps)/(r/(b-a))) fib = fibonacci(N+1) #Comparing the value of N, in our fibonacci sequence for i in range(0,len(fib)): if fib[i] >= N: N=i-1 #because F_(N+1)>= N, so value of iteration N, will be less than N+1. break print(N,"-iterations are needed to reach within the given range","\n") arr = np.empty((N, 7), dtype=object) ro = 1 - (fib[N]/fib[N+1]) a1 = a + ro*(b-a) b1 = a + (1-ro)*(b-a) for i in range(0,N): j = 0 f1 = f(a1); f2 = f(b1) ro = 1 - (fib[N-i]/fib[N+1-i]) # recalculating the value of rho, for every interval if ro == 0.5: #special case for the last iteration being handled in this if clause. if f1 <= f2: b = b1 b1 = a1 a1 = a + (ro-eps)*(b-a) else: a = a1 a1 = b1 b1 = a + (ro+eps)*(b-a) else: # for rest of the iterations before the last iteration, the following steps are executed. if f1 <= f2: b=b1 b1=a1 a1 = a + ro*(b-a) else: a = a1 a1 = b1 b1 = a + (1-ro)*(b-a) arr[i,j] = i ; arr[i,j+1] = ro; arr[i,j+2] = a1 ; arr[i,j+3] = b1 arr[i,j+4] = f1; arr[i,j+5] = f2; arr[i,j+6]= str([a,b]) if abs(a-b) <= r: arr1 = np.array(["Iter-k"," rho "," a "," b "," f(a) "," f(b) ", " New Uncertainity interval "]) arr = np.vstack((arr1,arr)) for i in arr: print(*i) a=1 b=2 r=0.2 fibonacci_search(a,b,r,eps=0.05) '''Link :https://medium.com/@jamesetaylor/create-a-derivative-calculator-in-python-72ee7bc734a4''' ''' Using the concept of finite difference :https://www.wikiwand.com/en/Finite_difference''' #Create a derivative and second derivative calculator in Python from math import * import numpy as np # Function definition def f(x): return x**2 + 4*np.cos(x) #function to calculate 1st derivative def der1(f,value): h = 0.00001 # substitute for h-> infinity top = f(value+h) - f(value) bottom = h slope = top/bottom #Returns slode to second decimal return float("%.2f"% slope) #function to calculate 2nd derivative def der2(f,value): h = 0.00001 top = f(value + 2*h) - 2*f(value + h) + f(value) bottom = h**2 slope = top/bottom #Returns slode to second decimal return float("%.2f"% slope) # Implementation of Newton method def Newton(x,eps): x0 = x N = 4 # same iterations as part (b) x1 = np.float("{:10.10f}".format(x0 - (der1(f,x0)/der2(f,x0)))) i=1 arr = np.empty((N,5), dtype=object) for i in range(0,4): # since the question asks to implement Newton's method using same num of iteration as part(b) j = 0 arr[i,j] = i ;arr[i,j+1] = x0 ; arr[i,j+2] = x1 ; arr[i,j+3] = der1(f,x0); arr[i,j+4]= der2(f,x0) x0=x1 x1 = np.float("{:10.10f}".format(x0 - (der1(f,x0)/der2(f,x0)))) arr1 = np.array(["Iter-k","x0","x1"," 1st derivative ","2nd Derivative"]) arr = np.vstack((arr1,arr)) for i in arr: print(*i) x = 1 eps = 0.00001 Newton(x,eps=0.00001) print("\n") print("Since we see in the 3rd iteration the 2nd derivative is negative, Newton method is not effective for this function ") print("\n") def fun(x): y = 8*math.exp(1-x) + 7*math.log(x) return y # Ploting the function x^2+4cos(x) x = np.arange(1,2,0.01) y = [0]* len(x) # define an empty array for storing values of y for i in range(0,len(x)): y[i] = fun(x[i]) plt.plot(x, y) # Golden section search function def golden_section_search(a,b,ro,r): ''' The number of iterations needed to reach the given range between two values,i.e|a-b|< epsilon (1-ro)^N <= 0.2(final range)/1(given initial range)''' N=math.ceil(np.log10(r/(b-a))/np.log10(1-ro)) print(N,": Iterations are needed to arrive at the given range") arr1 = np.array(["Iteration k","a","b","f(a)","f(b)", "New Uncertainity interval"]) arr = np.empty((N, 6), dtype=object) arr = np.vstack((arr1,arr)) a1 = a + ro*(b-a) b1 = a + (1-ro)*(b-a) for i in range(1,len(arr)): j=0 f1= fun(a1); f2 = fun(b1) if f1<=f2: b=b1 b1=a1 a1 = a + ro*(b-a) else: a = a1 a1 = b1 b1 = a + (1-ro)*(b-a) arr[i,j] = i ; arr[i,j+1]=a1 ; arr[i,j+2]=b1; arr[i,j+3]=f1; arr[i,j+4]=f2;arr[i,j+5]= str([a,b]) if abs(a-b) <= r: for i in arr: print(*i) a = 1 b = 2 ro = 0.3819 r = 0.23 golden_section_search(a,b,ro,r) # Fibonacci search Method def fibonacci_search_2(a,b,r,eps): ''' The number of iterations needed to reach the given range between two values,i.e|a-b|< epsilon (1+2*epsilon)^/F_(n+1) <= 0.23(final range)/1(given initial range)''' N = math.ceil((1+2*eps)/(r/(b-a))) fib = fibonacci(N+1) #Comparing the value of N, in our fibonacci sequence for i in range(0,len(fib)): if fib[i] >= N: N=i-1 #because F_(N+1)>= N, so value of iteration N, will be less than N+1. break print(N,"-iterations are needed to reach within the given range","\n") arr = np.empty((N, 7), dtype=object) ro = 1 - (fib[N]/fib[N+1]) a1 = a + ro*(b-a) b1 = a + (1-ro)*(b-a) for i in range(0,N): j = 0 f1 = fun(a1); f2 = fun(b1) ro = 1 - (fib[N-i]/fib[N+1-i]) # recalculating the value of rho, for every interval if ro == 0.5: #special case for the last iteration being handled in this if clause. if f1 <= f2: b = b1 b1 = a1 a1 = a + (ro-eps)*(b-a) else: a = a1 a1 = b1 b1 = a + (ro+eps)*(b-a) else: # for rest of the iterations before the last iteration, the following steps are executed. if f1 <= f2: b=b1 b1=a1 a1 = a + ro*(b-a) else: a = a1 a1 = b1 b1 = a + (1-ro)*(b-a) arr[i,j] = i ; arr[i,j+1] = ro; arr[i,j+2] = a1 ; arr[i,j+3] = b1 arr[i,j+4] = f1; arr[i,j+5] = f2; arr[i,j+6]= str([a,b]) if abs(a-b) <= r: arr1 = np.array(["Iter-k"," rho "," a "," b "," f(a) "," f(b) ", " New Uncertainity interval "]) arr = np.vstack((arr1,arr)) for i in arr: print(*i) a=1 b=2 r=0.24 fibonacci_search_2(a,b,r,eps=0.05)