Internal variables not calculated
Show older comments
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.
Accepted Answer
More Answers (2)
James Tursa
on 22 Jan 2020
1 vote
You didn't declare those variables global in the caller. You only have them global in the function. Add the global statements in the caller.
That being said, it would be better to pass these variables in the argument list as parameters instead of making them global.
6 Comments
Steven Wolf
on 22 Jan 2020
Steven Wolf
on 22 Jan 2020
Walter Roberson
on 22 Jan 2020
Could you name a language for us in which a declaration inside a routine that a variable is global has an effect on code outside the routine that does not declare the variable to be global?
Steven Wolf
on 22 Jan 2020
Walter Roberson
on 22 Jan 2020
Insert
global Q
global xPoint
global q
global L
global k
just before you assign values to the variables.
Steven Wolf
on 23 Jan 2020
James Tursa
on 23 Jan 2020
Maybe the behavior you are looking for is nested functions. Instead of a function at the end of a script, you have a function within another function. The nested function sees all of the parent function variables without the use of global variables. E.g.,
function parentfun
x = 5;
childfun;
function childfun
disp(x);
end % child
end % parent
And a sample run
>> parentfun
5
So you can see that childfun sees the variable x that is defined in parentfun without the use of global statements.
1 Comment
Categories
Find more on Scripts in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!