I am having a go at writing my first package and would like to do something similar to the following. Its a basically a very thin wrapper around NonlinearModelFit
.
BeginPackage["TestPackage`"]
gaussianFit::usage="Fits to a gaussian and returns list of best fit parameters.";
Begin["`Private`"]
gaussianFit[data_,Aest_, x0est_, σest_,y0est_]:= NonlinearModelFit[
data
, A E^(-(1/2) ((x-x0)/σ)^2)+y0
, { {A,Aest},{x0, x0est},{σ,σest},{y0, y0est} }
, x
]["BestFitParameters"]
End[]
EndPackage[]
I would like the function to return a list of rules such as
{A->1, x0->1, σ->1, y0->1}
but what actually gets returned is
{ TestPackage`Private`A -> -5.09821, TestPackage`Private`x0 -> 4.1642, TestPackage`Private`σ -> 1.29237, TestPackage`Private`y0 -> 2.25453}
I've read various things about fixing this by making the variables global but that doesn't seem like the right thing to do since I don't want to be able to globally overwrite them to something else.
Is there a way to achieve what I want?
{symbol, symbolEst}...
pairs instead of only the value. See: Derivative from my package function returns 0. Or you can returnValues @ ...
and associate them with symbols later or not at all. $\endgroup$BeginPackage[...]
(and probably also make themProtected
), and accept that you won't be able to use these symbols for something else after loading the package. $\endgroup$