英文:
How to change a global variable dependent of other global variables inside the Python function
问题
以下是您要翻译的代码部分:
我的任务是在Python中实现Euler的方法,其中有五个参数之一是依赖于变量t和x的函数f(确切地说:f(t,x))。当我尝试使用给定的函数f执行Euler的方法时,我遇到了错误,因为我没有声明变量t和x。
我将它们声明为等于1(可以是任何其他值),并决定在函数内部更改它们的值。不幸的是,在更改值后,我仍然得到x=t=1的结果(我使用**print(f)**进行了检查)。我尝试使用**global**,但看起来函数f仍然依赖于x=1和t=1。
如何在“Euler”函数内更改t和x的值?或者也许有更有效的方法来完成任务?
希望这可以帮助您。如果您有任何其他问题,请随时提出。
英文:
My task is to implement Euler's method in Python with five arguments and one of them is a function f dependent on variables t and x (precisely: f(t,x)). When I tried to execute Euler's method with a given function f, I got errors, because I did not declare variables t and x.
I declared them as equal to 1 (it could have been any other value) and decided to change their values inside the function. Unfortunately, after changing values, I still get a result for x=t=1 (I checked it by using print(f). I tried to use global, but it looks like function f is still dependent on x=1 and t=1.
How do I change values of t and x inside "Euler" function? Or maybe there is more efficient way to complete the task?
from math import sin,cos
def Euler(f,x_0,t_0,T,n):
h=(T-t_0)/n
x_list=[x_0]
t_list=[t_0]
for i in range(n):
global x
x=x_list[i]
global t
t=t_list[i]
next_x=x+h*f
x_list.append(next_x)
next_t=round(t+h, 2)
t_list.append(next_t)
return t_list,x_list
x=1
t=1
sol=Euler((-2)*(x-cos(t))-sin(t),1,0,2,20)
print(sol)
答案1
得分: 1
Firstly, it seems that there's a misunderstanding on how to pass a function as an argument in Python. When you call the Euler
function with (-2)*(x-cos(t))-sin(t)
as the first argument, Python immediately tries to evaluate this expression using the current values of x
and t
, which are both 1. This results in a number, not a function.
To correctly pass a function that depends on t
and x
, you should define a separate function, and then pass this function to your Euler
function.
The use of global variables t
and x
inside the Euler
function is unnecessary and can lead to confusion. Instead, the values for t
and x
should be obtained from the t_list
and x_list
variables, respectively, and passed to the function f
.
Here is a modified version of your code:
from math import sin, cos
def Euler(f, x_0, t_0, T, n):
h = (T - t_0) / n
x_list = [x_0]
t_list = [t_0]
for i in range(n):
x = x_list[i]
t = t_list[i]
next_x = x + h * f(t, x)
x_list.append(next_x)
next_t = round(t + h, 2)
t_list.append(next_t)
return t_list, x_list
# Defining the function to pass to Euler's method
def my_func(t, x):
return -2 * (x - cos(t)) - sin(t)
sol = Euler(my_func, 1, 0, 2, 20)
print(sol)
In this updated version, my_func
is defined as a function of t
and x
, and this function is passed to Euler
. Inside the Euler
function, my_func
(or more generally f
) is called with the appropriate arguments t
and x
.
英文:
Firstly, it seems that there's a misunderstanding on how to pass a function as an argument in Python. When you call the Euler
function with (-2)*(x-cos(t))-sin(t)
as the first argument, Python immediately tries to evaluate this expression using the current values of x
and t
, which are both 1. This results in a number, not a function.
To correctly pass a function that depends on t
and x
, you should define a separate function, and then pass this function to your Euler
function.
The use of global variables t
and x
inside the Euler
function is unnecessary and can lead to confusion. Instead, the values for t
and x
should be obtained from the t_list
and x_list
variables, respectively, and passed to the function f
.
Here is a modified version of your code:
from math import sin, cos
def Euler(f, x_0, t_0, T, n):
h = (T - t_0) / n
x_list = [x_0]
t_list = [t_0]
for i in range(n):
x = x_list[i]
t = t_list[i]
next_x = x + h * f(t, x)
x_list.append(next_x)
next_t = round(t + h, 2)
t_list.append(next_t)
return t_list, x_list
# Defining the function to pass to Euler's method
def my_func(t, x):
return -2 * (x - cos(t)) - sin(t)
sol = Euler(my_func, 1, 0, 2, 20)
print(sol)
In this updated version, my_func
is defined as a function of t
and x
, and this function is passed to Euler
. Inside the Euler
function, my_func
(or more generally f
) is called with the appropriate arguments t
and x
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论