def test(): import numpy as np import plot as p; reload(p) data = np.random.rand(100,200); p.plot(data, 'test') reload(p) pts = np.random.rand(10000,3); p.plot3d(pts);
def test(): import numpy as np import plot as p reload(p) data = np.random.rand(100, 200) p.plot(data, 'test') reload(p) pts = np.random.rand(10000, 3) p.plot3d(pts)
x += alpha * p xs.append(x.copy()) z.append(S(x)) r -= alpha * Ap rr1 = np.einsum('i,i', r, r) if np.sqrt(rr1) < tol: print('Found minimum, |r| = ', np.sqrt(rr1)) print('Minimum found in ', i, ' steps') print('Minimum = ', x) break p = r + (rr1 / rr0) * p rr0 = rr1 if i >= (max_steps + 1): print('Reached maximum number of steps; exiting minimization') print('|r| = ', np.sqrt(rr1)) print('x = ', x) return np.stack(xs, axis=0), np.asarray(z) # Build function to minimize A = np.array([[4.0, 1.0], [1.0, 3.0]]) b = np.array([1.0, 2.0]) x0 = np.array([-3.0, -3.0]) # Minimize the function xs, z = cg(A, b, x0) # Plot solution and minimization steps plot3d(xs[:, 0], xs[:, 1], z + 6, S2, grid_range=[-3.0, 3.0])
# Function to minimize def f_to_minimize(coords): x, y = coords return 2 * x**2 + (3 / 2) * y**2 + x * y - x - 2 * y + 6 # Function to pass as callback def getIterationSteps(solution): global step step += 1 x, y = solution z = f_to_minimize(solution) print("At step {:d}: x = {:f}, y = {:f}, f(x,y) = {:f}".format( step, x, y, z)) x_step.append(x) y_step.append(y) z_step.append(z) # Minimize step = 0 x_step = [] y_step = [] z_step = [] OptRes = minimize(f_to_minimize, (-3., -3.), method='CG', tol=1.e-10, callback=getIterationSteps) # Plot plot.plot3d(x_step, y_step, z_step, f_to_minimize)