Shreyas 1NT22EE099 POWERSYSTEM
Shreyas 1NT22EE099 POWERSYSTEM
clc;
disp('Z-bus Matrix Formulation');
numBuses = input('Enter the total number of buses: ');
zbus = zeros(numBuses);
continueLoop = true;
while continueLoop
fprintf('Current Z-bus Matrix:\n');
disp(zbus);
fprintf('\nMenu:\n');
fprintf('1. Connect new bus to reference bus\n');
fprintf('2. Connect existing bus to new bus\n');
fprintf('3. Connect between existing buses\n');
fprintf('4. Connect existing bus to reference bus\n');
fprintf('5. Display Z-bus Matrix\n');
fprintf('6. Visualize Z-bus Matrix\n');
fprintf('7. Exit\n');
choice = input('Enter your choice: ');
switch choice
case 1
zb = input('Enter the impedance value: ');
newBus = input('Enter the new bus number: ');
if newBus > 0 && newBus <= numBuses && zb > 0
zbus(newBus, newBus) = zb;
else
disp('Error: Invalid bus number or impedance value.');
end
case 2
existingBus = input('Enter the existing bus number: ');
newBus = input('Enter the new bus number: ');
zb = input('Enter the impedance value: ');
if existingBus > 0 && newBus > 0 && existingBus <= numBuses && newBus
<= numBuses && zb > 0
zbus(newBus, newBus) = zbus(existingBus, existingBus) + zb;
for i = 1:numBuses
if i ~= newBus
zbus(i, newBus) = zbus(i, existingBus);
zbus(newBus, i) = zbus(existingBus, i);
end
end
else
disp('Error: Invalid input. Please check the bus numbers and
impedance value.');
end
case 3
bus1 = input('Enter the first bus number: ');
bus2 = input('Enter the second bus number: ');
zb = input('Enter the impedance value: ');
if bus1 > 0 && bus2 > 0 && bus1 <= numBuses && bus2 <= numBuses && zb >
0
deltaZ = zb + zbus(bus1, bus1) + zbus(bus2, bus2) - 2 * zbus(bus1,
bus2);
if deltaZ ~= 0
ztemp = (1 / deltaZ) * ((zbus(:, bus1) - zbus(:, bus2)) *
(zbus(bus1, :) - zbus(bus2, :)));
zbus = zbus - ztemp;
else
disp('Error: Division by zero encountered in deltaZ
calculation.');
end
else
disp('Error: Invalid bus numbers or impedance value.');
end
case 4
existingBus = input('Enter the existing bus number: ');
zb = input('Enter the impedance value: ');
if existingBus > 0 && existingBus <= numBuses && zb > 0
deltaZ = zbus(existingBus, existingBus) + zb;
if deltaZ ~= 0
ztemp = (1 / deltaZ) * (zbus(:, existingBus) *
zbus(existingBus, :));
zbus = zbus - ztemp;
else
disp('Error: Division by zero encountered in deltaZ
calculation.');
end
else
disp('Error: Invalid bus number or impedance value.');
end
case 5
fprintf('Z-bus Matrix:\n');
disp(zbus);
case 6
figure;
heatmap(zbus);
title('Z-bus Matrix Heatmap');
xlabel('Bus Number');
ylabel('Bus Number');
G = graph(zbus ~= 0);
plot(G, 'EdgeLabel', G.Edges.Weight, 'NodeLabel', 1:numBuses);
title('Bus Network Graph');
xlabel('Bus Number');
ylabel('Bus Number');
case 7
disp('Exiting program.');
continueLoop = false;
otherwise
disp('Error: Invalid choice. Please select a valid option.');
end
end