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
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top