Computer Science‎ > ‎

C Program: Numerical Computing - the Bisection Method



 
The C++ Programming Language...
List Price: Rs.775
Our Price: Rs.736
Buy from FlipKart

The C++ Programming Language (Bjarne Stroustrup)

Beginning C++ Through Game Pr...
Our Price: Rs.2008
Buy from FlipKart

       
Beginning C++ through Game Programming 

 
C++ How to Program
List Price: Rs.695
Our Price: Rs.632
Buy from FlipKart

                               
 
The C Programming Language
List Price: Rs.175
Our Price: Rs.137
Buy from FlipKart

             
The C Programming Language (Kernighan)
 
C How To Program 6th  Edition
List Price: Rs.650
Our Price: Rs.624
            Buy from FlipKart


          
C How to Program (Deitel and Deitel)

 


C Program implementing the Bisection Method ( Numerical Computing )

/*This program in C is used to demonstarte bisection method.
Bisection method is one of the many root finding methods.
In this method we are given a function f(x) and we approximate 2 roots a and b for the function such that f(a).f(b)<0.
Then we find another point
c=(a+b)/2
if f(c)==0
then root=c;
else
if f(a).f(c)<0
b=c;
if f(b).f(c)<0
a=c;
and we repeat these steps for the given number of iterations*/
#include<stdio.h>
#include<math.h>
double F(double x)
{
    return(pow(x,3)+3*x-5);//This return the value of the function
}
int main()
{
    printf("This program illustrates the bisection method in C\n");
    printf("x^3 + 3*x - 5 = 0\n");
    double x0,x1;
    printf("Enter the first approximation to the root\n");
    scanf("%lf",&x0);
    printf("Enter the second approximation to the root\n");
    scanf("%lf",&x1);
    int iter;
    printf("Enter the number of iterations you want to perform\n");
    scanf("%d",&iter);
    int ctr=1;
    double l1=x0;
    double l2=x1;
    double r,f1,f2,f3;
    //We check if the initail approximations are the root or not
    if(F(l1)==0)
    r=l1;
    else
    if(F(l2)==0)
    r=l2;
    else
    {
    while(ctr<=iter)
    {//this is an implementation of the algorithm mentioned above
        f1=F(l1);
        r=(l1+l2)/2.0;
        f2=F(r);
        f3=F(l2);
        if(f2==0)
        {
            r=f2;
            break;
        }
        printf("The root after %d iteration is %lf\n",ctr,r);
        if(f1*f2<0)
        l2=r;
        else
        if(f2*f3<0)
        l1=r;
        ctr++;
    }
    }
    printf("The approximation to the root is %lf",r);
    getch();
}
/*A sample run of the program was carried out and the results were found as:-
This program illustrates the bisection method in C
x^3 + 3*x - 5 = 0
Enter the first approximation to the root
1
Enter the second approximation to the root
2
Enter the number of iterations you want to perform
9
The root after 1 iteration is 1.500000
The root after 2 iteration is 1.250000
The root after 3 iteration is 1.125000
The root after 4 iteration is 1.187500
The root after 5 iteration is 1.156250
The root after 6 iteration is 1.146025
The root after 7 iteration is 1.148438
The root after 8 iteration is 1.152344
The root after 9 iteration is 1.154297
The root is 1.154297
*/





 









C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers

Try out our Quizzes!