How to translate matlab to python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chriskim
    New Member
    • Jul 2010
    • 5

    How to translate matlab to python

    Hi,
    I am trying to translate a simple program that was written in matlab to python.

    I uses arrays to plot multiple data in single graph.
    I wrote the python according to the matlab language.
    But is seems like i made some mistake along the way.

    Please help me
    Thank you in advance

    This is matlab code that actually works

    Code:
    subplot(5,4,1)  
    a=0.02; b=0.2;  c=-65;  d=6; 
    V=-70;  u=b*V; 
    VV=[];  uu=[]; 
    tau = 0.25; tspan = 0:tau:100; 
    T1=tspan(end)/10; 
    for t=tspan 
        if (t>T1)  
            I=14; 
        else 
            I=0; 
        end; 
        V = V + tau*(0.04*V^2+5*V+140-u+I); 
        u = u + tau*a*(b*V-u); 
        if V > 30 
            VV(end+1)=30; 
            V = c; 
            u = u + d; 
        else 
            VV(end+1)=V; 
        end; 
        uu(end+1)=u; 
    end; 
    plot(tspan,VV,[0 T1 T1 max(tspan)],-90+[0 0 10 10]); 
    axis([0 max(tspan) -90 30]) 
    axis off; 
    title('(A) tonic spiking');
    This is my python code.
    Code:
    import sys 
    from math import * 
    from scipy.integrate import odeint # ODE integration 
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 
    from matplotlib.figure import Figure 
    import numpy 
    import pylab 
      
    a = 0.02 
    b = 0.2 
    c = -65 
    d = 6 
    V = -70 
    u = b * V 
    VV = [] 
    uu = [] 
    tau = 0.25 
    tspan = numpy.arange(0,101,tau) 
    T1 = tspan(-1)/10 
      
    for t in tspan: 
        if t>T1: 
            I = 14 
        else:  
            I = 0 
        V = V + tau*(0.04*V^2+5*V+140-u+I) 
        u = u + tau*a*(b*V-u) 
        if V>30: 
            VV[end+1]=30 
            V = c 
            u = u + d 
        else: 
            VV[end+1] = V 
        uu[end+1] = u 
    pylab.plot(tspan,VV) 
    pylab.show()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    But is seems like i made some mistake along the way.
    Those of us who don't use MatLab have no way a knowing what the mistakes are. Print the results of the calculations using known values, so you can tell where the errors are. Then, post any problems with an explanation of what the code is supposed to do. Note the following line will yield the integer portion only with Python2.x versions
    T1 = tspan(-1)/10
    If you want a floating point, you must convert to a floating point
    T1 = tspan(-1)/10.0

    Comment

    Working...