1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include <stdio.h>
#include <unsupported/Eigen/MatrixFunctions>
#include <iostream>
#include <math.h>
#include <conio.h>//define getch
#include <iomanip>
#include <fstreaM>
#include <string>
#include <map>
#include <random>
#include <chrono>
#include <vector>
//#define KMAX 2
//#define M ((KMAX+1)*(KMAX+2))/2
#define m 10
using namespace std;
using namespace Eigen;
void main()
{
int k = 1, L = 0, A = 0, i, j, e,max,M;
int z = 1, R = 0, C = 0; //variable for Q
int B = 0, T = 0, c = 1, q = 1, n = 0, H = 0,o = 0,u = 0,t=24,F; // variable for diagonal
//double P[KMAX];
//double Q[KMAX];
//double S[KMAX];
//double E[M][M];
//double Matrix[M + 1][M + 1];
//double I[M + 1][M + 1];
int a[m];
//double MX[M][M];
double p = 0.95;
double a1 = 20.79, a2 = 0.005;
int Vmax = 3, KM = 5;
cout.setf(ios::fixed);
cout.precision(3);
//////////////////////////////////////////////////////////////////////////////////////////
//value of X-axis
/////////////////////////////////////////////////////////////////////////////////////////
double X_axis[m];
for (i = 1; i <= m; i++)
{
X_axis[i] = ((i - 1) / 4.0);
cout << " " << X_axis[i] << endl;
}
///////////////////////////////////////////////////////////////////////////////////////
//random number follow poisson distribution
//////////////////////////////////////////////////////////////////////////////////////
cout << "some Poisson-distributed results:" << endl;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::poisson_distribution<int> distribution(2*X_axis[m]);
int KMAX;
for (int i = 0; i < m; ++i)
{
a[i] = distribution(generator);
std::cout << a[i] << " " << endl;
}
////////////////////////////////////////////////////////////////////////////////////
//maximum number of DSB
///////////////////////////////////////////////////////////////////////////////////
max=a[1];
for (int i = 0; i < m; ++i)
{
if (max <= a[i])
max = a[i];
KMAX = max;
}
cout << "maximum number is " << KMAX << endl;
unsigned short int size = KMAX;
std::vector<int> vec(size);
std::cout << "Size of vector is: " << vec.size() << '\n';
for (int i = 0; i < vec.size(); i++)
{
double P = (p*Vmax*i) / (KM + i);
double Q = (1 - p)*Vmax*i / (KM + i);
cout << "P[" << i << "] = " << P << "\t" << "Q[" << i << "] = " << Q << endl;
}
/////////////////////////////////////////////////////////////////////////////////////////
//matrix
////////////////////////////////////////////////////////////////////////////////////////
int M=((KMAX + 1)*(KMAX + 2)) / 2;
for (i = 0; i <= M; i++)//matrix 0
for (j = 0; j <= M; j++);
MX[M][M]=0;
for (i = KMAX; i >= 1; i--)// position P in matrix
{
for (j = 1; j <= i; j++)
{
MX[j - 1 + L][KMAX + k] = P[A + 1];
//cout <<"MX["<<j-1+L<<"]["<<KMAX+k<<"] = " <<MX[j-1+L][KMAX+k]<<endl;
k++;
}
//cout<<endl;
L = k + A;
A++;
}
for (i = KMAX; i >= 1; i--)// position Q in matrix
{
for (j = 1; j <= i; j++)
{
MX[j + R][KMAX + z] = Q[C + 1];
//cout <<"MX["<<j+R<<"]["<<KMAX+z<<"] = " <<MX[j+R][KMAX+z]<<endl;
z++;
}
//cout<<endl;
R = z + C;
C++;
}
for (i = KMAX + 1; i >= 1; i--)// calculation for diagonal
{
for (j = 1; j <= i; j++)
{
MX[B + c - q][B + c - q] = -(a1*double(j - 1) + a2*double(B)*double(B)) - ((Vmax*double(B)) / (KM + double(B)));
//cout <<"MX["<<B+c-q<<"]["<<B+c-q<<"] = "<< MX[B+c-q][B+c-q] << endl;
c++;
}
q++;
B++;
}
cout << "Here is the matrix:" << endl; //double array matrix
for (i = 0; i < M; i++)
{
for (j = 0; j < M; j++)
{
// cout << setw(7) << MX[i][j] << " ";
}
//cout << endl;
}
////////////////////////////////////////////////////////////////////////////////
//solve matrix exponential
///////////////////////////////////////////////////////////////////////////////
MatrixXd result(M, M); //apply eigen matrix
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
result(i,j)=MX[i][j]*t;
cout << " " << result << endl;
EigenSolver<MatrixXd> es(result);
MatrixXd D = es.pseudoEigenvalueMatrix();
MatrixXd V = es.pseudoEigenvectors();
cout << "The pseudo-eigenvalue matrix D is:" << endl << D << endl;
cout << "The pseudo-eigenvector matrix V is:" << endl << V << endl;
cout << "Finally, V * D * V^(-1) = " << endl << V * D * V.inverse() << endl;*/
_getch();
}
| |