Do constants created in primary function transfer over to subsidiary functions created?
Ältere Kommentare anzeigen
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);
Akzeptierte Antwort
Weitere Antworten (1)
Jan
am 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]);
1 Kommentar
Jaider
am 21 Feb. 2022
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!