Do constants created in primary function transfer over to subsidiary functions created?

1 次查看(过去 30 天)
So bisect is my primary function where I do my algorithm and call upon the other functions to be used. Is it necessary for me to pass all the constants created under bisect to all the subsidiary functions when I call them? Or can I safely ignore this and just say f(x) instead of f(x,h,xp,..)?
function bisect
%constants for routing problem
h=0.01;
xp=5;
yp=4;
xc=2;
yc=2;
r=1;
rho=10;
%numerical scheme constants
a=-5;
b=5;
n=20; % this will be the number of steps in the bisection method .
p=(a+b)/2;
omega=f(a);
eta=f(b);
gamma=f(p);
for i=1:n
z=(a+p)/2;
y=(p+b)/2;
alpha=f(z);
beta=f(y);
fmin=min(omega,eta,gamma,alpha,beta);
if (fmin==omega)&&(fmin==alpha)
b=p;
p=z;
eta=gamma;
gamma=alpha;
elseif fmin==gamma
a=z;
b=y;
omega=alpha;
gamma=beta;
elseif (fmin==beta)&&(fmin==eta)
a=p;
p=y;
omega=gamma;
gamma=beta;
end
fprintf(1,'i= %4i a= %18.14f b=%18.14f p=%18.14f \n',i,a,b,p);
x=abs(b-a);
end
fprintf(1,'i= %4i a= %18.14f b=%18.14f p=%18.14f \n',i,a,b,p);
function y=f(x,xc,yc,xp,yp,h,rho,r)
y=x+((x-xp).^2+yp.^2).^(1/2)+2*rho*r((-1)*((x-xp).*yc-yp.*(x-xc)).^(2)/((x-xp).^2+yp.^2)).^(1/2);
function y=df(x,h)
y=(f(x+h)-f(x-h))./(2.*h);
function y=ddf(x,h)
y=(f(x+h)-2.*f(x)+f(x-h))/(h.^2);

采纳的回答

John D'Errico
John D'Errico 2022-2-20
编辑:John D'Errico 2022-2-20
There are TWO distinct type of subsidiary functions in MATLAB, sub-functions, and nested functions.
Consider first the function subexample. As you can see, in the workspace of the sub-function, y does not exist. x exists for the sub-function, only because I passed it in.
subexample(1)
sub-functions cannot see the caller workspace Name Size Bytes Class Attributes x 1x1 8 double
Whos saw ONLY x, the argument passed in.
But now consider the function nestedexampe.
nestedexample(1)
Nested functions can see the caller workspace Name Size Bytes Class Attributes x 1x1 8 double y 1x1 8 double y, in the nested function workspace, before it was changed in the nested function.2 y, in the nested function workspace, after it was changed in the nested function.5 y, in the caller workspace, after it was changed in the nested function.5
Do you see that subb can see y? In fact, the caller workspace can see that y has been modified. The difference is where suba and subb are placed, in terms of the end statement for the caller function. In fact, If subb needs to use the variable x, I did not even need to pass it in.
function nestedexample(x)
% do stuff
y = 2;
subb
disp("y, in the caller workspace, after it was changed in the nested function." + y)
function subb
% in this function, y does exist in the workspace
disp("Nested functions can see the caller workspace")
whos
% we can use y here.
disp("y, in the nested function workspace, before it was changed in the nested function." + y)
y = y + 3;
disp("y, in the nested function workspace, after it was changed in the nested function." + y)
end
end
function subexample(x)
% do stuff
y = 2;
suba(x)
end
function suba(x)
% in this function, y does not exist in the workspace
disp("sub-functions cannot see the caller workspace")
whos
end

更多回答(1 个)

Jan
Jan 2022-2-20
The variables are shared with nested functions, but not with other function. So you have to provide them as arguments, or use nested functions.
What is the prupos of this line:
fmin=min(omega,eta,gamma,alpha,beta);
If min() is Matlab built-in function, this will fail. Mybe you mean:
fmin=min([omega,eta,gamma,alpha,beta]);
  1 个评论
Jaider
Jaider 2022-2-21
Yes, I had to debug the code for some time until I caught it, for some reason I thought that would work just fine. Thanks for letting me now

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by