带有 Pyplot 的斜率场指向错误的方向

布尔设计

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

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

代码正确生成点,所以一定是quiver应用有问题。任何帮助将不胜感激。

忘了它

如果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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

带有Pyplot的平滑表面图

来自分类Dev

Pyplot错误栏具有不同的x和y误差

来自分类Dev

带有 pyplot.plot 的简单数组列表

来自分类Dev

参数s中带有matplotlib.pyplot.scatter的matplotlib.pyplot问题

来自分类Dev

带有 Jquery 的空间场

来自分类Dev

pyplot hist 中的内存错误

来自分类Dev

Julia中的PyPlot错误:PyObject类型没有字段set_yscale

来自分类Dev

使用matplotlib的pyplot在每个单元格中带有文本的热图

来自分类Dev

使用pyplot在Python中绘制带有嵌入式if语句的lambda

来自分类Dev

Pyplot/Matplotlib:在 x 轴上带有字符串的二进制数据

来自分类Dev

ImportError:没有名为pyplot的模块

来自分类Dev

matplotlib.pyplot.contour 有问题

来自分类Dev

matplotlib.pyplot.streamplot中的轴错误

来自分类Dev

错误导入 matplolib.pyplot

来自分类Dev

带有subDataset的JasperReports错误

来自分类Dev

NSPredicate带有OR返回错误

来自分类Dev

带有ExternalLoginModule的AEM错误

来自分类Dev

带有systemtcl的错误蓝牙

来自分类Dev

UIStoryboardSegue带有selectedIndexPath的错误

来自分类Dev

带有 if 语句错误的 Makefile

来自分类Dev

带有UICollectionView的UISegmentedControl带有Switch语句的错误

来自分类Dev

如何指向带有函数重载的函数?

来自分类Dev

带有方向的Google静态地图

来自分类Dev

ScrollView和带有权重的方向

来自分类Dev

带有短语顺序斜率和OR子句的Lucene查询

来自分类Dev

Pyplot直方图具有超过7个数据集

来自分类Dev

使轴在matplotlib pyplot中具有相同的长度

来自分类Dev

使用matplotlib.pyplot时没有可见的text()

来自分类Dev

pyplot中所有子图的一个图例