Thursday 25 August 2016

Coding the well known Bisection Method

Getting root of an equation by Bisection Method through C programming language.

The Method:
The method is applicable for numerically solving the equation f(x) = 0 for the real variable x, where f is a continuous function defined on an interval [ab] and where f(a) and f(b) have opposite signs. In this case a and b are said to bracket a root since, by the intermediate value theorem, the continuous function f must have at least one root in the interval (ab).
At each step the method divides the interval in two by computing the midpoint c = (a+b) / 2 of the interval and the value of the function f(c) at that point. Unless c is itself a root (which is very unlikely, but possible) there are now only two possibilities: either f(a) and f(c) have opposite signs and bracket a root, or f(c) and f(b) have opposite signs and bracket a root.[5] The method selects the subinterval that is guaranteed to be a bracket as the new interval to be used in the next step. In this way an interval that contains a zero of f is reduced in width by 50% at each step. The process is continued until the interval is sufficiently small.
Explicitly, if f(a) and f(c) have opposite signs, then the method sets c as the new value for b, and if f(b) and f(c) have opposite signs then the method sets c as the new a. (If f(c)=0 then c may be taken as the solution and the process stops.) In both cases, the new f(a) and f(b) have opposite signs, so the method is applicable to this smaller interval.


SOURCE CODE:


#include<stdio.h>
float f(float x)
{
    float z;
    z= x*x*x-4*x-9;
    return z;
}// Getting the value of f(x)
void main()
{


    printf("\n \n Equation is \tx^3-4x-9\n");
    int n;
    float c,i,a,b,r,A,B,C;
    printf("\n Enter the number of iterations\n");
    scanf("%d",&n);// n is the number of the iterations

    for(i=0.0;i<3.0;i=i+0.001)
    {
        if(f(i)<0.001)
        {
            a=i;

            }
    }
    b=a+0.01;
//Here a and b variables are the intervals
    printf("Root lies between %f and %f ", a,b);
do
{
    c=(a+b)/2;
   if(f(a)*f(c)<0)
    b=c;
   if(f(b)*f(c)<0)
    a=c;

    r=f(c);
    --n;
}while(n!=0);
printf("\n Root is %f \n",(a+b)/2);


}

C program to find the sum of two matrices of order mxn.

#include<stdio.h>
void main()
{
   int i,j,a[20][20],b[20][20],s[20][20],max,m,n;
   printf("Name: Viraj Singh\nRoll No.:-1503213117\n\n");
   printf("Enter order of the matrix\n");
   scanf("%d%d",&m,&n);
   printf("Enter the matrix A\n");
   for(i=0;i<m;i++)
       for(j=0;j<n;j++)
       scanf("%d",&a[i][j]);
   printf("Enter the matrix B\n");
   for(i=0;i<m;i++)
       for(j=0;j<n;j++)
       scanf("%d",&b[i][j]);
   for(i=0;i<m;i++)
   {
       for(j=0;j<n;j++)
       {
           s[i][j]=a[i][j]+b[i][j];
       }
   }
   printf("Sum is:-\n");
   for(i=0;i<m;i++)
   {
       for(j=0;j<n;j++)
       printf("%d   ",s[i][j]);
       printf("\n");
   }

}

To find the roots of an equation

#include<stdio.h>
#include<math.h>
void main()
{
   
    int a,b,c;
    float d,x,y;
    printf("enter the value of a,b,c");
    scanf("%d%d%d",&a,&b,&c);
    d=(b*b)-(4*a*c);
    if(d>0){
     printf("roots are real and distinct");
     x=(-b+(sqrt(d)))/2*a;
     y=(-b-(sqrt(d)))/2*a;
     printf("roots are %f %f",x,y);
    }
    else if(d=0)
    {
        printf("roots are real and same");
        x=-b/(2*a);
        y=-b/(2*a);
        printf("roots are %f %f",x,y);
    }
    else
    {
        printf("roots are imaginary");
    }
}



Aim: To perform computer exponential airthmatic (Addition, Subtraction, Multiplication, Divison)

#include<stdio.h>
#include<math.h>
void main()
{
    int ch;
    float e1,e2,x1,x2,k,E,x;
   printf("Enter first number in FPR. \n");
    scanf("%f%f",&x1,&e1);
    printf("Enter second number in FPR. \n");
    scanf("%f%f",&x2,&e2);
    k=fabs(e1-e2);
    if(e1>e2)
    {
        x2=x2/(pow(10,k));
        E=e1;
    }
    else
    {
        x1=x1/(pow(10,k));
        E=e2;
    }
    printf("Enter : \n1 - Addition \n2 - Subtraction \n3 - Multiplication \n4 - Division \n");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1 :
        {
            x=x1+x2;
            if(x>0.1)
            {
                x1=x1/10;
                E=E+1;
            }
            printf("%fe%f ",x,E);
            break;
        }
        case 2 :
        {
            x=fabs(x1-x2);
            if(x<0.1)
            {
                x1=x1*10;
                E=E-1;
            }
            printf("%fe%f ",x,E);
            break;
        }
        case 3 :
        {
            x=fabs(x1*x2);
            E=e1+e2;
            printf("%fe%f ",x,E);
            break;
        }
        case 4 :
        {
            x=x1/x2;
            E=e1-e2;
            if(e1>99)
            {
                printf("Overflow.");
            }
            if(e1<-99)
            {
                printf("Underflow.");
            }
            printf("%fe%f ",x,E);
            break;
        }
        default :
        {
            printf("Invalid Choice. \n");
        }
    }
}




To find the area of the triangle.

#include<stdio.h>
#include<math.h>
void main()
{
  

    float a,b,c,s,area,k;
    printf("enter the three sides of triangle");
    scanf("%f%f%f",&a,&b,&c);
    s=(a+b+c)/2;
     k=s*(s-a)*(s-b)*(s-c);
     if(c<0)
     {
         printf("complex no");
     }
     else
    {
        area=sqrt(c);
    }
    printf("area of triangle is %f",area);
}


To find the relative , percentage, absolute error using normalized floating point representation.

#include<stdio.h>
#include<math.h>
void main()
{
    float abserror, relerror, percerror, trueval, approxval;
    printf("enter true value");
    scanf("%f",&trueval);
    printf("enter approx value");
    scanf("%f",&approxval);
    abserror=fabs(trueval-approxval);
    relerror=abserror/trueval;
    percerror=relerror*100;
    printf("absolute error = %f",abserror);
    printf("relative error = %f",relerror);
    printf("percentage error = %f",percerror);
}







Aim: Write a program in C to find the sum of two matrices of order mxn.

#include<stdio.h>
void main()
{
    int i,j,a[20][20],b[20][20],s[20][20],max,m,n;
    printf("Enter order of the matrix\n");
    scanf("%d%d",&m,&n);
    printf("Enter the matrix A\n");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        scanf("%d",&a[i][j]);
    printf("Enter the matrix B\n");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        scanf("%d",&b[i][j]);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            s[i][j]=a[i][j]+b[i][j];
        }
    }
    printf("Sum is:-\n");
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        printf("%d   ",s[i][j]);
        printf("\n");
    }

}

Write a program in C to find the largest number from the given n numbers.

#include<stdio.h>
void main()
{
    int i,a[20],max,n;
    printf("Enter no of elements\n");
    scanf("%d",&n);
    printf("Enter the array\n");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    max=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]>max)
            max=a[i];
    }
    printf("Max Element=%d",max);
}





Sunday 21 August 2016

Bisection Method

Getting root of an equation by Bisection Method through C programming language.

The Method:
The method is applicable for numerically solving the equation f(x) = 0 for the real variable x, where f is a continuous function defined on an interval [ab] and where f(a) and f(b) have opposite signs. In this case a and b are said to bracket a root since, by the intermediate value theorem, the continuous function f must have at least one root in the interval (ab).
At each step the method divides the interval in two by computing the midpoint c = (a+b) / 2 of the interval and the value of the function f(c) at that point. Unless c is itself a root (which is very unlikely, but possible) there are now only two possibilities: either f(a) and f(c) have opposite signs and bracket a root, or f(c) and f(b) have opposite signs and bracket a root.[5] The method selects the subinterval that is guaranteed to be a bracket as the new interval to be used in the next step. In this way an interval that contains a zero of f is reduced in width by 50% at each step. The process is continued until the interval is sufficiently small.
Explicitly, if f(a) and f(c) have opposite signs, then the method sets c as the new value for b, and if f(b) and f(c) have opposite signs then the method sets c as the new a. (If f(c)=0 then c may be taken as the solution and the process stops.) In both cases, the new f(a) and f(b) have opposite signs, so the method is applicable to this smaller interval.


SOURCE CODE:


#include<stdio.h>
float f(float x)
{
    float z;
    z= x*x*x-4*x-9;
    return z;
}// Getting the value of f(x)
void main()
{


    printf("\n \n Equation is \tx^3-4x-9\n");
    int n;
    float c,i,a,b,r,A,B,C;
    printf("\n Enter the number of iterations\n");
    scanf("%d",&n);// n is the number of the iterations

    for(i=0.0;i<3.0;i=i+0.001)
    {
        if(f(i)<0.001)
        {
            a=i;

            }
    }
    b=a+0.01;
//Here a and b variables are the intervals
    printf("Root lies between %f and %f ", a,b);
do
{
    c=(a+b)/2;
   if(f(a)*f(c)<0)
    b=c;
   if(f(b)*f(c)<0)
    a=c;

    r=f(c);
    --n;
}while(n!=0);
printf("\n Root is %f \n",(a+b)/2);


}