bisection method to find roots
Mar 16, 2010 at 1:51am UTC
I've worked out a method to solve the roots and too print the root. but I want to show the values at each iteration step. every time i seem to do something i mess the rest of my code up. how would i go about doing that? this is what i have currently.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include<iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
double f(double x);
double bisection(double xa, double xb, double eps);
int main()
{
double x0 = 0.0, xn = 1.0, eps = 0.000001;
int n = 10000;
cout<< "Root value: " << bisection(x0, xn, eps)<<endl;
return 0;
}
double f(double x)
{
double y;
y = x-(.2*sin(x))-0.5;
return y;
}
double bisection(double xa, double xb, double eps)
{
double xm, fa, fm;
fa = f(xa);
while (xb - xa > eps)
{
xm = (xa + xb) / 2;
fm = f(xm);
if (fm == 0.0)
break ;
else if (fa*fm > 0)
{
xa = xm;
fa = fm;
}
else {
xb = xm;
}
}
return xm;
}
Topic archived. No new replies allowed.