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

Plots exercise

parent 57bb0fae
No related branches found
No related tags found
No related merge requests found
#include<math.h>
double Erf(double x){
/// single precision error function (Abramowitz and Stegun, from Wikipedia)
if(x<0) return -Erf(-x);
double a[]={0.254829592,-0.284496736,1.421413741,-1.453152027,1.061405429};
double t=1/(1+0.3275911*x);
double sum=t*(a[0]+t*(a[1]+t*(a[2]+t*(a[3]+t*a[4]))));/* the right thing */
return 1-sum*exp(-x*x);
}
\ No newline at end of file
CFLAGS = $(shell gsl-config --cflags)
LDLIBS = $(shell gsl-config --libs)
default: Makefile erf.gnuplot.png gam.gnuplot.png
erf.gnuplot.png: err_data.txt tabulated_err_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";\
plot\
"$<" using 1:2 with line title "Erf from math.h",\
"$<" using 1:3 with line title "Erf from GSL",\
"$<" using 1:4 with line title "Erf from homemade approx.",\
"tabulated_err_data.txt" using 1:2 with points title "Tabulated data";\
' | gnuplot
gam.gnuplot.png: gam_data.txt tabulated_gam_data.txt
echo '\
set terminal png;\
set output "$@";\
set key top left;\
set tics out;\
set xlabel "x";\
set ylabel "y";\
set title "Gamma function";\
plot\
"$<" using 1:2 with line title "Gamma from math.h",\
"$<" using 1:3 with line title "Gamma from GSL",\
"$<" using 1:4 with line title "Gamma from homemade approx.",\
"tabulated_gam_data.txt" using 1:2 with points title "Tabulated data";\
' | gnuplot
err_data.txt gam_data.txt: main
./main
main: main.c Erf.c stirling.c
.PHONEY: test clean
clean:
$(RM) main err_data.txt gam_data.txt erf.gnuplot.png gam.gnuplot.png
test:
echo $(CFLAGS)
echo $(LDLIBS)
\ No newline at end of file
exercises/plots/erf.gnuplot.png

6.16 KiB

exercises/plots/gam.gnuplot.png

4.93 KiB

#include<stdio.h>
#include<math.h>
#include<gsl/gsl_sf_erf.h>
#include<gsl/gsl_sf_gamma.h>
double Erf(double);
double Gamma(double);
int main() {
FILE* err_stream = fopen("err_data.txt","w");
double xmin=-2,xmax=2;
for(double x=xmin;x<=xmax;x+=1.0/8) {
fprintf(err_stream,"%10g %10g %10g %10g\n",x,erf(x),gsl_sf_erf(x),Erf(x));
}
fclose(err_stream);
FILE* gam_stream = fopen("gam_data.txt","w");
double ymin=0.25,ymax=5;
for(double y=ymin;y<=ymax;y+=1.0/32) {
fprintf(gam_stream,"%10g %10g %10g %10g\n",y,tgamma(y),gsl_sf_gamma(y),Gamma(y));
}
fclose(gam_stream);
return 0;
}
\ No newline at end of file
#include<math.h>
double Gamma(double x){
///single precision gamma function (Gergo Nemes, from Wikipedia)
if(x<0)return M_PI/sin(M_PI*x)/Gamma(1-x);
if(x<9)return Gamma(x+1)/x;
double lnGamma=x*log(x+1/(12*x-1/x/10))-x+log(2*M_PI/x)/2;
return exp(lnGamma);
}
\ No newline at end of file
0 0 1
0.02 0.022564575 0.977435425
0.04 0.045111106 0.954888894
0.06 0.067621594 0.932378406
0.08 0.090078126 0.909921874
0.1 0.112462916 0.887537084
0.2 0.222702589 0.777297411
0.3 0.328626759 0.671373241
0.4 0.428392355 0.571607645
0.5 0.520499878 0.479500122
0.6 0.603856091 0.396143909
0.7 0.677801194 0.322198806
0.8 0.742100965 0.257899035
0.9 0.796908212 0.203091788
1 0.842700793 0.157299207
1.1 0.88020507 0.11979493
1.2 0.910313978 0.089686022
1.3 0.934007945 0.065992055
1.4 0.95228512 0.04771488
1.5 0.966105146 0.033894854
1.6 0.976348383 0.023651617
1.7 0.983790459 0.016209541
1.8 0.989090502 0.010909498
1.9 0.992790429 0.007209571
2 0.995322265 0.004677735
2.1 0.997020533 0.002979467
2.2 0.998137154 0.001862846
2.3 0.998856823 0.001143177
2.4 0.999311486 0.000688514
2.5 0.999593048 0.000406952
3 0.99997791 0.00002209
3.5 0.999999257 0.000000743
1 1
2 1
3 2
4 6
5 24
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