Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Last active October 3, 2016 19:31
Show Gist options
  • Save nicoguaro/97453dfb827da6851458fe805ccfcd2a to your computer and use it in GitHub Desktop.
Save nicoguaro/97453dfb827da6851458fe805ccfcd2a to your computer and use it in GitHub Desktop.
Automate a pipeline to run a program and visualize the results using make
/*
Generate a file with the values for the sine function and its
derivative
The derivative is computed using first order finite differences.
Author: Nicolas Guarin-Zapata
*/
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main ()
{
int cont, num_vals;
float x, x_ini, x_end, dx;
float fx, fx_new, dfx;
FILE *f;
x_ini = 0;
x_end = 1;
num_vals = 101;
dx = (x_end - x_ini)/(num_vals -1);
x = 0;
f = fopen("sine_data.txt", "w");
for (cont = 0; cont < num_vals; cont++)
{
fx = sin(2*PI*x);
fx_new = sin(2*PI*(x + dx));
dfx = (fx_new - fx)/dx;
fprintf(f, "%f, %f, %f\n", x, fx, dfx);
x = x + dx;
}
fclose(f);
return 0;
}
sine_plot.png : plot_data.py
python plot_data.py
plot_data.py: gen_sine_data
./gen_sine_data
gen_sine_data : gen_sine_data.c
cc -o gen_sine_data gen_sine_data.c -lm
"""Plot the data in sine_data.txt"""
from __future__ import division
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.style.use("seaborn-white")
red = "#e41a1c"
blue = "#377eb8"
data = np.loadtxt("sine_data.txt", delimiter=",")
x = data[:, 0]
f = data[:, 1]
df = data[:, 2]
plt.plot(x, f, label=r"$\sin(2 \pi x)$", linewidth=2, color=red)
plt.plot(x, df, label="Approximated derivative", linewidth=2, color=blue)
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
plt.legend(loc="best")
plt.savefig("sine_plot.png", dpi=300, bbox_inches="tight")
0.000000, 0.000000, 6.279052
0.010000, 0.062791, 6.254272
0.020000, 0.125333, 6.204808
0.030000, 0.187381, 6.130857
0.040000, 0.248690, 6.032710
0.050000, 0.309017, 5.910754
0.060000, 0.368125, 5.765474
0.070000, 0.425779, 5.597436
0.080000, 0.481754, 5.407313
0.090000, 0.535827, 5.195844
0.100000, 0.587785, 4.963875
0.110000, 0.637424, 4.712307
0.120000, 0.684547, 4.442155
0.130000, 0.728969, 4.154462
0.140000, 0.770513, 3.850377
0.150000, 0.809017, 3.531098
0.160000, 0.844328, 3.197879
0.170000, 0.876307, 2.852035
0.180000, 0.904827, 2.494943
0.190000, 0.929776, 2.128005
0.200000, 0.951057, 1.752669
0.210000, 0.968583, 1.370406
0.220000, 0.982287, 0.982744
0.230000, 0.992115, 0.591201
0.240000, 0.998027, 0.197327
0.250000, 1.000000, -0.197327
0.260000, 0.998027, -0.591207
0.270000, 0.992115, -0.982744
0.280000, 0.982287, -1.370406
0.290000, 0.968583, -1.752663
0.300000, 0.951057, -2.127999
0.310000, 0.929777, -2.494937
0.320000, 0.904827, -2.852035
0.330000, 0.876307, -3.197873
0.340000, 0.844328, -3.531086
0.350000, 0.809017, -3.850371
0.360000, 0.770514, -4.154456
0.370000, 0.728969, -4.442143
0.380000, 0.684548, -4.712307
0.390000, 0.637424, -4.963863
0.400000, 0.587786, -5.195838
0.410000, 0.535827, -5.407307
0.420000, 0.481754, -5.597431
0.430000, 0.425780, -5.765465
0.440000, 0.368125, -5.910748
0.450000, 0.309018, -6.032704
0.460000, 0.248691, -6.130849
0.470000, 0.187382, -6.204802
0.480000, 0.125334, -6.254265
0.490000, 0.062792, -6.279046
0.500000, 0.000001, -6.279065
0.510000, -0.062789, -6.254266
0.520000, -0.125332, -6.204803
0.530000, -0.187380, -6.130854
0.540000, -0.248689, -6.032707
0.550000, -0.309016, -5.910754
0.560000, -0.368123, -5.765471
0.570000, -0.425778, -5.597440
0.580000, -0.481752, -5.407310
0.590000, -0.535825, -5.195850
0.600000, -0.587784, -4.963875
0.610000, -0.637423, -4.712314
0.620000, -0.684546, -4.442155
0.630000, -0.728967, -4.154468
0.640000, -0.770512, -3.850383
0.650000, -0.809016, -3.531098
0.660000, -0.844327, -3.197885
0.670000, -0.876306, -2.852046
0.680000, -0.904826, -2.494955
0.690000, -0.929776, -2.128017
0.700000, -0.951056, -1.752675
0.710000, -0.968583, -1.370424
0.720000, -0.982287, -0.982755
0.730000, -0.992114, -0.591218
0.740000, -0.998027, -0.197345
0.750000, -1.000000, 0.197309
0.760000, -0.998027, 0.591189
0.770000, -0.992115, 0.982726
0.780000, -0.982288, 1.370388
0.790000, -0.968584, 1.752645
0.800000, -0.951057, 2.127987
0.810000, -0.929778, 2.494919
0.820000, -0.904828, 2.852017
0.830000, -0.876308, 3.197861
0.839999, -0.844330, 3.531069
0.849999, -0.809019, 3.850359
0.859999, -0.770515, 4.154444
0.869999, -0.728971, 4.442132
0.879999, -0.684550, 4.712290
0.889999, -0.637427, 4.963857
0.899999, -0.587788, 5.195827
0.909999, -0.535830, 5.407298
0.919999, -0.481757, 5.597422
0.929999, -0.425783, 5.765459
0.939999, -0.368128, 5.910742
0.949999, -0.309021, 6.032700
0.959999, -0.248694, 6.130846
0.969999, -0.187385, 6.204799
0.979999, -0.125337, 6.254262
0.989999, -0.062795, 6.279046
0.999999, -0.000004, 6.279084
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment