Computer Science‎ > ‎

C Program: Numerical Computing - the Jacobi Method


C program / source code - Implementing the Jacobi method (Numerical Computing)


/*This program is an implementaion of the Jacobi iteration method.
The relation used is
X(k+1)=(Diagonal matrix Inverse)(RHS Coefficient-)*(Lower triangle+Upper triangle)X(k))
*/
#include<stdio.h>
float coeff[10][10];
float Dinv[10][10];
float approx[10][1];
float R[10][10];//declare the relevant matrices
float matrixRes[10][1];
float b[10][1];
float temp[10][1];
int row,column,size,navigate;
void multiply(float matrixA[][10],float matrixB[][1])
{
    int ctr,ictr;
//function to perform multiplication
for(ctr=0;ctr<size;ctr++)
{
    matrixRes[ctr][0]=0;
        for(navigate=0;navigate<size;navigate++)
            matrixRes[ctr][0]=matrixRes[ctr][0]+matrixA[ctr][navigate]*matrixB[navigate][0];
}
}
int main()
{
printf("Enter the number of unknown(below 10)\n");
scanf("%d",&size);//enter the size
printf("Enter the coefficent matrix\n");
for(row=0;row<size;row++)
    for(column=0;column<size;column++)
        scanf("%f",&coeff[row][column]);
printf("Enter the first approximation\n");
for(row=0;row<size;row++)
scanf("%f",&approx[row][0]);
printf("Enter the RHS coefficient\n");
for(row=0;row<size;row++)
scanf("%f",&b[row][0]);
for(row=0;row<size;row++)//We calculate the diagonal inverse matrix make all other entries as zero except Diagonal entries whose resciprocal we store
    for(column=0;column<size;column++)
    {    if(row==column)
        Dinv[row][column]=1/coeff[row][column];
        else
        Dinv[row][column]=0;
    }
for(row=0;row<size;row++)
    for(column=0;column<size;column++)//calculating the R matrix L+U
    {    if(row==column)
        R[row][column]=0;
        else
        if(row!=column)
        R[row][column]=coeff[row][column];
    }
int iter;
printf("Enter the number of iterations:\n");
scanf("%d",&iter);//enter the number of iterations
int ctr=1;
int octr;
while(ctr<=iter)
{
multiply(R,approx);//multiply L+U and the approximation
for(row=0;row<size;row++)
temp[row][0]=b[row][0]-matrixRes[row][0];//the matrix(b-Rx)
multiply(Dinv,temp);//multiply D inverse and (b-Rx)
for(octr=0;octr<size;octr++)
approx[octr][0]=matrixRes[octr][0];//store matrixRes value in the next approximation
printf("The Value after iteration %d is\n",ctr);
for(row=0;row<size;row++)
printf("%.3f\n",approx[row][0]);//display the value after the pass
ctr++;
}
getch();
}
/*A sample run of the program works as:-
Enter the number of unknown(below 10)
3
Enter the coefficent matrix
5 -2 3
-3 9 1
2 -1 -7
Enter the first approximation
0
0
0
Enter the RHS coefficient
-1
2
3
Enter the number of iterations:
7
The Value after iteration 1 is
-0.200
0.222  
0.429  
The Value after iteration 2 is
0.146
0.203
-0.517
The Value after iteration 3 is
0.192
0.328
-0.416
The Value after iteration 4 is
0.181
0.332
-0.421
The Value after iteration 5 is
0.185
0.329
-0.424
The Value after iteration 6 is
0.186
0.331
-0.423
The Value after iteration 7 is
0.186
0.331
-0.423*/


*/



C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers