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

In summary, the speaker has written code to resolve the routes of a function using the bisection method, but it only works for the current route brackets and a decimal place value of 2. When the decimal place value is changed, the loop gets stuck. The speaker has requested help in resolving this issue.
  • #1
SolStis
12
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
cout << "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)));

cout << endl << i << " " << xm << " " << ans;

}

Any resolution to this would be greatly appreciated


Thanks

Sol
 
Technology news on Phys.org
  • #2
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.
 
  • #3
Thankyou v much
 

What is the Bisection Method?

The Bisection Method is a numerical procedure used to find the root of a function by repeatedly dividing the interval in which the root lies and determining which subinterval contains the root.

How does the Bisection Method work?

The Bisection Method involves choosing an interval [a, b] where the function changes sign, and then repeatedly dividing the interval in half until the root is found within a desired level of accuracy.

What are the advantages of using the Bisection Method?

The Bisection Method is simple to implement and guarantees convergence to a root as long as the function is continuous and changes sign within the given interval. It is also relatively immune to rounding errors.

What are the limitations of the Bisection Method?

The Bisection Method can be slow to converge, especially for functions with complex roots or multiple roots. It also requires that the function changes sign within the given interval, which may not always be the case.

How do I know when to stop the Bisection Method?

The Bisection Method can be stopped when the interval becomes sufficiently small, or when the difference between the upper and lower bounds of the interval is smaller than a predefined tolerance. It is also important to monitor the number of iterations to ensure that the method is not taking too long to converge.

Similar threads

  • Programming and Computer Science
Replies
1
Views
613
  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
23
Views
6K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
4
Views
8K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
4K
Back
Top