JakobCHR.com
 
Quick Navigation:
 
Personal:
 Go to Home
 MS Research
 PhD Research
 Curriculum Vitae

General:
 Linux

Soon to come:
 Matlab
 On-line Stores
 Cycling
 Medicine & Health
 LaTeX
 OOP & C++
 Sony PCM-R500 DAT


next up previous contents index
Next: The remedy variable number Up: Core flow implementation Previous: Main calculational procedure, core_hydraulics   Contents   Index

Implementing function-functions

For the problems we encounter the only function-functions8.3 we are forced to implement include some of the numerical functions, like

  • Functions which solve one non-linear equation.
  • Function which solve a system of non-linear equations.

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 $f(\hspace{0.2ex}\underline{x}{}\hspace{0.15ex})$ which implies that no parameters are allowed. This explains the detailedness of the discussion.

Let us discuss the problem by considering the following simple example. Suppose we want to solve the non-linear equation

\begin{displaymath}
f(x;\alpha) = 2(x+\alpha)^3-x^2+7x-15 = 0
\end{displaymath} (8.10)

where x is the independent variable and $\alpha$ is a fixed parameter $((x,\alpha) \in \Re)$, ie we want to find x0 iteratively such that

\begin{displaymath}
f(x_0;\alpha) = 0
\end{displaymath} (8.11)

for some fixed $\alpha$.

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 $f(x;\alpha)$ has the following prototype

        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 $\alpha$ could have the following appearance

        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 up previous contents index
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
Top Quality
Developed with

Danish
Brain Power
Linux Powered!