Defining Global Variables in the function

42 views (last 30 days)
Tashmid
Tashmid on 23 Oct 2025 at 21:14
Moved: Stephen23 on 26 Oct 2025 at 15:27
Hi. MATLAB Community. I am trying to get the following code to work. I’m working on a toy example that I hope to get running correctly before applying it to a larger numerical project. The issue I’m facing is that the values of k and rho_0 are not updating properly when I Area_Integral from the command window for different values of k and rho_0 (I am selecting k and rho_0 values so it stays in the if loop of the Area_Integral file. I uploaded the code for the various scripts I am using. When I write this in the command window, Area_Integral (1,0.4) and then Area_Integral (1.5,0.4) the value of k is not updated for the later case.
function out = Area_Integral(k, rho_0)
global k rho_0
if k>=1-rho_0^2
theta_array = linspace(0,pi/2,101);
root = zeros(size(theta_array));
for ii = 1:length(theta_array)
theta = theta_array(ii);
root(ii) = mnhf_secant(@poly4,[0.4 0.8],1e-8,0); %% Note: values of k, r0 and theta must be made accessible to poly4
end
out = 2*trapz(theta_array,root);
else
r_upperlim = sqrt(1+rho_0^2+sqrt(k^2+4*rho_0^2));
r_lowerlim = sqrt(1+rho_0^2-sqrt(k^2+4*rho_0^2));
fun_A2 = @(r) acos((r.^4+(1-rho_0^2)^2-k^2)./2./r.^2-rho_0^2).*r;
out = integral(fun_A2,r_lowerlim,r_upperlim);
end
if rho_0 == 1 && k <= 1e-12
out = pi;
end
end
function f = poly4(x)
global k rho_0 theta
disp(['Values: k=' num2str(k) ', rho_0=' num2str(rho_0) ', theta=' num2str(theta) ', x=' num2str(x)])
disp(['size(x): ' num2str(size(x))])
disp(['size(k): ' num2str(size(k))])
disp(['size(rho_0): ' num2str(size(rho_0))])
disp(['size(theta): ' num2str(size(theta))])
k
%Equation B3 from Shuo and Li (LF20B)
f = (x.^2-2.*x.*cos(theta)+1-rho_0^2).*(x.^2-2.*x.*cos(theta+pi)+1-rho_0^2)-k.^2;
disp(['f = ' num2str(f)])
end
function r=mnhf_secant(Fun,x,tol,trace)
%MNHF_SECANT finds the root of "Fun" using secant scheme.
%
% Fun - name of the external function
% x - vector of length 2, (initial guesses)
% tol - tolerance
% trace - print intermediate results
%
% Usage mnhf_secant(@poly1,[-0.5 2.0],1e-8,1)
% poly1 is the name of the external function.
% [-0.5 2.0] are the initial guesses for the root.
% Check inputs.
if nargin < 4, trace = 1; end
if nargin < 3, tol = 1e-8; end
if (length(x) ~= 2)
error('Please provide two initial guesses')
end
f = feval(Fun,x); % Fun is assumed to accept a vector
for i = 1:100
x3 = x(1)-f(1)*(x(2)-x(1))/(f(2)-f(1)); % Update the guess.
f3 = feval(Fun,x3); % Function evaluation.
if trace, fprintf(1,'%3i %12.5f %12.5f\n', i,x3,f3); end
if abs(f3) < tol % Check for convergenece.
r = x3;
return
else % Reset values for x(1), x(2), f(1) and f(2).
x(1) = x(2); f(1) = f(2); x(2) = x3; f(2) = f3;
end
end
r = NaN;
  1 Comment
Stephen23
Stephen23 on 24 Oct 2025 at 5:45
Moved: Stephen23 on 26 Oct 2025 at 15:27
"Okay is there a way in which I can get it to work, will it be something like,"
The best way to make it work:

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Oct 2025 at 22:47
Edited: Walter Roberson on 23 Oct 2025 at 22:49
function out = Area_Integral(k, rho_0)
global k rho_0
When you receive a parameter into the same name as a global variable, then the name is associated with the input value until the "global" statement is encountered. As soon as the "global" statement is encountered, the name becomes associated with the global variable, losing the input value, and gaining the current value of the global variable.
You never initialize the global variable "k" or "rho_0", so they have their default value (which is the empty array)
To state it explicitly:
When you pass in a value through the variable k and you later have global k then the global variable named "k" will not be assigned to automatically.
  2 Comments
Tashmid
Tashmid on 24 Oct 2025 at 4:22
Edited: Tashmid on 24 Oct 2025 at 4:23
Okay is there a way in which I can get it to work, will it be something like,
function out = Area_Integral(k_in, rho_0_in)
k_in = k;
rho_0_in = rho_0;
if k>=1-rho_0^2
theta_array = linspace(0,pi/2,101);
root = zeros(size(theta_array));
for ii = 1:length(theta_array)
theta = theta_array(ii);
root(ii) = mnhf_secant(@poly4,[0.4 0.8],1e-8,0); %% Note: values of k, r0 and theta must be made accessible to poly4
end
out = 2*trapz(theta_array,root);
else
r_upperlim = sqrt(1+rho_0^2+sqrt(k^2+4*rho_0^2));
r_lowerlim = sqrt(1+rho_0^2-sqrt(k^2+4*rho_0^2));
fun_A2 = @(r) acos((r.^4+(1-rho_0^2)^2-k^2)./2./r.^2-rho_0^2).*r;
out = integral(fun_A2,r_lowerlim,r_upperlim);
end
if rho_0 == 1 && k <= 1e-12
out = pi;
end
Walter Roberson
Walter Roberson on 24 Oct 2025 at 21:17
function out = Area_Integral(k_in, rho_0_in)
k_in = k;
rho_0_in = rho_0;
Those overwrite the value of k_in and rho_0_in that are passed in, replacing them with the seemingly undefined k and rho_0
if k>=1-rho_0^2
if rho_0 == 1 && k <= 1e-12
It appears that it is possible for neither of those tests to be true; if so then the variable out will not be defined.

Sign in to comment.

More Answers (0)

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!