4

I am studying the dynamics of a damped, driven pendulum with second order ODE defined like so, and specifically I am progamming:

d^2y/dt^2 + c * dy/dt + sin(y) = a * cos(wt)

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate


def pendeq(t,y,a,c,w):
    y0 = -c*y[1] - np.sin(y[0]) + a*np.cos(w*t)
    y1 = y[1]
    z = np.array([y0, y1])
    return z

a = 2.1
c = 1e-4
w = 0.666667     # driving angular frequency

t = np.array([0, 5000])   # interval of integration
y = np.array([0, 0])      # initial conditions

yp = integrate.quad(pendeq, t[0], t[1], args=(y,a,c,w))

This problem does look quite similar to Need help solving a second order non-linear ODE in python, but I am getting the error

Supplied function does not return a valid float.

What am I doing wrong??

Community
  • 1
  • 1
Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75
  • It looks like you're solving for the position of the pendulum as a function of several parameters, is that right? – Cody Piersall Nov 06 '14 at 01:30
  • Yes, that's correct. – Medulla Oblongata Nov 06 '14 at 01:31
  • 2
    `quad` integrates a scalar function. It does not solve an ordinary differential equation. Look again at the example you linked to--`scipy.integrate.odeint` (not `quad`) is used to generate the solution. See also http://wiki.scipy.org/Cookbook/CoupledSpringMassSystem – Warren Weckesser Nov 06 '14 at 02:17

1 Answers1

3

integrate.quad requires that the function supplied (pendeq, in your case) returns only a float. Your function is returning an array.

Cody Piersall
  • 8,312
  • 2
  • 43
  • 57
  • I've answered the part of the question "What am I doing wrong?", but I'm still trying to figure out how to do it right. I'll edit my answer if I can figure it out. At this point it's more of a physics problem than a programming problem. – Cody Piersall Nov 06 '14 at 01:34