These codes are tested in Dev C++ and are error free. If any error occurs, comment below. Other programs will be uploaded as soon as possible.
Thank you! :)

Friday, April 15, 2016

Jacobi Iteration Method

Equations:
2x1 - x2 + x3 = 5
3x1 - 5x2 + 2x3 = 15
2x1 + x2 + 4x3 = 8

Program:

#include<iostream.h>
#include<conio.h>
#define f(x) (5 - x2 - x3)/2
#define g(x) (15 - 3*x1 - 2*x3)/5
#define h(x) (8 - 2*x1 - x2)/4

int main()
{
    float x1, x2, x3, a, b, c;
    int i, n;
    cout << "How many iterations do you want? : " ;
    cin >> n;
    for(i=0; i<n; i++)
    {
          a = f(i);
          b = g(i);
          c = h(i);
          x1 = a;
          x2 = b;
          x3 = c;
          cout << "For i = " << i << endl;
          cout << "x1 = " << x1 << endl << "x2 = " << x2 << endl << "x3 = " << x3 << endl << endl << endl;
       
          }
          cout << "The process can be continued till the values of x reach a desired level of "<<endl << "accuracy. " ;
getch();
return 0;
}



Secant Method

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x - 4*x - 10
#define e 0.001
int main()
{
    float x0, x1, x2, f0, f1;
    double E;
    static int count;
    cout << "Enter x0:" ;
    cin >> x0;
    cout << "Enter x1:" ;
    cin >> x1;
    Top:
    f0 = f(x0);
    f1 = f(x1);
    x2 = x1 - ((f1 * (x1 - x0)) / (f1 - f0));
    E = (fabs((x2 - x1) / x2) * 100);
    if(E < e)
    {
         cout << "The root is " << x2 << endl;
         cout << "Number of iterations = " << count << endl;
         }
         else{
              count++;
              x0 = x1;
              x1 = x2;
              goto Top;
           
              }
              getch();
              return 0;
              }






Least Square Regression [Program]

This is coded in Dev C++ . If you find any error, please comment below. Thank you!
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
    int n, i, xi[20], yi[20];
    double sum1 = 0, sumpowx = 0, sum2 = 0, msum = 0, z, y;
    double b, a;
 
    cout << "Enter how many values you want? : " ;
    cin >> n;
    for(i=1;i<= n;i++)
    {
                     cout << "Enter value of xi's : " ;
                     cin >> xi[i];
                     sum1 = sum1 + xi[i];
                     z = pow(xi[i],2);
                     sumpowx = sumpowx + z;
                     }
    for(i=1; i<=n;i++)
    {
                     cout << "Enter value of yi's: ";
                     cin >> yi[i];
                     sum2 = sum2 + yi[i];
                     }
    for(i=1;i<=n;i++)
    {
                     msum = (xi[i] * yi[i]) + msum;
                     }
 
    cout << endl << endl <<"Summation xi = " << sum1 << endl << "Summation yi = " << sum2 << endl;
    cout << "Summation Square of xi = " << sumpowx << endl << "Summation xi * yi = " << msum << endl;
 
    b = (n * msum - (sum1 * sum2))/ (n * sumpowx) - ((sum1) * ( sum1));
    a = (sum2 / n)- ((n * msum - (sum1 * sum2))/ (n * sumpowx) - ((sum1) * ( sum1)))*(sum1 /n);
 
    cout << "The required line is Y = "<<a <<" + " <<b <<" x";
                     getch();
                     return 0;
                     }
                   



Tuesday, April 12, 2016

Newton-Raphson Method

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x - 4*x  - 10)
#define g(x) (2*x - 4)
#define e 0.05
int main()
{
    float x0, x1, f1, f2, E;
    static int count=0;
    cout << "Enter Xo: " ;
    cin >> x0 ;
    cout << endl << "Estimated error: " << e << endl;
    Top:
    f1 = f(x0);
    f2 = g(x0);
    x1 = x0 - (f1 / f2);
    E = ((fabs((x1 - x0) / x1)) * 100);
    if(E < e){
         cout << endl <<"Error: " << E << " %" << endl;
         cout << endl <<"The root is " << x1 << endl;
         cout << endl <<"Number of iterations: " << count;
         }
    else{
         count ++;
         x0 = x1;
         goto Top;
         }
    getch();  
    return 0;

}




Bisection Method

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x - 4*x - 10)
#define e 0.05
int main()
{
    static int count;
    float x1, x2, x0, f1, f2, f0, ab;
    cout << "Enter x1: " ;
    cin >> x1;
    cout << "Enter x2: " ;
    cin >> x2;
    cout << endl << "Estimated error: " << e << endl;
    f1 = f(x1);
    f2 = f(x2);
    if((f(x1) * f(x2))>0)
    {
              cout << "No roots " << endl;
              goto down;
              }
    else
    {
              Top:
                  ab = (fabs((x2 - x1)/x2) * 100);
                  if(ab< 0.05)
                  {
                          cout << endl << "The root is " << x1 << endl;
                          cout << "Error : " << ab << "%" << endl;
                          cout << "Total no of iteration is : " << count;
                     
                          goto down;
                          }
                          else{
                          count++;
                          x0 = (x1 + x2) /2;
                          f0 = f(x0);
                          if((f1 * f0)< 0)
                          {
                                 x2 = x0;
                                 goto Top;
                                 }
                          else{
                               x1 = x0;
                               f1 = f0;
                               goto Top;
                             }  }
    }
    down:
         getch();
         return 0;
         }