Sie sind auf Seite 1von 7

Bisection Method

Aim: To implement Bisection method to calculate the function f(c) and g(c).

Algorithm
Step 1: Start the program
Step 2: Define f and g
f return(pow(x, 3) -3 * x + 1)
g return(pow(x, 3) - 2 * sin(x))
Step 3: Define bisection method using parameters such as float f(float), float a, float b, int m
Step 4: Calculate the values c, fc, gc, and width and then display n, c, fc, width
Step 5: Using the method
a)
b)
c)
d)

declare fa = 0, fb = 1.0, ga = 0.5, gb = 2.0 as float data type


declare const int m = 10
display the first function values using bisection(f, fa, fb, m) method
display the second function values using bisection(g, ga, gb, m) method

Step 6: Stop the program

Flowchart
Start

float fa = 0
fb = 1.0
ga = 0.5
gb = 2.0
const int m = 10
print fa, fb
bisection(f, fa, fb, m)

print ga, gb

bisection(g, ga, gb, m)


Stop

A
Start

bisection(float f(float), float a, float b, int m)


float c, fa, fb, fc, width,
int n, fa= f(a), fb= f(b)

if
(fa*fb)>0
False
True

print a, b, fa, fb

i
width = b - a
True

for(n = 0; n < m; n++)


width *= 0.5
c = a + width
fc = f(c)
if
(fa*fc)<0
False

b=c
fb = fc
i
a=c
fa = fc
print n, c, fc, width
i
return

Start

float f(float x)
return(pow(x, 3) - 3*x+1

Stop

Start

float g(float x)
return(pow(x, 3) 2 * sin(x)

Stop

Program
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
float f (float x)
{
return (pow(x,3) - 3*x + 1);
}
float g (float x)
{
return (pow(x,3) - 2*sin(x));
}
void bisection (float f(float),float a, float b, int m)
{
float c, fa, fb, fc, width;
int n;
fa = f(a);
fb = f(b);
if (fa * fb > 0)
{
printf("Function has the same signs at a and b\n");
printf("%9e %9e %9e %9e\n", a,b,fa,fb);
}
else
{
width = b - a;
for (n = 0; n < m; n++)
{
width *= 0.5;
c = a + width;
fc = f(c);
if (fa * fc < 0)
{
b = c;
fb = fc;
}
else
{
a = c;
fa = fc;
}
printf("%4d%14e%14e%14e\n",n,c,fc,width);
}
}
}
void main()

{
float fa = 0, fb = 1.0, ga = 0.5, gb = 2.0;
const int m = 10;
clrscr();
printf ("("**--(57*s)--**\n");
printf ("*\t\t\tBisector Method \t\t*\n");
printf ("("**--(57*s)--**\n");
printf(" \nFirst function: F = X^3-3X+1\t a = %f, b = %f\n", fa, fb);
printf("\n step
c
f(c)
width\n\n");
bisection(f,fa,fb,m);
printf("\n Second function: G=X^3-2SIN(X)\t a = %f, b = %f\n", ga, gb);
printf("\n step
c
g(c)
width\n\n");
bisection(g,ga,gb,m);
getch();
}

Output

Das könnte Ihnen auch gefallen