Do constants created in primary function transfer over to subsidiary functions created?
4 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
John D'Errico
on 20 Feb 2022
Edited: John D'Errico
on 20 Feb 2022
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)
Whos saw ONLY x, the argument passed in.
But now consider the function nestedexampe.
nestedexample(1)
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
More Answers (1)
Jan
on 20 Feb 2022
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]);
See Also
Categories
Find more on Interactive Control and Callbacks 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!