Skip to content

Animating two functions #21

@tmiethlinger

Description

@tmiethlinger

Hello,

I wanted to ask if it's possible to animate two functions? I tried to expand the "demo_animation" in example-misc.cc, but that didn't work.

I tried the following code, among others:

#include
#include
#include
#include
#include
#include

#include "gnuplot-iostream.h"

#define M_PI 3.14159265358979323846

#include <unistd.h>
inline void mysleep(unsigned millis)
{
::usleep(millis * 1000);
}

void animation()
{
Gnuplot gp;

std::cout << "Press Ctrl-C to quit (closing gnuplot window doesn't quit)." << std::endl;

gp << "set yrange [-1:1]\n";

const int N = 1000;
std::vector<double> f1(N);
std::vector<double> f2(N);

double theta = 0;
while(true)
{
    for(int i=0; i<N; i++)
    {
        double alpha = (static_cast<double>(i)/N - 0.5) * 10;
        f1[i] = sin(alpha*8.0 + theta) * exp(-alpha*alpha/2.0);
        f2[i] = sin(alpha*8.0 + theta) * exp(-alpha*alpha/1.0);
    }

    // 1 function: works
    gp << "plot" << " '-' binary" << gp.binFmt1d(f1, "array") << "with lines title 'f1(x)'\n";
    gp.sendBinary1d(f1);
    gp.flush();

    // 2 functions: doesn't work
    //gp << "plot" << " '-' binary" << gp.binFmt1d(f1, "array") << "with lines notitle";
    //gp.sendBinary1d(f1);
    //gp << "plot" << " '-' binary" << gp.binFmt1d(f2, "array") << "with lines notitle\n";
    //gp.sendBinary1d(f2);
    //gp.flush();

    mysleep(10);
    theta += 0.05;
}

}

int main(int argc, char *argv[])
{
animation();
}

How can I include more than one function in the animation? And is it necessary to use binary here, or could I also use other functions like add_plot1d?

Thank you for your help,
Thomas

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions