Calling a function in itself.

55 views (last 30 days)
Lewis Waswa
Lewis Waswa on 29 Aug 2023
Commented: Lewis Waswa on 29 Aug 2023
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 Comment
YASSINE
YASSINE on 29 Aug 2023
Edited: YASSINE on 29 Aug 2023
Hi, if you want to create a recursive function, you should call it with different arguments; otherwise, you might create an infinite loop.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 29 Aug 2023
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)
Recursive case, calling function with 9. Recursive case, calling function with 8. Recursive case, calling function with 7. Recursive case, calling function with 6. Recursive case, calling function with 5. Recursive case, calling function with 4. Recursive case, calling function with 3. Recursive case, calling function with 2. Recursive case, calling function with 1. Recursive case, calling function with 0. Base case, n = 0
y = 3628800
check = factorial(10) % using the function included in MATLAB
check = 3628800
factorial2014476(0)
Base case, n = 0
ans = 1
factorial(0)
ans = 1
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?
  1 Comment
Lewis Waswa
Lewis Waswa on 29 Aug 2023
It does. The problem is that it does so recursively and does not update the input file in the excel sheet.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!