繁体   English   中英

具有Pyplot的倾斜场指向错误的方向

[英]Slope Fields with Pyplot point in the wrong direction

我正在为微积分课设计一个项目,需要为给定的微分方程生成一个斜率场。 我的代码如下:

from numpy import *
import matplotlib.pyplot as plt
import sympy as sym

def main():
    rng = raw_input('Minimum, Maximum: ').split(',')
    rng = [float(rng[i]) for i in range(2)]
    x = sym.Symbol('x')
    y = sym.Symbol('y')
    function = input('Differential Equation in terms of x and y: ')
    a = sym.lambdify((x,y), function)  # function a is the differential#
    x_points,y_points = meshgrid(arange(rng[0],rng[1],1),arange(rng[0],rng[1],1))
    f_x = x_points + 1
    f_y = a(x_points,y_points)
    print a(1,1),a(-1,-1),a(-5,-5),a(5,5)
    N = sqrt(f_x**2+f_y**2)
    f_x2,f_y2= f_x/N,f_y/N
    ax1 = plt.subplot()
    ax1.set_title(r'$\mathit{f(x)}\in \mathbb{R}^2$')
    ax1.set_xlabel(r'$\mathit{x}$')
    ax1.set_ylabel(r'$\mathit{y}$')
    ax1.grid()
    ax1.spines['left'].set_position('zero')
    ax1.spines['right'].set_color('none')
    ax1.spines['bottom'].set_position('zero')
    ax1.spines['top'].set_color('none')
    ax1.spines['left'].set_smart_bounds(True)
    ax1.spines['bottom'].set_smart_bounds(True)
    ax1.set_aspect(1. / ax1.get_data_ratio())
    ax1.xaxis.set_ticks_position('bottom')
    ax1.yaxis.set_ticks_position('left')
    ax1.quiver(x_points,y_points,f_x2,f_y2,pivot='mid', scale_units='xy')
    plt.show()

main()

乍一看,这会创建合适的斜率场,但是箭头实际上是不正确的。 dy / dx = x / y虽然看起来几乎正确,但适当的斜率字段看起来像: dy / dx = x / y

该代码会正确生成点,因此颤动应用程序一定存在问题。 任何帮助将不胜感激。

如果dy/dx = x/y ,则大致来说, Δy/Δx = x/y ,并且(x, y)处的向量从(x, y)变为(x+Δx, y+Δy)

如果我们使Δx = 1 ,则Δy = x/y * Δx = x/y 所以代替

delta_x = x_points + 1
delta_y = a(x_points,y_points)

我们应该使用

delta_x = np.ones_like(X)   #<-- all ones
delta_y = a(X, Y)

import numpy as np
import matplotlib.pyplot as plt
import sympy as sym
x, y = sym.symbols('x, y')

def main(rng, function):
    a = sym.lambdify((x, y), function)  # function a is the differential#
    num_points = 11
    X, Y = np.meshgrid(np.linspace(rng[0], rng[1], num_points),
                       np.linspace(rng[0], rng[1], num_points))

    delta_x = np.ones_like(X)
    delta_y = a(X, Y)
    length = np.sqrt(delta_x**2 + delta_y**2)
    delta_x, delta_y = delta_x/length, delta_y/length
    ax = plt.subplot()
    ax.set_title(r'$\mathit{f(x)}\in \mathbb{R}^2$')
    ax.set_xlabel(r'$\mathit{x}$')
    ax.set_ylabel(r'$\mathit{y}$')
    ax.grid()
    ax.spines['left'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['bottom'].set_position('zero')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_smart_bounds(True)
    ax.spines['bottom'].set_smart_bounds(True)
    ax.set_aspect(1. / ax.get_data_ratio())
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    ax.quiver(X, Y, delta_x, delta_y,
               pivot='mid',
               scale_units='xy', angles='xy', scale=1
               )
    plt.show()

def get_inputs():
    # separate user input from calculation, so main can be called non-interactively
    rng = input('Minimum, Maximum: ').split(',')
    rng = [float(rng[i]) for i in range(2)]
    function = eval(input('Differential Equation in terms of x and y: '))
    return rng, function

if __name__ == '__main__':
    # rng, function = get_inputs()
    # main(rng, function)
    main(rng=[-10, 10], function=x / y)

在此处输入图片说明


请注意,您可以轻松地将Δx设为较小的值。 例如,

delta_x = np.ones_like(X) * 0.1
delta_y = a(X, Y) * delta_x

但归一化后的结果将完全相同:

length = np.sqrt(delta_x**2 + delta_y**2)
delta_x, delta_y = delta_x/length, delta_y/length

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM