% Unit 3 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%===================
% Wheatstone Bridge
%===================
clear all
close all
clc

% setup
V  = 5;
R1 = 10;
R2 = linspace(0,50,100);
R3 = 15;
R4 = 20;
R5 = 3;

% solve linear system for every single R2
b = [0; 0; 0; 0; V];

I = zeros(length(R2),5);
for i=1:length(R2)
    M = [ -1,      0, +1,    0,  -1;
           0,     -1,  0,   +1,  +1;
         -R1, +R2(i),  0,    0, +R5;
           0,      0, -R3, +R4, -R5;
          R1,      0, +R3,   0,   0];
    I(i,:) = inv(M)*b;
end

% visualize currents as a function of R2
figure(1)
hold on
plot(R2,I(:,1),'r')
plot(R2,I(:,2),'g')
plot(R2,I(:,3),'b')
plot(R2,I(:,4),'k')
plot(R2,I(:,5),'m')
plot(R2,zeros(1,length(R2)),'k')
plot(R4*R1/R3,0,'o')
xlabel('R2 [Ohm]')
ylabel('I [Ampere]')
legend('I1','I2','I3','I4','I5')
