Files Classes Functions Hierarchy
00001 #include <cassert> 00002 #include <vector> 00003 #include <iostream> 00004 using namespace std; 00005 00006 #include <exploretest.h> 00007 00008 #include <print.h> 00009 #include <dumbarray.h> 00010 00011 #include <exploreh.h> 00012 #include <leastsqrs.h> 00013 #include <probparab.h> 00014 #include <probsysequ01.h> 00015 #include <probrosenbrock.h> 00016 00017 00018 void exploretest01() 00019 { 00020 uintc n(3); 00021 exploreh<probparab,double*,double> g(n); 00022 double xi[n]; 00023 g.xiSet((double*)xi); 00024 00025 double x0[3] = { 0.0, 0.0, 0.0 }; 00026 00027 g.indexmax = 50; 00028 for (g.reset(x0); !g; ++g) 00029 cout << print<double*>(g.xi,g.xi+n) << endl; 00030 //cout << printvec<double*>(g.xi,n) << endl; 00031 00032 cout << "The exact solution is (1,2,5)" << endl; 00033 00034 } 00035 00036 void exploretest02() 00037 { 00038 vector<pt2> pts; 00039 00040 uintc psz =7; 00041 double const parray[2*psz] = 00042 { 00043 0.0, 0.0, 00044 1.0, 1.1, 00045 1.5, 2.8, 00046 2.8, 4.2, 00047 4.8, 4.0, 00048 6.8, 4.2, 00049 8.1, 5.5 00050 }; 00051 00052 for (uint i=0; i<psz; ++i) 00053 pts.push_back( pt2(parray[i*2],parray[i*2+1]) ); 00054 00055 leastsqrs fn(pts); 00056 00057 exploreh<leastsqrs &,double*,double> g(fn,psz); 00058 00059 double x0[psz]; 00060 for (uint i=0; i<psz; ++i) 00061 x0[i] = pts[i].y; 00062 00063 //g.h0Set(1.52E-5); 00064 00065 g.indexmax = 50; 00066 00067 for (g.reset(x0); !g; ++g) 00068 { 00069 g.print(cout) << endl; 00070 } 00071 00072 cout << endl << endl; 00073 00074 00075 cout << "The result" << endl; 00076 for (uint i=0; i<psz; ++i) 00077 { 00078 cout << pts[i].x << " " << g.xi[i] << endl; 00079 } 00080 00081 } 00082 00083 void exploretest05() 00084 { 00085 00086 probparab2 fn; 00087 exploreh<probparab2 &,double*,double> g(fn,3); 00088 00089 double x0[3] = { 0.0, 0.0, 0.0 }; 00090 00091 g.indexmax = 50; 00092 for (g.reset(x0); !g; ++g) 00093 cout << g.xi[0] << " " << g.xi[1] << " " << g.xi[2] << endl; 00094 00095 cout << "The exact solution is (1,2,5)" << endl; 00096 00097 } 00098 00099 void exploretest06() 00100 { 00101 exploreh<probparab2,double*,double> g(3); 00102 00103 double x0[3] = { 0.0, 0.0, 0.0 }; 00104 00105 g.indexmax = 50; 00106 for (g.reset(x0); !g; ++g) 00107 cout << g.xi[0] << " " << g.xi[1] << " " << g.xi[2] << endl; 00108 00109 cout << "The exact solution is (1,2,5)" << endl; 00110 } 00111 00112 void exploretest04() 00113 { 00114 cout << "Testing the hasStateChanged variable in exploreh" << endl; 00115 cout << "Looking at d xi" << endl; 00116 exploreh<probparab2,double*,double> g(3); 00117 00118 double x0[3] = { 0.0, 0.0, 0.0 }; 00119 double x1[3]; 00120 00121 g.indexmax = 50; 00122 g.reset(x0); 00123 double dist; 00124 for (g.reset(x0); !g; ++g) 00125 { 00126 if (g.hasStateChanged) 00127 { 00128 dist=0.0; 00129 for (uint k=0; k<3; ++k) 00130 { 00131 x1[k] = g.xi[k]-x0[k]; 00132 dist += x1[k]*x1[k]; 00133 x0[k] = g.xi[k]; 00134 } 00135 cout << SHOW(dist) << endl; 00136 } 00137 } 00138 cout << g.xi[0] << " " << g.xi[1] << " " << g.xi[2] << endl; 00139 cout << SHOW(g.fn.counter) << endl; 00140 cout << "The exact solution is (1,2,5)" << endl; 00141 } 00142 00143 void exploretest07() 00144 { 00145 exploreh<probparab3,dumbarray<double>,double> g(3); 00146 double x0[3] = { 0.0, 0.0, 0.0 }; 00147 00148 g.indexmax = 50; 00149 for (g.reset(x0); !g; ++g) 00150 cout << g.xi[0] << " " << g.xi[1] << " " << g.xi[2] << endl; 00151 00152 cout << "The exact solution is (1,2,5)" << endl; 00153 } 00154 00155 00156 00157 00158 00159 void exploretest03() 00160 { 00161 cout << "This test is to solve a non-linear system of equations" << endl; 00162 cout << " numerically and terminate the approximation when the" << endl; 00163 cout << " parital derivatives become small." << endl; 00164 exploreh<probsysequ01,double*,double> g(2,0.2,50000); 00165 00166 double x0[2] = { 0.5, 0.5 }; 00167 00168 g.reset(x0); 00169 00170 g.print(cout); 00171 00172 00173 double zero = 1e-5; 00174 cout << "Iterate until the partial derivative are less than" << endl; 00175 cout << " " << zero << " in magnitude." << endl; 00176 bool valid=true; 00177 00178 double pd[2]; 00179 00180 for (uint i=0; valid; ++i) 00181 { 00182 g.fn.partialderivative(pd[0],0,g.hi[0]); 00183 g.fn.partialderivative(pd[1],1,g.hi[1]); 00184 if (zero>(abs(pd[0])+abs(pd[1]))) 00185 valid=false; 00186 00187 cout << SHOW(i) << endl; 00188 ++g; 00189 00190 } 00191 00192 g.print(cout); 00193 cout << SHOW(g.fn.counter) << endl; 00194 00195 } 00196 00197 void exploretest08() 00198 { 00199 uintc n(2); 00200 exploreh<probrosenbrock01,double*,double> g(n); 00201 double xi[n]; 00202 g.xiSet((double*)xi); 00203 00204 double x0[n] = { 0.0, 0.0 }; 00205 00206 cout << "Enter initial point" << endl; 00207 cin >> x0[0] >> x0[1]; 00208 00209 g.indexmax = 80; 00210 uint i=1; 00211 uintc ireset=300; 00212 00213 for (g.reset(x0); !g; ++g) 00214 { 00215 if (i%ireset==0) 00216 { 00217 cout << "resetting g" << endl; 00218 g.reset(); 00219 } 00220 ++i; 00221 00222 g.print(cout); 00223 00224 //cout << printvec<double*>(g.xi,n) << endl; 00225 } 00226 00227 cout << SHOW(g.fn.counter) << endl; 00228 cout << SHOW(g.fmin) << endl; 00229 00230 cout << "The exact solution is (1,1)" << endl; 00231 00232 } 00233 00234
1.5.8