Commit cf6a9736 authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSim: Added max_steps parameter to bisection method.

parent dfcc5bcb
#include "bisection.h"
double bisec(func_ptr f, double left, double right,
double epsilon, unsigned int *steps) {
double mid, fmid;
double epsilon, unsigned int *steps, unsigned int max_steps) {
double mid = left, fmid;
for( *steps = 0; fabs(right - left) > 2 * epsilon; (*steps)++ ) {
for( *steps = 0; (fabs(right - left) > 2 * epsilon)
&& (!max_steps || (*steps < max_steps)); (*steps)++ ) {
mid = (right + left) / 2;
if( f(left) * (fmid = f(mid)) < 0 )
right = mid;
else if( f(right) * fmid < 0 )
left = mid;
else
else {
(*steps)++;
break;
}
}
return mid;
}
......@@ -5,6 +5,6 @@
#include "func_ptr.h"
double bisec(func_ptr f, double left, double right,
double epsilon, unsigned int *steps);
double epsilon, unsigned int *steps, unsigned int max_steps);
#endif
......@@ -20,7 +20,7 @@ int main(int argc, char *argv[]) {
end = atoi(argv[2]);
for( i = begin; i <= end; i++ ) {
bisection = bisec(&func, 0, 2, pow(10, -1.0 * i), &steps);
bisection = bisec(&func, 0, 2, pow(10, -1.0 * i), &steps, 100000);
printf("zero point: %.30f for epsilon = 1e-%d (%d steps)\n", bisection, i, steps);
}
......
......@@ -27,7 +27,7 @@ int main(int argc, char *argv[]) {
for( i = begin; i <= end; i++) {
epsilon = pow(10, -1.0 * i);
tmp= bisec(&f, 1, 2, epsilon, &steps);
tmp= bisec(&f, 1, 2, epsilon, &steps, 100000);
printf("Sqrt(2) using bisection: %.20f (%d steps; epsilon=%.0e)\n",
tmp, steps, epsilon);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment