Inspired by a great video on the Laplace transform, I was looking for an easy way to visualize complex functions of time, i.e., functions of time where the output is a complex number. This is fairly easy to do with Guile Scheme and Gnuplot, though the method could be polished more into a proper application, which I didn’t do.
This is the core function for plotting the data:
(define (complex-plot t0 tmax step f) (unless (> t0 tmax) (let ((z (f t0))) (display t0) (display " ") (display (real-part z)) (display " ") (display (imag-part z)) (display "\r\n") (complex-plot (+ t0 step) tmax step f))))
We just pass in the starting value (
t0), the maximum time value, a step value (usually some fraction of Pi), and a lambda with our complex function of time. Writing complex functions of time is pretty easy in Guile Scheme because all the numbers are represented internally as complex numbers anyway. E.g.:
scheme@(guile-user)> (complex-plot 0 25 (/ 3.1415 32) (lambda (t) (exp (* 0+i t)))) 0 1.0 0.0 0.098171875 0.9951850104692727 0.09801425884672994 0.19634375 0.9807864101254524 0.19508464243304188 0.294515625 0.9569428571883648 0.2902763649975122 0.3926875 0.9238839645735444 0.3826727322450213 ...etc...
We have to output this to a file:
(with-output-to-file "out.txt" (lambda () (complex-plot 0 25 (/ 3.1415 32) (lambda (t) (exp (* 0+i t))))))
Then we start
gnuplot and use a 3d splot with lines. (Credit goes to this tutorial.)
gnuplot> set xrange [0:25] gnuplot> set yrange [-2:2] gnuplot> set ticslevel 0 gnuplot> splot "out.txt" u 1:2:3 with lines
Another interesting complex function of time is the addition of two complex functions of time which leave only a signal on the real axis:
(lambda (t) (+ (exp (* 0+i t)) (exp (* 0-i t))))
And here is my hacker emacs screen, where the action is happening!