简体   繁体   English

考虑 F(x,y)=常数。 对于无法分离变量的复杂 F,如何确定所有 y 和 x 以及 plot(y vs x)?

[英]Consider F(x,y)=constant. How to determine all the y and x and plot (y vs x), for complicated F where I can't separate the variables?

I consider a function:我考虑一个 function:

 c= sin(x)+cos(y)-d*cos(x+y)/[sin(x) - cos(y)-k*cos(x+y)], 

where c, d, k are known constants . where c, d, k are known constants I want to make the plot y vs x.我想让 plot y vs x。

As I can't separate y from x on the equation, I bring everything on the left side and I define a function G:由于我无法在等式中将 y 与 x 分开,因此我将所有内容都放在左侧并定义一个 function G:

G=[sin(x) - cos(y)-k*cos(x+y)]-(sin(x)+cos(y)-d*cos(x+y))=0.

So, I firstly want all the pairs x,y for which the above function G is equal to 0 and make the plot y vs x.所以,我首先想要上面 function G 等于 0 的所有 x,y 对,并使 plot y 对 x。 Then, I want to use these pairs of x,y in order to determine two other functions that depend on these x,y:然后,我想使用这些 x、y 对来确定依赖于这些 x、y 的另外两个函数:

f1=x^(1/2) + 5cos(x),

f2=y^(1/3) + sin(y)

What I am doing is firstly creating an array with the following loops (to get the (x,y) pairs that give G=0):我正在做的是首先创建一个包含以下循环的数组(以获取 G=0 的(x,y)对):

arrayG = np.empty((0,3),float)
for i in x:
    for j in y:
        if G(i,j)>-0.001 and G<0.001 : 
            arrayG=np.append(arrayG,np.array([[i, j, G(i,j)]]), axis=0)

But with the "if" statement that I'm using, I can't consider G being exactly 0. Should I treat it differently?但是对于我正在使用的“if”语句,我不能认为 G 恰好为 0。我应该区别对待吗?

Assuming that I get the pairs of (x,y) from the above method, I want to implement these pairs on the f1,f2 expressions in order to get the values of f1,f2.假设我从上述方法中得到 (x,y) 对,我想在 f1、f2 表达式上实现这些对,以便获得 f1、f2 的值。 So, I'm writing the following code:所以,我正在编写以下代码:

array_f1_f2 = np.empty((0,5),float) #where 5 are the f1, f2, x, y, G (in order to show all 5) 
for row in array_f1_f2: 
    arrayG = np.append(array_f1_f2, np.array([[f1(row[0]), f2(row[1]), row[0], row[1], row[2] ]]), axis=0)

Is this a valid way of solving the problem?这是解决问题的有效方法吗?

Thanks in advance!提前致谢!

PS This is a simplified example of the problem I'm working on. PS 这是我正在处理的问题的简化示例。 I don't know if this is really solvable.我不知道这是否真的可以解决。 I'm looking for a way to approach this problem.我正在寻找一种方法来解决这个问题。 I have used numpy library for formulas and matplotlib for plots.我使用 numpy 库作为公式,matplotlib 用于绘图。

I'm sort of cheating here by using a non-numpy solution, but this is really easy to do with sympy :我在这里通过使用非 numpy 解决方案有点作弊,但这对sympy来说真的很容易

from sympy import plot_implicit, cos, sin, symbols, Eq
x, y = symbols('x y')


c = 0.25
d = 1
k = 4

p1 = plot_implicit(Eq((sin(x) - cos(y) - k*cos(x+y)) -
                      (sin(x) + cos(y) - d*cos(x+y)), 0))

This gives the following plot:这给出以下 plot:

由 sympy 生成的隐式函数图

If it's important to use numpy and matplotlib , you can do so without much additional effort, but the code is more opaque.如果使用numpymatplotlib很重要,您可以轻松完成,但代码更加不透明。

from matplotlib import pyplot
import numpy

X = numpy.linspace(-4, 4)[:, None]  # Create broadcastable X array.
Y = numpy.linspace(-4, 4)[None, :]  # Create broadcastable Y array.

c = 0.25
d = 1
k = 4

# Because X and Y have broadcastable shapes, Z is a 2d array.

Z = ((numpy.sin(X) - numpy.cos(Y) - k*numpy.cos(X+Y)) -
     (numpy.sin(X) + numpy.cos(Y) - d*numpy.cos(X+Y)))

# `contour` expects X and Y to be 1d, but for broadcasting 
# `numpy` wants 2d arrays with complimentary singleton dimensions.
# So we have to remove the singleton dim. Contour lines will be
# drawn at the `Z` values specified in the last argument (if it's
# an array-like object). We only want one value, so we pass `[0]`.

pyplot.contour(X[:, 0], Y[0, :], Z, [0])
pyplot.show()

This gives:这给出:

由 sympy 生成的隐式函数图

You are trying to solve the equation numerically so in my point of view there wont be any absolute zero for G. First try to choose the step of 'x' and 'y' as small as possible and then instead of 'if' statement, utilize np.mean(np.abs(.)) so it will make the margin more narrow.你正在尝试用数值求解方程,所以在我看来,G 不会有任何绝对零。首先尝试选择尽可能小的 'x' 和 'y' 的步长,然后而不是 'if' 语句,利用 np.mean(np.abs(.)) 所以它会使边距更窄。

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

相关问题 如何在python中绘制f(x,y)= sqrt(2x-y) - how to plot in python f(x,y)=sqrt(2x-y) 如何绘制离散f(x,y)输出 - How to plot discrete f(x,y) output 如何绘制grad(f(x,y))? - How to plot grad(f(x,y))? 如何使用matplotlib绘制f(x,y,z)= t - How to plot f(x,y,z) = t with matplotlib 在python中用多个参数绘制f(X,Y) - Plot f(X, Y) with multiple parameters in python 如何在x和y中找到f(x,y)的偏导数:python中的del ^ 2 f(x,y)/ [del(x)] [del(y)] - How to find Partial derivative of f(x,y) along x and y: del^2 f(x,y)/[del(x)][del (y)] in python 如何在两个单独列表中的 X 和 Y 值范围内计算函数 f(X,Y) 的值 - How to compute values of function, f(X,Y), over a range of values of X and Y in two separate lists Python绘图。 如何绘制f(x,y)? - Python Plotting. How to plot f(x,y)? 我们如何编写一个 function `f`,它返回一个 integer 使得 `f(x) == f(y)` 当且仅当 `x` 和 `y` 相同? - How can we write a function `f` which returns an integer such that `f(x) == f(y)` if and only if `x` and `y` are the same? 如何找到映射 f: X -&gt; Y 来表征 2 个时间序列 X 和 Y 之间的关系? - How do I find a mapping f : X -> Y to characterize the relationship between 2 time-series X and Y?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM