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 8148763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:42:35+00:00 2026-06-06T14:42:35+00:00

I have built a pendulum simulation using fourth order Runge-Kutta differentiation where everything is

  • 0

I have built a pendulum simulation using fourth order Runge-Kutta differentiation where everything is done step by step:

from scipy import *
from matplotlib.pyplot import *
##A pendulum simulation using fourth order 
##Runge-Kutta differentiation

ts=.05 #time step size
td=20 #trial duration
te=int(td/ts) #no of timesteps

mu=0.1 #friction factor
m=1 #mass
g=9.81 #grav. acceleration
l=1 #length

th=[((rand()*2)-1)*pi] #initial angle
om=[0] #initial angular velocity
u=0 #torque

for j in range(te):
    #Euler approximation
    th.append(th[j] + ts*om[j])
    f1 = (-mu*om[j] + m*g*l*sin(th[j]) + u)/(m*(l^2))
    om.append(om[j] + ts*f1)

    #approximation 1 at mid-interval
    th2 = th[j+1] + (ts/2)*om[j+1]
    f2 = (-mu*om[j+1] + m*g*l*sin(th[j+1]) + u)/(m*(l^2))
    om2 = om[j+1] + (ts/2)*f2

    #approximation 2 at mid-interval
    th3 = th2 + (ts/2)*om2
    f3 = (-mu*om2 + m*g*l*sin(th2) + u)/( m*(l^2))
    om3 = om2 + (ts/2)*f3

    #approximation at next time step
    th4 = th3 + (ts)*om3
    f4 = (-mu*om3 + m*g*l*sin(th3) + u)/( m*(l^2))
    om4 = om3 + (ts)*f4

    dth=(om[j] + 2*om[j+1] + 2*om2 + om3)/6
    dom=(f1 + 2*f2 + 2*f3 + f4)/6
    th[j+1] = th[j] + ts*dth
    om[j+1] = om[j] + ts*dom

subplot(211),plot(th),xlabel('Angle'),ylabel('')
subplot(212),plot(om,'r'),xlabel('Angular velocity'),ylabel('')
show()

This works wonderfully but I don’t find elegant which is why I decided to rewrite it using the odeint() function:

from scipy import integrate
from scipy import *
from matplotlib.pyplot import *
##A pendulum simulation

th=((rand()*2)-1)*pi #initial angle
om=0 #initial angular velocity
u=0 #torque

y0 = [om, th] #initial values
t = linspace(0, 20, 400) #

def f(y, t):
    mu=0.1 #friction factor
    m=1 #mass
    g=9.81 #grav. acceleration
    l=1 #length

    return (y[0],
        (-mu*y[0] + m*g*l*sin(y[1]) + u)/(m*(l^2)))


r = integrate.odeint(f, y0, t)

subplot(211),plot(t,r[:,1]),xlabel('Angle'),ylabel('')
subplot(212),plot(t,r[:,0],'r'),xlabel('Angular velocity'),ylabel('')
show()

And this new version looks nothing like a pendulum. Can anyone tell me what I’m doing wrong?

  • 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-06-06T14:42:37+00:00Added an answer on June 6, 2026 at 2:42 pm

    You’ve got mistakes in your derivative function for the motion for the pendulum.

    In python ^ is exclusive-or operator, rather than the power operator, which is **. So you’ll to use l**2. Also, AFAICT, the first element of the state vector should actually be y[1]. From similar work in the past this worked (assumed m = 1 and l = 1).

    def simple_pendulum_deriv(x, t, g = 9.81, mu = 0.5): 
        nx = np.zeros(2)
        nx[0] = x[1]
        nx[1] = -(g * np.sin(x[0])) - mu*x[1]
        return nx
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have built application using codeigniter, my controller name is userReg , in controller
I have built an enumeration in javascript using the following syntax: MyEnum = {
I have built a C++ exe using my own makefiles. I have Eclipse CDT.
I have built ffmpeg for Android using the code and method described at https://github.com/halfninja/android-ffmpeg-x264
I have built a music player which loads songs from a database in a
I have built a mobile app using Rhomobile, jQuery Mobile and Google Maps API.
I have built a simple StreamGeometry, using MSDN example: StreamGeometry geometry = new StreamGeometry();
I have built a small app using javascript. I am using javascript for form
I have built an app using Qt creator for symbiyan mobile; now, I am
I have built a processor using PTLSIM and want to test it, for educational

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.