将Matlab代码转换为Python的帮助,技巧和指导

问题描述:

我是一名沉重的Matlab程序员,需要完成Matlab本身无法完成的任务.为此,我选择了Python.我已经完成了Codecademy Python课程,并且正在阅读Zed Shaw的艰难地学习Python" .另外,我已经熟悉NumPy和SciPy.

I am a heavy Matlab programmer and need to accomplish a task which Matlab can't do natively. For this purpose, I have chosen Python. I have completed the Codecademy Python course and am going through Zed Shaw's Learn Python the Hard Way. Also, I have familiarized myself with NumPy and SciPy.

在过去的一周中,我尝试了几乎所有东西,从Matlab到Python的转换器到Matlab和Python之间的接口.我也花了很多时间直接用Python进行编码.但是我只是无法使任何东西正常工作.有了我的Matlab代码,我将提供我所苦苦挣扎的领域的清单.

Over the last week, I have tried almost everything from Matlab-to-Python converters to interfaces between Matlab and Python. I have also spent a lot of time coding directly in Python. But I just haven't been able to get anything to work. Given my Matlab code, I will provide a list of areas with which I am struggling.

Matlab代码: http://pastebin.com/4tkne94Z

Matlab code: http://pastebin.com/4tkne94Z

此代码的作用:它解决了一个常微分方程组,并输出了一个图.外部函数Array的输入参数N指定系统中ODE的数量,其中N为偶数. (ODE使用求和符号定义并进行相应索引.这就是为什么它们可以泛化的原因.)

What this code does: It solves a general system of ordinary differential equations and outputs a graph. The input parameter N to the outer function Array specifies the number of ODEs in the system, with N being an even number. (The ODEs are defined using summation notation and indexed accordingly. That is why they are generalizable.)

使用Python的目标:要运行一个以2为步长在N范围从2到1000循环的脚本,由于Matlab的开销,这几乎是不可能的,但在Python中是可行的(特别是考虑到我可以访问超级计算集群).

Goal of using Python: To run a script that loops over N ranging from 2 to 1000, in steps of 2. This would be almost impossible with Matlab due to its overhead, but feasible in Python (especially given that I have access to a supercomputing cluster).

翻译问题:我对Python语法感到满意.但是对于函数,包括外部函数,内部函数和嵌套函数,却没有那么多.因此,我的翻译尝试是徒劳的.

Issues with translation: I am comfortable with Python syntax. But not so much with functions, including outer, inner, and nested ones. Thus my attempts at translation have been futile.

  1. 如何在Python中相应地在代码中定义函数?
  2. 我应该在哪里纳入主要的for循环系列?
  3. 最后,我不确定如何在Python中正确定义全局变量!

感谢您的帮助.

我在matlab中的经验有限,但这是基于查看代码的输入

My experience in matlab is limited, but here's my input based off of looking at your code

  1. 我将在主函数之外定义EqnsArray,并将N添加为参数之一.
  2. 如果您使用的是matlab,我不认为这将需要更改
  3. 基于我所看到的,我认为您不需要将任何内容声明为全局.函数内部未定义的任何在函数内部遇到的变量都将被视为python中的全局变量.