Thread Closed

Bisection Method

 
Share Thread Thread Tools
Nov8-08, 05:29 AM   #1
 

Bisection Method


Hi, Ive 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
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Bird's playlist could signal mental strengths and weaknesses
>> Minus environment, patterns still emerge: Computational study tracks E. coli cells' regulatory mechanisms
>> Bacterium uses natural 'thermometer' to trigger diarrheal disease, scientists find
Nov8-08, 08:34 AM   #2
 
Recognitions:
Science Advisor Science Advisor
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.
Nov8-08, 08:50 AM   #3
 
Thankyou v much
Thread Closed
Thread Tools


Similar Threads for: Bisection Method
Thread Forum Replies
Fortra 90 : Error in my program of bisection method Programming & Comp Sci 4
Bisection method General Math 1
Bisection (trigonometry) General Math 1
Show that the Bisection Method converges to... Calculus & Beyond Homework 6
Bisection method of approximation(proof) Calculus 0