import matplotlib.pyplot as plt
import numpy as np
#F(x;y) = a · x + b · y
def F1(a, b):
x = np.linspace(-6,9,100)
y = a*x + b
return x, y
#F(x; y) = a · x - b · y
def F2(a, b):
x = np.linspace(-6,9,100)
y = a*x - b
return x, y
#F(x; y) = a · x · b · y
def F3(a, b):
x = np.linspace(-6,9,100)
y = a*x*b
return x, y
#F(x; y) = (a · x) / (b · y)
def F4(a, b):
x = np.linspace(-6,9,100)
y = (a*x)/(b*y)
return x, y
plt.title("Euler's method")
plt.xlabel("x")
plt.ylabel("y")
a = 5
b = 1
x, y = F1(a, b)
plt.plot(x, y, label="F1(x,y): a · x + b · y")
x, y = F2(a, b)
plt.plot(x, y, label="F2(x,y): a · x - b · y")
x, y = F3(a, b)
plt.plot(x, y, label="F3(x,y): a · x · b · y")
x, y = F4(a, b)
plt.plot(x, y, label="F4(x,y): (a · x) / (b · y)")
plt.legend()
plt.show()
我在Python中收到以下错误(我使用的是Jupyter):
UnboundLocalError: local variable 'y' referenced before assignment
in line 25 and 44
25 line y = (a*x)/(b*y)
44 line x, y = F4(a, b)
怎么解决呢?
1条答案
按热度按时间f0brbegy1#
在你的F4函数中,你需要改变你的方程来得到y值。。你不能在给y赋值之前让它在右边。