Solving a Problem with Bisection Method (C Program)

Click For Summary

Discussion Overview

The discussion revolves around a C program implementing the bisection method for finding roots of a polynomial equation. Participants are addressing issues related to syntax and logical errors in the code, as well as the algorithm's requirements for proper functioning.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares a C program but notes that the output is infinite, suggesting potential syntax errors rather than logical errors.
  • Another participant comments on the use of the "goto" statement and suggests that the reassignment of variable "x" at the "read" label negates the effect of the conditional assignments.
  • A later reply proposes a modification to the code, recommending that "x1" or "x2" should be assigned in the conditional statements instead of "x".
  • Another participant points out that the if statement should involve the function values rather than the variables themselves, suggesting the introduction of a new variable for clarity.
  • Concerns are raised about the initial values for "x1" and "x2", emphasizing that they must be chosen such that the polynomial changes sign between them for the bisection method to work effectively.

Areas of Agreement / Disagreement

Participants express differing opinions on the effectiveness of previous responses, indicating a lack of consensus on the quality of assistance provided. There is no clear agreement on the best approach to resolve the issues with the code.

Contextual Notes

Participants highlight the need for careful selection of initial values for the bisection method and the importance of ensuring that the polynomial crosses the x-axis appropriately. There are unresolved questions regarding the algorithm's implementation and the specific requirements for the function being analyzed.

swty todd
Messages
69
Reaction score
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
Code:
goto read;

Arf!
 
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;
}
 
okkk
sry but thnx...
 
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.
 
thnx...i will try my best...
 
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:
 
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:
 
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...
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 23 ·
Replies
23
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K