diff --git a/practical_work/rc_circuit/Makefile b/practical_work/rc_circuit/Makefile index 4879775f3361556c67d2abd2566f512ba2cc4608..7580a51a5dd9711480606667893c38b22f6234d7 100644 --- a/practical_work/rc_circuit/Makefile +++ b/practical_work/rc_circuit/Makefile @@ -1,6 +1,7 @@ CC=clang OPTS=-g -O3 -Wall -Wextra -fsanitize=address -fsanitize=leak -std=c11 -LINK=-lm -fsanitize=address -fsanitize=leak +INCLUDE=-I/usr/include/plplot/ +LINK=-lm -fsanitize=address -fsanitize=leak -lplplot # OPTS=-g -O3 -Wall -Wextra -std=c11 # LINK=-lm @@ -9,7 +10,7 @@ main: main.o rc.o ode_o1.o $(CC) $(OPTS) -o $@ $^ $(LINK) main.o: main.c - $(CC) $(OPTS) -c $^ + $(CC) $(OPTS) $(INCLUDE) -c $^ ode_o1.o: ode_o1.c ode_o1.h $(CC) $(OPTS) -c $^ diff --git a/practical_work/rc_circuit/main.c b/practical_work/rc_circuit/main.c index 82b3938d7ce69db91a97eb32b0a4667ebb2c7d48..6e41bbc35d2ce07104b87bc61bf0bb5293bb7283 100644 --- a/practical_work/rc_circuit/main.c +++ b/practical_work/rc_circuit/main.c @@ -1,20 +1,61 @@ #include "rc.h" #include <stdio.h> #include <stdlib.h> +#include <math.h> +#include <plplot.h> -int main() { +#define NSIZE 5000 + +int main(int argc, char *argv[]) { rc_state rc = rc_state_create(1.0, 1.0, 1.0); + +// ============================================================ // +// RC with constant != 0 tension at input and 0 initial tension // +// ============================================================ // double v, v_ini; v = v_ini = 0.0; double dt = 0.001; double max_t = 5.0; + int i = 0; + + PLFLT x[NSIZE], y[NSIZE]; + PLFLT xmin = 0., xmax = max_t, ymin = 0., ymax = rc.eps; + for (double t = 0.0; t < max_t; t += dt) { double ve = exact_solution(t, v_ini, &rc); printf("t = %f, v = %f, v_e = %f, diff = %f\n", t, v, ve, ve - v); + x[i] = t; + y[i] = v; v = rc_advance(v, dt, &rc); + i += 1; } + v = v_ini = 1.0; + double omega = 0.0; + for (double t = 0.0; t < max_t; t += dt) { + rc.eps = sin(omega * t); + double ve = exact_solution(t, v_ini, &rc); + printf("t = %f, v = %f, v_e = %f, diff = %f\n", t, v, ve, ve - v); + v = rc_advance(v, dt, &rc); + } + + // Parse and process command line arguments + plparseopts( &argc, argv, PL_PARSE_FULL ); + + // Initialize plplot + plinit(); + + // Create a labelled box to hold the plot. + plenv( xmin, xmax, ymin, ymax, 0, 0 ); + pllab( "t", "v", "Simple PLplot demo of of the charge of a capacitor" ); + + // Plot the data that was prepared above. + plline( NSIZE, x, y ); + + // Close PLplot library + plend(); + return EXIT_SUCCESS; } \ No newline at end of file