如何在Python函数内更改依赖其他全局变量的全局变量。

huangapple go评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月8日 06:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定