MATLAB Answers

How to repeat whole coding by increasing value of variable

8 views (last 30 days)
Lee Dira
Lee Dira on 14 Jun 2020
Commented: Aditya Verma on 15 Jun 2020
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)

  0 Comments

Sign in to comment.

Answers (1)

Aditya Verma
Aditya Verma on 14 Jun 2020
Edited: Aditya Verma on 14 Jun 2020
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

Show 1 older comment
Aditya Verma
Aditya Verma on 14 Jun 2020
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
Aditya Verma
Aditya Verma on 15 Jun 2020
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.

Community Treasure Hunt

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

Start Hunting!

Translated by