尝试求解隐式方程时出现 UnboundLocalError 'referenced before assignment'
UnboundLocalError 'referenced before assignment' when trying to solve an implicit equation
我在尝试求解这个隐式方程时遇到错误:
y = x^3 +sqrt(y)
问题:当 x = [0, 1, 2.5, 2.8, 3, 3.2]
.
时求 y
"UnboundLocalError: local variable 'y' referenced before assignment"
我该如何解决这个问题:
from math import *
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve
def f(x):
y = np.power(x,3.0) - sqrt(y)
return y
x = [0, 1, 2.3611, 2.9033, 3.2859, 3.5915]
x= fsolve(f, 0)
print(x)
而不是:
y = np.power(x,3.0) - sqrt(y)
你可以试试:
y = np.power(x,3.0)
y = y - sqrt(y)
您声明的变量 y 不存在
我在尝试求解这个隐式方程时遇到错误:
y = x^3 +sqrt(y)
问题:当 x = [0, 1, 2.5, 2.8, 3, 3.2]
.
y
"UnboundLocalError: local variable 'y' referenced before assignment"
我该如何解决这个问题:
from math import *
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve
def f(x):
y = np.power(x,3.0) - sqrt(y)
return y
x = [0, 1, 2.3611, 2.9033, 3.2859, 3.5915]
x= fsolve(f, 0)
print(x)
而不是:
y = np.power(x,3.0) - sqrt(y)
你可以试试:
y = np.power(x,3.0)
y = y - sqrt(y)
您声明的变量 y 不存在