0% found this document useful (0 votes)
2 views5 pages

clc

The document contains MATLAB code implementing two power flow analysis methods: Newton-Raphson and Gauss-Seidel. Both methods utilize bus and line data to construct the Ybus matrix, perform iterations to calculate voltage magnitudes and angles, and determine power flow directions in the lines. The results include voltage values, complex power at bus 1, iteration count, computational speed, accuracy, and error metrics.

Uploaded by

obaid ur rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

clc

The document contains MATLAB code implementing two power flow analysis methods: Newton-Raphson and Gauss-Seidel. Both methods utilize bus and line data to construct the Ybus matrix, perform iterations to calculate voltage magnitudes and angles, and determine power flow directions in the lines. The results include voltage values, complex power at bus 1, iteration count, computational speed, accuracy, and error metrics.

Uploaded by

obaid ur rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

NEWTON RAPHSON:

clc
clear all
close all
% Start timer
tic;

% Bus Data
bus_data = [
1 0 0 1.04 0;
2 0.5 0.2 0 0;
3 1.0 0.5 0 0;
4 0.3 0.1 0 0
];

% Line Data
line_data = [
1 2 0.05 0.15;
1 3 0.1 0.3 ;
2 3 0.15 0.45;
2 4 0.1 0.3 ;
3 4 0.05 0.15
];

% Number of buses
num_buses = size(bus_data, 1);

% Constructing Ybus matrix


Ybus = zeros(num_buses);
for k = 1:size(line_data, 1)
from_bus = line_data(k, 1);
to_bus = line_data(k, 2);
R = line_data(k, 3);
X = line_data(k, 4);
Y = 1 / (R + 1i * X);

Ybus(from_bus, to_bus) = -Y;


Ybus(to_bus, from_bus) = -Y;
Ybus(from_bus, from_bus) = Ybus(from_bus, from_bus) + Y;
Ybus(to_bus, to_bus) = Ybus(to_bus, to_bus) + Y;
end

% Power Flow Iterations


max_iterations = 20;
tolerance = 1e-6;

V = ones(num_buses, 1); % Initialize voltage magnitudes


delta = zeros(num_buses, 1); % Initialize voltage angles

for iter = 1:max_iterations


V_prev = V;
delta_prev = delta;

for i = 2:num_buses
S_injection = bus_data(i, 2) - 1i * bus_data(i, 3);
% Power flow equation
S = V(i) * conj(Ybus(i, :) * V) - S_injection;

% Jacobian matrix
J11 = real(Ybus(i, i)) * V(i);
J12 = -imag(Ybus(i, i)) * V(i)^2;
J21 = -imag(Ybus(i, i));
J22 = real(Ybus(i, i)) * V(i);

J = [J11, J12; J21, J22];

% Update voltage values


deltaV = J \ [real(S); imag(S)];
delta(i) = delta(i) - deltaV(1);
V(i) = V(i) - deltaV(2);
end

% Check convergence
if max(abs(V - V_prev)) < tolerance && max(abs(delta - delta_prev)) <
tolerance
break;
end
end

% Calculate complex power at Bus 1


S1 = V(1) * conj(Ybus(1, :) * V);

% Determine power flow direction in each line


power_flow_direction = zeros(size(line_data, 1), 1);
for k = 1:size(line_data, 1)
from_bus = line_data(k, 1);
to_bus = line_data(k, 2);

P_flow = real(V(from_bus) * conj(Ybus(from_bus, to_bus)) * V(to_bus));


if P_flow > 0
power_flow_direction(k) = 1; % Power flows from 'from_bus' to
'to_bus'
else
power_flow_direction(k) = -1; % Power flows from 'to_bus' to
'from_bus'
end
end

% Stop timer
elapsed_time = toc;

% Calculate error
error_V = max(abs(V - V_prev));
error_delta = max(abs(delta - delta_prev));

% Display results
disp('Voltage magnitudes and angles at buses 2, 3, and 4:');
disp(abs(V(2:4)));
disp(angle(V(2:4)) * 180 / pi);

disp('Complex power at bus 1:');


disp(S1);
disp('Power flow direction in each line:');
disp(power_flow_direction);

% Report
disp('Number of iterations:');
disp(iter);

disp('Computational speed (elapsed time in seconds):');


disp(elapsed_time);

disp('Accuracy (tolerance):');
disp(tolerance);

disp('Error in voltage magnitudes:');


disp(error_V);

disp('Error in voltage angles:');


disp(error_delta);

GAUSS SIEDEL:
clc
clear all
close all
% Start timer
tic;

% Bus Data
bus_data = [
1 0 0 1.04 0;
2 0.5 0.2 0 0;
3 1.0 0.5 0 0;
4 0.3 0.1 0 0
];

% Line Data
line_data = [
1 2 0.05 0.15;
1 3 0.1 0.3 ;
2 3 0.15 0.45;
2 4 0.1 0.3 ;
3 4 0.05 0.15
];

% Number of buses
num_buses = size(bus_data, 1);

% Constructing Ybus matrix


Ybus = zeros(num_buses);
for k = 1:size(line_data, 1)
from_bus = line_data(k, 1);
to_bus = line_data(k, 2);
R = line_data(k, 3);
X = line_data(k, 4);
Y = 1 / (R + 1i * X);
Ybus(from_bus, to_bus) = -Y;
Ybus(to_bus, from_bus) = -Y;
Ybus(from_bus, from_bus) = Ybus(from_bus, from_bus) + Y;
Ybus(to_bus, to_bus) = Ybus(to_bus, to_bus) + Y;
end

% Power Flow Iterations


max_iterations = 20;
tolerance = 1e-6;

V = ones(num_buses, 1); % Initialize voltage magnitudes


delta = zeros(num_buses, 1); % Initialize voltage angles

for iter = 1:max_iterations


V_prev = V;
delta_prev = delta;

for i = 2:num_buses
S_injection = bus_data(i, 2) - 1i * bus_data(i, 3);

% Gauss-Seidel iteration
V(i) = (1 / Ybus(i, i)) * (S_injection / conj(V(i)) - Ybus(i, :) * V +
Ybus(i, i) * V(i));
end

% Check convergence
if max(abs(V - V_prev)) < tolerance && max(abs(delta - delta_prev)) <
tolerance
break;
end
end

% Calculate complex power at Bus 1


S1 = V(1) * conj(Ybus(1, :) * V);

% Determine power flow direction in each line


power_flow_direction = zeros(size(line_data, 1), 1);
for k = 1:size(line_data, 1)
from_bus = line_data(k, 1);
to_bus = line_data(k, 2);

P_flow = real(V(from_bus) * conj(Ybus(from_bus, to_bus)) * V(to_bus));


if P_flow > 0
power_flow_direction(k) = 1; % Power flows from 'from_bus' to
'to_bus'
else
power_flow_direction(k) = -1; % Power flows from 'to_bus' to
'from_bus'
end
end

% Stop timer
elapsed_time = toc;

% Calculate error
error_V = max(abs(V - V_prev));
error_delta = max(abs(delta - delta_prev));
% Display results
disp('Voltage magnitudes and angles at buses 2, 3, and 4:');
disp(abs(V(2:4)));
disp(angle(V(2:4)) * 180 / pi);

disp('Complex power at bus 1:');


disp(S1);

disp('Power flow direction in each line:');


disp(power_flow_direction);

% Report
disp('Number of iterations:');
disp(iter);

disp('Computational speed (elapsed time in seconds):');


disp(elapsed_time);

disp('Accuracy (tolerance):');
disp(tolerance);

disp('Error in voltage magnitudes:');


disp(error_V);

disp('Error in voltage angles:');


disp(error_delta);

You might also like