import opt_utils #defining function f(t) - g(t) and derivative function f'(t) - g'(t) def fct(t): return (1.1 * (t - 2.5)**2) - (5 * t + 2.5) def df_dt(t): return 2.2 * (t - 2.5) - 5 #applying newton's method to find crossover points for t in range(-10, 10): opt_utils.my_Newton(fct, df_dt, t, tol=1e-4, N=20) t += 1 #needed outputs of my_Newton t_1 = 0.4366 t_2 = 9.1088 #function values for t_1 and t_2 f_t1 = (1.1 * (t_1 - 2.5)**2) g_t1 = (5 * t_1 + 2.5) f_t2 = (1.1 * (t_2 - 2.5)**2) g_t2 = (5 * t_2 + 2.5) print f_t1, g_t1, f_t2, g_t2 #~~~~~~~~~~ANSWERS TO A AND B~~~~~~~~~~~~
tn = float(t0) eps = 1e-5 N = 20 i = 0 while abs(fct_h_t( tn, c, A, t0)) > eps and i < N: t_next = tn - fct_h_t( tn, c, A, t0)/dh_dt(tn, c, A, t0) print(1, 'fct_value', abs(fct_h_t(tn, c, A, t0)), t_next) tn = t_next i += 1 if abs(fct_h_t( tn, c, A, t0)) < eps: return t_next else: return np.nan ''' t_root = opt_utils.my_Newton(fct_h_t, dh_dt, 5) t_root2 = opt_utils.my_Newton(fct_h_t, dh_dt, 0) a_t = np.linspace(tmin, tmax) a_ft = fct_f_t(a_t, c, t0) a_gt = fct_g_t(a_t, A, t0) a_ht = fct_h_t(a_t) plt.plot(a_t, fct_f_t(a_t, c, t0), 'r', label='f(t)') plt.plot(a_t, fct_g_t(a_t, A, t0), 'b', label='g(t)') plt.plot([t_root], [fct_f_t(t_root, c, t0)], 'r*', ms=10) plt.plot([t_root2], [fct_g_t(t_root2, A, t0)], 'r*', ms=10) # plt.plot( a_t, fct_h_t(a_t, c, A, t0), 'g', label = 'h(t)') #plt.figure(2) #plt.plot( a_ht, fct_h_t(a_t), 'k-')
""" import opt_utils import numpy as np import matplotlib.pyplot as plt def fct(t): return (1.1 (t - 2.5)**2) - (5 * t + 2.5) #defining the t function def df_dt(t): #defining the derivative of the t function return 2.2 * (t - 2.5) - 5 for t in range(-10, 10): #range bounds opt_utils.my_Newton(fct, df_dt, t, tol=1e4, N=20) #N is number of max trials t = +1 t_1 = 0.4366 t_2 = 9.1088 f_t1 = (1.1 (t_1 - 2.5)**2) - (5 * t_1 + 2.5 ) #plugs in first value into the f function f_t2 = 2.2 * (t_1 - 2.5) - 5 #intersections = f_func(t)- g_func(t) = 0 #set t min to -10 #set t max to 10
return (1.1 * (t - 2.5)**2) - (5 * t + 2.5) def f_minus_g_dt(t): return 2.2 * (t - 2.5) - 5 #============================================================================= #calculations #============================================================================= # from drawing f(t) and g(t) on paper, I expect two intersections, one at around # 1 and the other at around 9. This is also expected since f(t) is a downward # parabola and g(t) is positive linear intersect_1 = ou.my_Newton(f_minus_g, f_minus_g_dt, 1) intersect_2 = ou.my_Newton(f_minus_g, f_minus_g_dt, 9) #============================================================================= # printing and plotting answers for parts a - c #============================================================================= print( "NOTE: I did edit opt_utils and the 2_1 file a bit, so I attached them alongside with the homework." ) print("answers:") print("a.)") print("there are two intersections") print("b.)") print('intersection times, t: ', intersect_1, 'and ', intersect_2)
guesses based off of where they might me. ''' plt.figure(1) plt.plot(t, f(t)) plt.plot(t, g(t)) plt.show() #---------------------------------------------------------------------------- # Find crossover points (part B) #---------------------------------------------------------------------------- #Using Newton's method function from opt_utils find the two crossover points #Root one found with a guess closer to the lower bound Root1 = opt_utils.my_Newton(F, dFdt, -10, tol=1e-4, N=20) #Root two found with a guess closer to the upper bound Root2 = opt_utils.my_Newton(F, dFdt, 10, tol=1e-4, N=20) print "t at crossover 1 =", str(Root1), "f(t) at crossover 1 =", f( Root1), "g(t) at crossover 1 =", g(Root1) print "t at crossover 2 =", str(Root2), "f(t) at crossover 2 =", f( Root2), "g(t) at crossover 2 =", g(Root2) #---------------------------------------------------------------------------- # Compare with previous assignment (Part C) #---------------------------------------------------------------------------- Cross1 = 0.43521761 #crossover taken from the week 2 in class assignment # plot showing the two functions,
return c*(t-t0)**2 def dfdt(t): return 2*c*(t-t0) def g(t): return A*t+t0 def dgdt(t): return A def h(t): return f(t) - g(t) def dhdt(t): return dfdt(t) - dgdt(t) #=================================================================================== # find roots #=================================================================================== t_root = opt_utils.my_Newton( h, dhdt, t0) t_root1 = opt_utils.my_Newton( h, dhdt, t1) print ('First t value', t_root, 'First f(t) and g(t) value', f(t_root)) print ('Second t value', t_root1, 'Second f(t) and g(t) value', f(t_root1)) # values for t, g(t), and f(t) of both crossover points. Note that g(t) and fIt) should be the same #=================================================================================== # plots #=================================================================================== a_t = np.linspace(tmin, tmax, 1000) # x axis plt.figure(1) plt.plot(a_t, f(a_t), 'k-') plt.plot(a_t, g(a_t), 'b-') # There are two crossover points plt.plot( [t_root], [f(t_root)], 'r*' , ms = 14) # first crossover point
#f(t) - g(t) where f(t) = 1.1*(t - 2.5)**2 and g(t) = 5*t + 2.5 def fct(t): return (1.1 * (t - 2.5)**2) - (5 * t + 2.5) #f'(t) - g'(t) where f'(t) = 2.2*(t-2.5) and g'(t) = 5 def df_dt(t): return 2.2 * (t - 2.5) - 5 ax = np.array([]) #where ax is array of cross over points. for t in range(-10, 10): x = opt.my_Newton(fct, df_dt, t, tol=1e-4, N=20) ax = np.append(ax, x) t += 1 #get the cross over points of f(x) and g(x) ax_round = np.around(ax, 4) values = np.unique(ax_round) #number of cross over points numOFX = np.size(values) print("A) Number of Cross over points: ", numOFX) print('B) + C)') for r in range(0, numOFX): print('value of t at x-point', r + 1, ':', values[r]) print('value of f(x) at X-point', r + 1, ':', f_t(values[r]))
#derivative of g(t) def dg_t(x): return (5) #f(t) - g(t) def fg_t(x): return((1.1*(x-2.5)**2)-(5 * x + 2.5)) #f'(t) - g'(t) def dfg_t(x): return(2.2*(x-2.5) - 5) #------------------------------------------------------------------------------ # use newtons' method thru opt_utils #------------------------------------------------------------------------------ root1 = opt_utils.my_Newton(fg_t, dfg_t, 50) print('There is a cross-over at time: ',root1) root2 = opt_utils.my_Newton(fg_t, dfg_t, 1) print('There is a cross-over at time: ',root2) #------------------------------------------------------------------------------ # plot #------------------------------------------------------------------------------ plt.plot(time, fg_t(time), 'ro' , ms = 2) plt.plot([root1, 0],[root2,0], 'ko', ms = 3) plt.xlabel('Time') plt.ylabel('Function') plt.grid(True) plt.show()
plt.figure() plt.plot(t, f(t)) plt.plot(t, g(t)) plt.xlim(-10, 10) plt.show() """ From this graph, you can see that the two functions intersect twice on the interval from (-10, 10). """ # b) # find first intersection point newton = opt_utils.my_Newton(F, dFdt, -10, tol=1e-4, N=20) print "The crossover point is at x =", str(newton), "y =", str(f(newton)) # find second intersection point newton2 = opt_utils.my_Newton(F, dFdt, 10, tol=1e-4, N=20) print "The crossover point is at x =", str(newton2), "y =", str(f(newton2)) # c) """ The cross value is taken from the week 2 in-class assignment """ cross = 0.43521761
#=================================================================================== # computation #=================================================================================== ''' #from sympy import Symbol t = Symbol ('x') Ft_dt = sym.diff( F(t), t) #derive the function print Ft_dt ''' def Ftdt(t): return 2.2 * t - 10.5 print Ftdt(t) CO1 = opt.my_Newton(F, Ftdt, 0) #find cross over points using Newton method CO2 = opt.my_Newton(F, Ftdt, 9) print CO1 #first crossover point at approx (0.4366, 4.375) print CO2 # second crossover at approx (9.1088, ) plt.plot(t, f(t), 'r', label="f(t)") #plotting f(t) and g(t) show 2 crossover points plt.plot(t, g(t), 'k', label="g(t)") #plt.plot(t, F(t), 'b', label = "F(t)") plt.legend()
t_0 = 2.5 A = 5. Ans = 2*c*(t-t_0) - A return Ans # ============================================================================= # Define variables # ============================================================================= t0 = 2.6 # Guess for where the root is t = np.arange(-10, 11, 1) # print(t) # Unit testing t_root = opt.my_Newton(diff, ddiffdt, t0) for i in np.arange(0,21,1): if t_root == t[i]: print t_root if i < 20: i += 1 else: break # For some reason, even though I'm setting it to a variable, the my_Newton # function is still printing variables. I'm assuming this is causing # the for loop I created to not work, as the code "print t_root" simply isn't # doing anything # From here I would see how many numbers are printed, allowing me to solve
def g_t( t, t0, A): return A*t + t0 def fg_t (t): c = 1.1 A = 5 return c*t**2 - (A+2*c*t0)*t + c*t0**2 - t0 def dfgdt(t): c = 1.1 A = 5 return 2*c*t - A -2*c cross = [] for i in troot: troot = opt_utils.my_Newton( fg_t, dfgdt, i) print("-----------\nf(t) = %s and g(t) = %s at t = %s\n-----------"%(f_t(troot, t0, c), g_t(troot, t0, A), troot)) cross.append(troot) ##########in class assignment #============================================================================== # Parameters #============================================================================== tmin,tmax = -10,10 iN = 1000 f_dt = float(tmax-tmin)/iN #function parrameters
return 2 * c * t - 2 * c * t0 def dgdt(A): return A def minus(t): return ft(t) - gt(t) def dminus(t): return dfdt(t) - dgdt(A) root1 = opt_utils.my_Newton(minus, dminus, -10, tol=1e-4, N=20) root2 = opt_utils.my_Newton(minus, dminus, 10, tol=1e-4, N=20) #opt_utils #========================================================================= # Plots #========================================================================= plt.figure() plt.plot(t, ft(t), 'r-') plt.plot(t, gt(t), 'b') #k- is a black line. The other letters are just the colors plt.plot([root1], [ft(root1)], 'r*', ms=20) plt.plot([root2], [ft(root2)], 'b*', ms=20) # ms is the size of the stars
def dg_t(t): return 5 def f_minus_g(t): return f_t(t) - g_t(t) def df_minus_g(t): return df_t(t) - dg_t(t) #T = np.linspace(-10, 10, 100) i tried using linspace to plot this for t in range (-10, 0): ou.my_Newton(f_minus_g, df_minus_g, t, tol = 1e-4, N = 10) print('****************Intersections occer at time above****************') #nextguess = guess - f_t(guess)/df_t(guess) #print(nextguess) #guess = nextguess for t in range (0, 10): ou.my_Newton(f_minus_g, df_minus_g, t, tol = 1e-4, N = 10) print('*************Intersections also occer at time above*************') #there are two zero values in these two functions #at t = 0.436 and at t = 9.108 #now the values of f_t and g_t is
def g_t(x): return A * x + t0 def fandgfunction(x): return c * (x - t0)**2 - (A * x + t0) a_fandg = fandgfunction(time_array) def fandg_derivative(x): return 2 * c * (x - t0) - 2 * A x_roots = op.my_Newton(fandgfunction, fandg_derivative, x0) a_x = np.linspace(xmin, xmax, 1000) plt.figure(1) plt.plot(time_array, a_fandg, 'k--', label="f(t) - g(t) Newton's Method") plt.legend(loc="upper right") plt.plot([x_roots], [0], 'ko') plt.plot() plt.grid(True) plt.xlabel('x') plt.ylabel('f(x)') plt.show() #only one cross over point in the between x = -10 and x = 10 print("t root: ", x_roots)
return 2 * c * (t - t0) def dg_dt(t): return (A) def h_t(t): # f_t - g_t return ((c * (t - t0)**2) - (A * t + t0)) def dfct_dt(t): return (df_dt(t) - dg_dt(t)) t_minroot = opt_utils.my_Newton(h_t, dfct_dt, -10, tol=1e-5, N=50) t_maxroot = opt_utils.my_Newton(h_t, dfct_dt, 10, tol=1e-5, N=50) print('The Functions intersect at the value t= [%.3f, %.3f]') % (t_minroot, t_maxroot) plt.plot(t, f_t(t), 'k-', ms=2) plt.plot(t, g_t(t), 'b-', ms=2) #plt.plot( t, h_t(t), 'go', ms=2) plt.plot([t_minroot], [h_t(t_minroot)], 'r*', ms=14) plt.plot([t_maxroot], [h_t(t_maxroot)], 'b*', ms=10) plt.plot([t_min, t_max], [0, 0], 'r--') plt.grid(True) plt.xlabel('x') plt.ylabel('Fct values f(x)') plt.show()
def f_t(t): return 1.1 * (t - 2.5)**2 - (5 * t + 2.5) def g_t(t): return 5 * t + 2.5 def df_dt(t): return 2 * 1.1 * (t - 2.5) - 5 for t in range(-10, 10): root = opt_utils.my_Newton(f_t, df_dt, t, tol=1e-4, N=20) t += 1 #2 crossover points based on my_Newton output t1 = 0.4366 t2 = 9.108 f_t1 = 1.1 * (.4366 - 2.5)**2 f_t2 = 1.1 * (9.108 - 2.5)**2 print 'Crossover Points: ' print 't1,f(t1),g(t1): ' + str(t1) + ' ' + str(f_t1) + ' ' + str(g_t(t1)) print 't2,f(t2),g(t2): ' + str(t2) + ' ' + str(f_t2) + ' ' + str(g_t(t2)) #--------------------------------------------------------------- # PART C