How can I fix the bisection method loop when the decimal place value is changed?

Click For Summary
SUMMARY

The forum discussion centers on a coding issue with the bisection method implemented in C++. The user encounters a problem where the loop becomes unresponsive when changing the decimal place value for accuracy. The solution provided identifies a critical error in the conditional statement that checks the signs of the function values at the endpoints, suggesting that the comparison should be made with the midpoint instead. This correction ensures the bisection method functions correctly across varying decimal precision.

PREREQUISITES
  • Understanding of the bisection method in numerical analysis
  • Proficiency in C++ programming, specifically with loops and conditionals
  • Familiarity with mathematical functions and their evaluations
  • Knowledge of floating-point precision and its implications in calculations
NEXT STEPS
  • Review C++ conditional statements and their proper usage
  • Study the implementation of the bisection method in numerical analysis
  • Learn about floating-point arithmetic and precision issues in programming
  • Explore debugging techniques for iterative algorithms in C++
USEFUL FOR

Students and professionals in computer science, particularly those focusing on numerical methods, algorithm development, and C++ programming. This discussion is also beneficial for anyone troubleshooting similar issues in iterative methods.

SolStis
Messages
10
Reaction score
0
Hi, I've written code to resolve the routes of a function using the bisection method. The code works for the current route brackets (xb and xt) and for a decimal place value of 2. However when the decimal place value is changed the loop gets stuck. Code below:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

//declaration and initiation of fixed variables
double m=5.0,L=0.6,k=1000.0,g=9.81;

//Setup of function to be solved
double f(double x){
return tan(x)-sin(x)-(m*g)/(k*L);
}

//Main function
int main(){

//Declaration of decimal places var d
int d;

//Declaration of upper, lower and middle limit vars
double xb,xt,xm,dp;

//Promt user to input no. dec places of accuracy
count << "Please input the number of decimal places to which the calculation will be accurate to.. " << endl;
cin >> d;

//Declaration of count var i
int i;

//Setup of initial route bracket and decimal place var dp
xb=0.5;
xt=1.0;
dp=pow(0.1,d);
double ans;

do{
xm=(xb+xt)/2;
ans=f(xm);

if((f(xb)*f(xt))>0){
xb=xm;
}
else{
xt=xm;
}
i++;
}

while ((ans>dp)||(ans<-(dp)));

count << endl << i << " " << xm << " " << ans;

}

Any resolution to this would be greatly appreciated


Thanks

Sol
 
Technology news on Phys.org
It's only by good luck that it even works for d=2 actually.

The error is in the line "if((f(xb)*f(xt))>0)". Here you are testing if f(xb) and f(xt) are the same sign, but you really should be comparing the sign of f(xm) and that of the end points.
 
Thankyou v much
 

Similar threads

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