Files Classes Functions Hierarchy
00001 #include <cassert> 00002 #include <cmath> 00003 #include <iostream> 00004 using namespace std; 00005 00006 00007 #include <fixedpoint.h> 00008 #include <fixedpointtest.h> 00009 00010 // <TODO> find where numberical constant lives. 00011 #define PI 3.14159265 00012 00013 class f2 00014 { 00015 public: 00016 00017 // f is the system of equations as a vector function = 0. 00018 void f(double fx[2], double const x[2]) const; 00019 00020 // Inverse Derivative of vector equ's f or inverse grad f. 00021 void IDf(double idf[2][2], double const x[2]) const; 00022 00023 }; 00024 00025 void f2::f(double fx[2], double const x[2]) const 00026 { 00027 fx[0] = 3.0*x[0]+2.0*x[1]-7.0; 00028 fx[1] = -5.0*x[0]+4*x[1]-3.0; 00029 } 00030 00031 00032 void f2::IDf(double idf[2][2], double const x[2]) const 00033 { 00034 double det = 22.0; 00035 00036 idf[0][0] = 4.0/det; 00037 idf[0][1] = -2.0/det; 00038 idf[1][0] = 5.0/det; 00039 idf[1][1] = 3.0/det; 00040 } 00041 00042 00043 00044 void fixedpointtest::test02() 00045 { 00046 00047 fixedpoint< f2, 2 > p; 00048 00049 double x0[2]; 00050 double x[2]; 00051 00052 x0[0] = 20.0; 00053 x0[1] = -20.0; 00054 00055 p.inc(x,x0); 00056 00057 cout << x0[0] << " " << x0[1] << endl; 00058 cout << x[0] << " " << x[1] << endl; 00059 00060 x0[0] = x[0]; x0[1]=x[1]; 00061 p.inc(x,x0); 00062 cout << x[0] << " " << x[1] << endl; 00063 00064 } 00065 00066 00067 00068 /* 00069 * Two arms of length a and b. 00070 * At angles theta and phi respectively. 00071 * 00072 * 00073 * The arms are connected like a human arm where 00074 * the origin is the schoulder and (px,py) is a 00075 * point above shoulder height. 00076 * 00077 * x = cos(theta) and y = sin(phi) were substited into 00078 * px = a*cos(theta) + b*cos(phi) 00079 * py = a*sin(theta) + b*sin(phi) 00080 * the algebra simplified to remove the square root 00081 * and this non-linear equation solved with the fixed 00082 * point method. 00083 * Finally converting back to an angle. 00084 */ 00085 00086 class arm 00087 { 00088 public: 00089 00090 // f is the system of equations as a vector function = 0. 00091 void f(double fx[2], double const x[2]) const; 00092 00093 void IDf(double idf[2][2], double const x[2]) const; 00094 00095 double a; 00096 double b; 00097 00098 double px; 00099 double py; 00100 00101 void print( double const x[2] ) const; 00102 00103 }; 00104 00105 00106 void arm::print( double const x[2] ) const 00107 { 00108 double theta = acos(x[0]); 00109 double phi = asin(x[1]); 00110 00111 cout << "theta=" << theta*180/PI << endl; 00112 cout << "phi=" << phi*180/PI << endl; 00113 00114 cout << endl; 00115 cout << "a*cos(theta)=" << a*cos(theta) << endl; 00116 cout << "a*sin(theta)=" << a*sin(theta) << endl; 00117 00118 cout << "b*cos(phi)=" << b*cos(phi) << endl; 00119 cout << "b*sin(phi)=" << b*sin(phi) << endl; 00120 00121 cout << "|a*cos(theta)|+|b*cos(phi)|=" << 00122 a*abs(cos(theta)) + b*abs(cos(phi)) << endl; 00123 cout << "|a*sin(theta)|+|b*sin(phi)|=" << 00124 a*abs(sin(theta)) + b*abs(sin(phi)) << endl; 00125 00126 cout << endl << endl; 00127 cout << "a*cos(theta)+b*cos(phi)=" << 00128 a*cos(theta) + b*cos(phi) << endl; 00129 cout << "|a*sin(theta)|+b*sin(phi)=" << 00130 a*sin(theta) + b*sin(phi) << endl; 00131 } 00132 00133 void arm::f(double fx[2], double const x[2]) const 00134 { 00135 double t; 00136 t = (px-a*x[0]); 00137 fx[0] = t*t + b*b*(x[1]*x[1]-1.0); 00138 t = (py-b*x[1]); 00139 fx[1] = t*t + a*a*(x[0]*x[0]-1.0); 00140 } 00141 00142 void arm::IDf(double idf[2][2], double const x[2]) const 00143 { 00144 double det = 4.0*a*b*(px*py-px*b*x[1]-py*a*x[0]); 00145 double z = -2.0 / det; 00146 00147 idf[0][0] = z*b*(py-b*x[1]); 00148 idf[0][1] = z*b*b*x[1]; 00149 idf[1][0] = z*a*a*x[0]; 00150 idf[1][1] = z*a*(px-a*x[0]); 00151 } 00152 00153 void fixedpointtest::test03() 00154 { 00155 fixedpoint< arm, 2 > p; 00156 00157 arm & f(p.f); 00158 00159 f.a = 3.0; 00160 f.b = 2.0; 00161 f.px = 4.0; 00162 f.py = 1.0; 00163 00164 double x0[2]; 00165 double x[2]; 00166 00167 x0[0] = 1.0; 00168 x0[1] = 0.0; 00169 00170 00171 unsigned int imax = 8; 00172 for (unsigned int i=0; i<imax; ++i) 00173 { 00174 00175 x0[0] = x[0]; x0[1]=x[1]; 00176 p.inc(x,x0); 00177 cout << x[0] << " " << x[1] << endl; 00178 } 00179 00180 f.print(x); 00181 }
1.5.8