473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
1 1980
dwblas
626 Expert 512MB
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

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

Similar topics

13
by: yy0127 | last post by:
I don't know why i entered the below code and it will miss some records. Anyone can help me??? users = {} users1 = {} while 1: user, serviceType, msgType, inOut, date, time, numBytes =
22
by: Ray | last post by:
Hello, I've been learning Python in my sparetime. I'm a Java/C++ programmer by trade. So I've been reading about Python OO, and I have a few questions that I haven't found the answers for :) ...
3
by: Mike | last post by:
public class Broadcaster: MarshalByRefObject, IBroadcaster { public event General.MessageArrivedHandler MessageArrived; public void BroadcastMessage(string msg) { Console.WriteLine("Will...
9
by: lskov | last post by:
Does sombody in here know how i can set an wariable in matlab from python. I have found numpy an pymat, but i can not find out how to use it. Im using python 2.5 and matlab 2006b.
7
by: =?Utf-8?B?QWxleGFuZGVy?= | last post by:
Hi! I want to learn C# in the near future. But for now, I would be more than happy if someone could translate this short c# source code into c++. (I searched the web for c++ equivalents but after...
0
by: Chriskim | last post by:
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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.