From 2125f01f97abfe343fc5e0cc078bf1d081b2e441 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 20 May 2016 20:30:15 +0000 Subject: Initial commit --- doc/libinum-examples/src/root_newton.c | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 doc/libinum-examples/src/root_newton.c (limited to 'doc/libinum-examples/src/root_newton.c') diff --git a/doc/libinum-examples/src/root_newton.c b/doc/libinum-examples/src/root_newton.c new file mode 100644 index 0000000..f307c58 --- /dev/null +++ b/doc/libinum-examples/src/root_newton.c @@ -0,0 +1,58 @@ +#include +#include +// define MPFR_USE_VA_LIST to enable the use of mpfr_inits and mpfr_clears +#define MPFR_USE_VA_LIST +#include +#include + +int func(mpfr_t* out, mpfr_t in, void* extra_args); +int dfunc(mpfr_t* out, mpfr_t in, void* extra_args); + +int main(int argc, const char* argv[]){ + int ret; + mpfr_t init, prec, x; + unsigned int maxiter; + int extra_arg; + + mpfr_inits(init, prec, x, NULL); + + // start at 2 + mpfr_set_ui(init, 2, MPFR_RNDN); + // precision + mpfr_set_d(prec, 1.e-30, MPFR_RNDN); + // maximum number of iterations before error + maxiter=10000; + // extra argument + extra_arg=4; + + // compute root + ret=root_newton_mpfr(&x, &func, &dfunc, init, prec, maxiter, &extra_arg); + + // return codes + if(ret==LIBINUM_ERROR_MAXITER){ + printf("error: maximum number of iterations reached\n"); + } + else if(ret==LIBINUM_ERROR_NAN){ + printf("error: infinity encountered\n"); + } + else{ + mpfr_printf("% 14.7Re\n", x); + } + + mpfr_clears(init, prec, x, NULL); + + return(0); +} + +// x^2-1 +int func(mpfr_t* out, mpfr_t in, void* extra_args){ + mpfr_pow_ui(*out, in, 2, MPFR_RNDN); + mpfr_add_d(*out, *out, -1./ *((int*)extra_args), MPFR_RNDN); + return(0); +} + +// 2*x +int dfunc(mpfr_t* out, mpfr_t in, void* extra_args){ + mpfr_mul_ui(*out, in, 2, MPFR_RNDN); + return(0); +} -- cgit v1.2.3-70-g09d2