MATLAB: Internal variables not calculated

functionsMATLABvariable

I don't understand why this code works:
% Important constants and variables. All units SI unless noted otherwise.

k = 8.99e9;
q = 1e-6; % Point charge magnitude

Q = 1e-3; % Rod charge magnitude

L = 1; % Rod length

xPoint = 0.25; % Distance from center of rod to point charge

N = 10;
% Initialize the force

f = [0, 0];
% Calculate the magnitude of each charge in the rod given N chunks

dQ = Q/N;
% Calculate the separation between charges

% (Depends on method, value is incorrect)

chargeSep = L/N;
% y coordinate of top charge (Depends on method, value is incorrect)

y = L/2 - chargeSep/2;
for i = 1:N
% For the given coordinates, find the vector to the point charge

% from the ith charge on the rod (values are incorrect)

rVec = [xPoint, -y];
rMag = sqrt(xPoint^2 + y^2);
rHat = rVec/rMag;
% Calculate the force for this particular point charge using

% Coulomb's law

dF = k*dQ*q/rMag^2 * rHat;
% Add to the total force

f = f + dF;
% Advance to the next charge on the rod

y = y - chargeSep;
end
f
(f = 64.3779 0)
While this one doesn't
% Important constants and variables. All units SI unless noted otherwise.
k = 8.99e9;
q = 1e-6; % Point charge magnitude
Q = 1e-3; % Rod charge magnitude
L = 1; % Rod length
xPoint = 0.25; % Distance from center of rod to point charge
rodForce(10)
% Make a function that calculates the force on the rod given the number of
% chunks. For now, we will make the only input the number of chunks
function f = rodForce(N)
% Declare global variables
global Q
global xPoint
global q
global L
global k
% Initialize the force
f = [0, 0];
% Calculate the magnitude of each charge in the rod given N chunks
dQ = Q/N;
% Calculate the separation between charges
% (Depends on method, value is incorrect)
chargeSep = L/N;
% y coordinate of top charge (Depends on method, value is incorrect)
y = L/2 - chargeSep/2;
for i = 1:N
% For the given coordinates, find the vector to the point charge
% from the ith charge on the rod (values are incorrect)
rVec = [xPoint, -y];
rMag = sqrt(xPoint^2 + y^2);
rHat = rVec/rMag;
% Calculate the force for this particular point charge using
% Coulomb's law
dF = k*dQ*q/rMag^2 * rHat;
% Add to the total force
f = f + dF;
% Advance to the next charge on the rod
y = y - chargeSep;
end
end
When I examine the output it seems that my variables dQ, chargeSep, etc (those calculated within the function) are not used. This is maddening.

Best Answer

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.