In MATLAB, I can save and recall my own function using an M-File(function or script)
How can I do something similar in Mathematica?
I already searched and know how to define functions and modules like the following:
f[x_]:=Sin[x]+Cos[x]
g[x_]:=Module[{a}, a*f[x]]
When I restart Mathematica, however, my own function is deleted.
Of course, I can save my notebook(.nb) file, but it is just saving my script, not saving my definition of the function.
How can I deal with this problem?
Put
,PutAppend
,Save
,DumpSave
andGet
in the documentation. That should give you enough to get started. There are several ways to do it in Mathematica — initialization cells,Put/Get
, and in more advanced cases, using packages. In your case, the function definition (as you've written it) should still be available in the saved notebook (assuming you didn't delete the cell); just that the kernel doesn't know about it. All you need to do then is to reevaluate the cell. $\endgroup$