Python Help with Python RK4: Solving Error Plotting 1st Order ODE

  • Thread starter Thread starter gulaman
  • Start date Start date
  • Tags Tags
    Python Rk4
AI Thread Summary
The discussion centers on an issue encountered while attempting to plot a first-order ordinary differential equation (ODE) using Python's RK4 method. The user is facing a TypeError indicating that a function object has no length, which occurs when trying to execute the RK4 function. The user shares their code, which includes the definition of the ODE and the setup for time steps. A suggestion is made that the error may stem from not passing the necessary arguments to the function `f` in the RK4 call or that the values of `t[i]` or `Y[i]` could be null. The user seeks assistance to resolve the error and proceed with their work.
gulaman
Messages
18
Reaction score
0
i have been attempting to plot a first order ODE with pyhton's rk4

i can't display the graph and i have been having this error:

Traceback (most recent call last):
File "C:\Documents and Settings\AP155user38\Desktop\gladys python codes\differential equations and runge-kutta\1ODE.py", line 20, in <module>
Y[i+1] = rk4(t, Y, f)
File "c:\python25\lib\site-packages\matplotlib-0.98.3.0001-py2.5-win32.egg\matplotlib\mlab.py", line 928, in rk4
yout = np.zeros( (len(t),), np.float_)
TypeError: object of type 'function' has no len()

i need help in understanding this so i could move on with solving the error. :)

i hope you guys could help me out.

here's my code:

from scipy import *
from pylab import *


h = 0.1 #time step
ti = 0. #start
tf = 10. #end
t = arange(ti, tf, h) #time array

N = len(t) #number of time steps
Y = zeros(N) #stores value of y at each time step

Y[0] = 0. #initial condition

def f(t, Y):
dYdt = -9.81 - 0.5*Y
return dYdt

for i in range(N-1):
Y[i+1] = rk4(t, Y, f)

plot(t,Y)
show()

thanx.

gulaman
Quezon City, Philippines
 
Technology news on Phys.org
I don't use Python but I had "learned" it enough to pass a class.

I think you're getting the error because you're not passing any arguments to f for rk4 to use.

Or

The value of t or Y is a null value.

Hope that helps. :-/
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top