Question
Can someone explain me the bold part of this Python code? def f(x): return x**4 + 4 * x**3 + x**2 - 10 * x
Can someone explain me the bold part of this Python code?
def f(x): return x**4 + 4 * x**3 + x**2 - 10 * x + 1
def gradf(x): return 4 * x**3 + 12 * x**2 + 2 * x - 10
def grad(f, x, delta=1e-5): return (f(x + delta) - f(x - delta)) / (2 * delta)
def g(x): return np.log(1 + np.exp(x))
def gradg(x): return np.exp(x) / (1 + np.exp(x))
def h(x): return g(f(x))
def gradh(x): return gradg(f(x)) * gradf(x)
x = np.arange(-5.0, 3.0, 0.01) y = h(x)
plt.clf() plt.plot(x, y)
def grad_desc(f, x0, alpha, gradf=None, eps=1e-5, maxiters=100): # initializaton, check arguments, ... x = x0 y = f(x) if gradf == None: gradf = lambda x: grad(f, x) xvals = [x] yvals = [y] for it in range(maxiters): d = gradf(x) new_x = x - d * alpha print("it=", it, "x=", x, "new_x=", new_x, "d=", d) if abs(d) < eps: break x = new_x xvals.append(x) yvals.append(f(x)) plt.plot(xvals, yvals, "o-") return x
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started