The Gnu Scientific Library (GSL) is a public domain package with a wide variety of codes for doing scientific calculations, such as Fourier transforms, wavelet transforms, root finding, and least squares fitting among others.

Documentation for the library is available from the Gnu software site. Local documentation is available in the Gnu info system.

- Find the routines you want.

Look through the index in the Gnu software manual or look through the**gsl-ref**menu in the Gnu info system and select the application you want. Then study the example code. You may need to use several subprogram functions in the package.

- Understand what is inside the black box

Be sure you understand the numerical methods used in the package routine, and know their limitations. The manual explains what they do.

- Find out what arguments are needed for the routine you have
chosen.

The details are given in the function index or under**gsl-ref**->**Function Index**of the Gnu info system.

- Compile your program making sure you list the libraries you need.
For example

g++ myprog.cc -o myprog -lgsl -lgslcblas -lm

- Be skeptical

It is always a good idea to check your results against problems with known solutions before trusting that you have understood how to make the black box work.

Suppose you want to fit a straight line to a set of

int gsl_fit_wlinear (const double * x, const size_t xstride, const double * w, const size_t wstride, const double * y, const size_t ystride, size_t n, double * c0, double * c1, double * cov00, double * cov01, double * cov11, double * chisq)and the explanation of the parameters. The stride parameter is usually 1. Setting it to 2 causes the fitter to use every other value in the list, beginning with the first (??).

Armed with this information we write our code as follows:

#include <iostream> #include <gsl/gsl_fit.h> #define MAX 100 int main(){ int i, n; double x[MAX], y[MAX], e[MAX]; double c0, c1, cov00, cov01, cov11, chisq; // Read the values of the matrix std::cout << "Enter n \n"; std::cin >> n; std::cout << "On each line type x y sigma:\n"; for(i = 0; i < n; i++){ for(i = 0; i < n; i++)std::cin >> x[i] >> y[i] >> e[i]; if(std::cin.fail()) { std::cout << "Failed to read data\n"; return 1; } } // Fit the straight line gsl_fit_wlinear (x, 1, w, 1, y, 1, n, &c0, &c1, &cov00, &cov01, &cov11, &chisq); // Write the answer std::cout << "Fit to y = m*x + b gives\n"; std::cout << "m = " << c1 << " +/- " << sqrt(cov11) << "b = " << c0 << " +/- " << sqrt)cov00) << "\n"; std::cout << "chisq = " << chisq << " for " << n-2 << "df\n"; return 0; }Note that we are not required to use the same names as given in the prototype.

Physics Department Home Page

*This page is maintained by:
*