# Chapter - 13 : One-Dimensional Unconstrained Optimization¶

## Ex:13.1 Pg: 356¶

In [6]:
from math import sin
#f(x) = 2sinx - x**2/10
xl=[]
xu=[]
xl.append(0)
xu.append(4)
d=[]
x1=[]
x2=[]
m=[]
n=[]
for i in range(0,10):
d.append(((5)**(0.5) - 1)*(xu[i-1] - xl[i-1])/2)
x1.append(xl[i-1] + d[i-1])
x2.append(xu[i-1] - d[i-1])
m.append(2*sin(x1[i-1]) - (x1[i-1]**2)/10)
n.append(2*sin(x2[i-1]) - (x2[i-1]**2)/10)
if n[i-1] > m[i-1]:
xu.append(x1[(i-1)])
xl.append(xl[(i-1)])
else:
xl.append(x2[i-1])
xu.append(xu[i-1])

print "xl =",xl
print "\nx2 =",x2
print "\nx1 =",x1
print "\nxu =",xu

xl = [0, 0, 0, 0, 0, 0.9442719099991588, 0.9442719099991588, 0.9442719099991588, 0.9442719099991588, 0.9442719099991588, 0.9442719099991588]

x2 = [1.5278640450004204, 1.5278640450004204, 0.0, 0.9442719099991588, 0.9442719099991588, 0.9442719099991588, 0.9442719099991588, 1.5278640450004206, 1.5278640450004206, 1.5278640450004206]

x1 = [2.4721359549995796, 2.4721359549995796, 2.4721359549995796, 1.5278640450004208, 1.5278640450004208, 1.5278640450004208, 2.4721359549995796, 1.8885438199983178, 1.8885438199983178, 1.8885438199983178]

xu = [4, 2.4721359549995796, 2.4721359549995796, 2.4721359549995796, 2.4721359549995796, 2.4721359549995796, 2.4721359549995796, 2.4721359549995796, 2.4721359549995796, 1.8885438199983178, 1.8885438199983178]


## Ex:13.2 Pg: 360¶

In [21]:
from math import sin
#f(x) = 2sinx - x**2/10
x0= [0]#
x1= [1]
x2= [4]#
m=[];n=[];r=[];x3=[];s=[]
for i in range(0,6):
m.append(2*sin(x0[(i)]) - (x0[(i)]**2)/10)
n.append(2*sin(x1[(i)]) - (x1[(i)]**2)/10)
r.append(2*sin(x2[(i)]) - (x2[(i)]**2)/10)
x3.append(((m[(i)]*(x1[(i)]** 2 -x2[(i)] ** 2)) + (n[(i)]*(x2[(i)] ** 2 -x0[(i)] ** 2)) + (r[(i)]*(x0[(i)] ** 2 -x1[(i)] ** 2)))/((2*m[(i)]*(x1[(i)] -x2[(i)]))+(2*n[(i)]*(x2[(i)] -x0[(i)]))+(2*r[(i)]*(x0[(i)] -x1[(i)]))))
s.append(2*sin(x3[(i)]) - (x3[(i)]**2)/10)
if x1[(i) ]> x3[(i) ]:
if n[(i)]<s[(i)]:
x0.append(x0[(i)])
x1.append(x3[(i)])
x2.append(x1[(i)])
else:
x0.append(x1[(i)])
x1.append(x3[(i)])
x2.append(x2[(i)])

else:
if n[(i)]>s[(i)]:
x0.append(x0[(i)])
x1.append(x3[(i)])
x2.appedn(x1[(i)])
else:
x0.append(x1[(i)])
x1.append(x3[(i)])
x2.append(x2[(i)])

print "x0 = ",x0
print "\nx1 = ",x1
print "\nx2 = ",x2

x0 =  [0, 1, 1, 1.3813008689454946, 1.382057051632978, 1.4273175717149764, 1.4274221422858844]

x1 =  [1, 1.5921843781407843, 1.3813008689454946, 1.382057051632978, 1.4273175717149764, 1.4274221422858844, 1.4275508501677177]

x2 =  [4, 4, 1.5921843781407843, 1.5921843781407843, 1.5921843781407843, 1.5921843781407843, 1.5921843781407843]


## Ex:13.3 Pg: 361¶

In [13]:
from math import sin, cos
#f(x) = 2sinx - x**2/10
x= [.5]
#f'(x) = 2cosx - x/5
#f"(x) = -2sinx - 1/5
for i in range(1,10):
x.append(x[(i-1)] - (2*cos(x[(i-1)]) - x[(i-1)]/5)/(-2*sin(x[(i-1)]) - 1/5))

print "x = ",x

x =  [0.5, 2.2261962395657777, 1.1766358162650659, 1.465127166023216, 1.4238568730046828, 1.4279262963228776, 1.427513951598196, 1.427555600748671, 1.4275513926124817, 1.4275518177794067]