Write a MATLAB program to solve N simultaneous equations Inc
Solution
1)
let Say x1 = x, x2 =y and x3=z
Declare the system of equations.
>> syms x y z
>> eqn1 = x +4*y +7*z == 10;
>> eqn2 = 2*x +9*y - z == 15;
>> eqn3 = 12*x +6*z == 20;
% Use equationsToMatrix to convert the equations into the form AX = B. The second input to equationsToMatrix specifies the independent variables in the equations.%
>> [A,B]= equationsToMatrix([eqn1,eqn2,eqn3],[x,y,z])
A =
[ 1, 4, 7]
[ 2, 9, -1]
[ 12, 0, 6]
B =
10
15
20
% Use linsolve to solve AX = B for the vector of unknowns X.%
>> X=linsolve(A,B)
X =
580/399 =x=x1
185/133 =y=x2
170/399 =z=x3
2) similarly,
>> syms x1 x2
>> eqn1=3*x1 +4*x2 ==52;
>> eqn2=2*x1-3*x2 == -5;
>> [A,B]= equationsToMatrix([eqn1,eqn2],[x1,x2])
A =
[ 3, 4]
[ 2, -3]
B =
52
-5
>> X=linsolve(A,B)
X =
8
7
Solution are x1 = 8, and x2 =7
3)
>> syms x1 x2 x3 x4 x5
>> eqn1= x1+ 2*x2+3*x3+4*x4+5*x5 == 30;
>> eqn2= 10*x1+11*x2+12*x3 ==40;
>> eqn3= 21*x1+22*x3+23*x4 ==50;
>> eqn4=x1-x2+x3-x4+x5 ==60;
>> eqn5=9*x1+8*x2+7*x3+6*x4+5*x5 ==70;
>> [A,B]= equationsToMatrix([eqn1,eqn2],[x1,x2,x3,x4,x5])
>> [A,B]= equationsToMatrix([eqn1,eqn2,eqn3,eqn4,eqn5],[x1,x2,x3,x4,x5])
A =
[ 1, 2, 3, 4, 5]
[ 10, 11, 12, 0, 0]
[ 21, 0, 22, 23, 0]
[ 1, -1, 1, -1, 1]
[ 9, 8, 7, 6, 5]
B =
30
40
50
60
70
>> X=linsolve(A,B)
X =
10480/607
-7110/607
-385/1214
-8065/607
21915/1214
Therefore, the solutiond are x1=10480/607 , x2= -7110/607 , x3 = -385/1214, x4= -8065/607 , x5= 21915/1214

