Animation with GNUPlot

An Introduction to visulaization of Physical system


By, Arabindo on 29th May, 2021 Comments
For any error/mistakes feel free to contact me at arabindo@pm.me

Animation

DISCLAIMER: The “Animate a Cycloid” topic is developed step by step. There is a some repeatation of large code blocks. So it may seems to be lengthy but it is actually not!

Table of Contents

Simple Animation Animate simple physical system From numerical solution Animate using Data file (Kapitza Pendulum as eg.) Conclusion
1. Introduction Problem Statement Introdution Resources
2. Why GNUPlot Working Equations The “Hello World!”  
3. The Final Goal Remarks on Function Working Equations  
4. Scripted GNUPlot The Final Code Phase Space  
4.1. The ‘Hello world!’   Final Code  
5. Animate a Cycloid   Configuration Space  
5.1. Final script for the Cycloid   Appendix-I (Derivations)  
6. Be an active learner      
7. A Bonus Project      

Introduction

Visualizing a system by plotting a graph is always of great interest for practising professionals in any stream. But the advancement of technology now allows us more than just plotting. Nowadays “Just plotting” is an old fashion. Either it is teaching or presenting an idea in front of the audience, or just for self-learning, it is always a good idea to animate the system somehow. Or you may just want to plot a time-varying graph. Here I’ll discuss techniques and tricks for the same.

Why GNUPlot

GNUPlot is simple, elegant and great for First and Fast learning. Although there are several packages/software, some are too advanced, we will be using good old GNUPlot. My personal believe (fortunately, many people agree to that) is that if you can learn one such package, the skill is transferable. So you can go on learning as per your preference.

The Final Goal

I believe, most of you is already equipped with Lagrangian and Hamiltonian formalism. So the idea is to show the technique to produce GIFs(Graphics Interchange Format) and then to show how to simulate a simple system when the Lagrangian(or Hamiltonian) of the system is given a priori. After the second half, the reader must be able to animate simple systems of their choice. In a later post, I’ll also show how to do animation from a data file. That will give you a wing to use GNUPlot with any other programming language. Although GNUPlot terminal can be linked to other programming languages, let’s don’t make thing complicated with that technical jargon. Now it’s enough of hunky-junky. Let’s get into the topic with the so-called ‘hello-world GNUPlot script for the animation.

Scripted GNUPlot

Unlike writing command directly in the terminal, we can create a simple text file listing all the commands. Save it with an extension ‘.gnu’(say, cos.gnu). Then from the terminal just type gnuplot cos.gnu and hit Enter. The file may contain the following

reset
set term qt
set xrange[-2*pi:2*pi]
set yrange[-1.5:1.5]
plot cos(x)
reread #optional

Sometimes it is convenient to put the reread statement at the end line. It’ll keep the plotting window open. For some system, it may not be required for ordinary plotting. However, if you’re using reread, then keep in your mind that closing the plot window will not do the job, it’ll pop up again. Rather you would like to execute Ctrl+C in the terminal to stop the execution and close the plotting window. *Alternativel, for static plots, one may use --persists argument in the command line, like gnuplot cos.gnu --persists

Now let’s have a train of sine wave

Your First Animation - The ‘Hello world!’

sinetrain

reset
set term qt
set nokey #Optional
set title 'Sine wave train'
set xrange[-2*pi:2*pi]
set yrange[-1.5:1.5]
func(x, w, t) = sin(x + w*t)
do for[loopNo=1:100]{
    plot func(x, 0.5, loopNo)
}
reread

Output in your screen is too fast, isn’t it? No problem we will fix it later. Have some patience! :P

Notice that we’ve used the plot command within the loop. And that’s the trick, you’re just plotting the same thing over and over again for different instances(here loopNo) and you get the animation like output. There are various method to save this animation. We will discuss those from time to time in the various examples.

NOTE:

Now, we’ll look into a better picture, a better animation!

Tracing a Cycloid

Cycloid

We will build this script step by step! In the end, we will see a stunning animated cycloid just like the above GIF.

Let’s get started

The idea is to trace a particular point of a moving circle. So, first thing first. We will need a circle. For that, we will consider the parametric setup.

#setting up the enviorment

reset 
set term qt
set nokey

set paramteric

s= 4 # radius of the circle
plot s*cos(t), s*sin(t) 
# in the parametric enviorment, the parameter is predefined as 't' by GNUPlot itself

reread #you must be executing Ctrl+C in the terminal to stop the execution.

If you inspect the output closely, the point is rotating anti-clockwise but the advancement of the circle in the positive direction. To change the direction, just subtract the angle by 2*pi from cirX and cirY function. Another point is that the rotation starts from the line y=radius line. I wanted to start the rotation from y=0, so I’ll change the phase by pi/2.

The Final Script

Here we will set gif animate type terminal and an output file with a .gif extension. gif animate usually take a long time(strictly speaking, depends upon the computational complexity of the problem and the number of frame/instance you are producing) to produce the output file. So you may need to wait for a while.

The output will be very much similar to the above one. You may like to use this website to speed up or reduce the size of GIF, or maybe you want to convert the GIF into a video!

Be an active learner

If you’re having any difficulty, you can always contact me. But it’ll be better if you do comment below this post. Since community learning is the best thing that one can have!

The only thing I’m asking in return, please share your feedback, either by email/social-media or comment here.

Next Page

A Bonus Project

Are you wondering how to have an animation that I’ve used at the top of this page? Okay, that’s a project for you! You can test your understanding by plotting that animation. Here are some tips(Or you can develop your own technique, it’s your choice) you may find useful for this problem:

These tips may not seem to be clear unless you really start to work on the problem. Only then you’ll understand what I’m talking about.

array array_name=[element1, element2, element3....]

To access the elements you may use array_name[index]. In GNUPlot the index starts from 1.

Next Page | Second Page | Resources