Clear Filters
Clear Filters

I have a MATLab invalid expression when calling variables.

4 views (last 30 days)
I have a MATLab code but keeps giving me ivalid expression when calling variable in the code. Below is the code.
% Sample MATLAB code for OPF analysis of a distribution network with OLTC and SVC
% Load the bus admittance matrix, generation, and load data
% You should replace these with your actual data
Y_bus = load('bus_admittance_matrix.mat');
generation = load('generation_data.mat');
load_data = load('load_data.mat');
% Initialize variables
num_buses = 15;
num_gen_buses = 3; % Number of buses with generation
num_oltc_buses = 2; % Number of buses with OLTC
svc_bus = 8; % Bus number with SVC
% Define parameters
V_max = 1.05; % Maximum voltage magnitude
V_min = 0.95; % Minimum voltage magnitude
S_max = 100; % Maximum apparent power limit
% Initialize optimization variables
P_gen = zeros(num_gen_buses, 1);
Q_gen = zeros(num_gen_buses, 1);
V_mag = ones(num_buses, 1); % Initialize voltage magnitudes to 1.0 p.u.
% Perform OPF
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'interior-point');
[x, fval] = fmincon(@(x) objective_function(x, Y_bus), [P_gen; Q_gen; V_mag]; ...
[]; []; []; []; []; [];
@(x) constraints(x;Y_bus; num_gen_buses; num_oltc_buses; svc_bus; V_max;V_min; S_max), options);
% Extract results
optimal_P_gen = x(1:num_gen_buses);
optimal_Q_gen = x(num_gen_buses+1:2*num_gen_buses);
optimal_V_mag = x(2*num_gen_buses+1:end);
% Display results
disp('Optimal Generation:')
disp(optimal_P_gen);
disp('Optimal Voltage Magnitudes:')
disp(optimal_V_mag);
% Define the objective function to be minimized
function cost = objective_function(~, ~)
% Define your objective function here
% This could be a cost function related to generation, voltage deviations, etc.
% Example: cost = sum(x(1:num_gen_buses)); % Minimize total generation
end
% Define the constraints
function [c, ceq] = constraints(~, ~, ~, ~, ~, ~, ~, ~)
% Define your inequality and equality constraints here
% Example: c = x(1:num_gen_buses) - 50; % Generation should be at least 50 MW
% ceq = x(2*num_gen_buses+1:end) - 1.0; % Voltage magnitudes should be 1.0 p.u.
end

Accepted Answer

Torsten
Torsten on 6 Sep 2023
Edited: Torsten on 6 Sep 2023
@(x) constraints(x;Y_bus; num_gen_buses; num_oltc_buses; svc_bus; V_max;V_min; S_max)...
Function inputs are separated by ",", not by ";".

More Answers (1)

MarKf
MarKf on 6 Sep 2023
What would be the error?
I can already see some errors in the OPF (I'm guessing the mistake would be using semicolons ";" instead of commas "," which is why the actual error thrown is missing a closed parethesis ")", you're also likely missing a next line "..."). Something like this maybe:
[x, fval] = fmincon(@(x) objective_function(x, Y_bus), [P_gen; Q_gen; V_mag], ...
[], [], [], [], [], [], ...
@(x) constraints(x,Y_bus, num_gen_buses, num_oltc_buses, svc_bus, V_max,V_min, S_max), options);
The cost and constrain functions are also not complete. So this is not the final code. If that's the case, we wouldn't be able to recreate the error, also without the data, but if this is what you are talking about, then I suggest you be more careful or brush on your basics.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!