Skip to main content

C Program for Gauss-Seidel Method

C Program for Gauss-Seidel Method
// Given system is:
// 20x+y-2z=17
// 3x+20y-z=-18
// 2x-3y+20z=25

#include <stdio.h>
#include <conio.h>
#include <math.h>

float f(float x,float y,float z)
{
  return ((17-y+2*z)/20);
// x=(17-y+2*z)/20
}

float s(float x,float y,float z)
{
  return ((-18+z-3*x)/20);
// y=(-18+z-3*x)/20
}

float t(float x,float y,float z)
{
  return ((25-2*x+3*y)/20);
// z=(25-2*x+3*y)/20
}

void main()
{
  float x0,y0,z0,x1=0,y1=0,z1=0,tempx,tempy,tempz,acc=0.0001;
  int iteration=0;
  printf("Enter initial guesses:\n");
  scanf("%f%f%f",&x0,&y0,&z0);
  do
  {
    tempx=x1;
    tempy=y1;
    tempz=z1;

    x1=f(x0,y0,z0);
    y1=s(x1,y0,z0);
    z1=t(x1,y1,z0);

    iteration++;

    x0=x1;
    y0=y1;
    z0=z1;
  }while(fabs(tempx-x1)>acc && fabs(tempy-y1)>acc && fabs(tempz-z1)>acc);

  printf("\n\nFinally,\n");
  printf("x=%f Ans\ny=%f Ans\nz=%f Ans\n",x1,y1,z1);
  printf("Iteration=%d",iteration);
  getch();
}

Comments

Popular posts from this blog

BSc CSIT VS Computer Engineering

BSc CSIT VS Computer Engineer After passing plus two many student feel hard to choose either CSIT or Engineering. In this article I will try to show what is the main difference between these two subjects. If you don't want to read out all this article then in one line let me make you clear about their difference, CSIT deals with theory, principles, algorithms and software side of computer field where as Engineering deals with hardware and design aspect of same field. Lets talk in detail. What is Computer Science and Information Technology (CSIT)? It is a blend of Computer Science and Information Technolog. It is the systematic study of algorithmic methods for representing and transforming information, including their theory, design, implementation, application, and efficiency. The discipline emerged in the 1950s from the development of computability theory and the invention of the stored-program electronic computer. The roots of computer science extend deeply into mathe...