// ac_circuit.java simple AC circuit analysis // java myjava.ac_circuit < ac_circuit.dat // // Label the circuit nodes consecutively, zero 0 must be ground // Every node must be a part of a current loop and the circuit // must be connected. The input is: // N n n is highest node number // R i j value_in_ohm resister between node i and node j // C i j value_in_farad capacitor between node i and node j // L i j value_in_henry inductor between node i and node j // I i j value_in_amp current source, flow from node i to node j // F value_in_hertz_initial value_in_hertz_maximum factor // frequency is set to initial value and circuit analyzed, // node voltages are printed in magnitude and complex, // frequency is multiplied by 'factor' and analysis repeated, // the program exits when frequency is greater that maximum // // A voltage source V must have a series resistor R. This is coded // for input as two lines I i j value_V/R // R i j value_R node j is positive // A value of zero is not allowed for any component. // Solutions may not exists for some circuits at some frequencies. // // Method: Construct admittance matrix Y and current source vector I // Solve system of equations Y*V=I for node voltages V // The subscript zero is never used in the Y matrix, I vector or V vector // I i j V results in I(i)=I(i)-V I(j)=I(j)+V // R i j V results in Y(i,i)=Y(i,i)+1/V Y(j,j)=Y(j,j)+1/V // Y(i,j)=Y(i,j)-1/v Y(j,i)=Y(j,i)-1/V // C i j V results in YC = (0,2.0*Pi*F*V) complex number // Y(i,i)=Y(i,i)+YC Y(j,j)=Y(j,j)+YC // Y(i,j)=Y(i,j)-YC Y(j,i)=Y(j,i)-YC // L i j V results in YL = (0,-1.0/(2*Pi*F*V)) complex number // Y(i,i)=Y(i,i)+YL Y(j,j)=Y(j,j)+YL // Y(i,j)=Y(i,j)-YL Y(j,i)=Y(j,i)-YL // The Y matrix must be built for every frequency. // The matrix equation is solved for the node voltages for each frequency. // package myjava; import myjava.*; class ac_circuit { Complex Y[][] = new Complex[3][3]; Complex I[] = new Complex[3]; Complex V[] = new Complex[3]; double F, F_initial, F_final, F_factor; int n; int n_input; // for storing input for various frequencies char type[] = new char[100]; int node1[] = new int[100]; int node2[] = new int[100]; double value[] = new double[100]; ac_circuit() { double val, TPIF; Complex cval; int ii, jj; System.out.println("ac_circuit running"); Complex i1 = new Complex(0.0,1.0); System.out.println("ac_circuit about to read input"); // no read routine, just fill in circuit data F_initial = 1000.0; F_final = 2000.0; F_factor = 2.0; n = 3; n_input = 7; type[0] = 'N'; node1[0] = 3; type[1] = 'I'; node1[1] = 0; node2[1] = 1; value[1] = 1.0; type[2] = 'R'; node1[2] = 0; node2[2] = 1; value[2] = 8.0; type[3] = 'R'; node1[3] = 1; node2[3] = 2; value[3] = 2.0; type[4] = 'R'; node1[4] = 2; node2[4] = 3; value[4] = 4.0; type[5] = 'R'; node1[5] = 2; node2[5] = 0; value[5] = 12.0; type[6] = 'R'; node1[6] = 0; node2[6] = 3; value[6] = 8.0; F = F_initial; while(F<=F_final) { System.out.println("ac_circuit about to build Y and I"); TPIF = 6.28*F; // radians per second ComplexMatrix.zero(Y); ComplexMatrix.zero(V); ComplexMatrix.zero(I); for(int i=0; i=0) Y[ii][ii]=Y[ii][ii].add(val); if(jj>=0) Y[jj][jj]=Y[jj][jj].add(val); if(ii>=0 && jj>=0) Y[ii][jj]=Y[ii][jj].subtract(val); if(ii>=0 && jj>=0) Y[jj][ii]=Y[jj][ii].subtract(val); } else if(type[i]=='L' || type[i]=='l') { ii = node1[i]-1; jj = node2[i]-1; cval = (i1.divide(TPIF*value[i])).negate(); if(ii>=0) Y[ii][ii]=Y[ii][ii].add(cval); if(jj>=0) Y[jj][jj]=Y[jj][jj].add(cval); if(ii>=0 && jj>=0) Y[ii][jj]=Y[ii][jj].subtract(cval); if(ii>=0 && jj>=0) Y[jj][ii]=Y[jj][ii].subtract(cval); } else if(type[i]=='C' || type[i]=='c') { ii = node1[i]-1; jj = node2[i]-1; cval = i1.multiply(TPIF*value[i]); if(ii>=0) Y[ii][ii]=Y[ii][ii].add(cval); if(jj>=0) Y[jj][jj]=Y[jj][jj].add(cval); if(ii>=0 && jj>=0) Y[ii][jj]=Y[ii][jj].subtract(cval); if(ii>=0 && jj>=0) Y[jj][ii]=Y[jj][ii].subtract(cval); } else if(type[i]=='I' || type[i]=='i') { ii = node1[i]-1; jj = node2[i]-1; val = value[i]; if(ii>=0) I[ii]=I[ii].subtract(val); if(jj>=0) I[jj]=I[jj].add(val); } } System.out.println("admittance matrix Y"); ComplexMatrix.print(Y); System.out.println("current vector I"); ComplexMatrix.print(I); ComplexMatrix.solve(Y, I, V); System.out.println("node voltages at frequency = "+F); ComplexMatrix.print(V); F = F * F_factor; } // end while loop on frequency } // end class ac_circuit public static void main(String args[]) { new ac_circuit(); } } // end ac_circuit.java