MS Research PhD Research Curriculum Vitae
On-line Stores Cycling Medicine & Health LaTeX OOP & C++ Sony PCM-R500 DAT |
![]() |
Next: The remedy variable number Up: Core flow implementation Previous: Main calculational procedure, core_hydraulics   Contents   Index Implementing function-functionsFor the problems we encounter the only function-functions8.3 we are forced to implement include some of the numerical functions, like
The problem is how to deal with the fixed parameters of the functions which are
passed to the function-functions as input. The author found this subject very
important since I could not find any literature which treated this
subject and furthermore, the implementations the author have seen simply assumes
functions to be of the form
Let us discuss the problem by considering the following simple example. Suppose
we want to solve the non-linear equation
where x is the independent variable and
for some fixed We would like an implementation of a non-linear solver to work on a general non-linear function f(x). This is accomplished by passing a function pointer to the non-linear solver, ie the C-prototype for the solver could look something like
double solver( double guess, double (*FCN) (double));
The problem with this prototype is that the solver can only solve functions
which have x as the only argument.
The remedy could be to introduce a global variable as shown below.
The function
double f_alpha(double x, double alpha);
We make the following dummy function
double dummy_f_alpha(double x)
{
return f_alpha(x,GLOBAL_ALPHA);
}
The main program which solves f_alpha for a fixed
double GLOBAL_ALPHA; //Define a global alpha-value.
double solve_f_alpha(double alpha)
{
GLOBAL_ALPHA = alpha;
return solve(0.0, dummy_f_alpha);
}
The method described above has the drawback that we have to use a global variable. Hence this implementation is prone to unpleasant and erroneous side effects. Furthermore it is rather clumsy to drag around with all these global variables--in practice the number of parameters (which of course do not have to be of same type) may exceed, say, 20. As far as the author knows there is only one remedy--this is the subject of the next section.
Next: The remedy variable number Up: Core flow implementation Previous: Main calculational procedure, core_hydraulics   Contents   Index Revision 2.0, Copyright © 1999-2004 Jakob Christensen http://www.JakobCHR.com E-Mail: webmaster@JakobCHR.com
|