help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] How to find complex roots of a nonlinear function?


From: bowlder
Subject: [Help-gsl] How to find complex roots of a nonlinear function?
Date: Sat, 28 Apr 2007 15:43:20 +0800

Hello, all.
I want to know how to fnd the complex roots of a nonlinear function.
Eg. Find the first five complex roots of "x*tanh(x)-a=0", where a is an
argument.

As to real function, by the following code, I can find the root.
But for finding complex roots(the first five roots), how to change the code?

Thank you.

[CODE]
#include<stdio.h>
#include<gsl/gsl_errno.h>
#include<gsl/gsl_math.h>
#include<gsl/gsl_roots.h>

struct func_params
{
 double a;
};

double func(double x,void *params);
double func_deriv(double x,void *params);
void func_fdf(double x,void *params,double *y,double *dy);

double
func(double x, void *params)
{
 struct func_params *p=(struct func_params *) params;

 double a=p->a;

 return x*tanh(x)-a;

}

double
func_deriv(double x,void *params)
{
 struct func_params *p=(struct func_params *) params;

 double a=p->a;

 return tanh(x)+x*pow(1/cosh(x),2);

}

void
func_fdf(double x,void *params,double *y,double *dy)
{
 struct func_params *p=(struct func_params *) params;

 double a=p->a;

 *y=x*tanh(x)-a;
 *dy=tanh(x)+x*pow(1/cosh(x),2);

}



int
main(void)
{
 int status;
 int iter=0,max_iter=100;
 const gsl_root_fsolver_type *T;
 gsl_root_fsolver *s;

 double x0,x=5.0;
 double r_expected=sqrt(5.0);
 double a=0.73;

 gsl_function_fdf FDF;
 struct func_params params={a};

 FDF.f=&func;
 FDF.df=&func_deriv;
 FDF.fdf=&func_fdf;
 FDF.params=&params;

 T=gsl_root_fdfsolver_newton;
 s=gsl_root_fdfsolver_alloc(T);
 gsl_root_fdfsolver_set(s,&FDF,x);

 printf("using %s method\n",gsl_root_fdfsolver_name(s));

 printf("%5s %10s %10s %10s\n","iter","x","err","err(est)");

 do
 {
   iter++;
   status=gsl_root_fdfsolver_iterate(s);
   x0=x;
   x=gsl_root_fdfsolver_root(s);
   status=gsl_root_test_delta(x,x0,0,0.001);

   if(status==GSL_SUCCESS)
     printf("Converged:\n");

   printf("%5d %10.7f %+10.7f %10.7f\n",iter,x,x-r_expected,x-x0);
 }

 while(status==GSL_CONTINUE&&iter<max_iter);

 gsl_root_fdfsolver_free(s);

 return status;

}
[/CODE]


reply via email to

[Prev in Thread] Current Thread [Next in Thread]