FindMinumum and FindRoot for 1D functions.

Code Generation for FindMinimum[ ] and FindRoot[ ] 
of a 1D function using MathCode

The function FindMinimum[]  finds a local minumum of given function and given starting value. Function cannot be translated by MathCode directly because
--- its argument is a symbolic expression 
 --- numerical algorithms for minimization are not included in MathCode runtime library
 The solution is to write a Mathematica function which replicates FindMinimum[ ] operation using compilable subset.
 This notebook illustrates how to do that for finction with one argument. 
 FindRoot[ ] can be implemented using FindMinumum[ ] as well.

This is how FindMinumum works in Mathematica

FindMinimum[Cos[x], {x, 1}]

Needs["MathCode`"]

SetDirectory[
      ToFileName[{$MCRoot, "Demos", "FindMinimum"}]]

This function LookupMin[ ] replicates Mathematica FindMinimum[ ]. The arguments are: 
iters - maximal number of iterations
k - trial step multiplier
alphastart - a small number needed for computation of approximation of derivatives
startp - start value for search
accuracy - algorithm stoops when steps are less than accuracy
debug - whether additional debugging is needed.
The minimized function name is not passed to LookupMin[ ]. The name is always Func[ ]. Later we will see how to adjust this name "on the flight".

LookupMin[Integer iters_, Real k_, 
          Real alphastart_, Real startp_, Real accuracy_, 
          Integer debug_] -> {Real, 
          Real} := 
Module[{Real p = startp, 
          Integer i = 0, 
          Real currentAccuracy = 99999. , 
Real alpha = 
            alphastart, 
Real df}, 
While[(i < iters) && (currentAccuracy > 
                accuracy), 
df = (Func[p + alpha] - 
                  Func[p])/alpha; 
p = 
            p - k*df; 
alpha = 
            Abs[k*df]/2; 
i = 
            i + 1; 
currentAccuracy = 
            Abs[k*df]; 
If[debug > 0, 
            Print[{Func[p], p, 
                currentAccuracy}]];
]; 
{p, 
          Func[p]}

]

Functions to minimize

Func1[Real u_] -> Real := Cos[u] + 4;

Func2[Real u_] -> Real := u Sin[u + 1];

Plot[Func2[u], {u, -2, 2}]

Try to minimize in Mathematica

mm = FindMinimum[Func2[v], {v, -1}]

Try to minimize using our LookupMin function

Func[u_] := Func2[u];

lm = LookupMin[500, 0.1, 0.01, -1. , 0.0000001, 0]

Print[{mm[[1]] - 
          lm[[2]], (v /.  
              mm[[2]])  - 
          lm[[1]]}]

Compile LookupMin to C++. Note that we use Func2 instead of Func replacing it on the flight.

BuildCode[MacroRules -> {Func -> Func2}]

InstallCode[]

cm = LookupMin[500, 0.1, 0.01, -1. , 0.0000001, 0]

How accurate are C++ computations ?

Print[{mm[[1]] - 
          cm[[2]], (v /.  
              mm[[2]])  - 
          cm[[1]]}]

UninstallCode[];

Using FindRoot[ ]

Finding the root in Mathematica. This function is normally used for solving non-linear equations.

mathroot = x /. FindRoot[Sin[x] == Log[x], {x, 0.5}]

The problem is reduced to finding minumum of the residual function.

FindMinimum[(Sin[x] - Log[x])^2, {x, 0.5}]

Prepare the resifual function for MathCode compilation

Func3[Real u_] -> Real := (Sin[u] - Log[u])^2;

Compute the result

BuildCode[MacroRules -> {Func -> Func3}];

InstallCode[];

croot = LookupMin[500, 0.1, 0.01, 0.5, 0.0000001, 
          0][[1]]

Check accuracy

croot - mathroot

UninstallCode[];

CleanMathCodeFiles[Confirm -> False, CleanAllBut -> {}];