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

AI Thread Summary
The discussion centers on a coding issue related to implementing the bisection method for finding function roots. The code successfully operates with a decimal precision of 2 but encounters problems when the precision is adjusted. The main issue identified is in the conditional statement that checks if the function values at the endpoints (xb and xt) have the same sign. The correct approach should involve comparing the sign of the function value at the midpoint (xm) with those at the endpoints. This adjustment is crucial for the algorithm to function correctly across different decimal precision settings.
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
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
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top