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

Click For Summary
SUMMARY

The discussion focuses on implementing the Bisection Method in C++ to find the roots of the polynomial equation x^3 + 4x^2 - x - 1. The user seeks assistance with a condition statement to terminate the loop when the function value is less than 0.001. The provided code contains a logical error in the do-while loop, preventing it from executing correctly. The loop should continue until the absolute value of the function at the midpoint is less than 0.01, ensuring accurate root approximation.

PREREQUISITES
  • Understanding of the Bisection Method for root-finding
  • Proficiency in C++ programming language
  • Familiarity with function definitions and floating-point arithmetic
  • Knowledge of control flow statements, specifically loops and conditionals
NEXT STEPS
  • Review C++ control flow statements to correct logical errors in loops
  • Learn about numerical methods for root-finding, focusing on the Bisection Method
  • Explore debugging techniques in C++ to identify and fix code issues
  • Investigate the use of absolute functions in mathematical computations
USEFUL FOR

Students and developers working on numerical methods, particularly those implementing root-finding algorithms in C++. This discussion is beneficial for anyone looking to enhance their understanding of the Bisection Method in C++.

Sumaya
Messages
29
Reaction score
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
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.
 
thank u all <3
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K