aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjustanothercatgirl <sotov2070@gmail.com>2024-11-18 23:40:21 +0300
committerjustanothercatgirl <sotov2070@gmail.com>2024-11-18 23:40:21 +0300
commit89d26d5d0e8ff2b026cc99606868699f6fcc08db (patch)
tree22181a56c8a1cf2ecae3c167402898c7aed38fb8
parente1bf912316b7f156218aaf5d8571443e47d880ed (diff)
added tempalte
l---------107/include1
-rw-r--r--README.md7
-rw-r--r--libprakipp/compile_flags.txt2
-rw-r--r--libprakipp/src/prakmath.cpp0
-rw-r--r--libprakpp/include/prakcommon.hpp (renamed from libprakipp/include/prakcommon.hpp)6
-rw-r--r--libprakpp/include/prakmath.hpp (renamed from libprakipp/include/prakmath.hpp)30
-rw-r--r--libprakpp/include/prakmatrix.hpp (renamed from libprakipp/include/prakmatrix.hpp)0
-rw-r--r--libprakpp/include/praktable.hpp (renamed from libprakipp/include/praktable.hpp)6
-rw-r--r--libprakpp/tests/compile_flags.txt2
-rw-r--r--libprakpp/tests/test_data (renamed from libprakipp/test_data)0
-rw-r--r--libprakpp/tests/test_include.cpp (renamed from libprakipp/include/tests.cpp)0
-rw-r--r--libprakpp/tests/tests.cpp (renamed from libprakipp/tests.cpp)0
-rw-r--r--tempalte/Makefile18
-rw-r--r--tempalte/README.md5
-rw-r--r--tempalte/compile_flags.txt4
l---------tempalte/include1
-rw-r--r--tempalte/main.cpp14
-rw-r--r--tempalte/plots.gp14
-rw-r--r--vtek3/Makefile10
-rw-r--r--vtek3/data_green12
-rw-r--r--vtek3/data_red12
l---------vtek3/include2
-rwxr-xr-xvtek3/mainbin0 -> 649768 bytes
-rw-r--r--vtek3/main.cpp79
-rw-r--r--vtek3/plots.gp48
25 files changed, 243 insertions, 30 deletions
diff --git a/107/include b/107/include
new file mode 120000
index 0000000..1b37f46
--- /dev/null
+++ b/107/include
@@ -0,0 +1 @@
+../libprakpp/include \ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..1c8c75d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,7 @@
+# Здесь собраны мои практикумы и программы для их обработки
+Я собирал их не совсем с начала года; что есть - то есть.
+
+`praklib.py` есть прототип библиотеки для обработки, который я использовал в 102, 117, 120 и vtek2.
+`libprakpp` есть библиотека на С++ со всем, что нужно для обработки праков. Документация в исходнике.
+
+* В папках праков может быть свой readme
diff --git a/libprakipp/compile_flags.txt b/libprakipp/compile_flags.txt
deleted file mode 100644
index 909d9cc..0000000
--- a/libprakipp/compile_flags.txt
+++ /dev/null
@@ -1,2 +0,0 @@
--std=c++2c
--Iinclude
diff --git a/libprakipp/src/prakmath.cpp b/libprakipp/src/prakmath.cpp
deleted file mode 100644
index e69de29..0000000
--- a/libprakipp/src/prakmath.cpp
+++ /dev/null
diff --git a/libprakipp/include/prakcommon.hpp b/libprakpp/include/prakcommon.hpp
index 5fee864..749d5ef 100644
--- a/libprakipp/include/prakcommon.hpp
+++ b/libprakpp/include/prakcommon.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <cmath>
+#include <functional>
#include <vector>
#include <iostream>
@@ -24,7 +25,10 @@
#define SUBSCR_CLS )
#define SUBSCR_OPRTR operator()
#endif
-
+
+template <typename T>
+using function_t = std::function<T(const std::vector<T> &)>;
+
namespace prak {
/// stolen from [cppreference.com](https://en.cppreference.com/w/cpp/types/numeric_limits/epsilon)
diff --git a/libprakipp/include/prakmath.hpp b/libprakpp/include/prakmath.hpp
index 342c0cd..3a4e4db 100644
--- a/libprakipp/include/prakmath.hpp
+++ b/libprakpp/include/prakmath.hpp
@@ -184,6 +184,36 @@ scalar:
return linear + finalize(buf);
}
+// take a derivative of function `f` with arguments `at` and with differentiable variable being at index `varidx`
+template <std::floating_point T>
+T deriv4(function_t<T> f, std::vector<T> /*I actually do want a copy here*/ at, size_t varidx) {
+ T h = 1e-5;
+ T sum = 0;
+
+ at[varidx] += 2*h;
+ sum -= f(at);
+ at[varidx] -= h;
+ sum += 8 * f(at);
+ at[varidx] -= 2*h;
+ sum -= 8*f(at);
+ at[varidx] -= h;
+ sum += f(at);
+
+ return sum / (12*h);
+}
+
+/// get error of the calculated value
+template <std::floating_point T>
+T sigma(function_t<T> f, const std::vector<T> &args, const std::vector<T> sigmas) noexcept(false) {
+ if (args.size() != sigmas.size()) throw dimension_error("function prak::sigma : Args.size() does not match sigmas.size()");
+ T sum = 0;
+ for (size_t i = 0; i < args.size(); ++i) {
+ T tmp = deriv4(f, args, i) * sigmas[i];
+ sum += tmp*tmp;
+ }
+ return std::sqrt(sum);
+}
+
/// calculate least-squares linear approximation to fit data
/// ax+b = y (ss is an error of Y value)
template <std::floating_point T>
diff --git a/libprakipp/include/prakmatrix.hpp b/libprakpp/include/prakmatrix.hpp
index 87b38b4..87b38b4 100644
--- a/libprakipp/include/prakmatrix.hpp
+++ b/libprakpp/include/prakmatrix.hpp
diff --git a/libprakipp/include/praktable.hpp b/libprakpp/include/praktable.hpp
index 53519a5..f32d00d 100644
--- a/libprakipp/include/praktable.hpp
+++ b/libprakpp/include/praktable.hpp
@@ -58,7 +58,7 @@ template <typename dtype>
class table {
std::vector<dtype> data;
- using function_type = std::function<dtype(const std::vector<dtype> &)>;
+ using function_type = function_t<dtype>;
using stringvec = std::vector<std::string>;
size_t index(const std::string &str) const {
@@ -67,7 +67,7 @@ class table {
FILE* open_gnuplot() {
#if defined(_POSIX_VERSION) || defined(__unix)
- FILE* = popen("gnuplot", "w");
+ return popen("gnuplot", "w");
#else
#warning Not implemented for non-unix operating systems
return NULL;
@@ -103,7 +103,7 @@ public:
/// Mandatory columnnames: names of columns
std::vector<std::string> names;
/// width used for printing, defaults to 8
- size_t column_width = 8;
+ size_t column_width = 12;
/// default constructor
table() = default;
diff --git a/libprakpp/tests/compile_flags.txt b/libprakpp/tests/compile_flags.txt
new file mode 100644
index 0000000..42a9c4d
--- /dev/null
+++ b/libprakpp/tests/compile_flags.txt
@@ -0,0 +1,2 @@
+-std=c++2c
+-I../include
diff --git a/libprakipp/test_data b/libprakpp/tests/test_data
index a68190d..a68190d 100644
--- a/libprakipp/test_data
+++ b/libprakpp/tests/test_data
diff --git a/libprakipp/include/tests.cpp b/libprakpp/tests/test_include.cpp
index af61860..af61860 100644
--- a/libprakipp/include/tests.cpp
+++ b/libprakpp/tests/test_include.cpp
diff --git a/libprakipp/tests.cpp b/libprakpp/tests/tests.cpp
index 3dda25a..3dda25a 100644
--- a/libprakipp/tests.cpp
+++ b/libprakpp/tests/tests.cpp
diff --git a/tempalte/Makefile b/tempalte/Makefile
new file mode 100644
index 0000000..eb309ff
--- /dev/null
+++ b/tempalte/Makefile
@@ -0,0 +1,18 @@
+
+CFLAGS = -std=c++2c -mavx -Iinclude -ggdb
+
+.PHONY: all run_main clean gnuplot
+
+run: gnuplot
+
+gnuplot: plots.gp run_main
+ gnuplot $<
+
+run_main: main
+ ./main
+
+main: main.cpp include/*
+ $(CXX) -o $@ $< $(CFLAGS)
+
+clean:
+ rm -fr main *.png *.plot
diff --git a/tempalte/README.md b/tempalte/README.md
new file mode 100644
index 0000000..96fd716
--- /dev/null
+++ b/tempalte/README.md
@@ -0,0 +1,5 @@
+<!-- Шаблон для прака -->
+<!-- файлы, заканчивающиеся на .plot считаются генерируемыми и удаляются через make clean -->
+# Обработка <> прака
+
+
diff --git a/tempalte/compile_flags.txt b/tempalte/compile_flags.txt
new file mode 100644
index 0000000..34ae930
--- /dev/null
+++ b/tempalte/compile_flags.txt
@@ -0,0 +1,4 @@
+-Iinclude
+-std=c++2c
+-mavx2
+
diff --git a/tempalte/include b/tempalte/include
new file mode 120000
index 0000000..2225752
--- /dev/null
+++ b/tempalte/include
@@ -0,0 +1 @@
+../libprakpp/include/ \ No newline at end of file
diff --git a/tempalte/main.cpp b/tempalte/main.cpp
new file mode 100644
index 0000000..b1ce4e1
--- /dev/null
+++ b/tempalte/main.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+
+#include "include/praktable.hpp"
+
+using table = prak::table<double>;
+
+void ex1(void) {
+ table t;
+}
+
+int main() {
+ ex1();
+ return 0;
+}
diff --git a/tempalte/plots.gp b/tempalte/plots.gp
new file mode 100644
index 0000000..702408a
--- /dev/null
+++ b/tempalte/plots.gp
@@ -0,0 +1,14 @@
+set term pngcairo size 1000, 800
+set tmargin at screen 0.95
+
+f1(x) = a1*x+b1
+fit f1(x) '.plot' using 1:2:3 yerr via a1, b1
+
+set output ''
+set label "" at graph 0.5, graph 1.025 center
+set xlabel ""
+set ylabel ""
+
+plot '.plot' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \
+ f1(x) title "" lc rgb "red", \
+
diff --git a/vtek3/Makefile b/vtek3/Makefile
index 4f9a99a..b89eb0b 100644
--- a/vtek3/Makefile
+++ b/vtek3/Makefile
@@ -2,5 +2,15 @@
CXXFLAGS += -std=c++2c -mavx2 -Iinclude
CXXFLAGS += -ggdb
+run: main
+ ./main
+ gnuplot plots.gp
+
main: main.cpp
$(CXX) -o $@ $^ $(CXXFLAGS)
+
+.PHONY: clean
+
+clean:
+ rm -fr plot*.data main *.png
+
diff --git a/vtek3/data_green b/vtek3/data_green
new file mode 100644
index 0000000..62a2831
--- /dev/null
+++ b/vtek3/data_green
@@ -0,0 +1,12 @@
+__name__ U DU sU I DI sI
+1 1.337 ? ? 0 ? ?
+2 1.7638 ? ? 0.000069 ? ?
+3 1.8074 ? ? 0.000228 ? ?
+4 1.8228 ? ? 0.000333 ? ?
+5 1.8448 ? ? 0.000555 ? ?
+6 1.8586 ? ? 0.000727 ? ?
+7 1.8899 ? ? 0.001337 ? ?
+8 1.9147 ? ? 0.001989 ? ?
+9 1.9554 ? ? 0.003330 ? ?
+10 1.9776 ? ? 0.004270 ? ?
+
diff --git a/vtek3/data_red b/vtek3/data_red
new file mode 100644
index 0000000..442efb4
--- /dev/null
+++ b/vtek3/data_red
@@ -0,0 +1,12 @@
+__name__ U DU sU I DI sI
+1 0.0377 ? ? 0 ? ?
+2 1.5335 ? ? 0.000050 ? ?
+3 1.5959 ? ? 0.000200 ? ?
+4 1.6150 ? ? 0.000301 ? ?
+5 1.6310 ? ? 0.000420 ? ?
+6 1.6535 ? ? 0.000666 ? ?
+7 1.6742 ? ? 0.001001 ? ?
+8 1.7147 ? ? 0.001999 ? ?
+9 1.7425 ? ? 0.003000 ? ?
+10 1.7685 ? ? 0.004200 ? ?
+
diff --git a/vtek3/include b/vtek3/include
index f5030d7..2225752 120000
--- a/vtek3/include
+++ b/vtek3/include
@@ -1 +1 @@
-../libprakipp/include/ \ No newline at end of file
+../libprakpp/include/ \ No newline at end of file
diff --git a/vtek3/main b/vtek3/main
new file mode 100755
index 0000000..41834ee
--- /dev/null
+++ b/vtek3/main
Binary files differ
diff --git a/vtek3/main.cpp b/vtek3/main.cpp
index 5e3d491..6984eff 100644
--- a/vtek3/main.cpp
+++ b/vtek3/main.cpp
@@ -37,14 +37,6 @@ RT err_MS8040(const std::vector<RT> &args) {
return 0.0005 * args[0] + 6 * args[1];
}
-RT S(const std::vector<RT> &args) {
- return prak::PI * args[0]*args[0]/4;
-}
-
-RT S_1(const std::vector<RT> &args) {
- return 1 / S(args);
-}
-
// args: I
// output: DI
RT D_M830B(const std::vector<RT> &args) {
@@ -61,7 +53,7 @@ RT err_M380B(const std::vector<RT> &args) {
return 0.01 * args[0] + 2 * args[1];
}
-void ex1(void) {
+void excercise1(void) {
std::unordered_map<struct wire, prak::table<RT>> map {
{{0.5, 0.00015}, prak::table<RT>{}},
{{0.5, 0.00025}, prak::table<RT>{}},
@@ -85,7 +77,6 @@ void ex1(void) {
table.apply(D_MS8040, {"U"}, "DU")
.apply(err_MS8040, {"U", "DU"}, "sU")
.apply(D_M830B, {"I"}, "DI")
- .apply(D_M830B, {"I"}, "DI")
.apply(err_M380B, {"I", "DI"}, "sI");
std::cout << index << ":\n" << table << std::endl;
if (prak::fequal<RT>(index.length, 0.5)) resist_d.add_row({index.diam, NAN, NAN, NAN, NAN});
@@ -95,33 +86,75 @@ void ex1(void) {
for (size_t i = 0; i < resist_d.rows; ++i) {
prak::table<RT> &cur = map.at({.length = 0.5, .diam = resist_d["d", i]});
auto [a, b] = cur.least_squares_linear("U", "I", "sI");
- std::cout << "diam = " << resist_d["d", i]
- << ": A = " << a
- << "; B = " << b
- << "; R = " << 1/a.val << std::endl;
resist_d["R", i] = 1/a.val;
resist_d["sR", i] = a.err/a.val/a.val;
- resist_d.apply(S_1, {"d"}, "1/S");
std::ofstream outf("plot_" + std::to_string(0.5f) + "_" + std::to_string(resist_d["d", i]) + ".data");
cur.write_plot("U", "I", "sI", outf);
}
+
+ std::ofstream rsplot("plot_r_1s.data");
+ resist_d
+ .apply([](const std::vector<RT> &args) -> RT {
+ return prak::PI * args[0]*args[0]/4;
+ }, {"d"}, "S")
+ .apply([](const std::vector<RT> &args) -> RT {
+ return 1 / (prak::PI * args[0]*args[0]/4);
+ }, {"d"}, "1/S")
+ .write_plot("1/S", "R", "sR", rsplot);
+
auto [rho, _] = resist_d.least_squares_linear("1/S", "R", "sR");
rho.val /= 0.5;
rho.err /= 0.5;
std::cout << "Rho = " << rho << " (error = " << _ << ")" << std::endl;
-
+
// LLS for d=const
- resist_d.apply(S, {"d"}, "S")
- .apply(S_1, {"d"}, "1/S");
- std::cout << resist_d;
+ for (size_t i = 0; i < resist_L.rows; ++i) {
+ prak::table<RT> &cur = map.at({.length = resist_L["L", i], .diam = 0.00015});
+ auto [a, b] = cur.least_squares_linear("U", "I", "sI");
+ resist_L["R", i] = 1/a.val;
+ resist_L["sR", i] = a.err/a.val/a.val;
+ }
+ RT _d = 0.00015;
+ auto rho_f = [](const std::vector<RT> &args) -> RT {
+ return args[1] * prak::PI * args[2] * args[2] / args[0] / 4;
+ };
+ resist_L
+ .apply([_d, &rho_f](const std::vector<RT> &args) {
+ return rho_f({args[0], args[1], _d});
+ }, {"L", "R"}, "Rho")
+ .apply([_d, &rho_f](const std::vector<RT> &args) -> RT {
+ return prak::sigma<RT>(rho_f, {args[0], args[1], _d}, {0, args[2], 0});
+ }, {"L", "R", "sR"}, "sRho");
+
+ // output
+ std::cout << resist_d << resist_L;
}
-void ex3(void) {
-
+void excercise3(void) {
+ prak::table<RT> red, green;
+ {
+ std::ifstream red_stream("data_red"), green_stream("data_green");
+ red.read(red_stream);
+ green.read(green_stream);
+ }
+ green .apply(D_MS8040, {"U"}, "DU")
+ .apply(err_MS8040, {"U", "DU"}, "sU")
+ .apply(D_M830B, {"I"}, "DI")
+ .apply(D_M830B, {"I", "DI"}, "sI");
+ red .apply(D_MS8040, {"U"}, "DU")
+ .apply(err_MS8040, {"U", "DU"}, "sU")
+ .apply(D_M830B, {"I"}, "DI")
+ .apply(D_M830B, {"I", "DI"}, "sI");
+ {
+ std::ofstream red_out("plot_red.data"), green_out("plot_green.data");
+ red.write_plot("U", "I", "sI", red_out);
+ green.write_plot("U", "I", "sI", green_out);
+ }
+ std::cout << "Red: " << red << "\nGreen: " << green;
}
int main(int argc, char *argvp[]) {
- ex1();
- ex3();
+ excercise1();
+ excercise3();
}
diff --git a/vtek3/plots.gp b/vtek3/plots.gp
new file mode 100644
index 0000000..a0e314a
--- /dev/null
+++ b/vtek3/plots.gp
@@ -0,0 +1,48 @@
+set term pngcairo size 1000, 800
+set tmargin at screen 0.95
+
+f1(x) = a1*x+b1
+f2(x) = a2*x+b2
+f3(x) = a3*x+b3
+
+set output 'plot_mnk.png'
+set label "Графики вольтамперных характеристик проводов разного диаметра" at graph 0.5, graph 1.025 center
+set xlabel "U, В"
+set ylabel "I, А"
+set xrange[0:1]
+set yrange[0:0.1]
+fit f1(x) 'plot_0.5_0.00015.data' using 1:2:3 yerr via a1, b1
+fit f2(x) 'plot_0.5_0.00025.data' using 1:2:3 yerr via a2, b2
+fit f3(x) 'plot_0.5_4e-04.data' using 1:2:3 yerr via a3, b3
+plot \
+ 'plot_0.5_0.00015.data' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \
+ 'plot_0.5_0.00025.data' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \
+ 'plot_0.5_4e-04.data' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \
+ f1(x) title "d=0.15 * 10^{-3}" lc rgb "red", \
+ f2(x) title "d=0.25 * 10^{-3}" lc rgb "green", \
+ f3(x) title "d=0.4 * 10^{-3}" lc rgb "blue"
+
+set output 'plot_rs.png'
+unset label
+set label "График зависимости R от 1/S сечения" at graph 0.5, graph 1.025 center
+unset xrange
+unset yrange
+set xlabel "S^{-1}, м^{-2}"
+set ylabel "R, Ом"
+fit f1(x) 'plot_r_1s.data' using 1:2:3 yerr via a1, b1
+plot 'plot_r_1s.data' using 1:2:3 with yerrorbars notitle lc rgb "red" pt 1 lw 2, \
+ f1(x) title "R" lc 0
+
+set out
+
+set output 'plot_diods.png'
+unset label
+set label "Вольтамперная характеристика красного и синего светодиодов" at graph 0.5, graph 1.025 center
+set xlabel "U, В"
+set ylabel "Y, A"
+set xrange[1:2]
+plot 'plot_red.data' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \
+ 'plot_green.data' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \
+ 'plot_red.data' with lines lc rgb "red" title "I(U) красный", \
+ 'plot_green.data' with lines lc rgb "green" title "I(U) зелёный"
+