## Matlab and Python Problem

This is the place for queries that don't fit in any of the other categories.

### Matlab and Python Problem

hello, my name is Francesco...
i am writing in matlab a code to simulate a mass spring system, here there is the code:
test.m
Code: Select all
`x0=[-1 0 1];y0=[0 1/2 0];z0=[0 0 0];Vx0=[0 0 0];Vy0=[0 1 0];Vz0=[0 0 0];A=[0 1 0; 1 0 1; 0 1 0];l = A ;k = A ;m = [1 1 1];u0 = [x0';y0';z0';Vx0';Vy0';Vz0'];tspan=[0 10];[t,u] = ode45(@molle,tspan,u0,[],A,l,k,m);x=u(:,1:3:3*size(A,1));y=u(:,2:3:3*size(A,1));z=u(:,3:3:3*size(A,1));vx=u(:,3*size(A,1)+1:3:6*size(A,1));vy=u(:,3*size(A,1)+2:3:6*size(A,1));vz=u(:,3*size(A,1)+3:3:6*size(A,1));maxX=max(max(x));maxY=max(max(y));maxZ=max(max(z));minX=min(min(x));minY=min(min(y));minZ=min(min(z));figure,for k = 1 : length(t)    [aa bb]=view;    plot3(x(k,:),y(k,:),z(k,:),'*')    view(aa,bb)    axis([minX maxX minY maxY minZ maxZ])    grid on    pause(0.5)end`

molle.m
Code: Select all
`function u = molle(tspan,u0,A,l,k,m)N=size(A,1)x=u0(1:3:3*N);y=u0(2:3:3*N);z=u0(3:3:3*N);vx=u0(3*N+1:3:6*N);vy=u0(3*N+2:3:6*N);vz=u0(3*N+3:3:6*N);for i = 1 : N    Fx=0;    Fy=0;    Fz=0;    for j = 1 : N        if A(i,j) ~= 0        d=((x(i)-x(j))^2+(y(i)-y(j))^2+(z(i)-z(j))^2)^0.5;        Fx=Fx+k(i,j)*(abs(x(j)-x(i))-l(i,j))*(x(j)-x(i))*A(i,j)/d;        Fy=Fy+k(i,j)*(abs(y(j)-y(i))-l(i,j))*(y(j)-y(i))*A(i,j)/d;         Fz=Fz+k(i,j)*(abs(z(j)-z(i))-l(i,j))*(z(j)-z(i))*A(i,j)/d;        end    end    u(1+3*(i-1))=u0(3*N+1+3*(i-1)); % dx    u(2+3*(i-1))=u0(3*N+2+3*(i-1)); % dy    u(3+3*(i-1))=u0(3*N+3+3*(i-1)); % dz    u(3*N+1+3*(i-1))=Fx/m(i) ;  % ax=F/m    u(3*N+2+3*(i-1))=Fy/m(i) ;  % ay=F/m    u(3*N+3+3*(i-1))=Fz/m(i) ;  % az=F/mendu=u';        `

i don't know how to write in python ode45 function....could you help me please????

Francesco
Last edited by Mekire on Fri Oct 18, 2013 11:24 am, edited 1 time in total.
Reason: First post lock.
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

So you're looking for a python equivalent to matlab's ode45?

I've never used it, but some googling suggests that the equivalent function is:
Code: Select all
`scipy.integrate.ode(f).set_integrator('dopri5')`

Of course, this will require that you have scipy installed.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Matlab and Python Problem

Now I wrote this:
Code: Select all
`import numpy as npfrom scipy import integrateimport ompcfrom matplotlib.pylab import *from numpy import *from numpy import matrixfrom numpy import linalgimport scipyfrom matplotlib import pyplotdef molle(tspan, u0, A, l, k, m):    N = 3    x = u0(mslice[1:3:3 * N])    y = u0(mslice[2:3:3 * N])    z = u0(mslice[3:3:3 * N])    vx = u0(mslice[3 * N + 1:3:6 * N])    vy = u0(mslice[3 * N + 2:3:6 * N])    vz = u0(mslice[3 * N + 3:3:6 * N])    for i in mslice[1:N]:        Fx = 0        Fy = 0        Fz = 0        for j in mslice[1:N]:            if A(i, j) != 0:                d = ((x(i) - x(j)) ** 2 + (y(i) - y(j)) ** 2 + (z(i) - z(j)) ** 2) ** 0.5                Fx = Fx + k(i, j) * (abs(x(j) - x(i)) - l(i, j)) * (x(j) - x(i)) * A(i, j) / d                Fy = Fy + k(i, j) * (abs(y(j) - y(i)) - l(i, j)) * (y(j) - y(i)) * A(i, j) / d                Fz = Fz + k(i, j) * (abs(z(j) - z(i)) - l(i, j)) * (z(j) - z(i)) * A(i, j) / d            end        end        u(1 + 3 * (i - 1)).lvalue = u0(3 * N + 1 + 3 * (i - 1))    # dx        u(2 + 3 * (i - 1)).lvalue = u0(3 * N + 2 + 3 * (i - 1))    # dy        u(3 + 3 * (i - 1)).lvalue = u0(3 * N + 3 + 3 * (i - 1))    # dz        u(3 * N + 1 + 3 * (i - 1)).lvalue = Fx / m(i)    # ax=F/m        u(3 * N + 2 + 3 * (i - 1)).lvalue = Fy / m(i)    # ay=F/m        u(3 * N + 3 + 3 * (i - 1)).lvalue = Fz / m(i)    # az=F/m    end    u = u.cTx0=matrix([-1,0,1])y0=matrix([0,0.5,0])z0=matrix([0,0,0])Vx0=matrix([0,0,0])Vy0=matrix([0,1,0])Vz0=matrix([0,0,0])A=matrix([[0,1,0],[1,0,1],[0,1,0]])print Al=Ak=Am=([1,1,1])u0=array([transpose(x0),transpose(y0),transpose(z0),transpose(Vx0),transpose(Vy0),transpose(Vz0)])tspan=matrix([0,10])[t,u]=scipy.integrate.ode(f).set_integrator('dopri5')`

Traceback (most recent call last):
File "C:/Python27/Francesco/bo", line 61, in <module>
[t,u]=scipy.integrate.ode(f).set_integrator('dopri5')
TypeError: 'ode' object is not iterable

i don't understad where is the problem..
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

Well, the code I've shown you gives you an scipy.integrate.ode object.
I'm guessing you need to do some stuff with it before you get the results.

But I don't know matlab, and haven never used this part of scipy, so I really can't give you any specifics.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Matlab and Python Problem

It could be a big problem...i need to complete the work within monday....
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

ther's no one????
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

I'm guessing others have, like myself, never used scipy.interpolate.ode before.

Even if they have, you haven't shown any effort to understand how to use it properly.
If you have tried, post some example code that didn't work.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Matlab and Python Problem

Code: Select all
`def molle(tspan,u0,A,l,k,m):    N = size(A, 1)    x = u0(mslice[1:3:3 * N])    y = u0(mslice[2:3:3 * N])    z = u0(mslice[3:3:3 * N])    vx = u0(mslice[3 * N + 1:3:6 * N])    vy = u0(mslice[3 * N + 2:3:6 * N])    vz = u0(mslice[3 * N + 3:3:6 * N])    for i in mslice[1:N]:        Fx = 0        Fy = 0        Fz = 0        for j in mslice[1:N]:            if A(i, j) != 0:                d = ((x(i) - x(j)) ** 2 + (y(i) - y(j)) ** 2 + (z(i) - z(j)) ** 2) ** 0.5                Fx = Fx + k(i, j) * (abs(x(j) - x(i)) - l(i, j)) * (x(j) - x(i)) * A(i, j) / d                Fy = Fy + k(i, j) * (abs(y(j) - y(i)) - l(i, j)) * (y(j) - y(i)) * A(i, j) / d                Fz = Fz + k(i, j) * (abs(z(j) - z(i)) - l(i, j)) * (z(j) - z(i)) * A(i, j) / d            end        end        u(1 + 3 * (i - 1)).lvalue = u0(3 * N + 1 + 3 * (i - 1))    # dx        u(2 + 3 * (i - 1)).lvalue = u0(3 * N + 2 + 3 * (i - 1))    # dy        u(3 + 3 * (i - 1)).lvalue = u0(3 * N + 3 + 3 * (i - 1))    # dz        u(3 * N + 1 + 3 * (i - 1)).lvalue = Fx / m(i)    # ax=F/m        u(3 * N + 2 + 3 * (i - 1)).lvalue = Fy / m(i)    # ay=F/m        u(3 * N + 3 + 3 * (i - 1)).lvalue = Fz / m(i)    # az=F/m    end    u = u.cTx0 = mcat([-1, 0, 1])y0 = mcat([0, 1 / 2, 0])z0 = mcat([0, 0, 0])Vx0 = mcat([0, 0, 0])Vy0 = mcat([0, 1, 0])Vz0 = mcat([0, 0, 0])A = mcat([0, 1, 0, OMPCSEMI, 1, 0, 1, OMPCSEMI, 0, 1, 0])l = Ak = Am = mcat([1, 1, 1])u0 = mcat([x0.cT, OMPCSEMI, y0.cT, OMPCSEMI, z0.cT, OMPCSEMI, Vx0.cT, OMPCSEMI, Vy0.cT, OMPCSEMI, Vz0.cT])tspan = mcat([0, 10])[t, u] = molle( tspan, u0,A, l, k, m)x = u(mslice[:], mslice[1:3:3 * size(A, 1)])y = u(mslice[:], mslice[2:3:3 * size(A, 1)])z = u(mslice[:], mslice[3:3:3 * size(A, 1)])vx = u(mslice[:], mslice[3 * size(A, 1) + 1:3:6 * size(A, 1)])vy = u(mslice[:], mslice[3 * size(A, 1) + 2:3:6 * size(A, 1)])vz = u(mslice[:], mslice[3 * size(A, 1) + 3:3:6 * size(A, 1)])maxX = max(max(x))maxY = max(max(y))maxZ = max(max(z))minX = min(min(x))minY = min(min(y))minZ = min(min(z))figure()for k in mslice[1:length(t)]:    [aa, bb] = view    plot3(x(k, mslice[:]), y(k, mslice[:]), z(k, mslice[:]), mstring('*'))    view(aa, bb)    axis(mcat([minX, maxX, minY, maxY, minZ, maxZ]))    grid(mstring('on'))    pause(0.5)end`

but not works....
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

I looked at your original code. On your line 61 (where you are getting the error) are you sure you know what <f> is? I don't think you do, and this is the problem. After running your code, I get the same error as you: "ode object is not iterable". This raises the question: What is <f>?

Code: Select all
`>>> f<built-in method f of mtrand.RandomState object at 0x00000000024D5198>`

Aha! You imported numpy (from numpy import *...problematic) AFTER importing scipy. <f> is a numpy method. If you type "help(np.random.mtrand.RandomState.f" into your console you will see that this returns information from an f distribution. There is an <f> parameter in the integrate.ode, which I think you may be getting confused by. Also, I don't see <f> defined anywhere, which needs to be accounted for.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

### Re: Matlab and Python Problem

anastasi.fr wrote:[code]
for i in mslice[1:N]:....

Where is mslice defined?
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

### Re: Matlab and Python Problem

f is molle.....i made a mistake....but if I replace it with molle, doesn't work....
please could you modify the code??I'm going crazy....
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

molle requires input parameters (tspan, u0, A, l, k, m), so you should have:

Code: Select all
`[t,u] = scipy.integrate.ode(molle(tspan,u0,A,l,k,m)).set_integrator('dopri5')`

However, we still do not know what mslice is...
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

### Re: Matlab and Python Problem

this is the matlab code for matlab:
Code: Select all
`x=u0(1:3:3*N);`

mslice is the translation i found on web...
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

it's important...
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

Why are you trying to do this in Python, why not just stick with MatLab?
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

### Re: Matlab and Python Problem

because my teacher thinks that python is more powerful than matlab....i don't think so...but i don't care about, i didn't never used python and i am trying to learn using some tutorials on youtube...but no one could help me on this problem....
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

Then it doesn't make sense to use ompc. The point would be to actually use Python, not call Matlab scripts from Python.
I've used both and I would say, in general, Python is more powerful than MatLab. Python is an actual programming language that is quite versatile. MatLab is good too, it just has more limited functionality and costs quite a lot.

This is not the type of problem you want to tackle if you are just learning Python. To make your code work there are many changes that must be made (you really need to get away from ompc). Perhaps if you were to start from scratch you would have a better chance. As is, I think the learning curve is too high for a 2-3 day timeline.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

### Re: Matlab and Python Problem

could you write it for me, please?
I am really in trouble....
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

### Re: Matlab and Python Problem

That's not the point. The purpose of your assignment is to learn to do it on your own. If someone else does it for you then you learn nothing.

Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

### Re: Matlab and Python Problem

we are trying to realize a fish using a mass-spring system...i create it using matlab,not so difficul, using kineticsKit in Python....
but my teacher want it in python code without using the kineticsKit, i don't know why....i had the assignment three days ago...and i must complete it within monday, but my teacher thinks that if i am able to use python with the kinetickKit i could use python in general....
so i am in trouble...
anastasi.fr

Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

Next