Bracketing Method-Chapter(9)


kutta
01-29-2009, 09:25 PM
Hello Comrades

Shall be glad to receive your solution for this Template class
function as the same requires proper initiation of the variables.
As Initition procedure is different from that of an ordinary type and template type of functions,
your try is sought with plethora of thanks.
Though simple but gets complicated with too many parametres
Thats why time and again i have been wanting if somebody suggests an universal way of tackling initiating proplems or any body can alogorithamise(if at all this verbaal request is apt) the same enabling for quick use of initialising..
the same .

#include "nr3.h"
#include <iomanip.h>
#include <iostream>
#include <algorithm>
#include <functional>
#define func
using namespace std;

template <class T>
Bool zbrac(T &func, Doub &x1, Doub &x2)
{

const Int NTRY=50;
const Doub FACTOR=1.6;


if (x1 == x2) throw("Bad initial range in zbrac");

Doub f1=func(x1);
Doub f2=func(x2);
for (Int j=0;j<NTRY;j++) {
if (f1*f2 < 0.0) return true;
if (abs(f1) < abs(f2))
f1=func(x1 += FACTOR*(x1-x2));
else
f2=func(x2 += FACTOR*(x2-x1));
}
return false;
}

int main()
{

Doub x1[3]={3.4,6.7,8.9};
Doub x2[3]={1.2,3.5,5.6};
Doub f1;//= func(x1);
Doub f2;//= func(x2);
//zbrac(f1,3,3);

cout << "Bracketed X1-array";
for(int i=0;i,3;i++)
cout << x1[i] << " ";
cout << endl;


//zbrac(f2,3,3);
cout << "Bracketed -y array";
for(int j =0;j< 3;j++)
cout << x2[j] << " " ;
cout << endl;
"zbrac(func,6.7,3.5)";

return 0;
}





/**


The output is as follows though it is incomplete inlieu of its void function
since its declaration is vague in the above example;
Bracketed X1-array3.4 6.7 8.9 4.24399e-314 1.09719e+270 1.78797e-307 4.98786e-30
6 -1.#QNAN 2.31288e-306 4.98617e-306 5.56268e-307 1.78811e-307 2.71615e-311 -1.5
4428 -5.02206e-301 2.58465e+161 -7.41456e-304 2.58472e+161 3.68421e+180 5.68175e
-322 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -3.58008e-301 -7.53703e+0
06 1.78807e-307 1.59123e-314 1.66227e+024 5.56268e-307 -1.75139 1.59122e-314 -1.
54396 1.80501e-313 1.07413e-314 0 0 0 0 0 0 0 0 1.31719e-309 2.31408e-306 -1.#QN
AN 1.07413e-314 -4.3711e-282 1.59126e-314
*/.



Thanking you
As
Kutta(C.R.Muthukumar)

:)

davekw7x
01-30-2009, 11:46 AM
...time and again i have been wanting if somebody suggests an universal...I am sorry, but I don't think there is a "universal way" to tell anyone how to solve all of the problems that can use Numerical Recipes Functions.


Using a function in the Numerical Recipes Version 3 library involves writing some C++ code. The text contains a description of each of the functions and some narrative that might indicate when and why you might want to use one or more of the functions.

For example, the function zbrac() tries to find an interval for which a user function takes on opposite signs at the end points.
The idea is that once you have such a "bracketed interval," you might use the bisection method to try to find a real zero of the function.

In general, there are no guarantees that any program procedure can find a bracketed interval, but it tries.

You give zbrac your function and a couple of numbers and it tries. For example, if your function is sin(x)/x, you could make a test program as follows: For this example, I have hard-coded values of 1.0 and 2.0 as starting points. You could write a program that gets these values from the user (with cin>>) or you could make a loop that tries different values from arrays, or whatever...


//
// Illustration of use of zbrac with a "plain old function"
//
// davekw7x
//

#include "../code/nr3.h"
#include "../code/roots.h"

//
// A function that returns sin(x)/x
//
Doub f(Doub x)
{
if (x == 0) {
return 1;
}
return sin(x) / x;
}

int main()
{
Doub x1 = 1.0;
Doub x2 = 2.0;
Bool success;

cout << "zbrac will try to find a bracketed interval for sin(x)/x"
<< endl << endl;

cout << scientific;
cout << "Initially : x1 = " << x1 << ", x2 = " << x2 << endl;

//
// Feed our function and points to zbrac
//
success = zbrac(f,x1,x2);

if (success) {
cout << "After zbrac: x1 = " << x1 << ", x2 = " << x2 << endl << endl;
cout << "There may be a root on the interval (x1,x2), since" << endl
<< "f(" << x1 << ") = " << f(x1)
<< " and f(" << x2 << ") = " << f(x2) << endl;
}
else {
cout << "zbrac failed to find a bracketed interval." << endl;
}
return 0;
}


Output:

zbrac will try to find a bracketed interval for sin(x)/x

Initially : x1 = 1.000000e+00, x2 = 2.000000e+00
After zbrac: x1 = 1.000000e+00, x2 = 3.600000e+00

There may be a root on the interval (x1,x2), since
f(1.000000e+00) = 8.414710e-01 and f(3.600000e+00) = -1.229223e-01



Now, suppose you want to use zbrac() to try to find a bracketed interval for the function sin(a*x)/(a*x), where a is some constant not known at compile time. When you call zbrac(), you can't give it the function and a value of a and two values of x. You can just give it the function and two values of x.

That is where functors come in. If you aren't familiar with the terminology, a functor is simply an overloaded () operator for a C++ class. If you don't know what that means, then I respectfully suggest that you should expect to have to learn more C++ before using tools like this.

//
// Illustration of use of zbrac with a functor
//
// davekw7x
//
#include "../code/nr3.h"
#include "../code/roots.h"
//
// A class with a functor that returns sin(a*x)/(a*x)
//
class Ftor {
private:
Doub a;
public:

// Constructor requires one non-zero parameter
Ftor(Doub m) {
//
// If it is illegal, a civilized program might throw
// an exception and, maybe allow some kind of recovery,
// but to keep it simple, for now, we simply bail
// out of the entire program.
//
if (m == 0) {
cerr << "Can't initialize with a value of zero." << endl;
exit(EXIT_FAILURE);
}
else {
a = m;
}
}
//
// This is the function that is fed to zbrac
//
Doub operator ()(const Doub & x) const
{
if (x == 0) {
return 1;
}
return sin(a*x)/(a*x);
}
};



int main()
{
Doub x1 = 0.5;
Doub x2 = 1.0;
Doub y; // The parameter for the function.
Bool success;
cout << "Enter a number: ";
cin >> y;
if (!cin) {
cout << "Invalid entry." << endl;
exit(EXIT_FAILURE);
}

Ftor f(y); // Initialize the class with the user's value;

cout << "zbrac will try to find a bracketed interval for"
<< " sin(" << y << "*x)/(" << y << "*x)" << endl << endl;

cout << scientific;
cout << "Initially : x1 = " << x1 << ", x2 = " << x2 << endl;
//
//Now call zbrac with the functor
//
success = zbrac(f,x1,x2);

if (success) {
cout << "After zbrac: x1 = " << x1 << ", x2 = " << x2 << endl << endl;
cout << "There may be a root on the interval (x1,x2)" << endl
<< "since f(" << x1 << ") = " << f(x1)
<< " and f(" << x2 << ") = " << f(x2) << endl;
}
else {
cout << "zbrac failed to find a bracketed interval." << endl;
}
return 0;
}


Output for a run

Enter a number: 2.5
zbrac will try to find a bracketed interval for sin(2.5*x)/(2.5*x)

Initially : x1 = 5.000000e-01, x2 = 1.000000e+00
After zbrac: x1 = 5.000000e-01, x2 = 1.800000e+00

There may be a root on the interval (x1,x2)
since f(5.000000e-01) = 7.591877e-01 and f(1.800000e+00) = -2.172289e-01


Regards,

Dave

kutta
02-01-2009, 01:56 AM
Hello Comrade

Indeed your solution is equivalent to MegaType,inlieuof its simplicty,comprehensive.....
I have contextually been able to grasp the essence.Please note that the unversality version for the whole NR routines,which I ref,is a tough job as for the algorithmic /routines usage but not so for initialisation is concerned .
The initialisation method for the whole lot of stufff at NR can definitely be done atleast with the minimum clues or hints whereever required can be generallised by way of comments etc in a more programming way (Though this is already avaiable in SeconEdition Of NR in a vague and non comprehensible manner especially so for the amatuered programmers withleast or little knowledge in Maths).
Atleast the use of relevent Input variable,OutputVariables
and their Pointers etc if given concretely , can really be a big blooming pat for the programmers to motivate without giving room for ambiguos start Please correct me if I am wrong.


With Thanks
As
Kutta(C.R.Muthukumar)