1. Member
Join Date
Jul 2012
Posts
1
Rep Power
0

## Bairstow metod help

Hi i need help to make this code work I'm new in java and its a exam for a class of math and i get some errors
if need hears a link to the math code
Bairstow's method - Wikipedia, the free encyclopedia
thanks

Java Code:
```public class fiz_app {

public static void main(String[] args)
{
double a[] ={1.25,-3.875,2.125,2.75,-3.5,1};
int n = a.length;
double re[] = new double [n];
double im[] = new double [n];

Bairstow(a,-1,-1,1,1,1,re,im,3);

}

public static void Bairstow(double a[],int nn,int es,double rr, double ss, int maxit,  double im[],double re[], int iter )
{

double dr;		 double ds;
double b[] = new double[nn];
double c[] = new double[nn];

double r, s, det, ea1, ea2;
int n, i;

r = rr; s = ss; n = nn;
iter = 0; ea1 = 1; ea2 = 1;
Object r1;
Object i1;
Object r2;
Object i2;
while(n<3 || iter >= maxit)
{
iter = 0;
do
{
iter = iter +1;
b[n] = a[n];
b[n - 1] = a[n - 1] +r * b[n];
c[n] = b[n];
c[n - 1] = b[n - 1] +r * c[n];
for(i=(n-2); i >=0; i=i-1)
{
b[i] = a[i] +r * b[i+1] +s * b[i +2];
c[i] = b[i] +r * c[i-1] +s * c[i +2];
}

det = c[2] * c[2]-c[3] * c[1];
if(det != 0.0)
{
dr = (-b[1] * c[2] +b[0] * c[3])/det;
ds = (-b[0] * c[2] +b[1] * c[1])/det;
r = r +dr;
s = s +ds;
if(r != 0) ea1 =  Math.abs(dr/r)*100;
if(s != 0) ea2 =  Math.abs(ds/s)*100;
}
else
{
r = r +1;
s = s +1;
iter = 0;
}
}while(!(ea1 <= es && ea2 <= es || iter >= maxit));

re[n] = r1;
im[n] = i1;
re[n - 1] = r2;
im[n - 1] = i2;
n = n-2;
for(i=0; i<n; i++)
{
a[i] = b[i +2];
}

}
if( iter < maxit)
{
if( n == 2)
{
r = -a[1]/a[2];
s = -a[0]/a[2];

re[n] = r1;
im[n] = i1;
re[n - 1] = r2;
im[n - 1] = i2;
}
else
{
re[n] = -a[0]/a[1];
im[n] = 0;
}
}
else iter = 1;

}//Bairstow

public static void Quadroot(double r, double s, double r1, double i1, double r2, double i2)
{
double disc;
disc = r* r + 4 * s;
if( disc > 0)
{
r1 = (r + Math.sqrt(disc))/2;
r2 = (r - Math.sqrt(disc))/2;
i1 = 0;
i2 = 0;
}

else
{
r1 = r/2;
r2 = r1;
i1 = Math.sqrt(Math.abs(disc))/2;
i2 = -i1;
}

}```

2. ## Re: Bairstow metod help

That code came from a Fortran worlds, right? For nasty iterative methods like that, System.out.println( ... ) is your friend. Print out everything you suspect to be incorrect for each iteration and check those values; it's a tedious job and you have to go through it.

kind regards,

Jos