How to repeat whole coding by increasing value of variable

Hi, i wonder if i can run my coding 5 times but vary my bus_value. I want get value of Ploss, location1, location2, real1,real2, reactive1 and reactive2 automaticly for each load (20, 30, 40, 50 and 60) instead of changing manually the bus_value=30 then run. copy the 'accepted value' into excel. Then change again the bus_value=40. Run again and copy in excel again...until bus_value=60.
bus_no=20; % chosen load bus, Qd20
bus_value=20; %% 20 MVAR, Qd20=20 MVAR (vary 20MVAR to 30-60 MVAR)
for i1=1:20
%caling 30-bus syst
[basemva,accuracy,maxiter,busdata,linedata]=Bus_30;
busdata(bus_no,6)=bus_value; %%% Stress the load
%%fitness 1-inject Pdg & Qdg in 30-syst to find new Ploss
busdata(x1_Dg1(i1),5)=-x1_Pdg1(i1);
busdata(x1_Dg1(i1),6)=x1_Qdg1(i1);
busdata(x1_Dg2(i1),5)=-x1_Pdg2(i1);
busdata(x1_Dg2(i1),6)=x1_Qdg2(i1);
%run loadflow
lfybus;; %forms the bus admittance matrix
lfnewton;; %power flow solution by newton raphson method
busout;; %prints the power flow solution on the screen
linefloww;;
%%%%% FITNESS 1 %%%%%
VminDg1(i1)=min(Vm);
PlossDg1(i1)=real(SLT);
DataF1(i1,:)=[x1_Dg1(i1),x1_Dg2(i1),x1_Pdg1(i1),x1_Pdg2(i1),x1_Qdg1(i1),x1_Qdg2(i1),VminDg1(i1),PlossDg1(i1)];
end
%%% value accepted%%
ploss=DataF1(1,8)
location1=DataF1(1,1)
location2=DataF1(1,2)
real1=DataF1(1,3)
real2=DataF1(1,4)
reactive1=DataF1(1,5)
reactive2=DataF1(1,6)

Answers (1)

Hi, you can use functions to avoid code repetition:
for bus_value = 20:10:60
[ploss, location1, location2, real1, real2, reactive1, reactive2] = myFunction(bus_value);
% Do something with your output
end
If you're new to this concept I would suggest you to try the free MATLAB onramp course, feel free to skip to the section "Calling Functions".
You can read more about functions herehttps://www.mathworks.com/help/matlab/functions.html

4 Comments

Hi, thank you for answering my question. May i know where should i put you code? under my code or under myFunction.m file? If the answer is under my code, what should i write under myFunction file? Because from i read from the link given, at myFunction file i should write
function [ploss, location1, location2, real1, real2, reactive1, reactive2] = myFunction(bus_value);
What i understand about calling function is, the input value from main script is carry into calling function script. All calculation is made in this script. Then, the output is carry back into the main script.
Yes, you're getting it right. The code that I provided goes in the main script. And the function has to be put in another script with the same name that of the function. "myFunction" was just an example, you can name it anything that you like. Just make sure that it matches the file name. To summarize, you have to create two files:
myFunction.m
function [ploss, location1, location2, real1, real2, reactive1, reactive2] = myFunction(bus_value)
% Do calculation and assign values.
end
main.m
for bus_value = 20:10:60
[ploss, location1, location2, real1, real2, reactive1, reactive2] = myFunction(bus_value);
% Do something with your output
end
Thank you for teaching me. I will try the coding.
You're welcome. If you've found this answer helpful and it resolves your doubt, then you can mark it as accepted.

Sign in to comment.

Categories

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

Asked:

on 14 Jun 2020

Commented:

on 15 Jun 2020

Community Treasure Hunt

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

Start Hunting!