0% found this document useful (0 votes)
41 views7 pages

Bisection, Simpleson

Uploaded by

ad599066
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views7 pages

Bisection, Simpleson

Uploaded by

ad599066
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Bisection Method Algorithm:

Following is the algorithm of the Bisection Method in C.

1. Start the program.


2. Input two initial guesses x1 and x2. The 'e' is the absolute error to get the
desired degree of accuracy.
3. Compute the function value: f1 = f(x1) and f2 = f(x2)
4. Now compare the product of f1 and f2 with 0, as

If (f1 * f2) > 0, it displays the initial guesses are wrong and transfer control to step 11.

5. Get the mid value: x = (x1 + x2) / 2


6. If ( [ (x1 - x2) / x] < e), then print the value x and jump to (11).
7. Else, it shows: f = f(x)
8. If (( f * f1) > 0), then assign x1 = x and f1 = f.
9. Else, assign x2 = x and f2 = f.
10. Jump to 5. And the loop continues with new values.
11. Terminate the program.
Program to find the real root of the (x3 + 3x - 5 = 0)
equation using the Bisection method
#include <stdio.h>
#include <conio.h>
// create a function
double bisect ( double num)
{
// it returns the value of the function
return ( pow (num, 3) + 3 * num - 5);
}

int main ()
{
printf ( " \n Display the real roots of the given equation using the Bisection method: ");
printf ( " \n x ^ 3 + 3 * x - 5 = 0 \n ");
double x0, x1;

printf ( " \n Enter the first approximation of the root: ");


scanf (" %lf", &x0);

printf ( " \n Enter the second approximation of the root: ");


scanf (" %lf", &x1);

int iterate;
printf (" \n Input the number of iteration you want to perform: ");
scanf (" %d", &iterate);

int count = 1;
double l1 = x0;
double l2 = x1;
double r, f1, f2, f3;

// now check whether the initial approximation are the real root
if (bisect (l1) == 0) // it is a root
{
r = l1;
}
else if ( bisect (l2) == 0)
{
r = l2;
}

// if the above two values are not the root of the given equation
else
{
while (count <= iterate) // here count is initialized with 1
{
f1 = bisect (l1);
r = (l1 + l2) / 2.0; // get the mid value of the interval l1 and l2
f2 = bisect (r);
f3 = bisect (l2);

// check f2 is equal to 0
if (f2 == 0)
{
r = f2;
break; // break the execution from the while loop statement
}

printf (" \n The root after %d iterations is %lf. \n", count, r);

// check multiplication of f1 * f2 not less than 0


if ( f1 * f2 < 0)
{
l2 = r;
}
else if (f2 * f3 < 0)
{
l1 = r;
}
count++;
}
}
// return final value after mentioned the iteration
printf (" \n The approximation of the root is: %lf \n ", r);
return 0;
}
Program for solving numerical integration by
Simpson’s 1/3 rule
Integration is an integral part in science and engineering to calculate things such as
area, volume, total flux, electric field, magnetic field and many more. Here, we are going
to take a look at numerical integration method (Simpson’s 1/3 rule in particular using C
language) to solve such complex integration problems.

For this, let’s discuss the C program for Simpson 1/3 rule for easy and accurate
calculation of numerical integration of any function which is defined in program.

In the source code below, a function f(x) = 1/(1+x) has been defined. The calculation
using Simpson 1/3 rule in C is based on the fact that the small portion between any two
points is a parabola. The program follows the following steps for calculation of the
integral.

 As the program gets executed, first of all it asks for the value of lower
boundary value of x i.e. x0, upper boundary value of x i.e. xn and width of the
strip, h.
 Then the program finds the value of number of strip as n=( xn – x0 )/h and
checks whether it is even or odd. If the value of ‘n’ is odd, the program refines
the value of ‘h’ so that the value of ‘n’ comes to be even.
 After that, this C program calculates value of f(x) i.e ‘y’ at different
intermediate values of ‘x’ and displays values of all intermediate values of ‘y’.
 After the calculation of values of ‘c’, the program uses the following formula to
calculate the value of integral in loop.
Integral = *((y0 + yn ) +4(y1 + y3 + ……….+ yn-1 ) + 2(y2 + y4 +……….+ yn-2 ))

 Finally, it prints the values of integral which is stored as ‘ans’ in the program.
If f(x) represents the length, the value of integral will be area, and if f(x) is area, the
output of Simpson 1/3 rule C program will be volume. Hence, numerical integration can
be carried out using the program below; it is very easy to use, simple to understand, and
gives reliable and accurate results.

f(x) = 1/(1+x)
#include<stdio.h>
#include<math.h>
float f(float x)
{
return(1/(1+x));
}
int main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\n Refined value of n and h are:%d %f\n",n,h);
printf("\n Y values: \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}

}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\n Final integration is %f",ans);

return 0;
}
OUTPUT:
Enter values of x0,xn,h: 1 5 2
Refined value of n and h are:2 2.000000

Y values:

0.500000

0.250000

0.166667

Final integration is 1.111111


OUTPUT:
Display the real roots of the given equation using the Bisection method:
x^3+3*x-5=0

Enter the first approximation of the root: 3

Enter the second approximation of the root: 4

Input the number of iteration you want to perform: 5

The root after 1 iterations is 3.500000.

The root after 2 iterations is 3.500000.

The root after 3 iterations is 3.500000.

The root after 4 iterations is 3.500000.

The root after 5 iterations is 3.500000.

The approximation of the root is: 3.500000

Process returned 0 (0x0) execution time : 22.487 s


Press any key to continue.

You might also like