Commit 9fa04dd8 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Movedd Newton-Raphson method to header file.

parent d431e629
#ifndef FUNC_PTR_H
#define FUNC_PTR_H
typedef double (*func_ptr)(double);
#endif
#include "func_ptr.h"
double newton_raphson(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)++ ) {
if( *steps == MAX_STEPS )
return NAN;
last_x = x;
x -= f(x) / df(x);
}
return x;
}
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "func_ptr.h"
#include "newton_raphson.h"
#define EPSILON 1e-11
#define MAX_STEPS 1000
#define BISEC_STEPS 5
double bisec_limited(func_ptr f, double left, double right, int max_steps) {
int i;
double mid;
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_raphson(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)++ ) {
if( *steps == MAX_STEPS )
return NAN;
last_x = x;
x -= f(x) / df(x);
}
return x;
}
/*
* Manually define the functions and their derivatives.
*/
......
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