# Node analysis with voltage source, How to solve with matlab?

So i have this matlab homework where i need to find voltage on Y2 and Y3 using node analysis. Arrows indicate direction of current flow in branches.

simulate this circuit – Schematic created using CircuitLab

With LTspice i got:

Y2=17.158V and Y3=11.297V

Also solving this system i got the same results:

KCL on Y2 { (Y2+E1)/(R1+R2) + (Y2-Y3+E2)/(R7+R6+R5) + (Y2-Y3)/R8 - 9 = 0;
KCL on Y3 { -(Y2-Y3)/R8 - (Y2-Y3+E2)/(R7+R6+R5) + Y3/(R3+R4) = 0;

The problem starts when i try to solve this system as matrix in matlab. I think i have written the matrix wrong, but i cant figure out how to write it properly. Got confused at E1 and E2.

My incorrect Code:

E1 = 5;
E2 = 10;
J3 = 9;
R1=1;R2=2;R3=3;R4=4;R5=5;R6=6;R7=7;R8=8;

G1=1/R1; G2=1/R2; G3=1/R3; G4=1/R4; G5=1/R5; G6=1/R6; G7=1/R7; G8=1/R8;

G = [G1+G2+G5+G6+G7+G8 -G5-G6-G7-G8;
-G8-G7-G6-G5 G8+G7+G6+G5+G4+G3];

E = [-E1*(G1+G2)-E2*(G5+G6+G7)+J3; E2*(G5+G6+G7)];

U=G\E;

Y2 = U(1)
Y3 = U(2)


Later i will switch E1 and E2 with functions.

Thanks!

I just ignored G=1/R and wrote it with in-built functions like this instead:

E1 = 5;
E2 = 10;
J3 = 9;
R1=1;R2=2;R3=3;R4=4;R5=5;R6=6;R7=7;R8=8;

% Equation

syms Y2 Y3
eqn1 = ((Y2+E1)/(R1+R2))+((Y2-Y3+E2)/(R7+R6+R5))+((Y2-Y3)/R8)==J3;
eqn2 = -((Y2-Y3)/R8)-((Y2-Y3+E2)/(R7+R6+R5))+(Y3/(R3+R4))==0;

% Equation to matrix
[A,B] = equationsToMatrix([eqn1, eqn2], [Y2, Y3])

% format to decimals
A = vpa(A,4);
B = vpa(B,4);
% Solving matrix
U=A\B
Y2 = U(1)
Y3 = U(2)


A =

[  37/72,  -13/72]
[ -13/72, 163/504]

B =

61/9
5/9

U =

17.1584
11.297

Y2 =

17.1584

Y3 =

11.297