Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

solve abcd this is for QC QD import numpy as np import matplotlib.pyplot as plt ### Fill in ################################ def f ( x ) :

solve abcd
this is for QC QD
import numpy as np
import matplotlib.pyplot as plt
### Fill in ################################
def f(x):
return 1*x**4-5*x**2-3*x
def gradient_f(x):
return #FILL IN
############################################
def plot_live(ax, x, y):
ax.plot(x, y, linestyle=':', marker='o', markersize=8, color='blue', alpha=0.3)
print('w ={:.4f} f(w)={:.4f}'.format(x,y))
plt.pause(1) #this is intentionally added to show the
def gradient_descent(w_init, step_size, n_iter=100, tolerance=1e-04, ax=None):
w = w_init
itr =0
while(itr n_iter):
if ax:
plot_live(ax, w, f(w))
### FILL IN ###############################
gradient = #FILL IN
diff = #FILL IN
w -= #FILL IN
###########################################
stop_criterion = np.abs(diff)
if np.all(stop_criterion = tolerance):
if ax:
plot_live(ax, w, f(w))
print('converged...')
break
itr +=1
if f(w)>1e+8:
print('diverged...')
break
return w
if __name__=="__main__":
fig, ax = plt.subplots()
w_min =-3.1
w_max =3.1
w = np.linspace(w_min, w_max, 1000)
ax.plot(w, f(w), linestyle='-', color='black')
ax.set_ylabel('f(w)', fontsize=15)
ax.set_xlabel('w', fontsize=15)
ax.grid()
ax.set_xlim([np.min(w), np.max(w)])
ax.set_ylim([np.min(f(w)), np.max(f(w))])
gradient_descent(w_init=-1, step_size=0.2, n_iter=100, tolerance=1e-04, ax=ax) #change w_init, step_size here!
plt.show()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions