200 likes | 386 Views
CSE 20232 Lecture 35 – Gauss Elimination & Circuits, and more. Electrical mesh circuits Systems of simultaneous equations Solving using Gauss Elimination Simple C program to count letters Using gnuplot to see results. R 1. R 3. R 5. +. +. R 2. R 4. V 1. I 1. I 2. I 3. V 2. -. -.
E N D
CSE 20232Lecture 35 – Gauss Elimination & Circuits, and more • Electrical mesh circuits • Systems of simultaneous equations • Solving using Gauss Elimination • Simple C program to count letters • Using gnuplot to see results
R1 R3 R5 + + R2 R4 V1 I1 I2 I3 V2 - - 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
R1 R3 R5 + + R2 R4 V1 i1 i2 i3 V2 - - Electrical mesh circuits • Sum of voltages within each circuit is zero, so … • We have 3 linear equations with 3 unknowns • 0 = -V1 + i1R1 + (i1-i2)R2 • 0 = (i2-i1)R2 + i2R3 + (i2-i3)R4 • 0 = (i3-i2)R2 + i3R5 + V2
Electrical mesh circuits • These 3 linear equations • 0 = -V1 + i1R1 + (i1-i2)R2 • 0 = (i2-i1)R2 + i2R3 + (i2-i3)R4 • 0 = (i3-i2)R2 + i3R5 + V2 • Can be rewritten as • (R1+R2)i1+ R2i2 = V1 • -R2i1 + (R1+R2+R3)i2 – R4i3 = 0 • -R4i2 + (R4+R5)i3= -V2
A brief aside:Matrix form of linear system • Any 3 linearly independent equations … • a1x + b1y + c1z = d1 • a2x + b2y + c2z = d2 • a3x + b3y + c3z = d3 • Can be placed in matrix form … a1 b1 c1 x = d1 a2 b2 c2 y = d2 a3 b3 c3 z = d3
Augmented matrix • The same system in augmented matrix form looks like this … a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3
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
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 vy = (v – ez)/d 0 0 f w z = w/f
Electrical mesh circuits • The mesh equations stored in an augmented Matrix are … (R1+R2) R2 0 V1 -R2 (R1+R2+R3) –R4 0 0 -R4 (R4+R5) -V2
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 }
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]; } }
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
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
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
Something differentCounting 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 <stdio.h> #include <string.h> #include <ctype.h> int main(int argc, char *argv[]) { int i, ltrCount[26], ltrTotal; char ch;
Something differentCounting 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 <stdio.h> #include <string.h> #include <ctype.h> int main(int argc, char *argv[]) { int i, ltrCount[26], ltrTotal; char ch;
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]); }
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; }
gnuplot of “lcdata.txt” – data from running lc on “lc.c” 0 39 1 3 2 40 3 21 4 47 5 23 6 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