14

3

I want to compile a variety of commands, including a function. I need to map this function, inside the compilation, and I can't include the function without facing errors such as "*CompiledFunction::cfex: Could not complete external evaluation at instruction 27; proceeding with uncompiled evaluation.*"

The code finds the root of a function, it's a complicated way to do it but as I want to compute roots of complicated functions I can't simply use *FindRoot*.

For simplicity I'll include an example in which I try to evaluate the root of $y^3+5y^2−5$, such as 0.919089. Note that the problem is in the line "*gg[y_] = y^3 + 5 y^2 - 5;*"

```
Raiz = Compile[{{xmin, _Real}, {xmax, _Real}, {npts, _Real}},
Module[{xrange, tab, par, roots, pos, xtab, xt, xmin2, xmax2, xmed},
xrange = With[{d1 = N@xmin, d2 = N@xmax},
d1+(d2-d1)/(npts-1) Range[0,npts-1]];
gg[y_] = y^3 + 5 y^2 - 5;
tab = Map[gg, xrange];
par = Partition[tab, 2, 1];
roots = Flatten@Select[par, NonPositive[Times @@ #] &];
pos = Flatten[Position[tab, #] & /@ roots];
xtab = Partition[xrange[[pos]], 2];
Table[
xmin2 = xt[[1]]; xmax2 = xt[[2]];
Do[
xmed = N@(xmax2 + xmin2)/2;
If[Sign[gg[xmin2] gg[xmed]] == 1, xmin2 = xmed, xmax2 = xmed];
,npts];
(xmin2 + xmax2)/2
, {xt, xtab}]
]
]
```

Sorry if it's a duplicate, I couldn't find any answer for my problem.

Could be helpful https://mathematica.stackexchange.com/q/24595/66

– faysou – 2017-05-13T15:46:11.240