LAB MANUAL SUBJECT: NUMERICAL METHODS AND COMPUTATION LAB

1.    Write a C program to solve algebraic equations by using Method of Bisection.

2.    Write a C program to solve algebraic equations by using Method of False position.

3.    Write a C program to solve algebraic equations by using Newton Raphson Method.

4.    Write a C program to solve linear system of equations by using Gauss Jordan Method.

5.    Write a C program to solve linear system of equations by using Gauss Seidal Method.


Experiments 1:
Aim:  Program to solve algebraic equations by using Method of Bisection.

Algorithm:
1.    Start.
2.    Define the function f(x).
3.    Initialized x=0.
4.    Starting loop for then checking f9x0*f(x+1)>0
    if not go to step 5 then checking f(x)==0
5.    Set a=x and b=x+1
6.    If |b-a|>0.000001 go to step 7
    If not then goto step 11
7.    Calculate x=(a+b)/2
8.    Checking f(a)*f(x)<0
    set b=x then print b
    if not goto step 9
9.    Set a=x print a
10.    Print x
11.    Stop.

Program:

#include<stdio.h>
#include<math.h>

float f(float);

main()
{
    float x,a,b,err=0.00001;
    printf("\nEnter values of a and b=");
    scanf("%f%f",&a,&b);
    if(f(a)*f(b)>0)
        printf("\nRoot does not lies between %f and %f",a,b);
    else
    {
        do
        {
            x=(a+b)/2;
            if(f(x)==0)
            {
                break;
            }
            else if(f(a)*f(x)<0)
                b=x;
            else
                a=x;
        }while(fabs(b-a)>=err);
        printf("\nRoot of the equation=%f",x);
    }
}

float f(float x)
{
    return(x*x-x-1);
}

Experiments 2:
Aim: Program to solve algebraic equations by using Method of False position.

Algorithm:
1.    Start.
2.    Define the function f(x).
3.    Initialized k=0.
4.    Input a,b.
5.    If f(x)>=0.00001
    Then print no solutio goto step 10.
    if not go to step 6.
6.    Calculate h=[|f(a)|(b-a)]/[|f(a)|+|f(b)|]
    x=a+h, k=k+1
7.    If f(a)*f(x)<0 then goto step 8
    if not go to step 9.
8.    Set b=x then print b
9.    Set a=x print a
10.    Stop.

Program:

#include<stdio.h>
#include<math.h>

float f(float);

main()
{
 float x0,x1,x2,err=0.00001;
 printf("\nEnter the value of x0: ");
 scanf("%f",&x0);
 printf("\nEnter the value of x1: ");
 scanf("%f",&x1);
 if(f(x0)*f(x1)>0)
  printf("\nRoot does not lies between %f and %f",x0,x1);
 else
 {
  do
  {
   x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));
   printf("\nx0=%f, f(x0)=%f, x1=%f, f(x1)=%f, x2=%f, f(x2)=%f",x0,f(x0),x1,f(x1),x2,f(x2));
   if(f(x0)*f(x2)<0)
   {
    x1=x2;
   }
   else
   {
    x0 = x2;
   }
  }while(fabs(f(x2))>err);
  printf("\n\nApp.root = %f",x2);
 }
}

float f(float x)
{
 return(x*x*x-2*x-5);
}
Experiments 3:
Aim: Program to solve algebraic equations by using Newton Raphson Method.

Algorithm:
1.    Start.
2.    Define the function f(x) and f'(x).
3.    Calculate f(x) and f'(x).
4.    Initialized k=0.
5.    Input a.
6.    Set x=a.
7.    Set h=-(f(x)/f'(x))
8.    Set a=h, x=x+h.
9.    If |x-a|>0.00001 then goto step 10
    if not go to step 8.
10.    Print a
11.    Stop.

#include<stdio.h>
#include<math.h>
float f(float);
float df(float);

main()
{
    float a,x,h,err=0.00001;
    printf("\nEnter initial value=");
    scanf("%f",&a);
    x=a;
    do
    {
        h=-(f(x)/df(x));
        a=x;
        x=x+h;
    }while(fabs(x-a)>=err);
    printf("\nThe approximate value is %f",x);
}

float f(float x)
{
    return(x*x*x-8*x-4);
}

float df(float x)
{
    return(3*x*x-8);
}

Experiments 2:
Aim: Program to solve linear system of equations by using Gauss Jordan Method.
Algorithm:
1.    Start.
2.    Input elements in matrix mat[i][j].
3.    Set k=mat[1][0]/mat[0][0]
    l=mat[2][0]/mat[0][0]
4.    Loop for i=0 to 3.
    Set mat[1][i]=mat[1][i]-(mat[0][i]*k)
    mat[2][i]=mat[2][i]-(mat[0][i]*l)
    end loop
5.    Set m=mat[2][1]/mat[1][1]
    n=mat[0][1]/mat[1][1]
6.    Loop for i=0 to 3.
    Set mat[2][i]=mat[2][i]-(mat[1][i]*m)
    mat[0][i]=mat[0][i]-(mat[1][i]*n)
    end loop
7.    Print matrix mat[i][j]
8.    z=mat[2][3]/mat[2][2]
    y=(mat[1][3]-mat[2][2]*z)/mat[1][1]
    x=(mat[0][3]-mat[0][2]*z)/mat[0][1]
9.    Stop.

#include<stdio.h>
void make1( float a[][20],int, int,int );
void make0( float a[][20],int, int,int );
void show(float a[][20],int r,int c);
int rankA(float a[][20],int r);
int rankAB(float a[][20],int r,int c);

main()
{
    float a[ 20 ][ 20 ];
    int r,c,i,j;
    printf("\nEnter number of variables=");
    scanf("%d",&r);
    c=r+1;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c-1;j++)
        {
            printf("\nEnter coefficient of x%d=",j);
            scanf("%f",&a[i][j]);
        }
        for(;j<c;j++)
        {
            printf("\nEnter constant value=");
            scanf("%f",&a[i][j]);
        }
    }
    printf("\nThe augmented matrix is \n");
    show(a,r,c);
    for(i=0;i<r;i++)
    {
            make1(a,i,r,c);
            show(a,r,c);
            make0(a,i,r,c);
            show(a,r,c);
    }
    if(rankA(a,r)==r && rankAB(a,r,c)==r)
    {
        printf("\nSolutions are\n");
        for(i=0;i<r;i++)
            printf("x%d%d=%f\t",i,i,a[i][c-1]);
    }
    else if(rankA(a,r)==rankAB(a,r,c))
        printf("\nInfinite solutions\n");
    else
        printf("\nNo solutions\n");
}

int rankA(float a[][20],int r)
{
    int rnk=0,i,j;
    for(i=0;i<r;i++)
    {
        if(a[i][i]!=0)
            rnk++;
    }
    return rnk;
}

int rankAB(float a[][20],int r,int c)
{
    int rnk=0,i,j;
    for(i=0;i<r;i++)
    {
        if(a[i][i]!=0)
            rnk++;
        else if(a[i][c]!=0)
            rnk++;
    }
    return rnk;
}

void show(float a[][20],int r,int c)
{
    int i,j;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c-1;j++)
        {
            printf("%20f",a[i][j]);
        }
        printf(" | %20f",a[i][j]);
        printf("\n");
    }
    printf("\n");
}

void make1( float a[][20], int i,int r,int c )
{
    float t=a[i][i];
    int j=i;
    printf("\nMaking 1\n");
    if(t==0)
        return;
    for(j=i;j<c;j++)
    {
        a[i][j]=a[i][j]/t;
    }
}

void make0( float a[][20], int k,int r,int c )
{
    int i,j;
    float temp;
    printf("\nMaking 0\n");
    for(i=0;i<r;i++)
    {
        if(i!=k)
        {
            temp=a[i][k];
            for(j=k;j<c;j++)
            {
                a[i][j]=a[i][j]-a[k][j]*temp;
            }
        }
    }
}
Experiments 2:
Aim: Program to solve linear system of equations by using Gauss Seidal Method.
Algorithm:
1.    Start.
2.    Input elements in matrix mat[i][j].
3.    Loop for i=0 to 4.
4.    If i=0 then goto step 5
    if not then goto step 6.
5.    Set x=fun1(0,0) goto A
    y=fun2(x,0) goto B
    z=fun3(x,y) goto C
    then goto step 7
6.    Set x=fun1(y,z) goto 1
    y=fun2(x,z) goto 2
    z=fun3(x,y) goto 3
    then goto step 7
7.    Print x,y,z
8.    Stop.

#include<stdio.h>
#include<math.h>

void show(float a[][200],int r);
main()
{
    float a[200][200],err,aerr=0.00001,x[200]={0},maxerr,t;
    int r,i,j,maxitr=10,itr,s;
    printf("\nEnter number of variables=");
    scanf("%d",&r);
    for(i=0;i<r;i++)
    {
        for(j=0;j<r;j++)
        {
            printf("\nEnter coefficient of x%d=",j);
            scanf("%f",&a[i][j]);
        }
        printf("\nEnter constant value=");
        scanf("%f",&a[i][r]);
    }
    printf("\nThe augmented matrix is \n");
    show(a,r);
    printf("Iteration\t");
    for(i=0;i<r;i++)
        printf("     x[%2d]     ",i+1);
    printf("\n");
    for(itr=1;itr<=maxitr;itr++)
    {
        maxerr=0;
        for(i=0;i<r;i++)
        {
            s=0;
            for(j=0;j<r;j++)
            {
                if(j!=i)
                {
                    s+=a[i][j]*x[j];
                }
            }
            t=(a[i][r]-s)/a[i][i];
            err=fabs(x[i]-t);
            if(err>maxerr)
                maxerr=err;
            x[i]=t;
        }
        printf("%5d     ",itr);
        for(i=0;i<r;i++)
        {
            printf("%15f",x[i]);
        }
        printf("\n");
        if(maxerr<aerr)
        {
            printf("Converges in %3d Iteration\n",itr);
            for(i=0;i<r;i++)
            {
                printf("x[%d]=%f",i,x[i]);
            }
            return 0;
        }
    }
    printf("\nSolution does not converge, needs more iteration \n");
}

void show(float a[][200],int r)
{
    int i,j;
    for(i=0;i<r;i++)
    {
        for(j=0;j<r;j++)
        {
            printf("%20f",a[i][j]);
        }
        printf("  %20f",a[i][r]);
        printf("\n");
    }
    printf("\n");
}

1 comment: