I am new to Mathematica programming and am trying to develop a package. My questions are:
- I have a small set of functions defined in a package inside of a Begin[“`Private`”]. If I call one of my functions from a notebook, I cannot get the numeric value of the function (even if I use //N). What is the correct way to call a function embedded in a package? The code of my simple package is below.
BeginPackage["EKRScaleupPower`"]
(* Exported symbols added here with SymbolName::usage *)
averagePowerRC::usage = "averagePowerRC[V, R, C, f] returns the average power of an RC circuit for V volts and f Hertz."
capacitanceParallelPlate::usage = "capacitanceParallelPlate[A, d, relativePermittivity, freePermittivity] returns the capacitance of a parallel plate condensor neglecting fringe effects."
reactance::usage = "reactance[C,f] returns capacitive reactance in ohms."
resistance::usage = "resistance[A, d, resistivity] returns resistance in ohms (Pouillet's law)."
Begin["`Private`"]
(* Implementation of the package *)
averagePowerRC[V_,R_,C_,f_]:=
Module[
{x,p},
x = 1/ (2 Pi f C);
p = 0.5 V^2 R / (R^2 + x^2)
]
capacitanceParallelPlate[A_,d_,relativePermittivity_,freePermittivity:8.854 10^-12]:=
Module[
{c},
c = relativePermittivity freePermittivity A / d
]
resistance[A_, d_, resistivity_]:=
Module[
{r},
r = resistivity d / A
]
reactance[C_,f_]:=
Module[
{x},
x= 1/(2 Pi f C)
]
End[]
EndPackage[]
- My call from my notebook is below (I suspect that this is the problem):
<< EKRScaleupPower`
cap20L = capacitanceParallelPlate[0.02^(2/3), 0.02^(1/3), 50]
- The first time I tried to call this from my .nb, I received an error message saying that my function was found in multiple contexts. If I understand contexts properly, they are similar in idea to the .net namespace. A logical collection of classes and symbols. In .net, if I have the same method in multiple namespaces or classes, I can specify the method that I want with a call that uses a Namespace.Class.Method syntax. For example, FunctionGeneratorsNamespace.Agilent33220a.SquareWave(20, 120) would call the SquareWave method in the Agilent33220a class from the FunctionGeneratorsNamespace. It looks like the context in Mathematica is similar. Could anyone point me to a set of example code that clearly demonstrates Mathematica contexts?