# Function in notebook vs function in package?

**URL:** <https://community.wolfram.com/t/function-in-notebook-vs-function-in-package/20163>\
**Category:** Questions\
**Tags:** calculus, equation-solving, packages-and-paclets\
**Created:** [June 15, 2022, 7:59am UTC](https://community.wolfram.com/t/function-in-notebook-vs-function-in-package/20163 "2022-06-15T07:59:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phuongnguyen](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@phuongnguyen](https://community.wolfram.com/u/phuongnguyen)\
**Post date:** [June 15, 2022, 7:59am UTC](https://community.wolfram.com/t/function-in-notebook-vs-function-in-package/20163/1 "2022-06-15T07:59:17Z")

</div>

Hi all,

I started to write a package for my project, where I put all the useful functions and then import the package to use them. Problems arise, so here is the function:

The function is used to generate odes to use in NDSolve

```
MakeSystem[var_, vart_, sys_]:=
Module[{v, vt, S, St, Sf},
v = var;
vt = vart;
S = sys;
St = S/.Thread[v-> vt]; 
Thread[D[vart, t] == St]]

```

This is an example to use the function:

```
dndt = r * (1 - k * n) * n - c * n * p;

dpdt = c * n * p;

var = {n, p};

vart = {n[t], p[t]}

system = {dndt, dpdt};

MakeSystem[var, vart, system]

```

If I put the function in the notebook, it produces exactly what I want:

```
n'[t] == r * (1 - k * n[t]) * n[t] - c * n[t] * p[t]

p'[t] == c * n[t] * p[t]

```

But if I put the function in the package, the D[vart, t] produces zeros, so the output is

```
0 == r * (1 - k * n[t]) * n[t] - c * n[t] * p[t]

0 == c * n[t] * p[t]

```

Any help?

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 15, 2022, 3:49pm UTC](https://community.wolfram.com/t/function-in-notebook-vs-function-in-package/20163/2 "2022-06-15T15:49:22Z")

</div>

Your variable `t` is the problem. It shows up “raw” in several places. When you put your `MakeSystem` definition inside of a package, that `t` will not be in the `Global'` context, but in whatever context your package uses, so it might be something like `Test'Private't` (btw, all of the ’ should be backticks) You end up producing a derivative of an expression with respect to a variable that isn’t in that expression, and such a derivative will evaluate to 0.

One fix would be to ask the caller to provide the symbol, so add a fourth argument to `MakeSystem` that is expected to be the symbol/variable with respect to which we’re going to take the derivative. The problem with this is that you need to make sure you use the same variable in `vart`, but that’s also a good thing, because it makes the intent very explcit.

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 15, 2022, 5:39pm UTC](https://community.wolfram.com/t/function-in-notebook-vs-function-in-package/20163/3 "2022-06-15T17:39:15Z")

</div>

Actually, you could just get rid of `vart` altogether–it can be constructed from `var` and the new argument. I would have called the new argument `var`, but… 🙂

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 15, 2022, 6:10pm UTC](https://community.wolfram.com/t/function-in-notebook-vs-function-in-package/20163/4 "2022-06-15T18:10:45Z")

</div>

And since I started making refactoring suggestions, I guess I’ll continue… You also don’t need Module. A couple of simplifications would lead to:

```
MakeSystem[fnNames_List, var_, rhs_List] :=
 With[
  {fns = Through[fnNames[var]]},
  Thread[Equal[D[fns, var], rhs /. Thread[fnNames -> fns]]]]

```

---

<div class="post-metadata">

**Author:** ![phuongnguyen](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@phuongnguyen](https://community.wolfram.com/u/phuongnguyen)\
**Post date:** [June 16, 2022, 8:26am UTC](https://community.wolfram.com/t/function-in-notebook-vs-function-in-package/20163/5 "2022-06-16T08:26:25Z")

</div>

Thanks a lot Eric for the solution.

Yup, that’s what I also suspect about the parameter t. I’m just not sure how to handle it.

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 16, 2022, 2:17pm UTC](https://community.wolfram.com/t/function-in-notebook-vs-function-in-package/20163/6 "2022-06-16T14:17:46Z")

</div>

The code I posted above handles it. I could make it clearer by using pattern matching on `var`:

```
MakeSystem[fnNames_List, var_Symbol, rhs_List] := 
 With[{fns = Through[fnNames[var]]}, 
  Thread[Equal[D[fns, var], rhs /. Thread[fnNames -> fns]]]]

```

Now using your data:

```
MakeSystem[{n, p}, t, {r*(1 - k*n)*n - c*n*p, c*n*p}]
(*{Derivative[1][n][t] == r n[t] (1 - k n[t]) - c n[t] p[t], Derivative[1][p][t] == c n[t] p[t]} *)

```
