Bessel function problem


jdkim
07-24-2009, 04:10 AM
Hi,
I was trying to make a figure of the Bessel function
with Microsoft Visual C++ 6.0 compiler.
My code is a simple combination of bessel.h and psplot.h
from NR3 as follows:
***********
#include "nr3.h"
#include "psplot.h"
#include "bessel.h"

Int main(void) {
const Int RES=500;
const Doub XLL=75., XUR=525., YLL=250., YUR=700.;
char *plotfilename = tmpnam(NULL);
VecDoub xx(RES), yy(RES);
Doub x1,x2;
Doub x3;
Int i;
Bessjy mybessel;
FILE *fp;

fp=fopen("test.txt","w");
for (;;) {
Doub ymax = -9.99e99, ymin = 9.99e99, del;
cout << endl << "Enter x1 x2 (x1=x2 to stop) :" << endl;
cin >> x1 >> x2;
if (x1==x2) break;
for (i=0;i<RES;i++) {
xx[i] = x1 + i*(x2-x1)/(RES-1.);
x3=xx[i];
yy[i] = mybessel.jn(0, x3);

fprintf(fp,"%f %f \n",xx[i],yy[i]);

if (yy[i] > ymax) ymax=yy[i];
if (yy[i] < ymin) ymin=yy[i];
}
del = 0.05*((ymax-ymin)+(ymax==ymin ? abs(ymax) : 0.));


PSpage pg(plotfilename);
PSplot plot(pg,XLL,XUR,YLL,YUR);
plot.setlimits(x1,x2,ymin-del,ymax+del);
plot.frame();
plot.autoscales();
plot.xlabel("x");
plot.ylabel("Bessel J_0(x)");
plot.setlinewidth(1.);
plot.lineplot(xx,yy);
if (ymax*ymin < 0.) plot.lineseg(x1,0.,x2,0.);
plot.display();
}

remove(plotfilename);

fclose(fp);
return 0;
}
**************
This code is not optimal. Anyway it was successfully compiled.
The figure showed nice up-down curve between 0 and 8.
But around x=8, there are abrupt changes.
In addition, if I increase the order n, the graph showed like a histogram.
I hope you have an answers for this.
Would you show me a track?
Thanks in advance.
jdkim

davekw7x
07-24-2009, 09:40 AM
...I hope you have an answers for this...

The problem is not with bessel.h; it's in the way that your particular compiler treats psplot.h.


The old, creaky, cranky MSVC++ Version 6 has a bug that shows up in psplot.h. The following thread shows how to change nr3.h to work around that particular problem: http://www.nr.com/forum/showthread.php?t=1459

On Windows xp compiling with Visual C++ Version 6 and using the original nr3.h from my Version 3.0 CD I see exactly what you describe. With the fixes in nr3.h, it looks OK.


Regards,

Dave

Footnote: I notice that the link given in the above thread to download nr3.h actually gives me the original distribution's nr3.h, not the fixed nr3.h, so here's what I recommend:

1. Make a copy of your nr3.h (just in case...). Call it something like "nr3_distribution.h"

2. Apply the changes to the nr3.h that you will be using.

3. Compile again.

With the original distribution's nr3.h, using Windows XP and compiling from a command line with Visual C++ Version 6, I got the following compiler messages:

./nr3.h(43) : warning C4172: returning address of local variable or temporary
./psplot.h(165) : see reference to function template instantiation 'const double &__cdecl MIN(const double &,const double &)' being compiled
./nr3.h(33) : warning C4172: returning address of local variable or temporary
./psplot.h(166) : see reference to function template instantiation 'const double &__cdecl MAX(const double &,const double &)' being compiled

With the fixed version, there were no compiler messages.

Bottom line: Sometimes compiler warnings are significant. They don't necessarily mean there is a bug in the program, but in this case they definitely mean that the compiler is not doing the Right Thing.

jdkim
07-24-2009, 08:11 PM
Hi, Dave
Thanks for your comments.
My problem was solved and now I feel the NR much more comfortably. Maybe I should change my C compiler.
JD