用matplotlib在轮廓上绘制矢量场的正确方法

时间:2015-11-18 17:28:45

标签: python matplotlib plot

我正在实施最陡的下降,我想在等高线图上绘制解决方案的路径。为了这个目的,我在for循环中保存了必要的数据,然后我尝试在轮廓图上绘制路径(一组正交矢量),但我搞砸了事情(矢量头应该到达其他矢量尾部):< / p>

enter image description here

内椭圆内的线是矢量。我想要这样的东西:

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import rc
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import LinearLocator, FormatStrFormatter

def column(matrix, i):
    return [row[i] for row in matrix]

def compute_functional(A, b, x_ini, x_end, y_ini, y_end, rate_of_points):
    """Computes functional values for Ax - b = 0 linear system

    :param A: matrix of coefficients
    :param b: independent term
    :param x_ini: x initial value
    :param x_end: x end value
    :param y_ini: y initial value
    :param y_end: y end value
    :return: Returns x1, x2 and fq (functional)
    """
    x1, x2 = np.meshgrid(np.arange(x_ini, x_end, rate_of_points), np.arange(y_ini, y_end, rate_of_points))
    fq = np.zeros(np.shape(x1))


    for i in xrange(len(x1)):
        for j in xrange(len(x2)):
            x = np.array([x1[i][j], x2[i][j]])
            fq[i][j] = np.dot(x, np.dot(A, x)) - 2 * np.dot(x, b)

    return (x1, x2, fq)


def steepest_descent(A, b, x, x1, x2, fq, tol, maxit):
    """ Solves a linear system Ax - b = 0 by steepest descent method

    :param A: matrix of coefficients
    :param b: independent term
    :param x_ini: x initial values
    :param x1: mesh grid for x1
    :param x2: mesh grid for x2
    :param fq: mesh grid for functional
    :param tol: tolerance of solution
    :param maxit: maximum number of allowed iterations
    """
    print('Running Steepest Descent.')
    data = []
    print("k\txk0\t\t\txk1\t\t\tvk0\t\t\tvk1\t\t\terror")
    for k in range(maxit):
        vk = b - np.dot(A, x)
        vkAvk = np.dot(np.transpose(vk), np.dot(A, vk))

        if vkAvk == 0:
            break

        tk = np.dot(np.transpose(vk), vk) / vkAvk
        correction = tk * vk
        xnew = x + correction

        data.append(np.array([float(x[0]), float(x[1]), float(correction[0]), float(correction[1])]))

        xdiffnorm = np.max(np.abs(xnew - x))
        xnorm = np.max(np.abs(xnew))
        error = xdiffnorm / xnorm

        print("%d\t%f\t%f\t%f\t%f\t%f") % (k, x[0], x[1], vk[0], vk[1], error)

        x = xnew

        if error < tol: break

    # plot contour
    plt.figure()
    plt.contour(x1, x2, fq, colors='k')
    plt.quiver(column(list(data),0),column(list(data),1),column(list(data),2),column(list(data),3), width=0.01, headwidth=5, scale=1, units='xy')

    plt.show()
    plt.close()
    return x


#Creating linear system
A = np.array([[3,2],[2,6]])
b = np.array([[2],[8]])
x = np.ones((np.shape(A)[0], 1))

#Configuring parameteres
tol = 0.001
maxit = 100
x_ini = -2
x_end = 2
y_ini = -2
y_end = 2
rate_of_points = 0.25

x1, x2, fq = compute_functional(A, b, x_ini, x_end, y_ini, y_end, rate_of_points)
steepest_descent(A, b, x, x1, x2, fq, tol, maxit)

0 个答案:

没有答案