Numerical Linear Algebra

Codes of Basic and imp Methods in NLA //C++
Bisection method:
Secant Method:
False Position Method:
Newtons Method:
Muller method:
BISECTION METHOD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
      float fx1;
      fx1=3*x+sin(x)-exp(x);
      return(fx1);
}
float f(float x);
main()
{
 float x1,x2,x3;
 int counter=0;
 int iter;
 cout<<"enter x1= ";
 cin>>x1;
 cout<<"enter x2= ";
 cin>>x2;
 cout<<"enter no of iter= ";
 cin>>iter;
 //fx1=3*x1+sin(x1)-exp(x1);
 //fx2=3*x2+sin(x2)-exp(x2);    
                   cout<<"iter  "<<"x1  "<<"|x2  "<<"|x3  "<<"|fx1  "<<"|fx2  "<<"|fx3  "<<endl;       
 do{
                   for(int i=0;i<iter;i++)
                  { 
                  if(counter==iter)
                  {
                  break;
                  }
                  x3=(x1+x2)/2;

                  cout<<i<<"  "<<x1<<"  "<<x2<<"  "<<x3<<"  "<<f(x1)<<"  "<<f(x2)<<"  "<<f(x3)<<endl;
                  if(f(x1)*f(x3)<0)
                  {
                  x2=x3;
}
else
{
x1=x3;                  
}
counter++;
}}

while(abs(x1-x2)<0.0001||f(x3)==0);
cout<<"root= "<<x2;
getch();
}

SECANT METHOD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
      return (3*x+sin(x)-exp(x));
          }
float f(float x);
main()
      {
              float E,x0=1,x1=0,x2=0;
              E=0.0000001;
              int counter=0;
              int iter;
              cout<<"tolerence = "<<E<<endl;
              cout<<"enter no of iter= ";
              cin>>iter;
              cout<<endl;
              cout<<"iter    "<<"xn-1    "<<"xn    "<<"xn+1    "<<"|fxn+1|    "<<"|xn+1-xn|"<<endl;
                           if(fabs(f(x0))<fabs(f(x1)))
                   {
                            float temp=x0;
                            x0=x1;
                            x1=temp;
             }
              do
                    {
                            for(int i=0;i<iter;i++)
                            {
                                                 if(counter==iter)
                                                       {
                                                                  break;
             }
            x2=x1-f(x1)*((x0-x1)/(f(x0)-f(x1)));
             x0=x1;
             x1=x2;
             cout<<i<<"     " <<x0<<"     "<<x1<<"     "<<x2<<"    "<<fabs(f(x2))<<"    "<<fabs(x2-x1)<<endl;
             counter++;
             }}
             while(fabs(f(x2))<E);
             cout<<"root = "<<x2;
             getch();
             }          

FALSE POSITION METHOD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
      return (3*x+sin(x)-exp(x));      
      }
float f(float x);
main()
{
      float E,x0=0,x1=1,x2=0;
      int iter, counter=0;
      cout<<"enter tolerence = E= ";
      cin>>E;
      cout<<"enter no of iter = ";
      cin>>iter;
      cout<<"iter "<<"|x0  "<<"|x1  "<<"|x2  "<<"| |fx2| |"<<"| |x2-x1|"<<endl;
      do{
                  for(int i=0;i<iter;i++)
                  {
                          if(counter==iter)
                          {break;}
                  x2=x1-f(x2)*(x0-x1)/(f(x0)-f(x1));
                  if(f(x0)*f(x2)<0)
                  {
                                   x1=x2;
                                   }
                                   else
                                   {
                                       x0=x2;
                                   }
                                   cout<<i<<"  "<<x0<<"  "<<x1<<"  "<<x2<<"   "<<f(x2)<<"  "<<fabs(f(x2))<<"  "<<fabs(x2-x1)<<endl;
                                   counter++;
                  }}
                  while(fabs(f(x2))<E);
                  if(f(x2)==0)
                  {
                              cout<<"root= "<<x2;
                              }
      
      getch();
      }

NEWTONS METHOD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<conio.h>
#include<iostream.h>
#include<math.h>
float f(float x)
{  
      return(3*x+sin(x)-exp(x)); 
    }
    float fd(float x)
    {
          return(3+cos(x)-exp(x));   
    }
//float f(float x);
//float fd(float x);
main()
{   
    float x0=0.0,E=0.0000001,x1=0;
    int counter=0;
    int iter=5;
    if(f(x0)!=0 && fd(x0)!=0)
              {   
                  x1=x0;} 
                  cout<<"iter"<<"x0  "<<"x1"<<"f(x0)"<<"  fd(x0)"<<endl;
                   while(fabs(f(x0))>E)
    {
                   for(int i=0;i<iter;i++)
    {               
                   
                  if(counter==iter)
   {
                  break;
                  }
                  x0=x0-f(x0)/fd(x0);
                  cout<<i<<"  "<<x0<<"  "<<x1<<"  "<<f(x0)<<"  "<<fd(x0)<<"  "<<endl; 
    counter ++;
    }}
    cout<<"root= "<<x1;   
    getch();
    }


Last edited on
Is there a question in here?
i need a code for muller's method in the same simple way...
and question from above is,
the condition is |x2-x1|<tolerance, or |fx2|<tolerance and i did it in this way using do-while loop
but in some codes from net i observed |x2-x1|>tol is used in while loop instead of |x2x1|<tol...and they both give the same result....i don't understand it.
Last edited on
Topic archived. No new replies allowed.