King Coffee
07-11-2007, 09:36 PM
Hi again,
I make minor changes to the xqroot program of NR 2.11 C++, as shown below. The quadratics of x^4 - 1 is x^2 -1 and x^2 + 1.
when I run the following program I get:
<b,c> = <0,-1> and <0,0>
You should get:
<b,c> = <0,-1> and <0,1>
Sincerely,
King
#include "stdafx.h" // default C++ compiling, VC++
#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
#include "nr.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int N=4; // degree of polynomial
const int NTRY=10;
const DP EPS=1.0e-6,TINY=1.0e-5;
const DP p_d[N+1]={-1,0.0,0.0,0.0,1.0};
bool newroot;
int i,j,nroot=0;
Vec_DP p(p_d,N+1),b(NTRY),c(NTRY);
cout << endl << "P(x)=x^4 - 1" << endl;
cout << "Quadratic factors x^2+bx+c" << endl << endl;
cout << setw(6) << "factor" << setw(11) << "b";
cout << setw(13) << "c" << endl << endl;
cout << fixed << setprecision(6);
for (i=0;i<NTRY;i++) {
c[i]=0.5*(i+1);
b[i] = -0.5*(i+1);
NR::qroot(p,b[i],c[i],EPS);
if (nroot == 0) {
cout << setw(4) << nroot << setw(16) << b[i];
cout << setw(13) << c[i] << endl;
nroot=1;
} else {
newroot=true;
for (j=0;j<nroot;j++)
if ((fabs(b[i]-b[j]) < TINY) && (fabs(c[i]-c[j]) < TINY))
newroot=false;
if (newroot) {
cout << setw(4) << nroot << setw(16) << b[i];
cout << setw(13) << c[i] << endl;
++nroot;
}
}
}
return 0;
}
I make minor changes to the xqroot program of NR 2.11 C++, as shown below. The quadratics of x^4 - 1 is x^2 -1 and x^2 + 1.
when I run the following program I get:
<b,c> = <0,-1> and <0,0>
You should get:
<b,c> = <0,-1> and <0,1>
Sincerely,
King
#include "stdafx.h" // default C++ compiling, VC++
#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
#include "nr.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int N=4; // degree of polynomial
const int NTRY=10;
const DP EPS=1.0e-6,TINY=1.0e-5;
const DP p_d[N+1]={-1,0.0,0.0,0.0,1.0};
bool newroot;
int i,j,nroot=0;
Vec_DP p(p_d,N+1),b(NTRY),c(NTRY);
cout << endl << "P(x)=x^4 - 1" << endl;
cout << "Quadratic factors x^2+bx+c" << endl << endl;
cout << setw(6) << "factor" << setw(11) << "b";
cout << setw(13) << "c" << endl << endl;
cout << fixed << setprecision(6);
for (i=0;i<NTRY;i++) {
c[i]=0.5*(i+1);
b[i] = -0.5*(i+1);
NR::qroot(p,b[i],c[i],EPS);
if (nroot == 0) {
cout << setw(4) << nroot << setw(16) << b[i];
cout << setw(13) << c[i] << endl;
nroot=1;
} else {
newroot=true;
for (j=0;j<nroot;j++)
if ((fabs(b[i]-b[j]) < TINY) && (fabs(c[i]-c[j]) < TINY))
newroot=false;
if (newroot) {
cout << setw(4) << nroot << setw(16) << b[i];
cout << setw(13) << c[i] << endl;
++nroot;
}
}
}
return 0;
}