Numerical Methods: Condition number and ill condition checking using C

Source Code:

#include<stdio.h>
#include<math.h>
void inverse(float matrix[6][6], int n){
    int i,j, k;
    float ratio,a;
    for(i = 0; i < n; i++){
        for(j = n; j < 2*n; j++){
            if(i==(j-n))
                matrix[i][j] = 1.0;
            else
                matrix[i][j] = 0.0;
        }
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(i!=j){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < 2*n; k++){
                    matrix[j][k] -= ratio * matrix[i][k];
                }
            }
        }
    }
    for(i = 0; i < n; i++){
        a = matrix[i][i];
        for(j = 0; j < 2*n; j++){
            matrix[i][j] /= a;
        }
    }
    for(i = 0; i < n; i++){
        for(j = n; j < 2*n; j++){
            matrix[i][j-n] = matrix[i][j];
        }
    }
}
float norms(float matrix[][6],int n){
    int i,j;
    float sum = 0;
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            sum += pow(matrix[i][j], 2);
        }
    }
    return sqrt(sum);
}
int main(){
    float matrix[6][6], cond_no, norm1, norm2, det;
    int i,j,n;
    printf("Enter order of matrix: ");
    scanf("%d",&n);
    printf("Enter the matrix: \n");
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            scanf("%f",&matrix[i][j]);
        }
    }
    norm1 = norms(matrix,n);
    inverse(matrix,n);
    norm2 = norms(matrix,n);
    cond_no = norm1*norm2;
    printf("\nCondition No: %.2f\n", cond_no);
    return 0;
}
SHARE Numerical Methods: Condition number and ill condition checking using C

You may also like...

Leave a Reply

Your email address will not be published.

Share