iterating a function within itself

Hey everyone. I have a function which performs adjustment based on 2 indices NPI and CPI as shown below.
[A,B,C,D,NPI,CPI]=Adjustingcode2(fdrpath1,sheetname,RepCondpath);
CPI and NPI are a 1 by 7 matrices. The optimal value in any of the index of CPI and NPI must be less than 1. So to do this, I need to iterate the function by itself conditionally - only stopping at the first instance where CPI and aNPI are <1. I have thus created an iterate function to do this as follows. However, the final values I obtain for the NPI are not less than 1. Anyone can point out where the problem is?
function [A1,B1,C1,D1,NPI_new,CPI_new] =CSS_iterate(fdrpath1,sheetname,RepCondpath)
[A,B,C,D,NPI,CPI]=Adjustingcode2(fdrpath1,sheetname,RepCondpath);
for i=1:length(R)
if NPI_updated(i) > 1
[A,B,C,D,NPI,CPI]=Adjustingcode2(fdrpath1,sheetname,RepCondpath);
end
if CPI_updated(i) > 1
[A,B,C,D,NPI,CPI]=Adjustingcode2(fdrpath1,sheetname,RepCondpath);
end
end

댓글 수: 8

Walter Roberson
Walter Roberson 2023년 8월 31일
Every time you call Adjustingcode2 you are calling it with exactly the same pararameters. You are looping over NPI_updated values but you run the same thing every time the condition is met. You might even run the exact same thing twice, if both conditions are met.
Thank you @Walter Roberson and @dpb. Would you please have a look at the following - which explains what the Adjustingcode2 function. Please also see the NCL_recon function attached.
function[NewRC,NewAmpC,NewR,NewAmp,NPI,CPI,R]=Adjustingcode3(fdrpath1,sheetname,RepCondpath)
tic; % the first 4 outputs could be A, B,C and D.
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);
for i=1: size(R, 1)
for j= 1:size(TR, 1)
if CPI(i) < 1
NewRC(i) = R(i); NewAmpC(i) = Amp(i);
elseif CPI(i) > 1
if TR(j) <= OptiCR_1(i)
NewRC(i) = TR(j);NewAmpC(i) = TAmp(j);
break;
end
end
end
end
for i=1: size(R,1)
for j=1:size(TR,1)
if NPI(i)<=1
NewR (i)= NewRC(i); NewAmp (i) =NewAmpC(i);
elseif NPI(i)>1
if TR(j)<=OptiVR_2(i) % original was R
NewR(i)= TR(j); NewAmp(i) =TAmp(j);
break
end
end
end
end
xlswrite(fname1,NewR.',sheet,ResRange1)
xlswrite(fname1,NewAmp.',sheet,AmpRange)
toc
A few things to note about your earlier statement: the argument values, that is the values on the fdrpath1 are updated everytime I write /update the excel file with the NewR and NewAmp.
This code runs well. However, the objective is to get an NPI and CPI values that are less than 1. This means that I have to iterate this program to achieve that. This is how I came to my trying to do it using the CSS iterate code above.
I can manually run this code while observing the NPI and CPI and get the right values. How do I automate that process?
Voss
Voss 2023년 8월 31일
@Lewis Waswa: Can you also upload the functions start_f7 and BNPerf (and any other functions necessary for us to run the code) as well as the necessary data files?
Lewis Waswa
Lewis Waswa 2023년 8월 31일
편집: Lewis Waswa 2023년 9월 1일
@Voss, here is the BNPerf function, The input file is updated based on the valued from the ACSR file, which is also pointed by the RepCondpath. I was avoiding providing the whole sets of files associated with start function since they are many - but please see the zip with the rest.
@Lewis Waswa: How about start_f7?
Adjustingcode3('Inputs_JP.xlsx','JP_fdr','ACSRtechnicalData.xlsx')
Unrecognized function or variable 'start_f7'.

Error in solution>Adjustingcode3 (line 9)
[alphaN,betaN,V_feeder,fdr_HC,VI_out,VA,VB,VC,VUB,IA,IB,IC]= start_f7(1,1,1,1,fdrcon,sheetname,fdrpath1);
function[NewRC,NewAmpC,NewR,NewAmp,NPI,CPI,R]=Adjustingcode3(fdrpath1,sheetname,RepCondpath)
tic; % the first 4 outputs could be A, B,C and D.
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);
for i=1: size(R, 1)
for j= 1:size(TR, 1)
if CPI(i) < 1
NewRC(i) = R(i); NewAmpC(i) = Amp(i);
elseif CPI(i) > 1
if TR(j) <= OptiCR_1(i)
NewRC(i) = TR(j);NewAmpC(i) = TAmp(j);
break;
end
end
end
end
for i=1: size(R,1)
for j=1:size(TR,1)
if NPI(i)<=1
NewR (i)= NewRC(i); NewAmp (i) =NewAmpC(i);
elseif NPI(i)>1
if TR(j)<=OptiVR_2(i) % original was R
NewR(i)= TR(j); NewAmp(i) =TAmp(j);
break
end
end
end
end
xlswrite(fname1,NewR.',sheet,ResRange1)
xlswrite(fname1,NewAmp.',sheet,AmpRange)
toc
end
dpb
dpb 2023년 8월 31일
편집: dpb 2023년 8월 31일
...
for i=1: size(R, 1)
for j= 1:size(TR, 1)
if CPI(i) < 1
NewRC(i) = R(i); NewAmpC(i) = Amp(i);
elseif CPI(i) > 1
if TR(j) <= OptiCR_1(i)
NewRC(i) = TR(j);NewAmpC(i) = TAmp(j);
break;
end
end
end
end
...
Just some comments w/o yet being able to run it...
  1. the above if...else...end clause is incomplete in that it leaves out CPI==1. That may never occur, but there is no clause for it if it does.
  2. NewRC(i) = R(i); NewAmpC(i) = Amp(i); is independent of j, it could be moved out of the loop on j and then only do the test for the alternative.
  3. What if ~any(TR<=OptiCR)? Again it may not occur, but is it guaranteed to be true?
Lewis Waswa
Lewis Waswa 2023년 9월 1일
I have added the rest of the code should any of you want to take run the same.
@Lewis Waswa: Thank you for the code. I've run Adjustingcode2 and Adjustingcode3, with my best guess of what the inputs should be, and both return NPI and CPI values that are all less than 1. See below:
[NewRC,NewAmpC,NewR,NewAmp,NPI,CPI,R] = Adjustingcode2('Inputs_JP.xlsx','JP_fdr','ACSRtechnicalData.xlsx');
Feeder under analysis: Excel_feeder This feeder is Voltage Constrained!! FeederMD = 6.183073e+01 kVA Feeder under analysis: Excel_feeder This feeder is Voltage Constrained!! FeederMD = 6.183073e+01 kVA
Elapsed time is 2.440178 seconds.
close all
all(NPI < 1)
ans = logical
1
all(CPI < 1)
ans = logical
1
[NewRC,NewAmpC,NewR,NewAmp,NPI,CPI,R] = Adjustingcode3('Inputs_JP.xlsx','JP_fdr','ACSRtechnicalData.xlsx');
Feeder under analysis: Excel_feeder This feeder is Voltage Constrained!! FeederMD = 6.183073e+01 kVA
close all
all(NPI < 1)
ans = logical
1
all(CPI < 1)
ans = logical
1
What do I need to do to get some CPI and/or NPI values >= 1? In other words, how do I reproduce the problem you asked about?

댓글을 달려면 로그인하십시오.

답변 (2개)

dpb
dpb 2023년 8월 30일

0 개 추천

Several issues here...
function [A1,B1,C1,D1,NPI_new,CPI_new] =CSS_iterate(fdrpath1,sheetname,RepCondpath)
[A,B,C,D,NPI,CPI]=Adjustingcode2(fdrpath1,sheetname,RepCondpath);
for i=1:length(R)
if NPI_updated(i) > 1
[A,B,C,D,NPI,CPI]=Adjustingcode2(fdrpath1,sheetname,RepCondpath);
end
if CPI_updated(i) > 1
[A,B,C,D,NPI,CPI]=Adjustingcode2(fdrpath1,sheetname,RepCondpath);
end
end
[A,B,C,D,NPI,CPI]=Adjustingcode2(fdrpath1,sheetname,RepCondpath);
for i=1:length(R)
if NPI_updated(i) > 1
  1. R is undefined
  2. NPI/CPI_updated are never calculated; you return the (so it says) adjusted values in the NPI/CPI arrays, but then test a variable that never changes.
  3. if() on and array is true, IF AND ONLY IF all elements of the array are true so the above tests will always be called until all elements in the array <=1. Whether that would be true if were testing the NPI and CPI values instead would all depend upon what the adjusting code actually does which we are not privy to.
That's for starters, at least. Undoubtedly there will be more if/when get enough to be able to test...
Walter Roberson
Walter Roberson 2023년 8월 31일

0 개 추천

Suppose you have the task of moving one brick at a time to the left until you reach the edge, and you start at brick #4. Then consider the following potential recursive solution:
function move_left_ver1(bricknum)
move_left_ver1(bricknum);
bricknum = bricknum - 1;
end
now we call move_left_ver1(4) and the first thing it does is call move_left_ver1(4) . And the first thing that does is call move_left_ver1(4) ... It cannot ever get a chance to subtract 1 from the brick number until all of the calls to move_left_ver1 are finished... but they never finish.
If you have a recursive program and the function calls itself with the exact same parameter values that were passed into it, the only way the program can stop running is if the program tests something external, such as a random number generator, or such as looking for the user to press a Cancel button.
Now let us consider
function move_left_ver2(bricknum)
bricknum = bricknum - 1;
move_left_ver2(bricknum);
end
We call move_left_ver2(4) and it subtracts 1 making bricknum into 3, and calls move_left_ver2(3) -- which is a different input parameter, so there is hope so far. move_left_ver2(3) will call move_left_ver2(2) which will call move_left_ver2(1) . Then move_left_ver2(1) will decrement bricknum to 0, and will call move_left_ver2(0) which will call move_left_ver2(-1) then -2 then ... and it will keep going until MATLAB decides you've probably made a mistake.
If you have a program that calls itself and you've made sure to call with different parameter values... you need some way of detecting that you have finished.
... and your test for being finished has to be before the recursive call. The chain of calls has to end somewhere!

카테고리

도움말 센터File Exchange에서 Desktop에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2023년 8월 30일

댓글:

2023년 9월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by