double mylog(double x){
assert(x>=0);
if(x==0) return -INFINITY /* defined in math.h */
if(x<1) return -mylog(1/x);
if(x>10) return mylog(x/10)+mylog(10);
do_your_stuff;
return the_calculated_value_of_the_logarithm_of_x ;
}
You might want to precalculate ln(10) for a bit better efficiency. But
do not do any more optimizations. Clarity is the keyword.
make your project before archiving
and submitting.
double mylog(double x){
do_your_stuff;
return the_calculated_value_of_the_logarithm_of_x ;
}
The "main" function should not implement any integrals, it should only do
its "main thing", like tabulate the implemented function:
int main(void){
double x,xstart=0.1,xstop=13,dx=0.1;
for(x=xstart;x<=xstop;x+=dx)
printf("%g %g\n", x, mylog(x) );
return 0;
}
Of course for a log-scale plot you might be better off using
a geometric sequence
for(x=xstart;x<=xstop;x*=factor)
Listen carefully, here comes an
important point:
when you integrate ordinary
differential equations (ODE) with gsl_odeiv2_driver you do not
have to do it in many small intervals, you should do it in one run from
start-point to stop-point. The driver automatically chooses the number of
steps and the approptiate
step-sizes in order to reach the stop-point with the desired accuracy.
In my example (Examples/gsl-odeiv) I only did the integration in many small steps in order to tabulate the function for the subsequent plot. It was a very unpedagogical quick-and-dirty example and I have already corrected it. My bad, sorry for that.
PROG 2014preferably from you University account. You have to prepare the examination project in a separate directory, say,
YourInitials_Exam_Dir; then make it; then
create an archive with the following commands (assuming that you are in
YourInitials_Exam_Dir):
cd ..
tar -cvzf yourinitials_exam.tgz YourInitials_Exam_Dir
The archive yourinitials_exam.tgz should then be attached to
the PROG 2014 email.
If your mailer refuses to send executable files, delete the executable files
in your directory and make the archive without executable files.
Group 0
29.01
31.01
05.02
Freddy Black
X
X
X
Helle Gucci
X
X
X
Lars Luksus
X
X
X
The prerequisite for the examination is "active participation" which
includes, say, 80% presence at the lectures. In case you have got
particular circumstances preventing you from attending the lectures,
you can talk to me and a get a dispensation.
FLT_EPSILON.
Some students find that float
expressions are actually calculated as double or even
as long double. Indeed it turns out that an impementation
dependend built-in variable FLT_EVAL_METHOD
determines the float-evaluation-method (read the C99
IEEE 754 floating point support wikipedia article). In short, if for
your implementation
FLT_EVAL_METHOD==0
the float is evaluated as float,
if FLT_EVAL_METHOD==1 float is evaluated as double, and
if FLT_EVAL_METHOD==2 float is evaluated as long double.
On my box (an aluminum iMac)
the following code (to be compiled with gcc it needs the option
-std=c99)
#include<stdio.h>
#include<float.h>
int main(){
float f=1; while(1+f!=1){f/=2;}f*=2; printf("f=%g\n",f);
double d; for(d=1;1+d!=1;d/=2){}d*=2; printf("d=%lg\n",d);
long double l; for(l=1;1+l!=1;l/=2){}l*=2; printf("l=%Lg\n",l);
printf("FLT_EVAL_METHOD=%i\n", FLT_EVAL_METHOD);
return 0;
}
produces the following output:
f=1.19209e-07
d=2.22045e-16
l=1.0842e-19
FLT_EVAL_METHOD=0
as one would expect. It does the same on lifa, molveno, and grendel.
However, on my router at home the same code produces the output
f=1.0842e-19
d=1.0842e-19
l=1.0842e-19
FLT_EVAL_METHOD=2
Therefore, if your FLT_EVAL_METHOD
is not zero, you have to do the exercise with explicit casts (also called
explicit
type conversions), like
(float), as illustrated in the wikipedia article Machine epsilon -- Approximation using C.
ssh to your account.
cd,
cp,
ls,
mkdir,
mv,
rm.
sudo apt-get install nano
(or your other favourite editor) and learn how to create, save, and edit
text files with it.
sudo apt-get install
gcc.
printf function;
tgmath.h.
malloc;
passing arrays as parameters to functions.
main invocation.
atoi (ascii to integer) and atof (ascii
to double) functions from <stdlib.h>.
sudo apt-get --yes install gnuplot
sudo apt-get --yes install pyxplot gv
The --yes option automatically answers 'yes' on all questions
apt-get might ask.
Some of you have managed to install a special version of gnuplot,
called gnuplot-nox where 'nox' means 'no X-window'. This
version, predictably, has no x-window terminal. If you plan to use
gnuplot interactively, you have to install 'gnuplot', not 'gnuplot-nox'.
The two plotting utilities have very similar scripting languages but otherwise are somewhat different. Pyxplot uses LaTeX to render all text on the plot which is rather convenient. Gnuplot has more terminals. Look at their web-pages, gnuplot and pyxplot (specifically, the examples), and choose the one you like most (or both).
sudo apt-get install libgsl0-dev gsl-bin
if I remember correctly.
gsl_odeiv2_system with the fields
function, jacobian, dimension, params to hold an
Ordinary Differential Equations Initial Value System.
gsl_odeiv2_step_rk2,
gsl_odeiv2_step_rk4,
gsl_odeiv2_step_rk45, ... ,
gsl_odeiv2_step_bsimp. Note which of the algorithms require the
Jacobian of the system.
gsl_odeiv2_driver_alloc_y_new
gsl_odeiv2_driver_apply
gsl_function
gsl_integration_qags
gsl_integration_qagiu
gsl_integration_qagil
gsl_multimin_function
gsl_multimin_fminimizer
gsl_multimin_fminimizer_nmsimplex2
gsl_multimin_fminimizer_alloc
gsl_multimin_fminimizer_set
gsl_multimin_fminimizer_iterate
gsl_multimin_fminimizer_size
gsl_multimin_fminimizer_test_size
gsl_multiroot_function
gsl_multiroot_fsolver_alloc
gsl_multiroot_fsolver_free
gsl_multiroot_fsolver_set
gsl_multiroot_fsolver_iterate
gsl_multiroot_test_residual
sudo apt-get install texlive-latex-extra
#include<assert.h> and use assertions plentifully:
you can easily disable all assertions by simply
recompiling the code with -DNDEBUG c-flag:
CFLAGS += -DNDEBUG
...inserting printf commands that output more or less carefully chosen status information at key points in the program flow, observing that information and deducing what's wrong based on that information. ...Although often looked down on by users of modern debugging software, printf() debugging continues to prove itself indispensable.
fprintf(stderr,...) command at the strategic
points of the program and localize the source of the error.
TRACE macro
(read the C
preprocessor chapter again)
and use it instead of directly using fprintf function. The
macro can then be simply disabled after debugging. For example:
#ifndef NDEBUG
#define TRACE fprintf
#else
#define TRACE(...)
#endif
or, more elegantly,
#ifndef NDEBUG
#define TRACE(...) fprintf(stderr,__VA_ARGS__)
#else
#define TRACE(...)
#endif