# -*- coding: utf-8 -*-
"""
Created on Tue Aug 16 15:05:18 2022

@author: 102194
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import mysearch as mys

def f(x,y):
    
    f = (x-1)**2 + (y)**2
    
    return f

def fline(t):

    x = xn + t*pn
    
    fline = f(x[0],x[1])
    return fline
    
x = np.linspace(-2,2,100)
y = np.linspace(-1,1,100)

X,Y = np.meshgrid(x,y)
#plt.pcolor(X,Y,f(X,Y))
#plt.contour(X,Y,f(X,Y),100)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, f(X,Y))

xn = np.array([-2,-1])
pn = np.array([1,0])

h = 0.2
for i in range(0,20):
    t = h*i
    
    x = xn + t*pn
    
    ax.plot(x[0],x[1],f(x[0],x[1]),'ro')
    
plt.show()


t0 = 0
t1 = 4
tol = 1e-5
blist = mys.minbracket(fline,t0,t1,20)

for i in range(len(blist)):
    tmin = mys.golden(fline,blist[i][0],blist[i][1],tol)
    xmin = xn + pn*tmin
    print(xmin)
    
    
    




