SOLVING LARGE SYSTEM OF EQUATIONS

13 views (last 30 days)
idriskameni
idriskameni on 5 Apr 2019
Commented: Stephan on 5 Apr 2019
Hi, this is my code (the only think that matters is the last line)
a = sym('a',[1 15]);
sym r
p1 = poly2sym(a,r);
diff = sym('diff');
eq = sym('eq');
fprintf('\n polynomial: %s\n', p1);
p2 =poly2sym(a,r);
p2=subs(p2,r,r-1);
%fprintf('\n p2: %s\n', p2);
diff= p1-p2;
%fprintf('\n diff: %s\n', simplify(diff));
diff = coeffs(diff,r);
branch2=prod(r-3:r+5)*(((1/1920)*(r+4)*(r+3)*(r^2+r-10))+((1/5120)*r*(r+1)*(r^2+r-10))+((1/9216)*(r+4)*(r+3)*(r+3)*(r-2)*(r-4)));
%fprintf('\n branch: %s\n', simplify(branch2));
%display(diff);
branch2=coeffs(branch2,r);
for u=1:14
%fprintf('DIFF: %d = %s\n', u, diff(u));
%fprintf('BRANCH: %d = %s\n', u, branch2(u));
eq(u)= diff(u)==branch2(u);
fprintf('EQ %d : %s\n', u, eq(u));
%eqcoef(u)=coeffs(diff(u),[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14]);
% fprintf('EQCOEF %d : %s\n', u, eqcoef(u));
end
[A,B] = equationsToMatrix([eq(1), eq(2), eq(3),eq(4), eq(5), eq(6),eq(7), eq(8), eq(9),eq(10), eq(11), eq(12),eq(13),eq(14)], [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14]);
I would like to solve the system. But this gives me an error that a1 is not defined. But it is. Any help?

Accepted Answer

Stephan
Stephan on 5 Apr 2019
Hi,
try:
a = sym('a',[1 15]);
syms r
p1 = poly2sym(a,r);
diff = sym('diff');
eq = sym('eq');
fprintf('\n polynomial: %s\n', p1);
p2 =poly2sym(a,r);
p2=subs(p2,r,r-1);
%fprintf('\n p2: %s\n', p2);
diff= p1-p2;
%fprintf('\n diff: %s\n', simplify(diff));
diff = coeffs(diff,r);
branch2=prod(r-3:r+5)*(((1/1920)*(r+4)*(r+3)*(r^2+r-10))+((1/5120)*r*(r+1)*(r^2+r-10))+((1/9216)*(r+4)*(r+3)*(r+3)*(r-2)*(r-4)));
%fprintf('\n branch: %s\n', simplify(branch2));
%display(diff);
branch2=coeffs(branch2,r);
for u=1:14
%fprintf('DIFF: %d = %s\n', u, diff(u));
%fprintf('BRANCH: %d = %s\n', u, branch2(u));
eq(u)= diff(u)==branch2(u);
fprintf('EQ %d : %s\n', u, eq(u));
%eqcoef(u)=coeffs(diff(u),[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14]);
% fprintf('EQCOEF %d : %s\n', u, eqcoef(u));
end
[A,B] = equationsToMatrix([eq(1), eq(2), eq(3),eq(4), eq(5), eq(6),eq(7), eq(8), eq(9),eq(10), eq(11), eq(12),eq(13),eq(14)],a);
this results in:
polynomial: a15 + a14*r + a1*r^14 + a2*r^13 + a3*r^12 + a4*r^11 + a5*r^10 + a6*r^9 + a7*r^8 + a8*r^7 + a9*r^6 + a10*r^5 + a11*r^4 + a12*r^3 + a13*r^2
EQ 1 : a2 - a1 - a3 + a4 - a5 + a6 - a7 + a8 - a9 + a10 - a11 + a12 - a13 + a14 == 45/2
EQ 2 : 14*a1 - 13*a2 + 12*a3 - 11*a4 + 10*a5 - 9*a6 + 8*a7 - 7*a8 + 6*a9 - 5*a10 + 4*a11 - 3*a12 + 2*a13 == 945/32
EQ 3 : 78*a2 - 91*a1 - 66*a3 + 55*a4 - 45*a5 + 36*a6 - 28*a7 + 21*a8 - 15*a9 + 10*a10 - 6*a11 + 3*a12 == -10541/640
EQ 4 : 364*a1 - 286*a2 + 220*a3 - 165*a4 + 120*a5 - 84*a6 + 56*a7 - 35*a8 + 20*a9 - 10*a10 + 4*a11 == -50033/1280
EQ 5 : 715*a2 - 1001*a1 - 495*a3 + 330*a4 - 210*a5 + 126*a6 - 70*a7 + 35*a8 - 15*a9 + 5*a10 == -275051/23040
EQ 6 : 2002*a1 - 1287*a2 + 792*a3 - 462*a4 + 252*a5 - 126*a6 + 56*a7 - 21*a8 + 6*a9 == 86971/9216
EQ 7 : 1716*a2 - 3003*a1 - 924*a3 + 462*a4 - 210*a5 + 84*a6 - 28*a7 + 7*a8 == 38813/5760
EQ 8 : 3432*a1 - 1716*a2 + 792*a3 - 330*a4 + 120*a5 - 36*a6 + 8*a7 == 7883/23040
EQ 9 : 1287*a2 - 3003*a1 - 495*a3 + 165*a4 - 45*a5 + 9*a6 == -3233/3840
EQ 10 : 2002*a1 - 715*a2 + 220*a3 - 55*a4 + 10*a5 == -181/768
EQ 11 : 286*a2 - 1001*a1 - 66*a3 + 11*a4 == 7/720
EQ 12 : 364*a1 - 78*a2 + 12*a3 == 311/23040
EQ 13 : 13*a2 - 91*a1 == 49/23040
EQ 14 : 14*a1 == 1/9216
>> A
A =
[ -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 0]
[ 14, -13, 12, -11, 10, -9, 8, -7, 6, -5, 4, -3, 2, 0, 0]
[ -91, 78, -66, 55, -45, 36, -28, 21, -15, 10, -6, 3, 0, 0, 0]
[ 364, -286, 220, -165, 120, -84, 56, -35, 20, -10, 4, 0, 0, 0, 0]
[ -1001, 715, -495, 330, -210, 126, -70, 35, -15, 5, 0, 0, 0, 0, 0]
[ 2002, -1287, 792, -462, 252, -126, 56, -21, 6, 0, 0, 0, 0, 0, 0]
[ -3003, 1716, -924, 462, -210, 84, -28, 7, 0, 0, 0, 0, 0, 0, 0]
[ 3432, -1716, 792, -330, 120, -36, 8, 0, 0, 0, 0, 0, 0, 0, 0]
[ -3003, 1287, -495, 165, -45, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[ 2002, -715, 220, -55, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[ -1001, 286, -66, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[ 364, -78, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[ -91, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[ 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>> B
B =
45/2
945/32
-10541/640
-50033/1280
-275051/23040
86971/9216
38813/5760
7883/23040
-3233/3840
-181/768
7/720
311/23040
49/23040
1/9216
Best regards
Stephan
  2 Comments
idriskameni
idriskameni on 5 Apr 2019
But this is not correct. Since the coefficients of a_1 are the ones in the diagonal. But they should be the ones in the first column. How can I fix that?
Thank you anyways!
Stephan
Stephan on 5 Apr 2019
For me the coefficients of a1 are in the first column.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!