Add lab session 4

Handed in on 10 Nov 2020.

Change-Id: Iac2b326e564a415d56c927b1900856d55fd6b9ed
diff --git a/p4/graphs/graph.gnu b/p4/graphs/graph.gnu
new file mode 100755
index 0000000..80f1931
--- /dev/null
+++ b/p4/graphs/graph.gnu
@@ -0,0 +1,95 @@
+#!/usr/bin/env gnuplot -c
+# == DEFINICIONS ==
+outputfile = ARG3 # Nom de la imatge resultant (sense extensió)
+outputfile2 = ARG4 # Nom de la imatge resultant 2 (sense extensió)
+datafile = ARG1 # Nom del fitxer de dades que es vol usar
+derivafile = ARG2 # Nom del fitxer de dades que es vol usar
+error_sist_b = 0.001 # Error sistemàtic en l'eix y
+
+# Set to 0 for quadratic fitting.
+# Set to 1 for linear fitting.
+linear = 0
+
+# == CONFIGURACIÓ DE L'OUTPUT PEL LATEX ==
+set terminal cairolatex size 7.4cm, 6cm
+set output outputfile.'.tex'
+
+# == CONFIGURACIÓ DEL PLOT ==
+set xlabel "$t \\, (\\si{\\second})$"
+set ylabel "$V \\, (\\si{\\milli\\volt})$"
+set xtics 200
+
+# Opcions per la llegenda:
+set key above
+set key spacing 1.5
+
+# == CONFIGURACIÓ DEL FIT ==
+set fit quiet
+
+if (linear == 1) {
+    f(x) = a*x + b # Funció a fitar
+    fit f(x) derivafile u 1:2 via a, b # Fem el fit de les dades
+
+    # Això s'usa per obtenir el valor del coeficient de correlació "r", que estarà
+    # guardat a la var. "STATS_correlation"
+    stats derivafile u 1:2 name "STATS" nooutput
+
+    # Funció per obtenir els dígits significatius d'una expressió
+    f_sd(n, i) = (int(n) == 0 ? f_sd(n*10, i+1) : (int(n) == 1 ? i+1 : i))
+    significant_digits(n) = f_sd(n - 10*int(n/10), 0)
+
+    significant_digits_r(n) = (n == 1 ? 0 : significant_digits(1 - n))
+
+    error_a = STATS_slope_err
+    sd_error_a = significant_digits(error_a)
+    error_b = sqrt(STATS_slope_err + error_sist_b)
+    sd_error_b = significant_digits(error_b)
+    r = STATS_correlation
+    sd_r = significant_digits_r(r)
+
+    print("=== ".derivafile." ===")
+    print("* Linear fit")
+    print(sprintf('delta(a) = %.'.sd_error_a.'f, sd=%.0f', error_a, sd_error_a))
+    print(sprintf('delta(b) = %.'.sd_error_b.'f, sd=%.0f', error_b, sd_error_b))
+
+    # Aquesta funció escriu, a partir dels valors del paràmetres a, b i r,
+    # l'equació de la regressió:
+    title_f(a, b, r) = sprintf('$Deriva d(t) = %.'.sd_error_a.'f t + (%.'.sd_error_b.'f)$, $r = %.'.sd_r.'f$', a, b, r);
+
+    plot [0:] datafile u 1:2 t "Senyal calorimètric" w p pt 7 ps 0.35, f(x) t title_f(a, b, r)
+} else {
+    print("=== ".derivafile." ===")
+    print("* Quadratic fit")
+
+    f(x) = a*x*x + b*x + c # Funció a fitar
+    fit f(x) derivafile u 1:2 via a, b, c # Fem el fit de les dades
+
+    title_f(a, b, c) = sprintf('$d(t) = %.1e t^2 + %.1e t + %.1e t$', a, b, c);
+
+    plot [0:] datafile u 1:2 t "Senyal calorimètric" w p pt 7 ps 0.35, f(x) t "Línia base"
+}
+
+stats datafile u (column(1) < 100 ? 0 : column(2) - f(column(1))) name "CORRECTED_STATS" nooutput
+
+print(sprintf("Y = %.3f", CORRECTED_STATS_max))
+
+# == CONFIGURACIÓ DE L'OUTPUT PER SVG ==
+# Això ho uso per generar també una imatge de previsualització que puc carregar
+# a l'ordinador per veure més o menys com a sortit el plot sense haver
+# d'inserir-ho al LaTeX per veure-ho.
+set terminal svg dashed size 600, 600 font "Computer Modern,Tinos,Helvetica,15"
+set output outputfile.'.svg'
+
+replot
+
+# Fem el plot del segon gràfic
+
+set terminal cairolatex size 7.4cm, 6cm
+set output outputfile2.'.tex'
+
+plot [0:] datafile u 1:(column(2) - f(column(1))) t "Senyal calor. corregit" w p pt 7 ps 0.35
+
+set terminal svg dashed size 600, 600 font "Computer Modern,Tinos,Helvetica,15"
+set output outputfile2.'.svg'
+
+replot