# -*- coding: utf-8 -*-
"""
Created on Wed Aug 10 17:54:01 2022

@author: Chris
Lab 3 Q3
"""
import numpy as np
import matplotlib.pyplot as plt
import math as maths

def f(x):
    f = x*np.cos(x)+x*np.sin(x/2)
    #f = x*np.sin(x**2)+x**2
    return f

phi = (1 + 5**0.5) / 2




#initial bracketing points
a = -5
b = 5

x = np.linspace(a,b,200)
plt.plot(x,f(x))

#a = 2
#b = 4

tol = 1e-5

while np.abs(b-a)>tol:
    
    #pick two new points:
    c = b - (b-a)/phi
    d = a + (b-a)/phi
    
    if f(d)<f(c):
        a = c      
    else:
        b = d
        
print("minimum found at",c)        







