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

Postby tnknepp » Fri Oct 18, 2013 7:23 pm

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
User avatar
tnknepp
 
Posts: 118
Joined: Mon Mar 11, 2013 7:41 pm

Re: Matlab and Python Problem

Postby anastasi.fr » Fri Oct 18, 2013 7:31 pm

http://www.youtube.com/watch?v=gHjzqHCf348

i mean this.....the project in this video it's not mine, but i realized this using KineticsKit on Python...it's really simple...
here is the url where you can find the tool http://kineticskit.sourceforge.net/#Download

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

Postby tnknepp » Fri Oct 18, 2013 8:27 pm

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
User avatar
tnknepp
 
Posts: 118
Joined: Mon Mar 11, 2013 7:41 pm

Re: Matlab and Python Problem

Postby anastasi.fr » Fri Oct 18, 2013 8:33 pm

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

Postby tnknepp » Fri Oct 18, 2013 8:51 pm

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 np
from scipy import integrate
#import ompc
from matplotlib.pylab import *
from numpy import *
from numpy import matrix
from numpy import linalg
import scipy
from matplotlib import pyplot

def 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.cT

x0=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 A
l=A
k=A
m=([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
User avatar
tnknepp
 
Posts: 118
Joined: Mon Mar 11, 2013 7:41 pm

Re: Matlab and Python Problem

Postby anastasi.fr » Fri Oct 18, 2013 8:58 pm

thank the same....
anastasi.fr
 
Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

Re: Matlab and Python Problem

Postby anastasi.fr » Fri Oct 18, 2013 9:05 pm

but...i had no time to study python...i need only this code...please...
anastasi.fr
 
Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

Re: Matlab and Python Problem

Postby micseydel » Fri Oct 18, 2013 9:25 pm

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!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Matlab and Python Problem

Postby anastasi.fr » Sat Oct 19, 2013 6:46 am

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

Postby anastasi.fr » Sun Oct 20, 2013 5:03 pm

Now...i am arrived here...it'all python code but it not works
Code: Select all
import numpy as np
from scipy.integrate import ode
import scipy
from 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 = A
k = A
m = 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

Postby Kebap » Tue Oct 22, 2013 9:52 am

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?

Image
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: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Matlab and Python Problem

Postby tnknepp » Tue Oct 22, 2013 12:25 pm

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
User avatar
tnknepp
 
Posts: 118
Joined: Mon Mar 11, 2013 7:41 pm

Re: Matlab and Python Problem

Postby anastasi.fr » Tue Oct 22, 2013 7:39 pm

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 np
from numpy import *
from pylab import *
from scipy.integrate import ode
import matplotlib.pyplot as plt
from scipy.special import gamma, airy
import warnings


def 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 u


x0 = array([-1, 1])
print x0
y0 = array([0, 0])
print y0

Vx0 = array([1, 0])
Vy0 = array([0, 0])


A =array([[0,1],[1,0]])
print A
l = A
k = A
m = 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=0
t1=10
time = arange(0,10, 1)
ychk = airy(time)[0]
print ychk[:36:6]
solver = ode(molle).set_integrator('vode', nsteps=1)
print solver
solver.set_initial_value(u0, time)
print solver
sol=([[],[]])
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

Return to General Coding Help

Who is online

Users browsing this forum: Coder151 and 0 guests