Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:24:19+00:00 2026-05-30T18:24:19+00:00

I am trying to run the following script, but get the error IndexError: list

  • 0

I am trying to run the following script, but get the error

IndexError: list index out of range

I have read that this is because when you create a list it is initially empty so you need to assign to it some value which I have done by doing the following

q.append(0)

but I still get the error. Can someone point out what I an doing wrong? Thank you!

import numpy
from numpy import *
import matplotlib.pyplot as plt

pfa = []                        #Create lists that will hold pf,qf values
qfa = []
pf = []
qf = []
p = []
q = []
pf.append(0)
qf.append(0)
p.append(0)
q.append(0)
q[0]  = -0.5         # initial p and q values
p[0]  = 0
h = 0.001
for i in range(10):

  k1 = -h*sin(q[i])
  j1 = h*(p[i])
  k2 = -h*sin(q[i]+(1/2)*j1)
  j2 = h*p[i]*(q[i]+(1/2)*k1)             
  k3 = -h*sin(q[i]+(1/2)*j2)
  j3 = h*p[i]*(q[i]+(1/2)*k2)
  k4 = -h*sin(q[i]+(1/2)*j3)
  j4 = h*p[i]*(q[i]+(1/2)*k3)
  pf[i+1] = p[i] +(h/6.0)*(k1+2*k2+2*k3+k4)
  qf[i+1] = q[i] +(h/6.0)*(j1+2*j2+2*j3+j4)
  pfa.append(pf)                   #append lists
  qfa.append(qf)

plt.plot(qfa,pfa)
plt.show()

the trace back and error

Traceback (most recent call last):
File "C:\Documents and Settings\My Documents\Symplectic Integrators\RK4_2.py", line  23, in <module>
j1 = h*(p[i])
IndexError: list index out of range
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:24:20+00:00Added an answer on May 30, 2026 at 6:24 pm

    Your lists contain only one element, and you are trying to access members at positions 0 to 9. Think about it:

    >>> p = []
    >>> p.append(0)
    >>> p
    [0]
    >>> for i in range(2):
    ...     print "position {0}, value {1}".format(p[i], i)
    ... 
    position 0, value 0
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    IndexError: list index out of range
    

    When you do p[i], p must have at least i + 1 size.

    And take a look about at your pf[i] == p[i], it is an expression, not an assignment.

    Maybe what you need is something like:

    pf = []
    for i in range(10):
        ...
        # at this point pf.append() assigns to position i
    
        pf.append(p[i] + (h / 6.0) * (k1 + 2*k2 + 2*k3 + k4))
        ...
    

    But be careful, because p needs to contain all the values you need before the for loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following error from the SQL Script I am trying to run:
I'm trying to run the following line: Directions.loadFromWaypoints((Waypoint[])waypoints.toArray(), opts); But I'm getting: 23:41:44.595 [ERROR]
I get the following error when trying to run the latest Cygwin version of
When I am trying to run tomcat using startup.bat I get the following error,
I'm pulling my hair out trying to get a jQuery script working that will
Hey everyone, I am trying to run the following program, but am getting a
I have a script that will run on server daily to download data from
I am trying to run the following commands as part of an MSBuild script:
I am trying to run a R script from: http://www.math.tu-berlin.de/~mkeller/R-progs/yahoo_opt.R Description at: http://www.math.tu-berlin.de/~mkeller/index.php?target=rcode I
I am trying to run a ruby script to rename some files but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.