Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Electrical mesh circuits Systems of simultaneous equations Solving using Gauss Elimination Simple C program to count letters Using gnuplot to see results.

Similar presentations


Presentation on theme: "1 Electrical mesh circuits Systems of simultaneous equations Solving using Gauss Elimination Simple C program to count letters Using gnuplot to see results."— Presentation transcript:

1 1 Electrical mesh circuits Systems of simultaneous equations Solving using Gauss Elimination Simple C program to count letters Using gnuplot to see results CSE 20232 Lecture 35 – Gauss Elimination & Circuits, and more

2 2 Electrical mesh circuits Basically a mesh circuit is one including both series and parallel paths Example shown below: Given all voltages and resistances Solve for the 3 unknown currents V1V1 V2V2 R4R4 R5R5 R3R3 R2R2 R1R1 I1I1 - + - + I2I2 I3I3

3 3 Electrical mesh circuits Sum of voltages within each circuit is zero, so … We have 3 linear equations with 3 unknowns 0 = -V 1 + i 1 R 1 + (i 1 -i 2 )R 2 0 = (i 2 -i 1 )R 2 + i 2 R 3 + (i 2 -i 3 )R 4 0 = (i 3 -i 2 )R 2 + i 3 R 5 + V 2 V1V1 V2V2 R4R4 R5R5 R3R3 R2R2 R1R1 i1i1 - + - + i2i2 i3i3

4 4 Electrical mesh circuits These 3 linear equations 0 = -V 1 + i 1 R 1 + (i 1 -i 2 )R 2 0 = (i 2 -i 1 )R 2 + i 2 R 3 + (i 2 -i 3 )R 4 0 = (i 3 -i 2 )R 2 + i 3 R 5 + V 2 Can be rewritten as ( R 1 +R 2 )i 1 + R 2 i 2 = V 1 -R 2 i 1 + ( R 1 +R 2 +R 3 )i 2 – R 4 i 3 = 0 -R 4 i 2 + ( R 4 +R 5 )i 3 = -V 2

5 5 A brief aside: Matrix form of linear system Any 3 linearly independent equations … a 1 x + b 1 y + c 1 z = d 1 a 2 x + b 2 y + c 2 z = d 2 a 3 x + b 3 y + c 3 z = d 3 Can be placed in matrix form … a 1 b 1 c 1 x = d 1 a 2 b 2 c 2 y = d 2 a 3 b 3 c 3 z = d 3

6 6 Augmented matrix The same system in augmented matrix form looks like this … a 1 b 1 c 1 d 1 a 2 b 2 c 2 d 2 a 3 b 3 c 3 d 3

7 7 Gauss Elimination By replacing selected rows in the matrix with linear (scaled) combinations of that and other rows, the leading coefficients can be reduced to 0 in this reduced row echelon pattern … a b c u 0 d e v 0 0 f w

8 8 Gauss Elimination The values of x, y and z can be computed from this matrix using a “back substitution” method Solving first for z, then y and finally x a b c u x = (u – by – cz)/a 0 d e v y = (v – ez)/d 0 0 f wz = w/f

9 9 Electrical mesh circuits The mesh equations stored in an augmented Matrix are … (R 1 +R 2 ) R 2 0 V 1 -R 2 (R 1 +R 2 +R 3 ) –R 4 0 0 -R 4 (R 4 +R 5 ) -V 2

10 10 Eliminating leading coefficients void eliminate(double a[][N+1], int n, int rc) { // add appropriate multiples of the key row (rc) to each row following // it so the leading coefficient in each following row is eliminated for (int row=rc+1; row<n; row++) { // find ratio of leading coefficients (in column rc) double scale = -a[row][rc]/a[rc][rc]; // add multiple of key row (rc) to this row for (int col=rc; col<=n; col++) a[row][col] += scale*a[rc][col]; } //------------------------------------------------------- // the function is called in main() from a loop like this for (int rc = 0; rc < N-1; rc++) { eliminate(a,N,rc); // eliminate leading coeff. in rows below rc }

11 11 Back substitution // Given this matrix for these equations we find these solutions // | a b c : u | ax + by + cz = u x = (u - cz - by) / a // | 0 d e : v | dy + ez = v y = (v - ez) / d // | 0 0 f : w | fz = w z = w / f // void back_substitute(double a[][N+1], int n, double soln[]) { // solve for last unknown soln[n-1] = a[n-1][n] / a[n-1][n-1]; // work through rows bottom up, solving for unknowns for (int row = n-2; row > =0; row--) { for (int col=n-1; col>row; col--) { // subtract multiples of other unknowns from constant a[row][n] -= a[row][col]*soln[col]; } // divide by leading coefficient soln[row] = a[row][n] / a[row][row]; }

12 12 Something different: Counting letters & plotting results Use a simple program to count all occurrences of each letter in a file Convert all to lower case and ignore other chars Dump results to screen (or file) Use gnuplot to see results Requires a plot command file or use interactively Data is in file in x y pairs

13 13 gnuplot Here is a sample data file “data.txt” 1 4 2 5 3 10 A sample command to display the data points connected by line segments and wait for a mouse click before closing display plot ‘data.txt’ with lines; pause mouse A sample command to display data with histogram type steps and wait for return key plot ‘data.txt’ with histep; pause -1 “hit ENTER” A sample command to display two data sets and wait 10 seconds before closing display plot ‘dataP.txt’ with histep, ‘dataC.txt’ with histep; pause 10

14 14 gnuplot Sample command file “plot_lc.cmd” plot ‘lcdata.txt’ with histep; pause mouse Sample use on command line to invoke gnuplot gnuplot plot_lc.cmd Note: this runs gnuplot in batch mode, so it opens and displays data files in response to commands in the command file

15 15 Something different Counting letters /* FILE: lc.c ** ** Read stdin and count number of occurrences of each letter. ** Case does not matter, and all other characters are skipped. ** single command line option specifies whether output summary ** is printed. List of letter positions 0..26 and totals is ** always output to stdout */ #include int main(int argc, char *argv[]) { int i, ltrCount[26], ltrTotal; char ch;

16 16 Something different Counting letters (lc.c) /* FILE: lc.c ** ** Read stdin and count number of occurrences of each letter. ** Case does not matter, and all other characters are skipped. ** single command line option specifies whether output summary ** is printed. List of letter positions 0..26 and totals is ** always output to stdout */ #include int main(int argc, char *argv[]) { int i, ltrCount[26], ltrTotal; char ch;

17 17 Counting letters (lc.c) /* initialize counters to zero */ ltrTotal = 0; for (i=0; i<26; i++) ltrCount[i] = 0; /* read input and count letters */ while (scanf("%c",&ch) == 1) if (isalpha(ch)) { ltrCount[tolower(ch)-'a']++; ltrTotal++; } /* always dump this list to screen */ for (i=0; i<26; i++) { printf("%d %d\n",i,ltrCount[i]); }

18 18 Counting letters (lc.c) /* if command line has argument –s then print a summary */ if (argc >= 2 && strcmp(argv[1], "-s")==0) { printf(“\n\nLetter counts and frequencies (c:count:freq) are...\n"); for (i=0; i<26; i++) { if (i%2 == 0) printf("\n"); printf("(%c:%4d:%7.4f) ", (i+'a'),ltrCount[i],(double)ltrCount[i]/ltrTotal); } printf("\n\n"); printf("Total number of letters : %7d\n",ltrTotal); } return 0; }

19 19 gnuplot of “lcdata.txt” – data from running lc on “lc.c” 0 39 1 3 2 40 3 21 4 47 5 23 6 7 17 8 59 9 0 10 1 11 40 12 10 13 54 14 44 15 19 16 2 17 55 18 32 19 82 20 26 21 2 22 5 23 0 24 4 25 2


Download ppt "1 Electrical mesh circuits Systems of simultaneous equations Solving using Gauss Elimination Simple C program to count letters Using gnuplot to see results."

Similar presentations


Ads by Google