Skip to content
Snippets Groups Projects
Commit 09296a02 authored by Lucas Christesen Ahler's avatar Lucas Christesen Ahler
Browse files

gsl-integ exercise

parent 2c4bae05
No related branches found
No related tags found
No related merge requests found
CFLAGS = -Wall -Werror -O1
CFLAGS += $(shell gsl-config --cflags)
LDLIBS += $(shell gsl-config --libs)
CC = gcc
default: integral erf.gnuplot.png Makefile
./$<
erf.gnuplot.png: erf_data.txt
echo '\
set terminal png;\
set output "$@";\
set key bottom right;\
set tics out;\
set xlabel "x";\
set ylabel "y";\
set title "Error function";\
set grid ytics xtics;\
show grid;\
plot\
"$<" using 1:2 with line title "Erf via integration",\
' | gnuplot
erf_data.txt: erf
./$< > ./$@
.PHONEY: clean
clean:
$(RM) erf integral erf.gnuplot.png erf_data.txt
\ No newline at end of file
#include<stdio.h>
#include<math.h>
#include <gsl/gsl_integration.h>
double integrand(double x, void* params) {
return exp(-pow(x,2));
}
double erf(double b) {
gsl_function F;
F.function = &integrand;
int limit = 9999;
gsl_integration_workspace* w;
w = gsl_integration_workspace_alloc(limit);
double a=0,acc=1e-6,eps=1e-6,result,error;
gsl_integration_qags(&F,a,b,acc,eps,limit,w,&result,&error);
gsl_integration_workspace_free(w);
return result;
}
int main() {
double xmin=-3,xmax=3,dx=1.0/1000;
for(double x=xmin;x<=xmax;x+=dx) {
printf("%10g%10g\n",x,erf(x));
}
return 0;
}
\ No newline at end of file
exercises/gsl-integ/erf.gnuplot.png

5.71 KiB

#include<stdio.h>
#include<math.h>
#include <gsl/gsl_integration.h>
double my_f(double x, void* params) {
return log(x)/sqrt(x);
}
int main() {
gsl_function F;
F.function = &my_f;
int limit = 9999;
gsl_integration_workspace* w;
w = gsl_integration_workspace_alloc (limit);
double a=0,b=1,acc=1e-6,eps=1e-6,result,error;
gsl_integration_qags(&F,a,b,acc,eps,limit,w,&result,&error);
printf("Integralet giver: %g med fejl %g\n",result,error);
gsl_integration_workspace_free(w);
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment