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
|
/*******************LINEAR CHAIN**********************
| this program is developed to calculate the eigenvalues and |
| eigenvectors of a linear chain of n-masses and (n+1)springs |
| where n is even. Using Jacobian's method for sym. matrix |
| The chain is limited up to diatomic chain |
******************************************************/
#include <iostream>
using namespace std;
#include <math.h>
//stdlib for memory allocation/ dynamic memory
#include <stdlib.h>
void matrix_alloc( double **array, int nrows, int ncolumns);
void matrix_init( double **array, int n);
void matrix_init_identity( double **array, int n);
void update( int k, double t, double& y, double e[], bool changed[], int& state);
/*void update(index k, parameter t, parameter y, eigenvector e[], changed[], state*/
void rotate(int k, int l, int i, int j, double **matrix, double c, double s);
int maxind (int k, double **array, int n);
int main(){
int n=10; // n need to be even to create continous boundary
double **E, **S;
//dynamic memory allocation to E[][] and S[][]
matrix_alloc(E, n, n);
matrix_alloc(S, n, n);
//varible declaration
double e[n];
int i, k, l, m, state;
double s, c, t, p, y;
int ind[n];
bool changed[n];
//manually set the spring constant nad mass
double spring[2] = {10, 10};
double mass [2] = {1, 1};
//Assign components to matrix S[][] only works for even nxn matrix
for ( i = 0; i < (n-2); i = i+2){ //set for upper, lower diagonal and diagonal
S[i][i+1] = -spring[1]/mass[0];
S[i+1][i] = -spring[1]/mass[1];
S[i][i] = (spring[0] + spring[1]) / mass[0];
};
for ( i = 1; i < (n-3); i = i+2){
S[i][i+1] = -spring[0]/mass[1];
S[i+1][i] = -spring[0] / mass[0];
S[i][i] = (spring[0] + spring[1]) / mass[1];
};
S[n-1][n-1] = (spring[0] + spring[1]) / mass[1];
S[0][n-1] = -spring[0] / mass[0];
S[n-1][0] = -spring[0] / mass[1];
//init E[][] and arrays ind[], e[], changed[]
matrix_init_identity( E, n);
state = n;
for (k=0; k<n; k++){
ind[k] = maxind( k, S, n);
e[k] = S[k][k];
changed[k] = true;
};
//rotation around biggest index - pivot p(k,l)- by Givens' matrix
while ( state != 0) {
m = 0;
//find pivot p( k, l)
for ( k=1; k < (n-1); k++){
if ( fabs( S[k][ind[k]]) > fabs(S[m][ind[m]]))
m = k;
};
//prepare Givens' matrix's components
k = m;
l = ind[m];
p = S[k][l];
y = (e[l] - e[k])/2; t = fabs(y) + sqrt( p*p + y*y );
s = sqrt( p*p + t*t);
c = t/s;
s = p/s;
t = p*p/t;
if (y<0){
s = -s;
t = -t;
};
S[k][l] = 0;
update( k, -t, y, e, changed, state);
update( l, t, y, e, changed, state);
//rotate rows and columns k, l
for ( i = 0; i < k; i++) rotate(i, k, i, l, S, c, s);
for ( i = k + 1; i < l; i++) rotate( k, i, i, l, S, c, s);
for ( i = l + 1; i < n; i++) rotate( k, i, l, i, S, c, s);
//rotate eigenvectors
for ( i = 0; i < n; i++) rotate ( k, i, l, i, E, c, s);
// update rows k, l by changing ind[k], ind[l]
ind[k] = maxind( k, S, n);
ind[l] = maxind(l, S, n);
};
};
//***************Function*******************
void matrix_alloc (double **array, int nrows, int ncolumns){
if (array != 0){
cout << "error allocating memory"<<endl;
return;
};
array = (double**)malloc(nrows * sizeof(int *));
if(array == NULL){
cout << "error allocating memory"<<endl;
return;
};
for(int i = 0; i < nrows; i++){
array[i] = (double*)malloc(ncolumns * sizeof(int));
if ( array[i] == NULL){
cout << "error allocating memory"<<endl;
return;
};
};
};
void matrix_init( double **array, int n){
for (int i=0; i < n; i++){
for (int j=0; j < n; j++) {
array[i][j] = 0;
};
};
};
void matrix_init_identity (double **array, int n){
for (int i=0; i < n; i++){
for (int j=0; j < n; j++) {
if (i == j){
array[i][j] = 1;
}else{
array[i][j] = 0;
};
};
};
};
void update( int k, double t, double& y, double e[], bool changed[], int& state){
y = e[k];
e[k] = y + t;
if (changed[k] && (y = e[k])){
changed[k] = false;
state = state - 1;
}else{
if ((!changed[k]) && (y != e[k])){
changed[k] = true;
state = state + 1;
};
};
};
void rotate(int k, int l, int i, int j, double **matrix, double c, double s){
double a, b;
a = c*matrix[k][l] - s*matrix[i][j];
b = s*matrix[k][l] + c*matrix[i][j];
matrix[k][l] = a;
matrix[i][j] = b;
};
int maxind (int k, double **array, int n){
/*find maximum component, off diagonal in (row k) of matrix (array) size (n)*/
int m = k+1;
for (int i = k+2; i < n; i++){
if ( fabs(array[k][i]) > fabs( array[k][m]))
m = i;
};
};
|