6 views (last 30 days)

Show older comments

Zulfiana Majid on 27 Oct 2020

Commented: Zulfiana Majid on 5 Apr 2021

Accepted Answer: Walter Roberson

Open in MATLAB Online

I use the eval function to store the matpower case that its control variable has been assigned and call matpower to run the power flow

....

eval(['savecase (''case_ieee30_test', num2str(i), '.mat'', baseMVA, bus, gen, branch)']);

eval(['initial_results_',num2str(i),'=runpf(''case_ieee30_test',num2str(i), '.mat'')']);

eval(['initial_losses_',num2str(i),'=sum(real(get_losses(initial_results_',num2str(i),')))']);

....

All of the code works great,but when I want to execute another power flow which DC system embedded I use 'runacdcpf' function. But it didn't work at all.. What could the problem? . I am using the following lines of code:

eval(['savecase (''case_ieee30_test', num2str(i), '.mat'', baseMVA, bus, gen, branch)']);

eval(['initial_results_',num2str(i),'=runacdcpf(''case_ieee30_test','case5_stagg_MTDCslack' ,num2str(i), '.mat'')']);

eval(['initial_losses_',num2str(i),'=sum(real(get_losses(initial_results_',num2str(i),')))']);

the error were:

Error using loadcase (line 246)

loadcase: specified MAT file does not exist

Error in runacdcpf (line 109)

[baseMVA, bus, gen, branch] = loadcase(caseac);

Error in pso_orpd_edit (line 63)

eval(['initial_results_',num2str(i),'=runacdcpf(''case_ieee30_test','case5_stagg_MTDCslack' ,num2str(i),

'.mat'')']);

Your help greatly appreciated!!

##### 1 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Stephen23 on 29 Oct 2020

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/628328-using-eval-function-for-execute-a-power-flow-with-dc-system-embedded#comment_1094443

"What could the problem?"

Numbered variables, i.e. initial_results_1, initial_results_2, etc., are a sign that you are doing something wrong.

Putting meta-data (e.g. pseudo-indices) into variable names is a sign that you are doing something wrong.

Both of these mean that you force yourself into writing slow, complex, inefficient, buggy code to access your data:

https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

The neat, simple, and very efficient approach is to use indexing with one array (e.g. numeric, cell, table, etc.).

Sign in to comment.

Sign in to answer this question.

### Accepted Answer

Walter Roberson on 27 Oct 2020

Don't do that!

casefile = sprintf('case_ieee30_test%d.mat', i);

savecase(casefile, baseMVA, bus, gen, branch);

And do not use dynamic variable names: use cell arrays.

staggfile = sprintf('case5_stagg_MTDCslack%d.mat', i) ;

initial_results_{i} = runacdcpf(casefile, staggfile);

initial_losses_{i} = sum(real(get_losses(initial_results_{i})));

##### 6 Comments Show 4 older commentsHide 4 older comments

Show 4 older commentsHide 4 older comments

Zulfiana Majid on 29 Oct 2020

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/628328-using-eval-function-for-execute-a-power-flow-with-dc-system-embedded#comment_1094158

Sorry for the late reponse, thank you very much for your kind suggestion. It worked very well, but the graph is the new issue now..I don't know why..but the graph is not run dinamically (I use the case for AC only before plugging the DC system). Everything is going well when I use the 'eval' function, Can you give me another suggestion please.. Thanks in advance...

This is the graph which use the 'eval' function;

and this is the graph after editing the code;

Walter Roberson on 29 Oct 2020

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/628328-using-eval-function-for-execute-a-power-flow-with-dc-system-embedded#comment_1094248

We would need to see more code fo diagnose that -- the original eval code through to plotting, and the revised version.

Zulfiana Majid on 29 Oct 2020

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/628328-using-eval-function-for-execute-a-power-flow-with-dc-system-embedded#comment_1094528

Open in MATLAB Online

Actually this is a conventional PSO algorithm that employs Matpower in power flow execution. so, I only describe the core parts of the discussion

%Initilization Parameters

iter=0;

iteration=100;

nvars = 12; %Number of variables

N = 50; %Number of Particles or Swarm size

%Acceleration constants

c1 = 2.05;

c2 = 2.05;

%Inertia Weight

w_max=0.9;

w_min=0.4;

w_temp(1)=w_max;

%load Data

[baseMVA, bus, gen, branch]=loadcase(case_ieee30);

%% Initialization of Swarm & velocity

Swarm=[unifrnd(0.95,1.10,N,6),unifrnd(0.90,1.10,N,4),unifrnd(0.00,0.20,N,2)];

%Initialize velocity

Velocity =[unifrnd(-0.003,0.003,N,6),unifrnd(-0.003,0.003,N,4), unifrnd(0.003,0.003,N,2)];

for i=1:N

v1=Swarm(i,1); %v1

bus(1,8)=v1; %Vm, column 8 is voltage magnitude (p.u.)

gen(1,6)=v1; %Vg, column 6 is voltage magnitude setpoint (p.u.)

...

qc24=Swarm(i,12); %Shunt capacitor 10, column 6 is BS

bus(24,6)=qc24;

eval(['savecase (''case_ieee30_test', num2str(i), '.mat'', baseMVA, bus, gen, branch)']);

eval(['initial_results_',num2str(i),'=runpf(''case_ieee30_test',num2str(i), '.mat'')']);

eval(['initial_losses_',num2str(i),'=sum(real(get_losses(initial_results_',num2str(i),')))']);

%Penalty for bus voltage violation

....

penalty_Vl_violation=sum(penalty_Vl);

...

%%Penalty for shunt violation

penalty_Qc_violation=sum(penalty_Qc);

%%Penalty for tap position violation

penalty_Tk_violation=sum(penalty_Tk);

%objective function=sum of active power losses of the transmission lines

losses(i)=eval(['initial_losses_',num2str(i)]); %sum of real power losses of all branches

Obj_fun_initial(i)=losses(i)+penalty_Vl_violation+penalty_Qc_violation+penalty_Tk_violation; %augumented objective function with penalty function

end

%% Initialize best position (Pbest) and global best postion (Gbest) matrix

Pbest=Swarm;

Val_Pbest=Obj_fun_initial;

%finding best particle in initial population

[Val_Gbest,m]=min(Val_Pbest);

Gbest=Swarm(m,:); %used to keep track of the best particle ever

Gbest_calc=repmat(Swarm(m,:),N,1);

%% PSO LOOP

figure('NumberTitle', 'off', 'Name', 'PSO Algorithm Based Optimal Reactive Power Dispatch');

title('ACTIVE POWER LOSS MINIMIZATION');

ylabel('Total Active Power Loss (MW)');

xlabel('Iteration Number');

grid on;

hold on

for iter=1:iteration

.....

....

.....

end

and this is the revised code (what is written is only revised part, the rest is the same):

casefile = sprintf('case_ieee30_test%d.mat', i);

savecase(casefile, baseMVA, bus, gen, branch);

initial_results_{i} = runpf(casefile);

initial_losses_{i} = sum(real(get_losses(initial_results_{i}));

.....

.....

losses(i)=initial_losses_{i}; %sum of real power losses of all branches

hopefully this can represent the whole code..Thanks in advance

Zulfiana Majid on 29 Oct 2020

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/628328-using-eval-function-for-execute-a-power-flow-with-dc-system-embedded#comment_1094543

Open in MATLAB Online

NB:

Embedding DC parameters even worse, MATLAB can not read the MAT-file even I save the case by using 'sprintf' syntax.

This what I wrote:

casefile = sprintf('case_ieee30_test%d.mat', i);

savecase(casefile, baseMVA, bus, gen, branch);

staggfile = sprintf('case5_stagg_MTDCslack_test%d.mat', i) ;

savecase(casefile, baseMVAdc, busdc, convdc, branchdc);

initial_results_{i} = runacdcpf(casefile,staggfile);

initial_losses_{i} = sum(real(get_losses(initial_results_{i}));

....

....

losses(i)=initial_losses_{i}; %sum of real power losses of all branches

and this is the result

Error using loadcase (line 246)

loadcase: specified MAT file does not exist

Error in runacdcpf (line 109)

[baseMVA, bus, gen, branch] = loadcase(caseac);

Error in pso_orpd (line 64)

eval(['initial_results_',num2str(i),'=runacdcpf(''case_ieee30_test',num2str(i), '.mat','case_ieee30_test',num2str(i), '.mat'')']);

I don't know why the 'savecase' didn't work for saving the case file.. once again please give me some suggestion..thanks a million..

Walter Roberson on 29 Oct 2020

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/628328-using-eval-function-for-execute-a-power-flow-with-dc-system-embedded#comment_1094598

Open in MATLAB Online

%Initilization Parameters

iteration=100;

nvars = 12; %Number of variables

N = 50; %Number of Particles or Swarm size

%Acceleration constants

c1 = 2.05;

c2 = 2.05;

%Inertia Weight

w_max=0.9;

w_min=0.4;

w_temp(1)=w_max;

%load Data

[baseMVA, bus, gen, branch]=loadcase('case_ieee30.mat'); %CHANGED

%% Initialization of Swarm & velocity

Swarm=[unifrnd(0.95,1.10,N,6),unifrnd(0.90,1.10,N,4),unifrnd(0.00,0.20,N,2)];

%Initialize velocity

Velocity =[unifrnd(-0.003,0.003,N,6),unifrnd(-0.003,0.003,N,4), unifrnd(0.003,0.003,N,2)];

initial_losses_ = cell(N,1); %NEW

initial_results_ = cell(N,1); %NEW

losses = zeros(N,1); %NEW

Obj_fun_initial = zeros(N,1); %NEW

for i=1:N

v1=Swarm(i,1); %v1

bus(1,8)=v1; %Vm, column 8 is voltage magnitude (p.u.)

gen(1,6)=v1; %Vg, column 6 is voltage magnitude setpoint (p.u.)

...

qc24=Swarm(i,12); %Shunt capacitor 10, column 6 is BS

bus(24,6)=qc24;

casefile = sprintf('case_ieee30_test%d.mat', i); %CHANGED

savecase(casefile, baseMVA, bus, gen, branch); %CHANGED

staggfile = sprintf('case5_stagg_MTDCslack_test%d.mat', i) ; %CHANGED

savecase(staggfile, baseMVAdc, busdc, convdc, branchdc); %CHANGED

initial_results_{i} = runacdcpf(casefile,staggfile); %CHANGED

initial_losses_{i} = sum(real(get_losses(initial_results_{i}))); %CHANGED

%Penalty for bus voltage violation

....

penalty_Vl_violation=sum(penalty_Vl);

...

%%Penalty for shunt violation

penalty_Qc_violation=sum(penalty_Qc);

%%Penalty for tap position violation

penalty_Tk_violation=sum(penalty_Tk);

%objective function=sum of active power losses of the transmission lines

losses(i)=initial_losses_{i}; %sum of real power losses of all branches %CHANGED

Obj_fun_initial(i)=losses(i)+penalty_Vl_violation+penalty_Qc_violation+penalty_Tk_violation; %augumented objective function with penalty function %CHANGED

end

%% Initialize best position (Pbest) and global best postion (Gbest) matrix

Pbest=Swarm;

Val_Pbest=Obj_fun_initial;

%finding best particle in initial population

[Val_Gbest,m]=min(Val_Pbest);

Gbest=Swarm(m,:); %used to keep track of the best particle ever

Gbest_calc=repmat(Swarm(m,:),N,1);

%% PSO LOOP

figure('NumberTitle', 'off', 'Name', 'PSO Algorithm Based Optimal Reactive Power Dispatch');

title('ACTIVE POWER LOSS MINIMIZATION');

ylabel('Total Active Power Loss (MW)');

xlabel('Iteration Number');

grid on;

hold on

for iter=1:iteration

.....

....

.....

end

Zulfiana Majid on 5 Apr 2021

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/628328-using-eval-function-for-execute-a-power-flow-with-dc-system-embedded#comment_1440147

Thank you so much.. it works..

Sign in to comment.

### More Answers (0)

Sign in to answer this question.

### See Also

### Categories

Control SystemsSimulink Design OptimizationParameter EstimationEstimate Parameters and States

Find more on **Estimate Parameters and States** in Help Center and File Exchange

### Tags

- eval
- dc power flow

### Community Treasure Hunt

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

Start Hunting!

**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom(English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

Contact your local office