4/19/2016

Gauss-Seidel method ,Bisection method



TOPICS:-
GAUSS SEIDEL METHOD
BISECTION METHOD



The one of the use of the programming is to solve different types of linear or non linear equations.In this section a program solve the linear equation by "Gauss-Seidel method".

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
cout<<"NOTE:-In this programming no limit condition(difference between the value of two consequtive iteration)is used.\n";
cout<<"Only for the set of three equations.\n";
int i,j;
float x,y,z;
float a[10][10];
cout<<"Enter augmented matrix row wise:\n";
for(i=1;i<=3;i++)
{
for(j=1;j<=4;j++)
{
cin>>a[i][j];
}
}
y=0;
z=0;
for(i=1;i<=6;i++)
{
x=(a[1][4]-(a[1][2]*y)-(a[1][3]*z))/a[1][1];
y=(a[2][4]-(a[2][1]*x)-(a[2][3]*z))/a[2][2];
z=(a[3][4]-(a[3][1]*x)-(a[3][2]*y))/a[3][3];
cout<<"Iteration"<<i<<endl;
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
}

cout<<"Solution:\n";
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z;
return 0;
}


Output:
Note:-In this programming no limit condition(difference between the value of two consequtive iteration)is used.
Only for the set of three equations.
Enter augmented matrix row wise:
20 1 -2 17
3 20 -1 -18
2 -3 20 25
Iteration1
0.85
-1.0275
1.01087
Iteration2
1.00246
-0.999826
0.99978
Iteration3
0.999969
-1.00001
1
Iteration4
1
-1
1
Iteration5
1
-1
1
Iteration6
1
-1
1
Solution:
x=1
y=-1
z=1

------------------------------------------
We have to seen that the successive values of x,y and z comes out to be very much near to each other and for last some iteration the values are same.Sometimes we have to take more iteration for finding solution.
-----------------------------------------

BISECTION METHOD

#include<iostream>
#include<math.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip>
#define e 2.71
#define f(y)(y*log10(y)-1.2)
using namespace std;
 
int main()
{
    int i=1;
    double a,b,sol;
    double epsilon;
 
    for(b=1;;b++)
    {
    if(f(b)>0)
        {
    break;
}
     
}
    for(a=b-1;;b--)
    {
        if(f(a)<0)
        {
    break;
}
    }
cout<<"Enter the epsilon value:";
    cin>>epsilon;
    if(f(a)*f(b)>0)
    {
    cout<<"Entrance are not sufficient";
        getch();
}
    do
    {
    sol=(a+b)/2;
        cout<<"After iteration "<<i<<" value of sol: "<<sol<<endl;
        if(f(sol)==0)
        {
        cout<<"Solution="<<sol;
    getch();
    }
    if(f(a)*f(sol)<0)
        {
    b=sol;
}
        else
    {
    a=sol;
}
        i=i+1;
}
 
    while(fabs(a-b)>=epsilon);
    cout<<"\nHence solution after iteration "<<i-1<< ":"<<sol;
   return 0;
 
}


Output:

Enter the epsilon value:0.0001
After iteration 1 value of sol:2.5
After iteration 2 value of sol:2.75
After iteration 3 value of sol:2.625
After iteration 4 value of sol:2.6875
After iteration 5 value of sol:2.71875
After iteration 6 value of sol:2.73438
After iteration 7 value of sol:2.74219
After iteration 8 value of sol:2.73828
After iteration 9 value of sol:2.74023
After iteration 10 value of sol:2.74121
After iteration 11 value of sol:2.74072
After iteration 12 value of sol:2.74048
After iteration 13 value of sol:2.7406
After iteration 14 value of sol:2.74066
Hence solution after iteration 14:2.74066

………………………………………………………

BISECTION METHOD

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#define f(x) (x*x*x-x-1)
using namespace std;
int main()
{
    int i=1;
float a,b,c;
    float fa,fb,fc;
    
for(b=1;;b++)
{
        fb=f(b);
if(fb>0)
{
          
break;
}}
for(a=b-1;;b--)
{
        fa=f(a);
if(fa<0)
{break;
}}

do
{
c=(a+b)/2;
        fc=f(c);
  cout<<"\nAfter iteration "<<i<<"  value of root:"<<c;      
if(fc==0)
{
cout<<"Solution="<<c;
getch();
}

if(fc*fa<0)
{
b=c;
}
else
{
a=c;
  fa=fc;   
}
        i=i+1;
}
while(i<=5);
cout<<"\nAPROX Solution after 5th iteration="<<c;
return 0;

}


Output:

After iteration 1 value of root:1.5
After iteration 2 value of root:1.25
After iteration 3 value of root:1.375
After iteration 4 value of root:1.3125
After iteration 5 value of root:1.34375
APROX solution=1.34375


SEARCH ALL PROGRAMMINGS AND TOPICS



Just write comment in comment box.




1 comment:

  1. A university(aku) examination question is:
    Solve by Gauss-Seidel method:
    20x+y-2z=17
    3x+20y-z=-18
    2x-3y+20z=25

    It's solution is same as the above program.Can you give the solution of this question?

    ReplyDelete