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

How to 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 19 '10 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 624
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
Oct 20 '10 #2

Post your reply

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