5

I'm a Python beginner. I'm trying to switch some programs that I have in matlab. I need solve a stiff ode equation, whose inputs are all matrices. In matlab I use

[ttT,uT] = ode23s('SST',t,fT);
Alex
  • 9,313
  • 1
  • 39
  • 44
marco
  • 915
  • 4
  • 17
  • 35
  • 3
    Can you describe it with mathematic notation as well? Not everyone is familiar with matlab. – Thomas Ahle Jan 05 '12 at 10:34
  • This may be of use: http://stackoverflow.com/questions/2088473/integrate-stiff-odes-with-python –  Jan 05 '12 at 10:48
  • ode23s is a solver to stiff equations in Matlab in this example, SST is an aux function, which stablish a State-Space relation, z = A * z + B, A and B are matrixes. A is square and B is column. t is an array with temporal increments and fT is an array with the initial conditions. – marco Jan 05 '12 at 15:44

2 Answers2

4

For most things you do in Matlab, you can do them with the NumPy module in Python. It can be found here.

You might also find the related module SciPy useful as well.

PyDSTool might also be of relevance to you. It's a wrapper around the Radau solver.

Then you might like to try matplotlib for plotting. It works quite like Matlab's plotting thing.

The following links might help, too:

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
G S
  • 35,511
  • 22
  • 84
  • 118
  • Thank you very much. I already installed numpy and scipy. With google, I found the mrgates page. However, I don't know how to install his "module". I think Gates' approach turns everything simpler. i'm not EPD subscriber. – marco Jan 05 '12 at 15:01
  • The last link, I'm sorry, but it's too fuzzy for me. I didn't understand which is useful or not! I "just" want a simple thing, but I'm figure in python, it isn't ! – marco Jan 05 '12 at 15:07
  • To install Gates' module follow these steps: 1) Unzip the archive. 2) Say you unzipped into C:\ode-0.1a4.tar. Confirm that there's a file called 'setup.py' inside this folder. 3) Now open the commandline at this folder and run this command: 'python setup.py install'. This should install the module on your machine. Now you can go and import this module and use it as Mr. Gates describes on his page. – G S Jan 06 '12 at 07:51
  • Sorry about the EPD link in case it requires subscription. I'll remove it. – G S Jan 06 '12 at 07:52
  • I've added links to PyDSTool and matplotlib modules. Do take a look at them too. – G S Jan 06 '12 at 08:04
  • Thank you @Frederick . I'm looking careful to all of this information. I think, my primary goal is solve. (get an array of results). I think "gates way" is the best. I did "python setup.py" in the folder and module installed with some warnings. – marco Jan 06 '12 at 14:12
  • On aptana I use the following sample code from http://www.ews.uiuc.edu/~mrgates2/ode/ . But module isn't "resolved". – marco Jan 06 '12 at 14:12
  • All information that you gave to me is useful to Switch from matlab to Python. However, If I'm not able to solve ode, I can't switch at all =) – marco Jan 06 '12 at 14:16
1

If you show me the differential equations I can help you a little more, but in general, a good way to solve a stiff ODE system is through the next sentence:

solution = scipy.integrate.solve_ivp(function, [t_0, t_f], y0, method='BDF', first_step =0.0001, dense_output=True)

where your function has to be defined previously in this way: function(t,variable,parameters)

t_0 = initial value for time
t_f = final value for time
y0  = value of your variables at t_0

For stiff ODE system, I suggest use the method 'BDF' (is typically used in the solve of microkinetic systems in reactors, where the importance of the time could change a lot)

for more infomation about the options of the code: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html