Calling a function in itself.
24 次查看(过去 30 天)
显示 更早的评论
I have a long code that uses two indices to update the resistance values in a powerflow program as below. The powerflow program is described by the function
[alphaN,betaN,V_feeder,fdr_HC,VI_out,VA,VB,VC,VUB,IA,IB,IC]= start_f7(1,1,1,1,fdrcon,sheetname,fdrpath1);
The rest of the code is as below - and it basically locates and updates the inputs.
function[NewRC,NewAmpC,NewR,NewAmp,NPI,CPI,R]=Adjustingcode2(fdrpath1,sheetname,RepCondpath)
tic;
fname1 = fdrpath1; sheet = sheetname; ResRange1 = "G6:G12"; AmpRange="AG6:AG12";
fdrcon_range="AK6:AM60";fdrcon = xlsread(fdrpath1, sheetname,fdrcon_range);
[alphaN,betaN,V_feeder,fdr_HC,VI_out,VA,VB,VC,VUB,IA,IB,IC]= start_f7(1,1,1,1,fdrcon,sheetname,fdrpath1);
R=xlsread(fname1,sheet,ResRange1); Amp=xlsread(fname1,sheet,AmpRange);
%% Branch and node performance - calculating replacement
[Vmax,Imax]=BNPerf(VA,VB,VC,IA,IB,IC);
[NPI,CPI,OptiCR_1, OptiVR_2] = NCL_recon(Vmax,Imax,fdrpath1,sheetname);
fname2 = RepCondpath;
sheet1 = 3;Range = "G2:G15";
TAmpRange = "I2:I15"; TR=xlsread(fname2,sheet1,Range); TAmp= xlsread(fname2,sheet1,TAmpRange);
i = 1;
while i <= size(R, 1)
j = 1;
while j <= size(TR, 1)
if CPI(i) < 1
NewRC(i) = R(i);
NewAmpC(i) = Amp(i);
break;
elseif CPI(i) > 1
if TR(j) <= OptiCR_1(i)
NewRC(i) = TR(j);
NewAmpC(i) = TAmp(j);
break;
end
end
j = j + 1;
end
i = i + 1;
end
i=1;
while i<= size(R,1)
j=1;
while j<=size(TR,1)
if NPI(i)<1
NewR (i)= NewRC(i);
NewAmp (i) =NewAmpC(i);
break
elseif NPI(i)>1
if TR(j)<=OptiVR_2(i) % original was R
NewR(i)= TR(j);
NewAmp(i) =TAmp(j);
break
end
end
j=j+1;
end
i=i+1;
end
xlswrite(fname1,NewR.',sheet,ResRange1)
xlswrite(fname1,NewAmp.',sheet,AmpRange)
Up to this point, my code works fine. So, I would like to do this repeetively till when my indices NPI and CPI are both compliant. I have written the following as the code to check if each of the indices meets the threshold and to recurse the program but it does not seem to be updating. Can someone help me see where the bug is?
option 1 using the while loop
%% option 1 using the while loop
[NPI, CPI, OptiCR, OptiVR] = NCL_recon(Vmax,Imax,fdrpath1,sheetname);
k = 1;
while k <= length(CPI)
if CPI(k) > 1
[NewRC, NewAmpC, NewR, NewAmp, NPI, CPI, R] = Adjustingcode2(fdrpath1, sheetname, RepCondpath);
end
if NPI(k)>1
[NewRC, NewAmpC, NewR, NewAmp, NPI, CPI, R] = Adjustingcode2(fdrpath1, sheetname, RepCondpath);
end
k = k + 1;
end
[~,~,~,~,~,VA,VB,VC,~,IA,IB,IC]=start_f7(1,1,1,1,fdrcon,sheetname,fdrpath1);
PL=size(IA,3);
toc;
[Vmax,Imax]=BNPerf(VA,VB,VC,IA,IB,IC);
option 2 using the for loop and OR
[NPI, CPI, OptiCR, OptiVR] = NCL_recon(Vmax, Imax, fdrpath1, sheetname);
for k = 1:length(CPI)
if CPI(k) > 1 || NPI(k) > 1
[NewRC, NewAmpC, NewR, NewAmp, UpdatedNPI, UpdatedCPI, R] = Adjustingcode2(fdrpath1, sheetname, RepCondpath);
NPI(k) = UpdatedNPI;
CPI(k) = UpdatedCPI;
end
end
[~,~,~,~,~,VA,VB,VC,~,IA,IB,IC]=start_f7(1,1,1,1,fdrcon,sheetname,fdrpath1);
PL=size(IA,3);
toc;
[Vmax,Imax]=BNPerf(VA,VB,VC,IA,IB,IC);
Any help will be appreciated.
1 个评论
回答(1 个)
Steven Lord
2023-8-29
It is possible to recursively call a function in MATLAB (within reason, there is a recursion limit in MATLAB to try to prevent your code from getting into an infinite loop and possibly crashing MATLAB and/or your computer.) In your code I see your "recursive step" but it's not clear to me what your "base case" is (using the terms from the Wikipedia page.)
y = factorial2014476(10)
check = factorial(10) % using the function included in MATLAB
factorial2014476(0)
factorial(0)
function F = factorial2014476(n)
arguments
n (1, 1) {mustBeNonnegative, mustBeInteger} % error checking
end
if n == 0
fprintf("Base case, n = 0")
F = 1; % base case, does not call the function recursively
else
fprintf("Recursive case, calling function with %d.\n", n-1)
F = n*factorial2014476(n-1); % recursive step, does call the function recursively
end
end
So under what circumstances does your function not call itself?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!