Last active 7 hours ago

Revision be9e1abd4b63c5e605725e17a207d3c02bc4a6e3

ME4231Lab2.c Raw
1#include <stdio.h>
2
3#define Vin 0.5
4#define timeFinal 5.0
5
6
7void run_simulation(double R, double C, double stepsize, int testNum);
8
9int main() {
10 run_simulation(10000, 0.0002, 0.1, 1);
11
12 run_simulation(5000, 0.0001, 0.1, 2);
13
14 run_simulation(10000, 0.0002, 0.5, 3);
15
16 run_simulation(5000, 0.0001, 0.5, 4);
17
18 return 0;
19}
20
21/*
22Function to simulate an RC circuit with voltage across C as output voltage
23Pass in resistance, capacitance, time step size, and test number
24*/
25void run_simulation(double R, double C, double stepsize, int testNum) {
26 double Vc = 0.0;
27 double time = 0.0;
28
29 double tau = R * C;
30
31 printf("Test number %d: R=%.0f, C=%.0f, Step=.2f, Tau=%.2f \n", testNum, R, C, stepsize, tau);
32
33 printf("Time (s) \t Voltage (V)\n");
34
35 printf("%.2f \t\t %.5f\n", time, Vc);
36
37 // loop until time = timeFinal
38 while (time < timeFinal) {
39 double newVc = Vc + (stepsize / tau) * (Vin - Vc);
40 Vc = newVc;
41 time += stepsize;
42
43 printf("%.2f \t\t %.5f\n", time, Vc);
44 }
45 printf("\n");
46}