function [I,PR,PV] = Circuit_C3_JMS( R, V )
% Problem 2 of control 3. 2023/05/05. Jose M. Soler
% Input:
%   R(6,1) : circuit resistances (Ohm)
%   V(3,1) : circuit source voltages (V)
% Output:
%   I(6,1)  : circuit intensities (Amp)
%   PR(6,1) : power consumed by circuit resistances (W)
%   PV(3,1) : power produced by circuit sources (W)

% Set Kirchoff equations matrices
Rmat = [   +1,   -1,    0,   +1,   -1,    0
            0,   +1,   -1,    0,   +1,   -1 
        -R(1),    0,    0,+R(4),    0,    0
            0,-R(2),    0,    0,+R(5),    0
            0,    0,-R(3),    0,    0,+R(6)
            0,    0,    0,+R(4),+R(5),+R(6)];
Vvec = [ 0; 0; V(1); V(2); V(3); V(1)+V(2)+V(3) ];

% Solve Kirchoff eqs. to find intensities
I = Rmat\Vvec;

% Find powers
PR = R.*I.^2;
PV = [ V(1)*I(4); V(2)*I(5); V(3)*I(6) ];

end
