19
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 CSE 20232 Lecture 35 – Gauss Elimination Lecture 35 – Gauss Elimination & Circuits, and more & Circuits, and more

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

Embed Size (px)

Citation preview

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

1

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

CSE 20232CSE 20232Lecture 35 – Gauss Elimination & Lecture 35 – Gauss Elimination &

Circuits, and moreCircuits, and more

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

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

V1 V2R4

R5R3

R2

R1

I1

-

+

-

+

I2 I3

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

3

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

V1 V2R4

R5R3

R2

R1

i1

-

+

-

+

i2 i3

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

4

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

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

5

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

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

6

Augmented matrix

The same system in augmented matrix form looks like this …

a1 b1 c1 d1

a2 b2 c2 d2

a3 b3 c3 d3

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

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

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

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 w z = w/f

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

9

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

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

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}

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

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]; }}

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

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

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

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

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

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

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

15

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;

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

16

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;

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

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]); }

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

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;}

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

19

gnuplot of “lcdata.txt” – data from running lc on “lc.c”0 391 32 403 214 475 236 67 178 599 010 111 4012 1013 5414 4415 1916 217 5518 3219 8220 2621 222 523 024 425 2