Csound Csound-dev Csound-tekno Search About

harmonic oscillator blues

Date1998-03-31 10:02
Fromkhalid
Subjectharmonic oscillator blues
This is supposed to be a mass attached to a spring:
        _________________
                |	
                S
                |
                M

This little system can be described by this equation:

	F + kx = 0  (Hookes law)

Below is an orchestra+score that realizes the equation.
If you pull the mass(im) it starts to swing.
As expected, the orc produces a sine wave. Now the problem:
the wave fades!! Even though there's now damper!!
Now this shouldn't happen!!

In the orc, there's a derivation. I put

a(n)=(v(n)-v(n-1))/(t(n)-t(n-1)=(v(n)-v(n-1))

because (t(n)-(t(n-1)) is 1 (sample), and similar with v(n).
What's going on here?

kd

;----------start orc---------------------
sr=44100
kr=44100
ksmps=1
nchnls=1

; F = -kx => M*a = -kx => v(n)-v(n-1)=-k/M*x(n) =>
; x(n) = -M/k*(x(n)-x(n-1)-x(n-1)+x(n-2)) = -M/k*(x(n)-2*x(n-1)+x(n-2))
; => (1+M/k)*x(n)=-M/k*(-2*x(n-1)+x(n-2)) =>
; x(n)=-M/(k+M)*(-2*x(n-1)+x(n-2))

        instr   1

im      init    10
ik      init    .01
ix      init    1000    ;       force at x(0)
ic      init    -im/(im+ik)

ax0     init    0	;	a(x(n))
ax1     init    0	;	a(x(n-1))
ax2     init    ix/ic   ;       a(x(n-2)), make F(x(0))=ix

ax0     =       ic*(-2*ax1+ax2)
        out     ax0

ax2     =       ax1
ax1     =       ax0

        endin
;---------------start score-------------------
i1 0 2