## Matlab and Python Problem

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

### Re: Matlab and Python Problem

What do you mean by "realize" the fish, do you mean weigh? Or are you wanting to make a simple harmonic oscillator-type function?
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

i mean this.....the project in this video it's not mine, but i realized this using KineticsKit on Python...it's really simple...

Now my teacher wants to create something similar using matlab and python....
anastasi.fr

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

### Re: Matlab and Python Problem

This is going to take longer than a weekend. The kineticskit has a lot built into it, and it will take time to replicate. My suggestion is to talk with your teacher about this on Monday and set some reasonable goals.

I am sorry that I cannot help you, but it seems like this is too much given your level of experience and the 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

but i don't need to create the fish using the tool within monday....i only need to view how to write in python the my m.file....please help me...
anastasi.fr

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

### Re: Matlab and Python Problem

There must be some learning here, and me just doing it for you will not help you in any way. Once you have this project complete you will likely have another one assigned, and this will likely be more challenging. So, if I do this for you then you will not be prepared for the next challenge.

There is a fundamental lack of understanding of how Python works in numpy/scipy. You must understand what is going on with the matrices/arrays and be able to make a complete break from MatLab (unless you are writing MatLab scripts). Running MatLab in Python is no benefit at all for you at this point. Before I knew this was a class assignment (which I should have known from the beginning) I made some few changes to your original code. I will post it below, with a clear indicator of where I stopped making meaningful changes. From this you should be able to grasp some of the differences between MatLab and Python syntax, but you really must spend many hours working in this to understand it. If you work hard you may be able to pull this off over the weekend, but my guess is this is too challenging. I am sorry that I cannot help more, but such is life.

Something work checking out, Kahn has many tutorials, including some for Python. https://www.khanacademy.org/science/computer-science

Code: Select all
`import numpy as npfrom scipy import integrate#import 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[0:N*3:3]     y = u0[1:N*3:3]      z = u0[2:N*3:3]    vx = u0[3*N+0:6*N:3]     vy = u0[3*N+1:6*N:3]     vz = u0[3*N+2:6*N:3]         # Reshape u0 for ease    u0 = u0.reshape( [u0.shape[0]*u0.shape[1],1],order='F' )        # Create output array    u = np.zeros(u0.shape)        for i in xrange(0,N):        Fx = 0        Fy = 0        Fz = 0        for j in xrange(0,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                    #-------------------------------------------------------------------------------------------# No more meaningful changes to this function...                u(1 + 3 * (i - 1)).lvalue = u0[3 * N + 1 + 3 * (i - 1)-1]    # dx        u(2 + 3 * (i - 1)).lvalue = u0[3 * N + 2 + 3 * (i - 1)-1]    # dy        u(3 + 3 * (i - 1)).lvalue = u0[3 * N + 3 + 3 * (i - 1)-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    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(molle(tspan, u0, A, l, k, m)).set_integrator('dopri5')`
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

thank the same....
anastasi.fr

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

### Re: Matlab and Python Problem

anastasi.fr

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

### Re: Matlab and Python Problem

I'm tired of this whining and begging for the work to be done for you. Future begging will be deleted and if I get tired of that, I'm going to ban you and/or try to figure out which school you're at and report you for academic misconduct.

We do want to help you. So try to actually learn something, and if you have any specific questions while you're doing your own work, feel free to seek our help. But seriously, stop whining.

Everyone else: If he PMs anyone, as people in his situation are apt to do, please let me know.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.

micseydel

Posts: 1936
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Matlab and Python Problem

You are quite right....so i think that i will start with learning how to could rewrite the same code using python coding and if i have a problem i will ask you help....
thanks to everybody...
anastasi.fr

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

### Re: Matlab and Python Problem

Now...i am arrived here...it'all python code but it not works
Code: Select all
`import numpy as npfrom scipy.integrate import odeimport scipyfrom pylab import *def molle(tspan,u0,A,k,l,m):    #    u=([[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]])    N = 2    x = u0.flatten()[0:3:8]    y = u0.flatten()[1:3:8]    z = u0.flatten()[2:3:8]    vx = u0.flatten()[8 + 0:3:17]    vy = u0.flatten()[8 + 1:3:17]    vz = u0.flatten()[8 + 2:3:17]        for i in range(0,N-1):        Fx = 0        Fy = 0        Fz = 0        print "ciccio"         for j in range(0,N-1):               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                            u[0 + 2 * (i - 1)] = u0[2 * N + 0 + 2 * (i - 1)]    # dx        u[1 + 2 * (i - 1)] = u0[2 * N + 1 + 2 * (i - 1)]    # dy        u[2 + 2 * (i - 1)] = u0[2 * N + 2 + 2 * (i - 1)]    # dz        u[2 * N + 0 + 2 * (i - 1)] = Fx / m[i]    # ax=F/m        u[2 * N + 1 + 2 * (i - 1)] = Fy / m[i]    # ay=F/m        u[2 * N + 2 + 2 * (i - 1)] = Fz / m[i]   # az=F/m    print "ciao"    return transpose(u)x0 = array([-1, 0, 1])y0 = array([0, 0.5, 0])z0 = array([0, 0, 0])Vx0 = array([0, 0, 0])Vy0 = array([0, 1, 0])Vz0 = array([0, 0, 0])A =array([[0,1,0],[1,0,1],[0,1,0]])l = Ak = Am = array([1, 1, 1])u0=array([[transpose(x0)],[transpose(y0)],[transpose(z0)],[transpose(Vx0)],[transpose(Vy0)],[transpose(Vz0)]])tspan=linspace(0,10,100)u = scipy.integrate.ode(molle(tspan,u0,A,l,k,m)).set_integrator('dopri5')`
...

i write teh code by myself without trying to translate from matlab....now can you help me?
anastasi.fr

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

### Re: Matlab and Python Problem

anastasi.fr wrote:it'all python code but it not works

....now can you help me?

Hi, can you please show the error message you receive, maybe even a way for us to reproduce the error on our side, as well as an explanation what is happening wrong, and what you would expect to happen instead. Thank you!

anastasi.fr wrote:i am writing in matlab a code to simulate a mass spring system

I don't understand the details of this. Does it have anything to do with Physics?

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap

Posts: 527
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: Matlab and Python Problem

Kebap: I think he just needs to build a simple harmonic oscillator (look at the video link he posted last week). It is easy enough to do this on paper (though I haven't done it since my quantum and differential equations courses...many moons ago), and simple enough to model. The problem we had last week is his lack of understanding Python fundamentals, which prohibited us from even getting out of the gate.
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

it's not an armonic oscillator....i need to create in python 3 mass connected with two spring....but not in 2D in 3D....
Code: Select all
`import numpy as npfrom numpy import *from pylab import *from scipy.integrate import odeimport matplotlib.pyplot as pltfrom scipy.special import gamma, airyimport warningsdef molle(u0,time):    N = 1    x = u0.flatten()[0:4:2]    y = u0.flatten()[1:4:2]    vx = u0.flatten()[4 + 0:9:2]    vy = u0.flatten()[4 + 1:9:2]    for i in range(0,N):          Fx = 0          Fy = 0          for j in range(0,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               u[0 + 2 * (i-1)] = u0[2 * N + 0 + 2 * (i-1)]    # dx    u[1 + 2 * (i-1)] = u0[2 * N + 1 + 2 * (i-1)]    # dy    u[2 * N + 0 + 2 * (i-1)] = Fx / m[i]    # ax=F/m    u[2 * N + 1 + 2 * (i-1)] = Fy / m[i]    # ay=F/m    return ux0 = array([-1, 1])print x0y0 = array([0, 0])print y0Vx0 = array([1, 0])Vy0 = array([0, 0])A =array([[0,1],[1,0]])print Al = Ak = Am = array([1, 1])u0=array([[x0[0]],[x0[1]],[y0[0]],[y0[1]],[Vx0[0]],[Vx0[1]],[Vy0[0]],[Vy0[1]]])print u0# Create the time samples for the output of the ODE solver.# I use a large number of points, only because I want to make# a plot of the solution that looks nice.t0=0t1=10time = arange(0,10, 1)ychk = airy(time)[0]print ychk[:36:6]solver = ode(molle).set_integrator('vode', nsteps=1)print solversolver.set_initial_value(u0, time)print solversol=([[],[]])while solver.successful():    solver.integrate(t1)###backend = 'vode'##backend = 'dopri5'###backend = 'dop853'####solver = ode(molle).set_integrator(backend, nsteps=1)##solver.set_initial_value(u0, t0)###suppress Fortran-printed warning##solver._integrator.iwork[2] = -1####while solver.t<t1:##    solver.integrate(t1, step=True)##   ##sol = []###warnings.filterwarnings("ignore", category=UserWarning)##while solver.t < t1:##    solver.integrate(t1, step=True)##    sol.append([solver.t, solver.y])##warnings.resetwarnings()##sol = np.array(sol)####plt.plot(sol[:,0], sol[:,1], 'b.-')##plt.show()`

here is the error...

Traceback (most recent call last):
File "C:\Python27\Francesco\test.py", line 67, in <module>
solver.integrate(t1)
File "C:\Python27\lib\site-packages\scipy\integrate\_ode.py", line 386, in integrate
self.f_params, self.jac_params)
File "C:\Python27\lib\site-packages\scipy\integrate\_ode.py", line 692, in run
args[5:]))
File "C:\Python27\Francesco\test.py", line 12, in molle
x = u0.flatten()[0:4:2]
AttributeError: 'float' object has no attribute 'flatten'
anastasi.fr

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

Previous