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
|
#include "mbed.h"
#include "math.h"
Serial pc(USBTX, USBRX);
void swapRows(double m[][8], int r1, int r2)
{
double temp;
for(int c=0; c<8; ++c)
{
temp = m[r1][c];
m[r1][c] = m[r2][c];
m[r2][c] = temp;
}
return;
}
int main()
{
double a[8][8]={2.0000,-2.0000,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0000,0.0,0.0,0.0,0.0,0.0,1.1111,0.0,0.0,-1.1111,0.0,0.0,0.0,0.0,0.0,1.3333,0.0,0.0,-1.3333,0.0,0.0,0.0,0.0,0.0,1.1111,-1.1111,0.0,0.0,0.0,0.0,0.0,0.0,-1.2500,0.0,0.0,2.6786,-1.4286,0.0,0.0,0.0,0.0,-1.3333,0.0,-1.4286,4.4286,-1.6667,0.0,0.0,0.0,0.0,-1.0000,0.0,-1.6667,2.6667};
double b[8][8]={1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0};
float mult[8][8];
float c[8][8]={2.0000,-2.0000,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0000,0.0,0.0,0.0,0.0,0.0,1.1111,0.0,0.0,-1.1111,0.0,0.0,0.0,0.0,0.0,1.3333,0.0,0.0,-1.3333,0.0,0.0,0.0,0.0,0.0,1.1111,-1.1111,0.0,0.0,0.0,0.0,0.0,0.0,-1.2500,0.0,0.0,2.6786,-1.4286,0.0,0.0,0.0,0.0,-1.3333,0.0,-1.4286,4.4286,-1.6667,0.0,0.0,0.0,0.0,-1.0000,0.0,-1.6667,2.6667};
int i,j,k,l;
float t;
pc.printf("Input matrix\n\r");
for (i=0;i<8;i++){
pc.printf("\n\r");
for(j=0;j<8;j++){
pc.printf("%f\t",a[i][j]);
}
}
for (j = 0; j<8; j++){
for (i=j; i<8; i++){
if( a[j][j] == 0.0 ){
for(k=j+1; k<8; ++k){
if( a[k][j] != 0.0 ){
swapRows(a, j, k);
swapRows(b, j, k);
break;
}
}
}
if (a[i][j] != 0.0){
t = 1.0/a[j][j];
for (k = 0; k<8; k++){
a[j][k] *= t;
b[j][k] *= t;
}
for (l = 0; l<8; l++){
if (l != j){
t = -a[l][j];
for (k = 0; k<8; k++){
a[l][k] = a[l][k] + t*a[j][k];
b[l][k] = b[l][k] + t*b[j][k];
}
}
}
}
}
}
pc.printf("\r\n\r\nInverse matrix\n\r");
for(l=0;l<8;l++){
pc.printf("\r\n");
for(k=0;k<8;k++){
pc.printf("%f\t", b[l][k]);
}
}
//Matrix multiplication
pc.printf("\r\n\r\nMatrix x Inverse to Create Identity Matrix\n\r\n");
for(i=0;i<8;i++){
for(j=0;j<8;j++){
mult[i][j]=0;
for(k=0;k<8;k++){
mult[i][j]+=b[i][k]*c[k][j];
}
pc.printf("%f\t",mult[i][j]);
}
printf("\r\n");
}
}
|