Defining Global Variables in the function
Show older comments
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
"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:
Accepted Answer
More Answers (0)
Categories
Find more on Scope Variables and Generate Names 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!