簡體   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