This is the mail archive of the
gsl-discuss@sources.redhat.com
mailing list for the GSL project.
Re: Some (probably) ridiculous questions
Jean-Max Redonnet writes:
> I think I have defined a surface function type as you proposed.
> This part seems to work as expected.
> but I don't see how to define a function to create the appropriate
> gsl_function...
>
The gsl_function has to be a parametric function. The example below
defines a gsl_function 'line.f' for |F|^2 on a path P(t),
(u(t),v(t)) = (0.5,0.5) + 0.1*(cos(t),sin(t))
and then finds the minimum (which is at t=pi) using the standard gsl
minimization routines. The relevant functions are line_eval() and
make_line_function().
#include <stdlib.h>
#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_diff.h>
#include <gsl/gsl_min.h>
#include <gsl/gsl_errno.h>
#define RAD2DEG(X) (X*180/M_PI)
#define DEG2RAD(X) (X*M_PI/180)
struct surf_function_struct {
double (* fx)(double u, double v);
double (* fy)(double u, double v);
double (* fz)(double u, double v);;
};
typedef struct surf_function_struct surf_function ;
double C0_fx(double u)
{
return 80*u;
}
double C0_fy(double u)
{
return 80*tan(DEG2RAD(22.5))*(u-0.5);
}
double C0_fz(double u)
{
return 40;
}
double C1_fx(double u)
{
return 80*u;
}
double C1_fy(double u)
{
return -80*tan(DEG2RAD(22.5))*(u-0.5);
}
double C1_fz(double u)
{
return -40;
}
double S_fx(double u, double v)
{
return (1-v)*C0_fx(u)+v*C1_fx(u);
}
double S_fy(double u, double v)
{
return (1-v)*C0_fy(u)+v*C1_fy(u);
}
double S_fz(double u, double v)
{
return (1-v)*C0_fz(u)+v*C1_fz(u);
}
struct path_function_struct {
void (* uv)(double t, double *u, double *v);
};
typedef struct path_function_struct path_function ;
void P_uv(double t, double *u, double *v)
{
*u = 0.5 + 0.1 * cos(t);
*v = 0.5 + 0.1 * sin(t);
}
struct line_function_struct
{
path_function p;
surf_function s;
gsl_function f;
};
typedef struct line_function_struct line_function ;
double
line_eval (double t, void * params)
{
line_function * line = (line_function *) params;
double u, v;
line->p.uv(t, &u, &v);
{
double a = line->s.fx(u,v);
double b = line->s.fy(u,v);
double c = line->s.fz(u,v);
/* compute |F|^2 */
return a*a + b*b + c*c;
}
}
void
make_line_function (path_function path, surf_function surf,
line_function * line)
{
line->p = path;
line->s = surf;
line->f.function = &line_eval;
line->f.params = line;
}
int main (void)
{
double u,v,t;
surf_function surf;
path_function path;
surf.fx = &S_fx;
surf.fy = &S_fy;
surf.fz = &S_fz;
path.uv = &P_uv;
for (t = 0 ; t < 2*M_PI; t+= 0.5)
{
path.uv(t, &u, &v);
printf(" | %f \n", S_fx(u,v));
printf(" S(%.3f,%.3f) = | %f \n", u, v, S_fy(u,v));
printf(" | %f \n", S_fz(u,v));
printf(" | %f \n", surf.fx(u,v));
printf(" S(%.3f,%.3f) = | %f \n", u, v, surf.fy(u,v));
printf(" | %f \n", surf.fz(u,v));
}
{
line_function line;
make_line_function(path, surf, &line);
{
int status, iterations = 0;
double m = 1.23;
gsl_interval x = {0.0, 2*M_PI};
gsl_min_fminimizer * s
= gsl_min_fminimizer_alloc (gsl_min_fminimizer_brent);
gsl_min_fminimizer_set (s, &line.f, m, x);
do
{
iterations++;
status = gsl_min_fminimizer_iterate (s);
m = gsl_min_fminimizer_minimum (s);
x = gsl_min_fminimizer_interval (s);
status = gsl_min_test_interval (x, 0.001, 0.0);
if (status == GSL_SUCCESS)
printf ("Converged:\n");
printf ("%5d [%.7f, %.7f] %.7f %.7f %+.7f\n",
iterations, x.lower, x.upper,
m, s->f_minimum, x.upper - x.lower);
}
while (status == GSL_CONTINUE && iterations < 100);
}
}
return 0;
}