Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a function for parameter estimation with these codes:

iparam1[u_, data_] := Module[{t, dif, bini, binit},
  t = (Mean[Transpose[data]] - Mean[Mean[Transpose[data]]])/
  bini = N[Log[(1 - Mean[u])/Mean[u]]];
  Which[4 <= bini, binit = 4, -4 >= bini, binit = -4, -4 < bini < 4, 
   binit = bini];
  dif = b /. FindRoot[Lb[1, b, 0, t, u], {b, binit, -4, 4}];

For using this function, below functions are pre-requisite:

p3[a_, b_, c_, t_] := c + (1 - c)/(1 + Exp[-1.7*a*(t - b)])
p2[a_, b_, t_] := 1/(1 + Exp[-1.7*a*(t - b)])
w[a_, b_, c_, 
  t_] := (p2[a, b, t]*(1 - p2[a, b, t]))/(p3[a, b, c, 
     t]*(1 - p3[a, b, c, t]))
Lb[a_, b_, c_, t_, u_] := (1 - c)*(-a)*
  Sum[(u[[i]] - p3[a, b, c, t[[i]]])*w[a, b, c, t[[i]]], {i, 1, 

I had trouble with writing compiled equivalent of my function with CompilationTarget -> "C". FYI,a,b,c and t are reals.In iparam1, the data is a matrix of 1s & 0s and u is a column of data.The output of iparam1 is a parameter (i.e. dif) for given column.

I searched for examples but I couldn't find out to write the complied version. Any help is appreciated.

UPDATE: I have been noticed that FindRoot is not compilable.Is there a solution for making my function iparam1 faster?

share|improve this question
One problem is that FindRoot is not compilable. (I assumed by "my function" you mean iparam1.) –  Michael E2 Dec 29 '13 at 0:42
Which function were you trying to compile? Note that you can't wrap any code in Compile and expect to get optimized C code. There is a list of compilable functions which are supported. Specifically, FindRoot is not in that list, so if you tried to compile iparam1, it won't work. Please also read halirutan's answer to that same question. Functions like FindRoot and friends are already highly optimized, which makes it unlikely that you'll be able to gain much with Compile (even if it were possible). –  rm -rf Dec 29 '13 at 0:42
Indeed, my function is iparam1.I have to use FindRoot for estimating dif parameter and since I'm going to replicate the calculation 100 times over large matrices I thought that there could a possibility for making it works faster. –  Amin Dec 29 '13 at 0:46
Perhaps Lb can be compiled? –  Michael E2 Dec 29 '13 at 0:59
I would be grateful if you help me on making the code faster in any possible way.I'm not very familiar with Compile. –  Amin Dec 29 '13 at 1:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.