<a href="https://colab.research.google.com/github/sandeep9583/optimization/blob/main/Optimization.ipynb" target="_parent"></a>


```
#Exhaustive search method
f = lambda x: x**2+54/x
a=0
b=5
n=10
print(f"a={a},b={b},n={n}")
dx = (b-a)/n
print(f"Δx = b-a/n = {b}-{a}/{n} = {dx}")
print(f"x1 = a, x2 = x1 + Δx,  x3 = x1 + 2Δx")
print("write function")
x1=a
x2=x1+dx
x3=x1+2*dx
i=1
flag=0
while(flag == 0):
  print("Iteration ",i)
  i+=1
  try:
      f1 = f(x1)
  except ZeroDivisionError:
      f1 = 99999999999999
  try:
      f2 = f(x2)
  except ZeroDivisionError:
      f2 = 99999999999999
  try:
      f3 = f(x3)
  except ZeroDivisionError:
      f3 = 99999999999999
  print(f"x1 = {x1}, x2 = {x1} + {dx} = {x2}, x3 = {x1} + 2*{dx} = {x3}") 
  print(f"f(x1) = f({x1}) = {f1} ,f(x2) = f({x2}) = {f2},f(x3) = f({x3}) = {f3}")           
  p =[x1,x2,x3,f1,f2,f3]
  p = [round(num,3) for num in p]
  print(p)

  if (f1>=f2<=f3):
    flag=1
    print("since f(x1)>=f(x2)<=f(x3)") 
    print("Yes")
    print(f"Desired Bracket is ({x1},{x3}) ")
  else:
    print("since f(x1)>=f(x2)<=f(x3)") 
    print("NO")
    print("x1 = x2, x2 = x3,x3 = x2 + Δx")
    x1=x2
    x2=x3
    x3=x2+dx
```


```
#Bounding phase method
import math

f = lambda x: math.sin(2*x)+x**2+21
x=1
d=0.5
k=0
xk=x
flag=0
i=1
while flag==0:
  print("Iteration ",i)
  i+=1
  xk1=xk+(2**k)*d
  print(f"k = {k}, Δ = {d}")
  print(f"x({k}) = {xk}, x({k+1}) = x({k}) + 2^({k})*{d}  = {x} + {(2**k)*d} = {xk1}")
  try:
    p = f(xk)
  except ZeroDivisionError:
    p = 99999999999999
  try:
    q = f(xk1)
  except ZeroDivisionError:
    q = 99999999999999
  print(f"f(x({k})) = {p}, f(x({k+1})) = {q}")  
  pr =[xk,xk1,p,q]
  pr = [round(num,3) for num in pr]
  print(pr)
  k+=1
  if(q<p):
    xk=xk1
    print("Yes")
  else:
    flag=1
    print("NO")
    print(f"Desired Bracket ({round(xk-(2**(k-2))*d,3)} ,{xk1})")
```


```
#Interval Halving method
f = lambda x: x**3+648*(x**(-2))
a=-1
b=4
e=10**(-3)
l=b-a
print(f"a={a},b={b},l={l},∈={e}")
while l>0.5:
  xm=(a+b)/2
  x1=a+l/4
  x2=b-l/4
  print(f"xm =(a+b)/2 = ({a}+{b})/2 = {xm}, x1 = a+l/4 = {a}+{l}/4 = {x1}, x2 = b-l/4 = {b}-{l}/4 = {x2}")

  try:
    f1 = f(x1)
  except ZeroDivisionError:
    f1 = 99999999999999
  try:
    f2 = f(x2)
  except ZeroDivisionError:
    f2 = 99999999999999
  try:
    fm = f(xm)
  except ZeroDivisionError:
    fm = 99999999999999 
  print(f"f(x1) = f({x1}) = {f1} ,f(x2) = f({x2}) = {f2},f(xm) = f({xm}) = {fm}")
  p=[x1,x2,xm,f1,f2,fm] 
  p = [round(num,3) for num in p]
  print(p)  
   
  if(f1>fm and f2>fm ):
    a=x1
    b=x2   
    l=b-a
    print("Two sides ")
    print(f"f(x1)>f(xm) and f(x2)>f(xm) \n\n,a = {a},b = {b} and l = {l}")
  elif(f1>fm and f2<fm):
    a=xm
    l=b-a
    print("Cut Two left side")
    print(f"f(x1)>f(xm) and f(x2)<f(xm)  \n\n,a = {a},b = {b} and l = {l}")
  elif(f1<fm and f2>fm):
    b=xm
    l=b-a
    print("Cut Two right side ")
    print(f"f(x1)<f(xm) and f(x2)>f(xm) \n\n,a = {a},b = {b} and l = {l}")


```


```
#Fibonacci search method

f = lambda x: x**3-9*x
a=1
b=4
k=2
n=3
l=b-a
print(f"a={a},b={b},k={k},n={n},L=b-a={l}")
def Fibonacci(n): 
    if n<0: 
        print("Incorrect input") 
    # First Fibonacci number is 0 
    elif n==0: 
        return 1
    # Second Fibonacci number is 1 
    elif n==1: 
        return 1
    else: 
        return Fibonacci(n-1)+Fibonacci(n-2) 

while(k<=n):
  lk=(Fibonacci(n-k+1)/Fibonacci(n+1))*l
  x1=a+lk
  x2=b-lk
  print(f"Lk*=(F(n-k+1)/F(n+1))*L")
  print(f"L{k}*=(F({n-k+1})/F({n+1}))*{l} = {lk}")
  print(f"x1=a+L{k}*={a}+{lk}={x1}")
  print(f"x2=b-L{k}*={b}-{lk}={x2}")
  try:
    f1 = f(x1)
  except ZeroDivisionError:
    f1 = 99999999999999
  try:
    f2 = f(x2)
  except ZeroDivisionError:
    f2 = 99999999999999  
  print(f"f(x1) = f({x1}) = {f1} ,f(x2) = f({x2}) = {f2}")  
  p=[x1,x2,f1,f2]  
  p = [round(num,3) for num in p]
  print(p)  
  k+=1
  if (f1>f2):
    a=x1
    print("f(x1)> f(x2)")
    print("left side\n")
    print(f"a={a},b={b},k={k},n={n}")
  elif (f1<f2):
    b=x2
    print("f(x1)< f(x2)")
    print("right side\n")
    print(f"a={a},b={b},k={k},n={n}")

```


```
#Golden Section search method
f = lambda x: x**4-14*(x**3)+60*(x**2)-70*x

a=1
b=4

fw = lambda w: f(w*(b-a)+a)

aw=0
bw=1
lw=bw-aw
print(f"w = x-a/b-a , aw =0,bw =1")
print(f"w = (x-{a})/({b}-{a}) , w = (x-{a})/({b-a})")
print(f"x = w*{b-a}+{a}")
print("Calculate f(w)")
while(lw>0.1):
  lw=bw-aw
  w1=aw+0.618*lw
  w2=bw-0.618*lw

  print(f"lw = {bw}-{aw} ={lw}")
  print(f"w1 = aw+0.618*lw ={aw}+0.618*{lw} = {w1}")
  print(f"w2 = bw-0.618*lw ={bw}-0.618*{lw} = {w2}")
  try:
    f1 = fw(w1)
  except ZeroDivisionError:
    f1 = 99999999999999
  try:
    f2 = fw(w2)
  except ZeroDivisionError:
    f2 = 99999999999999  
  print(f"f(w1) = f({w1}) = {f1} ,f(w2) = f({w2}) = {f2}")   
  p=[aw,bw,w1,w2,f1,f2] 
  p = [round(num,3) for num in p]
  print(p)   
  if (f1<f2):
    aw=w2
    print("f(x1)< f(x2)")
    print("cut left side \n")
    print(f"aw = {aw},bw = {bw}")
  elif (f1>f2):
    bw=w1
    print("f(x1)> f(x2)")
    print("cut right side \n") 
    print(f"aw = {aw},bw = {bw}")
  else:
    aw=w1
    bw=w2
    print("f(x1)=f(x2)")
    print("cut both side \n") 
    print(f"aw = {aw},bw = {bw}")

```


```
# Successive Quadratic Estimation Method
f = lambda x: x**2+54/x
x1 = 1
d = 1

x2 =x1+d
fx1 = f(x1)
fx2 =f(x2)
if(fx1>fx2):
  x3 = x1+2*d
  fx3 =f(x3)

fmin = min(fx1,fx2,fx3)
if(fmin==fx1):
  xmin = x1
elif(fmin == fx2):
  xmin = x2
else:
  xmin = x3    

a0 = fx1
a1 = (fx2-fx1)/(x2-x1)
a2 = (1/(x3-x2))*((fx3-fx1)/(x3-x1)-a1)
xd = ((x1+x2)/2)-(a1/(2*a2))

print(a0,a1,a2,xd)
```

    55.0 -24.0 10.0 2.7



```
#Newton-Raphson Method
import sympy as sp
from sympy import *
x= sp.Symbol('x')

f = lambda x: x**2+54/x

df = sp.diff(f(x),x)
dff =sp.diff(df,x)
print(f(x))
print(df)
print(dff)
df = lambdify(x, df)
dff = lambdify(x,dff)

x=1
k=1
xk=x

while(k<8):
  print(f"Iteration {k}")
  dfxk =round(df(xk),3)
  dffxk =round(dff(xk),3)
  xk1 = round(xk-dfxk/dffxk,3)
  dfxk1 = round( df(xk1),3)
  print(xk,dfxk,dffxk,xk1,dfxk1)
  k+=1
  xk = xk1
```

    x**2 + 54/x
    2*x - 54/x**2
    2 + 108/x**3
    Iteration 1
    1 -52.0 110.0 1.473 -21.942
    Iteration 2
    1.473 -21.942 35.792 2.086 -8.238
    Iteration 3
    2.086 -8.238 13.898 2.679 -2.166
    Iteration 4
    2.679 -2.166 7.617 2.963 -0.225
    Iteration 5
    2.963 -0.225 6.152 3.0 0.0
    Iteration 6
    3.0 0.0 6.0 3.0 0.0
    Iteration 7
    3.0 0.0 6.0 3.0 0.0



```
# Bisection Method
import sympy as sp
from sympy import *
x= sp.Symbol('x')

f = lambda x: x**2+54/x

df = sp.diff(f(x),x)
print(f(x))
print(df)
df = lambdify(x, df)

a = 2
b = 5
x1 = a
x2 = b
i=0
while(i<3):
  i+=1
  print(f"Iteration {i}")

  z = round((x1+x2)/2,3)
  dfz =round(df(z),3)
  print(x1,x2)
  print(z,dfz)
  if(dfz>0):
    print(f"eliminate the right part (the region ({z},{x2}))")
    x2 = z
  elif(dfz<0):
    print(f"eliminate the left part (the region ({x1},{z}))")
    x1 = z  

print(x1,x2)
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
y= np.linspace(a,b)
plt.plot(y,f(y))
#plt.plot(y,df(y))    
```


```
# Secant Method
import sympy as sp
from sympy import *
x= sp.Symbol('x')

f = lambda x: x**2+54/x

df = sp.diff(f(x),x)
print(f(x))
print(df)
df = lambdify(x, df)

a = 2
b = 5
x1 = a
x2 = b
i=0
while(i<5):
  i+=1
  print(f"Iteration {i}")
  dfa = round(df(x1),4)
  dfb = round(df(x2),4)
  z = round(x2 - dfb/((dfb-dfa)/(x2-x1)),4)
  dfz =round(df(z),4)
  print(x1,x2)
  print(dfa,dfb,z,dfz)
  if(dfz>0):
    print(f"eliminate the right part (the region ({z},{x2}))")
    x2 = z


%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
y= np.linspace(a,b)
plt.plot(y,f(y))
#plt.plot(y,df(y))    
```


```
#cubic search method

import sympy as sp
from sympy import *
x= sp.Symbol('x')

f = lambda x: 2*x**2+16/x

df = sp.diff(f(x),x)
print(f(x))
print(df)
df = lambdify(x, df)

d=0.5
x1=1

k=0
i=0
while(i<5):
  print(f"k = {k}")
  i+=1
  print(f"x{k} = {x1}")
  df1=df(x1)
  print(f"df({k}) = {df1}")
  if(df(x1)>0):
    print("\n")
  elif(df(x1)<0):
    d=0.5
    x2=x1+(2**k)*d
    x1=x2
    k+=1
    print(f"x{k} = {x2}")
    print("\n")




```

    2*x**2 + 16/x
    4*x - 16/x**2
    k = 0
    x0 = 1
    df(0) = -12.0
    x1 = 1.5
    
    
    k = 1
    x1 = 1.5
    df(1) = -1.1111111111111107
    x2 = 2.5
    
    
    k = 2
    x2 = 2.5
    df(2) = 7.4399999999999995
    
    
    k = 2
    x2 = 2.5
    df(2) = 7.4399999999999995
    
    
    k = 2
    x2 = 2.5
    df(2) = 7.4399999999999995
    
    


ed03d29e77e6415e0f4b039d60fed4cacc12d977
ipynb
Jupyter Notebook
Optimization.ipynb
sandeep9583/optimization
ed25325e06c62e7751ad6d30303aa1afe63095dc
MIT
Optimization.ipynb
sandeep9583/optimization
ed25325e06c62e7751ad6d30303aa1afe63095dc
MIT
Optimization.ipynb
sandeep9583/optimization
ed25325e06c62e7751ad6d30303aa1afe63095dc
MIT
Qwen/Qwen-72B
1. YES
2. YES