amirvahid
09-14-2011, 12:56 PM
I need to implement some FFT into my C++ code and since there was not any thread on this subject for the third edition in this forum, I am trying to provide some examples as I move on. davekw7x: please make comments/add samples as we move on. I could not find the twofft function in nr3 codes and hence I added it to four.h header file.
The example on p.637 (sec 12.7) of the nr3 book seems vague to me. davekw7x: could you please elaborate more on this with actual numbers and a complete example set. Also, I could not translate xfourf from numerical recipes example book to the new style. davekw7x, I wonder if you could help me to convert this into the current format of nr3.
I have tried to attach some example of the codes that I could convert to the new style. The examples are in the attachment. I had problems that I stated above and I wonder if you could help me on those. Thanks!
Amir Vahid
amirvahid
12-02-2011, 03:27 PM
Hi everybody,
Since it is a long time from the original post and Dave didn't reply, any other response would be highly appreciated and would be very helpful for educational purposes. I came along with my problem in a different way. Thanks!
kutta
12-11-2011, 01:52 AM
Hi everybody,
Since it is a long time from the original post and Dave didn't reply, any other response would be highly appreciated and would be very helpful for educational purposes. I came along with my problem in a different way. Thanks!
Hello NR colleague,
Though the example shown here is too fundamental,please
tell me if you are able to find any use.The example is a shooting solution for a bvp in C++.How ever the procedure for yourr Fourier FFt can be tried on the same method since you wanted it to be very simple example set,I am sure you would be able to codify similarly.
I am also yet to hear for my various suggestions made either
from reverevned authors or from the registerd users.
So do not get disheartened for no reply.
Pl reply
Thanks
As
C.R.Muthukumar(Kutta)
# include <iostream.h>
# include <math.h>
# include <fstream.h>
# include <iomanip.h>
#define Output
class s_ode
{
private:
int i, j, l, n;
double a, b, c, d, h, x, x1, xi, xf;
double *b1, *b2, *b3, *g, *y, *y1;
public:
s_ode () //Constructor
{
a = (sqrt(2)-1)/2; b = (2-sqrt(2))/2;
c = -sqrt(2)/2; d = 1+sqrt(2)/2;
}
void func(double p, double *q, double *r) //ODEs
{
r[0] = q[1];
r[1] = q[2];
r[2] = - q[0]*q[0]*q[1] - q[0]*cos(p) + sin(p)*sin(p);
}
void solution (); //Function for implementing the RKG method
~s_ode () { delete b1, b2, b3, g, y, y1; } //Destructor
};
//main function
int main ()
{
s_ode shooting;
shooting.solution ();
return 0;
}
//Apply RKG method
void s_ode :: solution ()
{
n = 3; //Number of differential equations = n
b1 = new double[n]; //Dynamic allocation of memory
b2 = new double[n];
b3 = new double[n];
g = new double[n];
y = new double[n];
y1 = new double[n];
l = 1000; //Number of divisions
xi = 0.0; //Starting value of x
xf = 30.0; //Ending value of x
for (i=0; i<n; i++)
{
cout<<"\nEnter y["<<i<<"] = ";
cin>>y[i];
}
h = (xf-xi)/l;
x = xi;
ofstream fout ("shooting.out"); //Open output file
fout.precision(4);
cout.precision(4);
for(i = 0; i<l; i++) //RKG method
{
for(j=0; j<n; j++)
{ y1[j] = y[j]; }
x1 = x;
func (x,y,g);
for(j=0; j<n; j++)
{
b1[j] = g[j];
y[j] = y1[j] + h*g[j]/2;
}
x = x1 + h/2;
func (x,y,g);
for(j=0; j<n; j++)
{
b2[j] = g[j];
y[j] = y1[j]+h*(a*b1[j]+b*g[j]);
}
func (x,y,g);
for(j=0; j<n; j++)
{
b3[j] = g[j];
y[j] = y1[j]+h*(c*b2[j]+d*g[j]);
}
x = x1 + h;
func (x,y,g);
for(j=0; j<n; j++ )
{ y[j] = y1[j]+h*(b1[j]+g[j]+2*(b*b2[j]+d*b3[j]))/6; }
Output
fout<<x<<setw(10)<<y[0]<<endl;
cout<<x<<setw(10)<<y[0]<<endl;
}
fout.close (); //Close output file
}
amirvahid
01-31-2012, 06:23 AM
Thanks Kutta for your message. You are always helpful.