demo_fn.h
Código c++:
Ver original
struct parametros_cuadratica { double a,b,c; }; double cuadratica(double x, void *parametros); double deriv_cuadratica(double x, void *parametros); void cuadratica_fdf(double x, void *parametros, double *y, double *dy);
demo_fn.cpp
Código c++:
Ver original
// demo_fn.cpp // // Copyright 2009 Cristobal Lopez // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, // MA 02110-1301, USA. #include <iostream> double cuadratica(double x, void *parametros) { struct parametros_cuadratica *p = (struct parametros_cuadratica *) parametros; double a = p->a; double b = p->b; double c = p->c; return (a*x+b)*x+c; } double deriv_cuadratica(double x, void *parametros) { struct parametros_cuadratica *p = (struct parametros_cuadratica *) parametros; double a = p->a; double b = p->b; double c = p->c; return 2.0*a*x+b; } void cuadratica_fdf(double x, void *parametros, double *y, double *dy) { struct parametros_cuadratica *p = (struct parametros_cuadratica *) parametros; double a = p->a; double b = p->b; double c = p->c; *y = (a*x+b)*x+c; *dy = 2.0*a*x+b; }
main.cpp
Código c++:
Ver original
// main.cpp // // Copyright 2009 Cristobal Lopez // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, // MA 02110-1301, USA. #include <iostream> #include <gsl/gsl_errno.h> #include <gsl/gsl_math.h> #include <gsl/gsl_roots.h> #include "demo_fn.h" #include "demo_fn.cpp" using namespace std; int main(int argc, char** argv) { int estado, iter = 0, max_iter = 100; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; double x0 = 0.0, x_hi = 5.0; gsl_function F; struct parametros_cuadratica parametros = {1.0, 0.0, -5.0}; F.function = &cuadratica; F.parametros = ¶metros; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc(T); gsl_root_fsolver_set(s, &F, x0, x_hi); cout<<"Utilizando el metodo"<<gsl_root_fsolver_name (s)<<endl; "raiz", "error", "error(est)"); do { iter++; estado = gsl_root_fsolver_iterate (s); r = gsl_root_fsolver_root(s); x0 = gsl_root_fsolver_x_lower(s); x_hi = gsl_root_fsolver_x_upper(s); estado = gsl_root_test_interval(x0, x_hi, 0, 0.001); if(estado == GSL_SUCCESS) cout<<"Solucion"<<endl; r-r_esperada, x_hi-x0); } while(estado == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free(s); return estado; return 0; }
Y el mensaje de error es el siguiente:
Cita:
Las opciones de compilación con GSL son:main.cpp: In function ‘int main(int, char**)’:
main.cpp:43: error: ‘struct gsl_function’ no tiene un miembro llamado ‘parametros’
main.cpp:43: error: ‘struct gsl_function’ no tiene un miembro llamado ‘parametros’
Cita:
Las GSL son estas librerias: buscad en google porque el foro no me deja poner el enlace por motivos de antispam.g++ -Wall -c "main.cpp" -lgsl -lgslcblas -lm
Si alguien me pudiese echar un cable se lo agradecería, ya que aprendo GSL por mi cuenta en mis ratos libres y no tengo a quien consultar.
Muchas gracias por adelantado