实现方程矩阵:使用循环定义函数(Python)

时间:2017-02-16 10:11:27

标签: python function iteration

对于上下文,我基本上使用数值积分器,它接受一组定义为函数的微分方程。大量的这些函数遵循常规模式,我想在循环中定义它们(或者任何最合适的方式)。例如;

#system coordinates
s = [y1,y2]

#system equations
def e1(s):
    x1 = s[1]**2 + 1
    return x1

def e2(s):
    x1 = s[2]**2 + 2
    return x1

#equations of motion
eom = [e1,e2]

并非所有的功能都会遵循确切的模式,对于那些虽然理想我需要的东西,

def en(s)
    x1 = s[n]**2 + n
    return x1

可以迭代一系列'n'值。谢谢你的任何建议。

2 个答案:

答案 0 :(得分:0)

为什么不简单地在函数中使用第二个参数:

def en(s, n)
    x1 = s[n]**2 + n
    return x1

result = []
for i in range(100):  # 100 is just for illustration purposes..
    result[0] = en(s, i)  # you do not have to store them in a list. just an example

答案 1 :(得分:0)

我会使用 public void CPUMonitorinigThreadCallback() { //If nothing is there in this function, the thread will start up and then immediately shutdown. To deal with this situation, use "_shutdownEvent" //The while loop checks the ManualResetEvent to see if it is "set" or not. while (!_shutdownEvent.WaitOne(0)) { // Replace the Sleep() call with the work you need to do //Sleep(1000); objCPUMonitor.StartCPUMonitorinig(); } } ,将值绑定到函数参数:

partial