I will try to give a minimal example of my problem. Say I define the following function, spitting out an NDSolve result, using Module:
f[case : True | False] := Module[{x, rhs},
If[case,
rhs[x_] = y[x] Cos[x + y[x]],
rhs[x_] = y[x]
];
NDSolve[{y'[x] == rhs[x], y[0] == 1}, y, {x, 0, 30}]
]
The argument of f thereby specifies the right hand side of the differential equation: If the argument it True, the right hand side is y[x] Cos[x + y[x]], if it is False, it is just y[x].
Now, the way I defined it above, everything works fine, and the result is displayed as the usual
{{y → InterpolatingFunction[...]}}
output of NDSolve. However y is a global variable, so I would like to protect it inside my function by adding y to the set of local variables:
f[case : True | False] := Module[{x, y, rhs},
If[case,
rhs[x_] = y[x] Cos[x + y[x]],
rhs[x_] = y[x]
];
NDSolve[{y'[x] == rhs[x], y[0] == 1}, y, {x, 0, 30}]
]
If I run the function now, it would still produce the correct solution curves. However the output is now displayed for example as
{{y$25947 → InterpolatingFunction[...]}}
where the concrete number after the dollar sign changes every time I execute.
So something is going wrong. I am not sure what, and how to fix it.
Thanks for help!


yis a global variable, so I would like to protect it inside my function..." Just curious, but what problem doesybeing a global variable cause? $\endgroup$