Sunday, July 26, 2009

Want working C++ code for gauss jacobi and gauss siediel iterations?

want code for solving a set of 3 linear equations using gauss jacobi and siediel iteration

Want working C++ code for gauss jacobi and gauss siediel iterations?
Free source codes for several numerical methods problems are available at http://www.sourcecodesworld.com/. please visit and download subjected to copyrights of distribution.
Reply:The subscipts are denoted within {}





Algorithm for Jacobi method:





Chose an initial guess φ0 to the solution


for k := 1 step 1 until convergence do


for i := 1 step until n do


σ = 0


for j := 1 step until n do


if j != i then


σ = σ + a{ij} * φ{j} ^ (k-1)


end if


end (j-loop)


σ{i}^k = (bi – σ) / a{ii}


end (i-loop)


check if convergence is reached


end (k-loop)














Algorithm for Gauss-Seidel method





Chose an initial guess φ0


for k := 1 step 1 until convergence do


for i := 1 step until n do


σ = 0


for j := 1 step until i-1 do


σ = σ + a{ij} * φ{j} ^ k


end (j-loop)


for j := i+1 step until n do





σ = σ + a{ij} * φ{j} ^ (k-1)


end (j-loop)


σ{i}^k = (bi – σ) / a{ii}


end (i-loop)


check if convergence is reached


end (k-loop)





The C++ Source code will be:


#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;math.h%26gt;


#include%26lt;iomanip.h%26gt;





float a1[4],a2[4],a3[4]; /* Array declaration */


void show(); /* function declaration


*/


void getdata(float [],float [],float []); /* // // //


*/


void display(float [],float [],float []); /* // // //


*/


int diagonally();


void swap(float [],float []); /* // // //


*/


void jacobi(float [],float [],float []); /* // // //


*/


void gauss(float [],float [],float []);


void answer();


/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...


/


/*....................MAIN FUNCTION OF METHOD............*/


void main() /* main function definition


*/


{


int count=4; /* { main function body}


*/


clrscr();





cout%26lt;%26lt;"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...


^^^^^^^^^^^"%26lt;%26lt;endl;


cout%26lt;%26lt;"..................THIS IS VALID ONLY FOR JACOBI ITTERATIVE


METHOD............."%26lt;%26lt;endl;





cout%26lt;%26lt;"_______________________________...


___________"%26lt;%26lt;endl;


show(); /* function calling */


getdata(a1,a2,a3); /* function calling */


clrscr();


cout%26lt;%26lt;endl%26lt;%26lt;endl;


display(a1,a2,a3); /* function calling */


count=diagonally(); /* function calling */


switch (count)


{


case 0:


{


answer(); /* function calling */


break;


}


case 2:


{display(a1,a2,a3);


answer(); /* function calling */


break;


}


default:


{


cout%26lt;%26lt;"SORRY;YOUR SYSTEM IS NOT DIAGONALLY DOMINENT";


break;


}


}


getch();


}


////////////////////////Function To Check


Diagonality////////////////////////


int diagonally()


{


int f=4,g=4 ,h=4;


int count=0;


float temp[4];


if(fabs(a1[0])%26lt;(fabs(a1[1])+fabs(a1[2]))...


{count++; f=1;}


if(fabs(a2[1])%26lt;(fabs(a2[0])+fabs(a2[2]))...


{count++; g=2;}


if(fabs(a3[2])%26lt;(fabs(a3[0])+fabs(a3[1]))...


{count++; h=3;}


if(f==1%26amp;%26amp;g==2%26amp;%26amp;h==4)


swap(a1,a2); /* function calling */


if(f==1%26amp;%26amp;h==3%26amp;%26amp;g==4)


swap(a1,a3); /* function calling */


if(g==2%26amp;%26amp;h==3%26amp;%26amp;f==4)


swap(a2,a3); /* function calling */





return(count);


}


////////////////////////////Function for jacobi itterative


method/////////////////////////


void jacobi(float a[],float b[],float c[]) /*function definition */


{


float temp[3];


long float j1,j2,j3;


cout%26lt;%26lt;endl%26lt;%26lt;"please enter the initial guess:"%26lt;%26lt;endl;


cout%26lt;%26lt;endl%26lt;%26lt;"X(1) =";


cin%26gt;%26gt;j1;


cout%26lt;%26lt;endl%26lt;%26lt;"X(2) =";


cin%26gt;%26gt;j2;


cout%26lt;%26lt;endl%26lt;%26lt;"X(3) =";


cin%26gt;%26gt;j3;





cout%26lt;%26lt;"-------------------------------...


----";





cout%26lt;%26lt;":::::::::::::::::::::::::::::::...


::::";





cout%26lt;%26lt;"_______________________________...


____";


cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"iterrations #"%26lt;%26lt;" "%26lt;%26lt;" X(1)"%26lt;%26lt;"


X(2)"%26lt;%26lt;" X(3)";


cout%26lt;%26lt;endl%26lt;%26lt;" 0"%26lt;%26lt;setw(17)%26lt;%26lt;j1%26lt;%26lt;setw(15)%26lt;%26lt;j2%26lt;%26lt;setw(14)...


cout%26lt;%26lt;endl;





for(int s=1;s%26lt;=20;s++)


{


temp[0]=j1;temp[1]=j2;temp[2]=j3;


j1=(a[3]-a[1]*temp[1]-a[2]*temp[2])/a[0]...


j2=(b[3]-b[0]*temp[0]-b[2]*temp[2])/b[1]...


j3=(c[3]-c[0]*temp[0]-c[1]*temp[1])/c[2]...


cout%26lt;%26lt;" "%26lt;%26lt;s%26lt;%26lt;setw(17)%26lt;%26lt;j1%26lt;%26lt;setw(15)%26lt;%26lt;j2%26lt;%26lt;setw(1...


if(j1==temp[0]%26amp;%26amp;j2==temp[1]%26amp;%26amp;j3==temp[2]...


break;


}


}


//////////////////////////Function Of


Swaping///////////////////////////////...


void swap(float a[],float b[]) /* function definition */


{


float temp[4];





cout%26lt;%26lt;"-------------------------------...


-------------"%26lt;%26lt;endl;


cout%26lt;%26lt;".....................Your System Is Not Diagonally


Dominent....................."%26lt;%26lt;endl;


cout%26lt;%26lt;"___________________Now It Have To Become Diagonally Dominent


As__________________"%26lt;%26lt;endl;


for(int i=0;i%26lt;4;i++)


{


temp[i]=a[i];


a[i]=b[i];


b[i]=temp[i];


}


}


//////////////////////Function To Show Equations


Form////////////////////////


void show() /* function definition */


{


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"__________________Your Equations Will Be Of The Form Like


This_________________"%26lt;%26lt;endl;


cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;endl;


cout%26lt;%26lt;" a(11)X1 + a(12)X2 + a(13)X3=


b(1)"%26lt;%26lt;endl


%26lt;%26lt;" a(21)X1 + a(22)X2 + a(23)X3= b(2)"%26lt;%26lt;endl


%26lt;%26lt;" a(31)X1 + a(32)X2 + a(33)X3= b(3)"%26lt;%26lt;endl;


}


//////////////////////Function To Get Data From


User/////////////////////////


void getdata(float a[],float b[],float c[]) /* function


definition


*/


{


for(int i=0;i%26lt;3;i++)


{


cout%26lt;%26lt;"a(1"%26lt;%26lt;i+1%26lt;%26lt;")=";


cin%26gt;%26gt;a[i];


cout%26lt;%26lt;endl;


}


cout%26lt;%26lt;"b(1) =";


cin%26gt;%26gt;a[3];


cout%26lt;%26lt;endl;


for(int j=0;j%26lt;3;j++)


{


cout%26lt;%26lt;"a(2"%26lt;%26lt;j+1%26lt;%26lt;")=";


cin%26gt;%26gt;b[j];


cout%26lt;%26lt;endl;


}


cout%26lt;%26lt;"b(2) =";


cin%26gt;%26gt;b[3];


cout%26lt;%26lt;endl;


for(int k=0;k%26lt;3;k++)


{


cout%26lt;%26lt;"a(3"%26lt;%26lt;k+1%26lt;%26lt;")=";


cin%26gt;%26gt;c[k];


cout%26lt;%26lt;endl;


}


cout%26lt;%26lt;"b(3) =";


cin%26gt;%26gt;c[3];


cout%26lt;%26lt;endl;


}


///////////////////////////Function To display


Equations/////////////////////


void display(float a[],float b[],float c[]) /* function


definition


*/


{





cout%26lt;%26lt;"-------------------------------...


-------";


cout%26lt;%26lt;" ::::::::::::::::Your Given System Is Like


This:::::::::::::::: "%26lt;%26lt;endl%26lt;%26lt;endl;


cout%26lt;%26lt;" "%26lt;%26lt;a[0]%26lt;%26lt;"X(1) + "%26lt;%26lt;a[1]%26lt;%26lt;"X(2) +


"%26lt;%26lt;a[2]%26lt;%26lt;"X(3) = "%26lt;%26lt;a[3]%26lt;%26lt;endl


%26lt;%26lt;" "%26lt;%26lt;b[0]%26lt;%26lt;"X(1) + "%26lt;%26lt;b[1]%26lt;%26lt;"X(2) + "%26lt;%26lt;b[2]%26lt;%26lt;"X(3) =


"%26lt;%26lt;b[3]%26lt;%26lt;endl


%26lt;%26lt;" "%26lt;%26lt;c[0]%26lt;%26lt;"X(1) + "%26lt;%26lt;c[1]%26lt;%26lt;"X(2) + "%26lt;%26lt;c[2]%26lt;%26lt;"X(3) =


"%26lt;%26lt;c[3];


cout%26lt;%26lt;endl%26lt;%26lt;endl;


}


///////////////////////////Function of Gauss Seidal


Method////////////////////


void gauss(float a[],float b[],float c[]) /*function


definition


*/


{


float temp[3];


long float j1,j2,j3;


cout%26lt;%26lt;endl%26lt;%26lt;"please enter the initial guess:"%26lt;%26lt;endl;


cout%26lt;%26lt;endl%26lt;%26lt;"X(1) =";


cin%26gt;%26gt;j1;


cout%26lt;%26lt;endl%26lt;%26lt;"X(2) =";


cin%26gt;%26gt;j2;


cout%26lt;%26lt;endl%26lt;%26lt;"X(3) =";


cin%26gt;%26gt;j3;





cout%26lt;%26lt;"-------------------------------...


----";





cout%26lt;%26lt;":::::::::::::::::::::::::::::::...


::::";





cout%26lt;%26lt;"_______________________________...


____";


cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"iterrations #"%26lt;%26lt;" "%26lt;%26lt;" X(1)"%26lt;%26lt;"


X(2)"%26lt;%26lt;" X(3)";


cout%26lt;%26lt;endl%26lt;%26lt;" 0"%26lt;%26lt;setw(17)%26lt;%26lt;j1%26lt;%26lt;setw(15)%26lt;%26lt;j2%26lt;%26lt;setw(14)...


cout%26lt;%26lt;endl;





for(int s=1;s%26lt;=20;s++)


{


temp[0]=j1;temp[1]=j2;temp[2]=j3;


j1=(a[3]-a[1]*j2-a[2]*j3)/a[0];


j2=(b[3]-b[0]*j1-b[2]*j3)/b[1];


j3=(c[3]-c[0]*j1-c[1]*j2)/c[2];


cout%26lt;%26lt;" "%26lt;%26lt;s%26lt;%26lt;setw(17)%26lt;%26lt;j1%26lt;%26lt;setw(15)%26lt;%26lt;j2%26lt;%26lt;setw(1...


if(j1==temp[0]%26amp;%26amp;j2==temp[1]%26amp;%26amp;j3==temp[2]...


break;


}


}


///////////////// FUNCTION OF TAKING GAUSS OR JACOBI


//////////////////


void answer()


{


char option;


do


{


cout%26lt;%26lt;"PRESS [G] FOR GAUSS SEIDAL METHOD: "%26lt;%26lt;endl


%26lt;%26lt;"AND"%26lt;%26lt;endl%26lt;%26lt;"PRESS [J] FOR JACOBI ITTERATIVE METHOD:"%26lt;%26lt;endl;


option=getche();


switch (option)


{


case 'j':


{


cout%26lt;%26lt;setw(25)%26lt;%26lt;"----------BY JACOBI ITTERATIVE


METHOD----------"%26lt;%26lt;endl;


jacobi(a1,a2,a3); /* function calling */


break;


}


case 'g':


{


cout%26lt;%26lt;setw(25)%26lt;%26lt;"----------BY GAUSS SEIDAL


METHOD----------"%26lt;%26lt;endl;


gauss(a1,a2,a3); /* function calling


*/


break;


}


default:


{


cout%26lt;%26lt;"-------------YOUR OPTION IS NOT CORRECT TRY


AGAIN-------------"%26lt;%26lt;endl;


break;


}


}


}while(getche()!='


');


}


No comments:

Post a Comment