Commit 22ff97f8 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Worked on ass4.

parent 836e82fe
...@@ -3,10 +3,35 @@ ...@@ -3,10 +3,35 @@
#include <math.h> #include <math.h>
#include "func_ptr.h" #include "func_ptr.h"
double newton(func_ptr f, func_ptr df, double x_start) { #define EPSILON 1e-11
double bisec_limited(func_ptr f, double left, double right, int max_steps) {
int i;
double mid;
return 0; for( i = 0; fabs(right - left) > 2 * EPSILON && i < max_steps; i++ ) {
mid = (right + left) / 2;
if( f(left) * f(mid) < 0 )
right = mid;
else if( f(right) * f(mid) < 0 )
left = mid;
else
break;
}
return mid;
}
double newton(func_ptr f, func_ptr df, double x_start, int *steps) {
double x = x_start, last_x = x - 1;
for( *steps = 0; x != last_x; (*steps)++ ) {
last_x = x;
x -= f(x) / df(x);
}
return x;
} }
double f1(double x) { double f1(double x) {
...@@ -34,7 +59,9 @@ double df3(double x) { ...@@ -34,7 +59,9 @@ double df3(double x) {
} }
int main(void) { int main(void) {
printf("f1: %.11f\n", newton(&f1, &df1, 1)); int steps;
printf("f1: %.11f (%d steps)\n", newton(&f2, &df2, -100, &steps), steps);
return 0; return 0;
} }
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