By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Please help me translate matlab to python

P: 5
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
Expand|Select|Wrap|Line Numbers
  1. subplot(5,4,1) 
  2. a=0.02; b=0.2;  c=-65;  d=6;
  3. V=-70;  u=b*V;
  4. VV=[];  uu=[];
  5. tau = 0.25; tspan = 0:tau:100;
  6. T1=tspan(end)/10;
  7. for t=tspan
  8.     if (t>T1) 
  9.         I=14;
  10.     else
  11.         I=0;
  12.     end;
  13.     V = V + tau*(0.04*V^2+5*V+140-u+I);
  14.     u = u + tau*a*(b*V-u);
  15.     if V > 30
  16.         VV(end+1)=30;
  17.         V = c;
  18.         u = u + d;
  19.     else
  20.         VV(end+1)=V;
  21.     end;
  22.     uu(end+1)=u;
  23. end;
  24. plot(tspan,VV,[0 T1 T1 max(tspan)],-90+[0 0 10 10]);
  25. axis([0 max(tspan) -90 30])
  26. axis off;
  27. title('(A) tonic spiking');
  28.  
This is my python code.
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. from math import *
  3. from scipy.integrate import odeint # ODE integration
  4. from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  5. from matplotlib.figure import Figure
  6. import numpy
  7. import pylab
  8.  
  9. a = 0.02
  10. b = 0.2
  11. c = -65
  12. d = 6
  13. V = -70
  14. u = b * V
  15. VV = []
  16. uu = []
  17. tau = 0.25
  18. tspan = numpy.arange(0,101,tau)
  19. T1 = tspan(-1)/10
  20.  
  21. for t in tspan:
  22.     if t>T1:
  23.         I = 14
  24.     else: 
  25.         I = 0
  26.     V = V + tau*(0.04*V^2+5*V+140-u+I)
  27.     u = u + tau*a*(b*V-u)
  28.     if V>30:
  29.         VV[end+1]=30
  30.         V = c
  31.         u = u + d
  32.     else:
  33.         VV[end+1] = V
  34.     uu[end+1] = u
  35. pylab.plot(tspan,VV)
  36. pylab.show()
  37.  
  38.  
Oct 15 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.