NPM : 2115031008
Kelas : TE C
Buatlah contoh perhitungan menggunakan metode golden section dengan menggunakan program
python atau ms. Excel
(Soal) Menentukan interval, batas atas dan batas bawah, dimana diprediksi hanya memiliki 1 nilai
optimum/ekstremum
X = 3, f ( x )=3,9
GR=
√ 5−1 =0,618
2
d=GR. ( b−a )
¿ 0,61 8 ( 3−1 )=1,236
x 1=b−d=3−1,236=1,764
x 2=a+ d=1+1,236=2,236
x 2=2,236 , f ( x 2 )=5 , 86
Jika f ( x 1 ) > f ( x 2 ) maka titik optimum berada diantara a dan x 2
Tahap 5 : Literasi 1 selesai, hilangkan area yang lebih kecil dan pindahkan nilai batas
Jika f ( x 1 ) > f ( x 2 ), maka eliminasi semua x > x 2, x 2 menjadi nilai b yang baru, nilai a tetap, hitung nilai
x 1 baru
Jika f ( x 1 ) < f ( x 2 ), maka eliminasi semua x > x 1, x 1 menjadi nilai a yang baru, nilai b tetap, hitung nilai
x 2 baru
Literasi 2
Jika f ( x 1 ) > f ( x 2 ), maka eliminasi semua x > x 2, x 2 menjadi nilai b yang baru, nilai a tetap, hitung nilai
x 1 menjadi x 2 baru
Jika f ( x 1 ) < f ( x 2 ), maka eliminasi semua x > x 1, x 1 menjadi nilai a yang baru, nilai b tetap, hitung nilai
x 2 menjadi x 1 baru
Literasi 3
Jika f ( x 1 ) > f ( x 2 ), maka eliminasi semua x > x 2, x 2 menjadi nilai b yang baru, nilai a tetap, hitung nilai
x 1 menjadi x 2 baru
Jika f ( x 1 ) < f ( x 2 ), maka eliminasi semua x > x 1, x 1 menjadi nilai a yang baru, nilai b tetap, hitung nilai
x 2 menjadi x 1 baru
def func_fx(x):
fx=np.sin(x)
return fx
def check_pos(x1,x2):
if x2<x1:
label='right'
else:
label=''
return label
def update_interior(x1,xu):
d=((np.sqrt(5)-1)/2)*(xu-x1)
x1=x1+d
x2=xu-d
return x1,x2
#fungsi plot
def plot_graph(x1,xu,x2):
clear_output(wait=True)
#plot point x1
plt.plot(x1,func_fx(x1),'ro',label='1')
plt.plot([x1,x1],[0,func_fx(x1)],'k')
#plot x2 point
plt.plot(x2,func_fx(x2),'bo',label='x2')
plt.plot([x2,x2],[0,func_fx(x2)],'k')
#plot xl line
plt.plot([xl,xl],[0,func_fx(xl)])
plt.annotate('xl',xy=(xl-0.01,-0.2))
#plot xu line
plt.plot([xu,xu],[0,func_fx(xu)])
plt.annotate('xu',xy=(xu-0.01,-0.2))
#plot x1 line
plt.plot([x1,x1],[0,func_fx(x1)],'k')
plt.annotate('x1',xy=(x1-0.01,-0.2))
#plot x2 line
plt.plot([x2,x2],[0,func_fx(x2)],'k')
plt.annotate('x2',xy=(x2-0.01,-0.2))
#y-axis limit
plt.ylim([-1.2,1.2])
plt.show()
def golden_search(xl,xu,mode,et):
it=0
e=1
while e>=et:
new_x=update_interior(xl,xu)
x1=new_x[0]
x2=new_x[1]
fx1=func_fx(x1)
fx2=func_fx(x2)
label=check_pos(x1,x2)
clear_output(wait=True)
plot_graph(xl,xu,x1,x2) #PLOTTING
plt.show()
it+=1
print ('Iteration: ',it)
r=(np.sqrt(5)-1)/2 #GOLDEN RATIO
e=((1-r)*(abs((xu-xl)/xopt)))*100 #Error
print('Error:',e)
time.sleep(1)