A piece of wire 100 cm in length is bent into the shape of a sector of a circle. Find the maximum value that the area A of the sector can have.
In [4]:
from math import sqrt, pi
In [18]:
# https://en.wikipedia.org/wiki/Golden-section_search
gr = (sqrt(5) + 1) / 2
def gss(f, a, b, c, tau = 1e-6):
'''
Python recursive version of Golden Section Search algorithm.
This code appears to be broken - see the talk page.
tau is the tolerance for the minimal value of function f
b is any number between the interval a and c
'''
if b < c:
x = b + (2 - gr) * (c - b)
else:
x = b - (2 - gr) * (b - a)
if abs(c - a) < tau * (abs(b) + abs(x)):
return (c + a) / 2
if f(x) < f(b):
return gss(f, b, x, c, tau) if b < c else gss(f, a, x, b, tau)
else:
return gss(f, a, b, x, tau) if b < c else gss(f, x, b, c, tau)
In [19]:
L = 100
In [20]:
def f(x):
r = L /(2 + (2*pi*x)/360)
area = (x/360) * pi * r **2
return area
In [21]:
gss(f, 1, 90, 179)
Out[21]: