// Gauss-Seidel iterative method #include<stdio.h> #include<conio.h> #include<math.h> #define e 0.01 void main() { int i,j,k,n; float a[10][10],x[10]; float sum,temp,error,big; printf("Gauss-Seidel iterative method\n\n"); printf("Enter the number of equations: "); scanf("%d",&n) ; printf("Enter the coefficients of the equations: \n"); for(i=1;i<=n;i++){ for(j=1;j<=n+1;j++){ printf("a[%d][%d]= ",i,j); scanf("%f",&a[i][j]); } } for(i=1;i<=n;i++){ x[i]=0; } do { big=0; for(i=1;i<=n;i++){ sum=0; for(j=1;j<=n;j++){ if(j!=i){ sum=sum+a[i][j]*x[j]; } } temp=(a[i][n+1]-sum)/a[i][i]; error=fabs(x[i]-temp); if(error>big){ big=error; } x[i]=temp; printf("\nx[%d] =%f",i,x[i]); } printf("\n"); } while(big>=e); printf("\n\nconverges to solution"); for(i=1;i<=n;i++){ printf("\nx[%d]=%f",i,x[i]); } printf("\n"); getch(); }OR
#include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> void main() { float a[20][20],x[20],e,big,temp,relerror,sum; int n,i,j,maxit,itr; printf("Gauss-Seidel iterative methodnn"); printf("Enter the number of equations : "); scanf("%d",&n); for(i=1;i<=n;i++){ printf("Enter the coefficints of equation %d : ",i); for(j=1;j<=n+1;j++) scanf("%f",&a[i][j]); } printf("Enter relative error and maximum number of iterations : "); scanf("%f%d",&e,&maxit); for(i=1;i<=n;i++) x[i]=0; for(itr=1;itr<=maxit;itr++){ big=0; for(i=1;i<=n;i++){ sum=0; for(j=1;j<=n;j++){ if(i!=j ) sum=sum+a[i][j]*x[j]; } temp=(a[i][n+1]-sum)/a[i][i]; relerror=fabs((x[i]-temp)/temp); if(relerror>big) big=relerror; x[i]=temp; } if(big<=e){ printf("nConverges to a solution in %d iterationsn",itr); for(i=1;i<=n;i++) printf("%.4ftn",x[i]); getch(); exit(1); } } printf("ndoes not converge in %d iterations n",maxit); getch();
Monday, 14 December 2015
New
[C/C++] Gauss-Seidel iterative method
About Fajar Nugraha Wahyu
Catatan
Product Tags:
Catatan
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment