Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

closed as off-topic by blochwave, m_goldberg, C. E., Karsten 7., Michael E2 Sep 27 '15 at 0:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "The question is out of scope for this site. The answer to this question requires either advice from Wolfram support or the services of a professional consultant." – blochwave, m_goldberg, C. E., Karsten 7., Michael E2
If this question can be reworded to fit the rules in the help center, please edit the 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). – R. M. 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