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

  • Context: Python 
  • Thread starter Thread starter gulaman
  • Start date Start date
  • Tags Tags
    Python Rk4
Click For Summary
SUMMARY

The forum discussion centers around troubleshooting a Python implementation of the Runge-Kutta 4th order (RK4) method for solving a first-order ordinary differential equation (ODE). The user encounters a TypeError indicating that a function object has no length, which is likely due to incorrect argument handling in the RK4 function. The provided code snippet initializes the ODE and attempts to plot the results using Matplotlib, but fails to execute properly. A suggestion is made to ensure that the function 'f' receives the correct arguments during the RK4 computation.

PREREQUISITES
  • Familiarity with Python programming, specifically version 2.5 or higher.
  • Understanding of numerical methods, particularly the Runge-Kutta method.
  • Basic knowledge of plotting in Python using Matplotlib.
  • Experience with handling functions and arguments in Python.
NEXT STEPS
  • Review the implementation of the RK4 method in Python to ensure proper argument passing.
  • Learn about error handling in Python to better diagnose issues in code execution.
  • Explore Matplotlib documentation for advanced plotting techniques and troubleshooting.
  • Study the principles of solving ordinary differential equations using numerical methods.
USEFUL FOR

Python developers, students learning numerical methods, and anyone interested in solving ordinary differential equations using the RK4 method and visualizing results with Matplotlib.

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. :-/
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
5K
  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 17 ·
Replies
17
Views
3K