Solving a Problem with Bisection Method (C Program)

In summary: To stop the loop, you need to make the values of x1 and x2 "converge" on the correct answer. This means getting them closer and closer together, so that the difference between them becomes smaller and smaller.To do this, you can add an "if" statement that checks the difference between x1 and x2. If it is small enough (for example, less than 0.0001), then you can stop the loop. Otherwise, you can continue the loop and make the values of x1
  • #1
swty todd
70
0
here is the c program for arriving at a solution using bisection method.
The ouputi s going infinte..pls help ...i think it has more of syntax errors than logical errors...

Code:
main()
{
	float a,b,c,x1,x2,x,series;
	double d;

	printf("enter a,b,c and x1(pos) & x2(neg)");
	scanf("%f%f%f%f%f", &a, &b, &c, &x1, &x2);

	read:

	x = (x1 + x2) / 2;
	series = a * x * x + b * x + c;
	d = fabs(series);

	if (d > 0.0001)
	{
		if (x * x1 < 0)
			x = x2;
		else
			x = x1;

		goto read;
	}
	else
	{
		printf("ans=%f", x);
	}

	return 0;
}
 
Last edited:
Technology news on Phys.org
  • #2
Code:
goto read;

Arf!
 
  • #3
You will probably get some help faster my learning to format code so someone can read it.



Code:
main()
{
	float a,b,c,x1,x2,x,series;
	double d;

	printf("enter a,b,c and x1(pos) & x2(neg)");
	scanf("%f%f%f%f%f", &a, &b, &c, &x1, &x2);

	read:

	x = (x1 + x2) / 2;
	series = a * x * x + b * x + c;
	d = fabs(series);

	if (d > 0.0001)
	{
		if (x * x1 < 0)
			x = x2;
		else
			x = x1;

		goto read;
	}
	else
	{
		printf("ans=%f", x);
	}

	return 0;
}
 
  • #4
okkk
sry but thnx...
 
  • #5
swty todd said:
read:
x=(x1+x2)/2;
...
if (x*x1<0) x=x2 ; else x=x1;
goto read;
Note that you reassign x each time at "read:" so the if statement assignements have no effect. I'm not famliar with the algorithm, but you probably should be assigning x1 or x2 in your if statement:

read:
x=(x1+x2)/2;
...
if (x*x1<0) x2=x ; else x1=x;
goto read;

Would have been nice if the previous posts offered some constructive help.
 
  • #6
thnx...i will try my best...
 
  • #7
Jeff Reid said:
Would have been nice if the previous posts offered some constructive help.

It would be even nicer if you decided to keep your opinions on earlier posts to yourself. :wink:
 
  • #8
shoehorn said:
It would be even nicer if you decided to keep your opinions on earlier posts to yourself. :wink:

yeah...dude and guess what ...it would be much more nicer if u followed what u preach :wink: :wink:
 
  • #9
i edited my code..bt still isn't workin..wld b glad 2 recive ur help

Code:
#include<stdio.h>
#include<math.h>
main()
{float a,b,c,x1,x2,x,series;
double d;



printf("enter a,b,c and x1(pos) & x2(neg)");
scanf("%f%f%f%f%f",&a,&b,&c,&x1,&x2);


x=0;
d=1;

 while(d>0.0001)
    {
      x=(x1+x2)/2;
      

     series=a*x*x+b*x+c;
      

     d=fabs(series);
      

         if(x*x1 < 0)
             x2=x;
         else
             x1=x;
    }

     

printf("ans=%f",x);



return 0;
}
 
  • #10
The if statement should multiply the function(x1) * function(x), not x1 and x themselves. Using a new variable called series1:

series = a*x*x + b*x + c;
series1 = a*x1*x1 + b*x1 + c;
...
if (series * series1) < 0) x2 = x; else x1 = x;
...

Also note that the initial values for x1 and x2 need to be chosen so that

a*x1*x1 + b*x1 + c > 0
a*x2*x2 + b*x2 + c < 0

A parabola would have to cross the x-axis twice for this method to work, and there would be two possible answers. The initial values for x1 and x2 would have to chosen so that only one of the answers would occur in the range x1 .. x2.
 
Last edited:
  • #11
u r rite..series and series1 get multiplied and the main equation shld better not b quadratic..thnx...
 

1. What is the Bisection Method?

The Bisection Method is a numerical method used to solve a problem by repeatedly dividing an interval in half and testing which half contains the solution. It is commonly used to find roots of equations.

2. How does the Bisection Method work?

The Bisection Method starts by defining an interval [a,b] where the solution is expected to be found. Then, the midpoint of the interval is calculated and tested against the desired solution. If the midpoint is closer to the solution than the endpoints, the interval is divided in half and the process is repeated until the desired accuracy is achieved.

3. What are the advantages of using Bisection Method?

One advantage of using Bisection Method is that it guarantees convergence to the solution, as long as the function is continuous and the initial interval is chosen properly. Additionally, it is relatively easy to implement and does not require any derivatives of the function.

4. What are the limitations of Bisection Method?

Bisection Method may be slow to converge, especially for functions with multiple roots or when the initial interval is chosen poorly. It also requires the function to be continuous, which may not always be the case.

5. How can I use Bisection Method in a C program?

To use Bisection Method in a C program, you will need to define the function you want to solve, choose an initial interval [a,b], and specify the desired accuracy. Then, you can use a loop to repeatedly divide the interval and test the midpoint until the desired accuracy is achieved. It is helpful to use a conditional statement to check if the midpoint is closer to the solution than the endpoints and update the interval accordingly. Finally, the midpoint can be returned as the solution to the problem.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
23
Views
6K
  • Programming and Computer Science
Replies
12
Views
953
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top