Ok, I'm writing my own answer. The folks who answered previously were helpful, but I'm putting it together for anyone else who might stumble across this. The answer is that Matlab assumes that your function code is in a separate file. Because of this, variables that you want to pass between a calculation script file and a function definition file need to be explicitly declared as global variables in both places. So Matlab treats the single file like this:
File 1 (calculation script):
% Declare global variables and define them
global k q Q %etc
k = 8.99e9;
q = 1e-6;
%...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% In the full script, I'm doing things with this function and these constants not
% important to this discussion/problem. I need to use the global variables in these calculations.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
File 2 (function script):
function f=rodForce(N)
% Declare global variables (same as above)
global k q Q %etc
% Then do the function as in the OP
end
Or you could combine the two files as I have done (in the comment to a different answer). But global variables need to be declared both places (before the variables are defined and inside the function).
This is different from other programming languages (such as R and Python) when variables that aren't defined within a function are automatically inherited from the global environment (unless you restrict the environment within the funciton definition). And if no variable exists with that name, the error message is explicit.