Upload
smarky0
View
214
Download
0
Embed Size (px)
DESCRIPTION
this is a part of my code using c++ for Brent's method in root finding.
Citation preview
BRENT’S METHOD
#include <stdio.h>#include <math.h>#include <time.h>
int main (void){ clock_t begin, end; double f(double x);
double time_spent,a,b,c,s,tol,root; int iter;
begin = clock ();
tol = 1.0e-10; iter = 0; a = 2.3; b = 3.4;
do { if ((f(a)*f(b))>0) { printf("There is no root in the given interval. \n"); return 0; } else { c=(a+b)/2; } if((f(a)!=f(c))&&(f(b)!=f(c))) { s=(a*f(b)*f(c))/((f(a)-f(b))*(f(a)-f(c)))+(b*f(a)*f(c))/((f(b)- f(a))*(f(b)-f(c)))+(c*f(a)*f(b))/((f(c)-f(a))*(f(c)-f(b))); } else { s=b-f(b)*((b - a)/(f(b)-f(a))); } if (c<s) { c=c; s=s; } else { c=s; s=c; }
if((f(c)*f(s))<0) { a=s; b=c; }
else { if((f(s)*f(b))<0.0) a=c; else b=s; } iter++;
} while ((fabs(a-b)>=tol));