Bisection Method for Finding Roots of x^3+4x^2-x-1 in C++

In summary, the conversation is about finding the roots of an equation using the bisection method in C++. The person is having trouble with their loop and is seeking advice on how to make it stop when the function value is less than 0.001. The code and equation being used are also provided.
  • #1
Sumaya
29
0

Homework Statement


find the roots ( interval halving ) , I want to know how to make a condition statement that when fun3 be less than 0.001 and then loop will stop and i get the root .(here in my code i don't know why the loop doesn't work as it should ..


Homework Equations



the equation is : x^3+4x^2-x-1

The Attempt at a Solution



Code:
// UniqueName.cpp : Bisection Method in c++
//

#include "stdafx.h"
#include<iostream>
#include <math.h>
using namespace std;
float fun1(float);
float fun2(float);
float fun3(float);
void main()
{
float a,b,c,a1,b1,c1;
int i=0;
cin>>a>>b;
a1=fun1(a);
b1=fun2(b);
c=(a+b)/2;
c1=fun3(c);
         
           if((a1*b1)<0)
			  do{
				
              if ((a1*c1)<0)
				  b=c;
			  else if((c1*b1)<0)
				  a=c;
			  else
				  break;
			  
			  }
			  while(fabs (c1)<0.01);
			  

		 cout<<"The root  is    "<<c<<endl;
}

		 
float fun1( float a)
{
	float y;
	y=a*a*a+4*a*a-a-1;
	return y;
}
float fun2( float b)
{
	float y;
	y=b*b*b+4*b*b-b-1;
	return y;
}
float fun3( float c)
{
	float y;
	y=c*c*c+4*c*c-c-1;
	return y;
}
thanx in advance
 
Physics news on Phys.org
  • #2
Sumaya said:

Homework Statement


find the roots ( interval halving ) , I want to know how to make a condition statement that when fun3 be less than 0.001 and then loop will stop and i get the root .(here in my code i don't know why the loop doesn't work as it should ..


Homework Equations



the equation is : x^3+4x^2-x-1

The Attempt at a Solution



Code:
// UniqueName.cpp : Bisection Method in c++
//

#include "stdafx.h"
#include<iostream>
#include <math.h>
using namespace std;
float fun1(float);
float fun2(float);
float fun3(float);
void main()
{
float a,b,c,a1,b1,c1;
int i=0;
cin>>a>>b;
a1=fun1(a);
b1=fun2(b);
c=(a+b)/2;
c1=fun3(c);
         
if((a1*b1)<0)
   do{
				
       if ((a1*c1)<0)
         b=c;
      else if((c1*b1)<0)
         a=c;
      else
         break;
   }
   while(fabs (c1)<0.01);
			  
   cout<<"The root  is    "<<c<<endl;
}

		 
float fun1( float a)
{
	float y;
	y=a*a*a+4*a*a-a-1;
	return y;
}
float fun2( float b)
{
	float y;
	y=b*b*b+4*b*b-b-1;
	return y;
}
float fun3( float c)
{
	float y;
	y=c*c*c+4*c*c-c-1;
	return y;
}
Just as in the other thread you started, the logic in your do while loop is backwards. The first time through fabs(c1) > .01, so the loop doesn't do more than one iteration.
 
  • #3
thank u all <3
 

What is the Bisection Method in C++?

The Bisection Method is a numerical algorithm used to find the root of a continuous function by repeatedly dividing an interval in half and checking which half contains the root. It is commonly used in numerical analysis and scientific computing.

How does the Bisection Method work in C++?

The Bisection Method works by first defining an interval [a, b] where the root of the function is expected to be. Then, the midpoint of the interval is calculated and checked to see if it is the root or if the root lies in the left or right sub-interval. This process is repeated until the desired accuracy is achieved.

What are the advantages of using the Bisection Method in C++?

The Bisection Method is a simple and easy-to-implement algorithm that guarantees convergence to a root if the function is continuous and the initial interval is chosen correctly. It also does not require the function to be differentiable.

What are the limitations of the Bisection Method in C++?

The Bisection Method can be slow to converge if the initial interval is chosen poorly or if the function has multiple roots. It also does not work for functions with discontinuities or non-real roots.

How do I implement the Bisection Method in C++?

To implement the Bisection Method in C++, you will need to define the function you want to find the root of, choose an initial interval, and write a loop that calculates the midpoint and updates the interval until the desired accuracy is achieved. You can also use libraries such as <cmath> to help with calculations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
22
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top