Help needed for this program!!!
Hi i have written a program and i had difficulty in breaking the program into smaller routines!I have tried but i end up with many errors such as my end results did not tally with results before the routines were implemented.
PLease help!
/* Example 10.6 Pg 347 from Bergen.
Demonstration of Newton-Raphson Iteration.
*/
import java.util.*;
import java.io.*;
import java.lang.Math;
import java.lang.ArrayIndexOutOfBoundsException;
publicclass NewtonRaphsonIt
{
publicstaticvoid main( String args[])throws ArrayIndexOutOfBoundsException
{
int Deg = 360;
int Loop = 3;
double pi = 3.141592653589793238;
double limit = Math.abs(5E-6);
int i,j,k,u;
double V1,V2,V3,P2,P3,Q3,deltaP2,deltaP3,deltaQ3;
double Theta1,Theta2,Theta3;
double P1,Q1,Q2;
double P_Loss,Q_Loss,PF;
double maxi,temp,temp2;
int tempi=0;
//values given
P2=0.6661; P3=-2.8653; Q3=-1.2244;
V1=1; V2=1.05; Theta1=0;
//values supposely to be chosen as a rough guess
Theta2=Theta3=0; V3=1;
double [][] Y_bus ={{-19.98,10,10},{10,-19.98,10},{10,10,-19.98}};
double [][]J=newdouble [3][3];
double []fx =newdouble [3];
double []oldx ={Theta2,Theta3,V3};
double []x={Theta2,Theta3,V3};
double []V={V1,V2,Theta1};
double []Bus ={P2,P3,Q3};
double []delta =newdouble [3];
double []ANS =newdouble [3];
int f = fx.length;
int y = Y_bus.length;
int z = J.length;
int c = Bus.length;
int h = delta.length;
int e = ANS.length;
int ox= oldx.length;
//System.out.println("Arrary size for Ybus is:" +n);
//output Ybus Matrix
System.out.println(" Ybus is:");
System.out.println("");
for ( i=0; i<y; i++)
for ( j=0; j><y; j++)
{
System.out.print(+Y_bus[i][j]+" "+", ");
}
System.out.println();
for ( u=1; u><Loop+1; u++){
//while(delta[0] >< limit){
//Jacobian matrix
//deltaP2/deltaTheta2
//-
J[0][0] = (Math.abs(V[1]))* (Math.abs(V[0])) * Y_bus[1][0] * (Math.cos(x[0]))
+ (Math.abs(V[1]))* (Math.abs(x[2])) * Y_bus[1][2] * (Math.cos(x[0]-x[1]));
//deltaP2/deltaTheta3
//-
J[0][1] = -(Math.abs(V[1]))* (Math.abs(x[2])) * Y_bus[1][2] * (Math.cos(x[0]-x[1]));
//deltaP2/delta|V3|
//--
J[0][2] = (Math.abs(V[1])) * Y_bus[1][2] * (Math.sin(x[0]-x[1]));
//deltaP3/deltaTheta2
//-
J[1][0] = -(Math.abs(V[1]))*(Math.abs(x[2])) * Y_bus[2][0] * (Math.cos(x[1]-x[0]));
//deltaP3/deltaTheta3
//-
J[1][1] = (Math.abs(V[0])) *(Math.abs(x[2])) * Y_bus[2][0] * (Math.cos(x[1]))
+(Math.abs(V[1])) *(Math.abs(x[2])) * Y_bus[2][1] * (Math.cos(x[1]-x[0]));
//deltaP3/delta|V3|
//--
J[1][2]= (Math.abs(V[0])) * Y_bus[2][0] * (Math.sin(x[1]))
+(Math.abs(V[1]))* Y_bus[2][1] * (Math.sin(x[1]-x[0]));
//deltaQ3/deltaTheta2
//-
J[2][0] = - Y_bus[2][0] * (Math.abs(x[2])) * (Math.abs(V[1])) *(Math.sin(x[1]-x[0]));
//deltaQ3/deltaTheta3
//-
J[2][1]= (Math.abs(x[2]))*(Math.abs(V[0])) * Y_bus[2][0] * (Math.sin(x[1]))
+(Math.abs(x[2]))*(Math.abs(V[1])) * Y_bus[2][1] * (Math.sin(x[1]-x[0]));
//deltaQ3/delta|V3|
//--
J[2][2]= -((Math.abs(V[0])) * Y_bus[2][0] * (Math.cos(x[1]))
+ (Math.abs(V[1])) * Y_bus[2][1] * (Math.cos(x[1]-x[0]))
+ (Math.abs(x[2]))* 2 * Y_bus[2][2]);
//output Jacobian Matrix
System.out.println();
System.out.println(+u+")"+" Jacobian Matrix is:");
System.out.println("--");
for ( i=0; i<z; i++)
for ( j=0; j><z; j++)
{
System.out.print(+J[i][j]+" "+", ");
}
System.out.println();
// variable P2
//
fx[0] = (Math.abs(V[1]))*(Math.abs(V[0])) * Y_bus[1][0] * (Math.sin(x[0]-V[2]))
+ (Math.abs(V[1]))*(Math.abs(x[2])) * Y_bus[1][2] * (Math.sin(x[0]-x[1]));
//System.out.println("P2 is :"+fx[0]);
// variable P3
//
fx[1] = (Math.abs(x[2]))*(Math.abs(V[0])) * Y_bus[2][0] * (Math.sin(x[1]-V[2]))
+ (Math.abs(x[2]))*(Math.abs(V[1])) * Y_bus[2][1] * (Math.sin(x[1]-x[0]));
//System.out.println("P3 is :"+fx[1]);
// variable Q3
//
fx[2] = -((Math.abs(x[2]))*(Math.abs(V[0])) * Y_bus[2][0] * (Math.cos(x[1]-V[2]))
+ (Math.abs(x[2]))*(Math.abs(V[1])) * Y_bus[2][1] * (Math.cos(x[1]-x[0]))
+ (Math.abs(x[2]))*(Math.abs(x[2])) * Y_bus[2][2]);
//System.out.println("Q3 is :"+fx[2]);
//Output f(x)
System.out.println();
System.out.println(+u+")"+" Function f(x) is:");
System.out.println("--");
for ( k=0; k><f; k++)
{
System.out.println(+fx[k]);
}
System.out.println();
//ouput P2,P3,Q3 as a matrix
System.out.println();
System.out.println(+u+")"+" Matrix [P2,P3,Q3] is:");
System.out.println("");
for ( k=0; k><c; k++)
{
System.out.println(+Bus[k]);
}
System.out.println();
//finding values of deltaP2,deltaP3,deltaQ3
System.out.println();
System.out.println(+u+")"+" Matrix [d.P2,d.P3,d.Q3] is:");
System.out.println("");
for ( k=0; k><h; k++)
{
System.out.println(delta[k]= Bus[k]- fx[k]);
}
System.out.println();
//find delta x={deltaTheta2,deltaTheta3,delta|V3|}
//Gaussian Elimination method
//solve Ax=b
//matrix J will be destroyed,solution will be in matrix delta
if(J[0].length != z)
{
System.out.println("Matrix J must be a Square Matrix!");
return;
}
for(j=0; j><z; j++)
{
maxi=-1E-12;
for(i=j; i><z; i++)
{
if(Math.abs(J[i][j])> maxi)
{
tempi=i;
maxi=Math.abs(J[i][j]);
//System.out.println(+tempi);
}
}
if(maxi<1E-12)
{
System.out.println
("The Set either has mutltiple solutions or no unique solution!");
return;
}
//Permuting tempi row with the j-th row
if(tempi != j)
{
for(k=j; k<z; k++)
{
temp=J[j][k];//store 1st row into temp space
J[j][k]=J[tempi][k];//store last row into 1st row
J[tempi][k]=temp;//store 1st row back into last row
}
temp=delta[j];//store 1st row into temp space
delta[j]=delta[tempi];//store last row into 1st row
delta[tempi]=temp;//store 1st row back into last row
}
//Divide j-th row by J[j][j]
temp=J[j][j];
delta[j]=delta[j]/temp;
for(k=j; k><z; k++)
{
J[j][k]=J[j][k]/temp;
}
for(i=0; i><z; i++)
{
if(i != j)
{
temp=J[i][j];
temp2=delta[i];
for(k=0; k><z; k++)
{
J[i][k] = J[i][k]-temp*J[j][k];
delta[i]= temp2-temp*delta[j];
//System.out.println(+delta[j]);
//System.out.println(+J[i][k]);
}
}
}
}
// values of deltax in radians
System.out.println(+u+")"+" The solution for AX=b is:");
System.out.println("-");
for(i=0; i><z; i++)
{
System.out.println(+delta[i]);
x[i] = delta[i];
}
// values of deltax in degrees
System.out.println();
System.out.println(+u+")"+" The solution for deltaX is:");
System.out.println("");
ANS[0] = x[0] * Deg/(2*pi);
ANS[1] = x[1] * Deg/(2*pi);
ANS[2] = x[2];
for(i=0; i><e; i++)
{
System.out.println(+ANS[i]);
}
//finding X^1=x^0 + deltax^0
System.out.println();
System.out.println(" The solution for X after iteration:"+"("+u+")");
System.out.println("");
for(i=0; i><e; i++)
{
x[i] = oldx[i] + ANS[i];
System.out.println(+x[i]);
System.out.println();
//System.out.println("oldx:" +oldx[i]);
//System.out.println();
}
//update the oldx with the new values from x
for(i=0; i><ox; i++)
{
oldx[i] = x[i];
//System.out.println("oldx:" +oldx[i]);
//System.out.println();
}
//convert to radians for Math.sin/cos()
x[0] = x[0] * (2*pi)/Deg;
x[1] = x[1] * (2*pi)/Deg;
x[2] = x[2];
}//for loop
//if (delta[0] == limit)
//{
//break;
//}
//}//while loop
// calcuate P1,Q1,Q2
P1 = (Math.abs(V[0]))*(Math.abs(V[1])) * Y_bus[0][1]* (Math.sin(V[2]-x[0]))
+ (Math.abs(V[0]))*(Math.abs(x[2])) * Y_bus[0][2] * (Math.sin(V[2]-x[1]));
System.out.println("P1 is :"+P1);
System.out.println();
Q1 = -((Math.abs(V[0]))*(Math.abs(V[1])) * Y_bus[0][1] * (Math.cos(V[2]-x[0]))
+ (Math.abs(V[0]))*(Math.abs(x[2])) * Y_bus[0][2] * (Math.cos(V[2]-x[1]))
+ (Math.abs(V[0]))*(Math.abs(V[0])) * Y_bus[0][0]);
System.out.println("Q1 is :"+Q1);
System.out.println();
Q2 = -((Math.abs(V[1]))*(Math.abs(V[0])) * Y_bus[1][0] * (Math.cos(x[0]-V[2]))
+ (Math.abs(V[1]))*(Math.abs(x[2])) * Y_bus[1][2] * (Math.cos(x[0]-x[1]))
+ (Math.abs(V[1]))*(Math.abs(V[1])) * Y_bus[1][1]);
System.out.println("Q2 is :"+Q2);
System.out.println();
//Power Loss P_Loss = (P1+P2) - P3
P_Loss = (P1+P2) - P3;
System.out.println("Power Loss is :"+P_Loss);
System.out.println();
System.out.println("P2 is :"+P2);
System.out.println();
System.out.println("P3 is :"+P3);
System.out.println();
System.out.println("Q3 is :"+Q3);
System.out.println();
//Reactive Power Loss
Q_Loss = (Q1+Q2) - Q3;
System.out.println("Reactive Power Loss:" +Q_Loss);
System.out.println();
System.out.println("Q1 is :"+Q1);
System.out.println();
System.out.println("Q2 is :"+Q2);
System.out.println();
System.out.println("Q3 is :"+Q3);
System.out.println();
//Power factor PF = P/sqrt[(P^2 + Q^2)]
PF = P3/(Math.sqrt( (P3*P3) + (Q3*Q3)));
System.out.println("Power Factor is :"+PF);
System.out.println();
}
}
>

