2
$\begingroup$

I am looking for defining a function returning a function. This function must return a polynomial fit. I managed to pass the function to fit as argument but i stuggle with the returned value of the function.

With the code below I get fit but this do not seem to be a pure function. This is a problem because the code after needs pure functions.

GetFitProba[f_Symbol, T_, V_, eta_, cut_, t_] := (
  data = Transpose[
    List[Table[i, {i, IntegerPart[cut], V}], 
     f[#, T, V, eta, t] & /@ Table[i, {i, IntegerPart[cut], V}]]];
  fit = LinearModelFit[
    data, {1, x, x^2, x^3, x^4, x^5, x^6, x^8, x^9, x^10, x^11, x^12},
     x];
  fit[x]
  )

Then I call it like that:

h[x_] := GetFitProba[f, T, V, eta, cut, t];

(where f, t, V, eta, cut, and t are well defined)

I think the problem come from the objet returned by LinearModelFit (which is a FittedModel) but I do not understand how I am supposed to managed it. I tried to return Normal[fit] and things like that but it didn't work.

Thank you for your help !

$\endgroup$
  • $\begingroup$ I don't understand what you are trying to do. fit works like a function when applied to numbers. If you want to return a function, return fit. Otherwise there are various pieces of information you can extract from fit using fit["property"]. The possible properties are documented in the LinearModelFit page. Always look under Details and Options. $\endgroup$ – Szabolcs Apr 19 '17 at 14:29
  • $\begingroup$ AsI need a lot of fit of this kind, with different function (but same parameters) i want a function for lighten my code. The Pilsy's sollution works fine ! $\endgroup$ – Dalnor Apr 19 '17 at 15:24
2
$\begingroup$

I am going to boldly plunge ahead despite being unable to test directly, because why not. As you may have noticed, fit returns a FittedModel object. Most such objects have a bunch of properties that you can find by passing them a string argument; you can get a list of all such properties with fit["Properties"]. In this case, you want fit["Function"].

ClearAll[GetFitProba];
GetFitProba[f_, T_, V_, eta_, cut_, t_] :=
 Module[{data = 
    Transpose[
     List[Table[i, {i, IntegerPart[cut], V}], 
      f[#, T, V, eta, t] & /@ Table[i, {i, IntegerPart[cut], V}]]],
   fit},

  fit = LinearModelFit[
    data, {1, x, x^2, x^3, x^4, x^5, x^6, x^8, x^9, x^10, x^11, x^12},
     x];

  fit["Function"]];

Just as an aside, I ditched the requirement that f have head Symbol, since you might want use a pure function (with head Function), or even perhaps a curried "operator form" of a function like Lookup. Also, I went ahead used Module to make the fit and data variables local, which is good practice.

Now that you have a pure function, you can just assign it to h directly:

ClearAll[h]; 
h = GetFitProba[f, T, V, eta, cut, t];
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.