【问题标题】:How to graph a one-variable linear inequality solution set with sympy python如何用 sympy python 绘制单变量线性不等式解决方案集
【发布时间】:2022-10-08 02:14:39
【问题描述】:

我是 sympy 绘图的新手,当我反复尝试为像 here 这样的一个变量线性不等式绘制解决方案集时,我无法弄清楚,谁能帮我解决我的问题?

我的代码是这样的

from sympy import symbols, plot
from sympy.plotting import plot 
from sympy import *
import numpy as np
x, y = symbols("x, y", real=True)
init_printing(use_unicode=True)
ekpr =  (4*x - 2 <= 5 + 3*x)
pprint(ekpr)
xs = np.linspace(0, 10, 11)
yvals = [solve(ekpr, x, xi) for xi in xs]
sol = solve(ekpr, x)
print(sol)
plot = plot(ys, xlim=[0.0, 10.10],
           markers=[{'args': [sol, [0], xs, yvals, 'ro']}])

我期望的图表结果如下图所示。 enter image description here

【问题讨论】:

  • 请告诉我们您想用以下代码行做什么:yvals = [solve(ekpr, x, xi) for xi in xs]

标签: python sympy


【解决方案1】:

我想在这里复制代码

这是代码

import numpy as np
import sympy as sp

x = sp.Symbol('x')
y = sp.Symbol('y')
ys = sp.cos(2 * x) * (sp.sin(2 * x) + 1.5) + sp.cos(x)

xs = np.linspace(-2, 3, 5)  # [-2.   -0.75  0.5   1.75  3.  ]
yvals = [ys.subs(x, xi) for xi in xs]

plot = sp.plot(ys, xlim=[-2.2, 3.2],
           markers=[{'args': [xs, yvals, 'ro']}])

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2017-02-20
    • 2021-11-25
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    相关资源
    最近更新 更多