Solving large number of equations

5 visualizzazioni (ultimi 30 giorni)
Ismita
Ismita il 15 Mar 2024
Commentato: Ismita il 16 Mar 2024
I am having issues to solve like following equations. I request for help/ suggestion to solve this type of equations. Thanks!
syms X1 X2 X3 X4 X5 X6 X7 X8
p_11 = 1; p_12 = 2; p_13 = 3; p_14 = 4; p_15 = 5; p_16 = 6; p_17 = 7;
p_21 = 8; p_22 = 9; p_23 = 10; p_24 = 11; p_25 = 12; p_26 = 13; p_27 = 14;
p_31 = 15; p_32 = 16; p_33 = 17; p_34 = 18; p_35 = 19; p_36 = 20; p_37 = 21;
p_41 = 22; p_42 = 23; p_43 = 24; p_44 = 25; p_45 = 26; p_46 = 27; p_47 = 28;
p_51 = 29; p_52 = 30; p_53 = 31; p_54 = 32; p_55 = 33; p_56 = 34; p_57 = 35;
p_61 = 36; p_62 = 37; p_63 = 38; p_64 = 39; p_65 = 40; p_66 = 41; p_67 = 42;
p_71 = 43; p_72 = 44; p_73 = 45; p_74 = 46; p_75 = 47; p_76 = 48; p_77 = 49;
p_81 = 50; p_82 = 51; p_83 = 52; p_84 = 53; p_85 = 54; p_86 = 55; p_87 = 56;
C_1 = [4 3 5];
C_2 = [2 4 6];
C_3 = [5 6 8];
C_4 = [1 2 3];
C_5 = [2 3 4];
C_6 = [3 4 5];
C_7 = [2 3 5];
C_8 = [3 6 2];
for i = 1:length(C_1)
eq1(i) = p_11*X1 + p_12*X2 + p_13*X3 + p_14*X4 + p_15*X5 + p_16*X6 + p_17*X7*sin(X8) == C_1(i);
eq2(i) = p_21*X1 + p_22*X2 + p_23*X3 + p_24*X4 + p_25*X5 + p_26*X6 + p_27*X7*sin(X8) == C_2(i);
eq3(i) = p_31*X1 + p_32*X2 + p_33*X3 + p_34*X4 + p_35*X5 + p_36*X6 + p_37*X7*cos(X8) == C_3(i);
eq4(i) = p_41*X1 + p_42*X2 + p_43*X3 + p_44*X4 + p_45*X5 + p_46*X6 + p_47*X7*sin(X8) == C_4(i);
eq5(i) = p_51*X1 + p_52*X2 + p_53*X3 + p_54*X4 + p_55*X5 + p_56*X6 + p_57*X7*sin(X8) == C_5(i);
eq6(i) = p_61*X1 + p_62*X2 + p_63*X3 + p_64*X4 + p_65*X5 + p_66*X6 + p_67*X7*sin(X8) == C_6(i);
eq7(i) = p_71*X1 + p_72*X2 + p_73*X3 + p_74*X4 + p_75*X5 + p_76*X6 + p_77*X7*sin(X8) == C_7(i);
eq8(i) = p_81*X1 + p_82*X2 + p_83*X3 + p_84*X4 + p_85*X5 + p_86*X6 + p_87*X7 == C_8(i);
end
% Solve the system of equations
sol = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8], [X1, X2, X3, X4, X5, X6, X7, X8]);
% Display solutions
disp(sol)
X1: [0x1 sym] X2: [0x1 sym] X3: [0x1 sym] X4: [0x1 sym] X5: [0x1 sym] X6: [0x1 sym] X7: [0x1 sym] X8: [0x1 sym]

Risposta accettata

Walter Roberson
Walter Roberson il 15 Mar 2024
syms X1 X2 X3 X4 X5 X6 X7 X8
p_11 = 1; p_12 = 2; p_13 = 3; p_14 = 4; p_15 = 5; p_16 = 6; p_17 = 7;
p_21 = 8; p_22 = 9; p_23 = 10; p_24 = 11; p_25 = 12; p_26 = 13; p_27 = 14;
p_31 = 15; p_32 = 16; p_33 = 17; p_34 = 18; p_35 = 19; p_36 = 20; p_37 = 21;
p_41 = 22; p_42 = 23; p_43 = 24; p_44 = 25; p_45 = 26; p_46 = 27; p_47 = 28;
p_51 = 29; p_52 = 30; p_53 = 31; p_54 = 32; p_55 = 33; p_56 = 34; p_57 = 35;
p_61 = 36; p_62 = 37; p_63 = 38; p_64 = 39; p_65 = 40; p_66 = 41; p_67 = 42;
p_71 = 43; p_72 = 44; p_73 = 45; p_74 = 46; p_75 = 47; p_76 = 48; p_77 = 49;
p_81 = 50; p_82 = 51; p_83 = 52; p_84 = 53; p_85 = 54; p_86 = 55; p_87 = 56;
C_1 = [4 3 5];
C_2 = [2 4 6];
C_3 = [5 6 8];
C_4 = [1 2 3];
C_5 = [2 3 4];
C_6 = [3 4 5];
C_7 = [2 3 5];
C_8 = [3 6 2];
for i = 1:length(C_1)
eq1(i) = p_11*X1 + p_12*X2 + p_13*X3 + p_14*X4 + p_15*X5 + p_16*X6 + p_17*X7*sin(X8) == C_1(i);
eq2(i) = p_21*X1 + p_22*X2 + p_23*X3 + p_24*X4 + p_25*X5 + p_26*X6 + p_27*X7*sin(X8) == C_2(i);
eq3(i) = p_31*X1 + p_32*X2 + p_33*X3 + p_34*X4 + p_35*X5 + p_36*X6 + p_37*X7*cos(X8) == C_3(i);
eq4(i) = p_41*X1 + p_42*X2 + p_43*X3 + p_44*X4 + p_45*X5 + p_46*X6 + p_47*X7*sin(X8) == C_4(i);
eq5(i) = p_51*X1 + p_52*X2 + p_53*X3 + p_54*X4 + p_55*X5 + p_56*X6 + p_57*X7*sin(X8) == C_5(i);
eq6(i) = p_61*X1 + p_62*X2 + p_63*X3 + p_64*X4 + p_65*X5 + p_66*X6 + p_67*X7*sin(X8) == C_6(i);
eq7(i) = p_71*X1 + p_72*X2 + p_73*X3 + p_74*X4 + p_75*X5 + p_76*X6 + p_77*X7*sin(X8) == C_7(i);
eq8(i) = p_81*X1 + p_82*X2 + p_83*X3 + p_84*X4 + p_85*X5 + p_86*X6 + p_87*X7 == C_8(i);
end
eqns = [eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8];
size(eqns)
ans = 1×2
1 24
symvar(eqns)
ans = 
You have 24 equations in 8 variables, and you are trying to solve them for 8 variables.
% Solve the system of equations
sol = solve(eqns, [X1, X2, X3, X4, X5, X6, X7, X8]);
solve() will immediately give up on trying to solve the 24 equations for 8 variables.
% Display solutions
disp(sol)
X1: [0×1 sym] X2: [0×1 sym] X3: [0×1 sym] X4: [0×1 sym] X5: [0×1 sym] X6: [0×1 sym] X7: [0×1 sym] X8: [0×1 sym]
If you are trying to do a least-squared fit then solve() of more equations than variables is the wrong way to do it.
  3 Commenti
Torsten
Torsten il 16 Mar 2024
Modificato: Torsten il 16 Mar 2024
"solve" from above says your system does not have a solution.
"fsolve" as a numerical solver also cannot find a solution for your system (see below).
As you can see, the Jacobian of your system has only rank 4 instead of rank 8. Thus it will be almost impossible to solve it for arbitrary right-hand sides.
p_11 = 1; p_12 = 2; p_13 = 3; p_14 = 4; p_15 = 5; p_16 = 6; p_17 = 7;
p_21 = 8; p_22 = 9; p_23 = 10; p_24 = 11; p_25 = 12; p_26 = 13; p_27 = 14;
p_31 = 15; p_32 = 16; p_33 = 17; p_34 = 18; p_35 = 19; p_36 = 20; p_37 = 21;
p_41 = 22; p_42 = 23; p_43 = 24; p_44 = 25; p_45 = 26; p_46 = 27; p_47 = 28;
p_51 = 29; p_52 = 30; p_53 = 31; p_54 = 32; p_55 = 33; p_56 = 34; p_57 = 35;
p_61 = 36; p_62 = 37; p_63 = 38; p_64 = 39; p_65 = 40; p_66 = 41; p_67 = 42;
p_71 = 43; p_72 = 44; p_73 = 45; p_74 = 46; p_75 = 47; p_76 = 48; p_77 = 49;
p_81 = 50; p_82 = 51; p_83 = 52; p_84 = 53; p_85 = 54; p_86 = 55; p_87 = 56;
C_1 = [4 3 5];
C_2 = [2 4 6];
C_3 = [5 6 8];
C_4 = [1 2 3];
C_5 = [2 3 4];
C_6 = [3 4 5];
C_7 = [2 3 5];
C_8 = [3 6 2];
for i = 1:length(C_1)
f1 = @(X1,X2,X3,X4,X5,X6,X7,X8) p_11*X1 + p_12*X2 + p_13*X3 + p_14*X4 + p_15*X5 + p_16*X6 + p_17*X7*sin(X8) - C_1(i);
f2 = @(X1,X2,X3,X4,X5,X6,X7,X8) p_21*X1 + p_22*X2 + p_23*X3 + p_24*X4 + p_25*X5 + p_26*X6 + p_27*X7*sin(X8) - C_2(i);
f3 = @(X1,X2,X3,X4,X5,X6,X7,X8) p_31*X1 + p_32*X2 + p_33*X3 + p_34*X4 + p_35*X5 + p_36*X6 + p_37*X7*cos(X8) - C_3(i);
f4 = @(X1,X2,X3,X4,X5,X6,X7,X8) p_41*X1 + p_42*X2 + p_43*X3 + p_44*X4 + p_45*X5 + p_46*X6 + p_47*X7*sin(X8) - C_4(i);
f5 = @(X1,X2,X3,X4,X5,X6,X7,X8) p_51*X1 + p_52*X2 + p_53*X3 + p_54*X4 + p_55*X5 + p_56*X6 + p_57*X7*sin(X8) - C_5(i);
f6 = @(X1,X2,X3,X4,X5,X6,X7,X8) p_61*X1 + p_62*X2 + p_63*X3 + p_64*X4 + p_65*X5 + p_66*X6 + p_67*X7*sin(X8) - C_6(i);
f7 = @(X1,X2,X3,X4,X5,X6,X7,X8) p_71*X1 + p_72*X2 + p_73*X3 + p_74*X4 + p_75*X5 + p_76*X6 + p_77*X7*sin(X8) - C_7(i);
f8 = @(X1,X2,X3,X4,X5,X6,X7,X8) p_81*X1 + p_82*X2 + p_83*X3 + p_84*X4 + p_85*X5 + p_86*X6 + p_87*X7 - C_8(i);
f = @(X)[f1(X(1),X(2),X(3),X(4),X(5),X(6),X(7),X(8));
f2(X(1),X(2),X(3),X(4),X(5),X(6),X(7),X(8));
f3(X(1),X(2),X(3),X(4),X(5),X(6),X(7),X(8));
f4(X(1),X(2),X(3),X(4),X(5),X(6),X(7),X(8));
f5(X(1),X(2),X(3),X(4),X(5),X(6),X(7),X(8));
f6(X(1),X(2),X(3),X(4),X(5),X(6),X(7),X(8));
f7(X(1),X(2),X(3),X(4),X(5),X(6),X(7),X(8));
f8(X(1),X(2),X(3),X(4),X(5),X(6),X(7),X(8))];
X = sym('X',[8 1]);
J = jacobian(f(X))
rank(J)
X0 = [1;2;3;4;5;6;7;8];
sol(i,:) = fsolve(f,X0)
end
J = 
ans = 4
Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 8.000000e+02.
sol = 1×8
1.0e+06 * -0.0003 0.0204 -1.6787 0.0627 4.8308 -3.2348 0.0000 0.0000
J = 
ans = 4
Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 8.000000e+02.
sol = 2×8
1.0e+06 * -0.0003 0.0204 -1.6787 0.0627 4.8308 -3.2348 0.0000 0.0000 0.0023 -1.9292 0.0913 3.6340 0.1631 -1.9616 0.0000 0.0000
J = 
ans = 4
Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 8.000000e+02.
sol = 3×8
1.0e+06 * -0.0003 0.0204 -1.6787 0.0627 4.8308 -3.2348 0.0000 0.0000 0.0023 -1.9292 0.0913 3.6340 0.1631 -1.9616 0.0000 0.0000 0.0000 -0.1246 1.9481 -1.2147 -2.9165 2.3077 -0.0000 0.0000
Ismita
Ismita il 16 Mar 2024
Thank you so much @Torsten.

Accedi per commentare.

Più risposte (1)

John D'Errico
John D'Errico il 15 Mar 2024
Modificato: John D'Errico il 15 Mar 2024
  1. This is not even a remotely large number of equations.
  2. Will you please learn to use matrices?
  3. X8 appears in only one form in your equations, as X7*sin(X8). Therefore, you could transform the problem trivially, by substituting a single new unknown into the problem. That is...
Y8 = X7*sin(X8).
Replace the product X7*sin(X8) in each place it appears in your system. Your system is now linear, in the set of unknowns {X1,X2,X3,X4,X5,X67,X7,Y8}. Solve that LINEAR system. Once you have found the values of X7 and Y8, you can recover X8.
X8 = asin(Y8/X7)
Remember this means there will be infinitely many equivalent solutions, but so what? And of course, if X7 were exactly zero, then no solution exists. Finally, if it is true that abs(Y8/X7)>1, then the only solutions possible will be complex solutions. Such is life.
So just use backslash to solve the problem. AND PLEASE LEARN TO USE MATRICES.
  3 Commenti
Torsten
Torsten il 15 Mar 2024
X7*cos(X8) in eq3 and p_87*X7 in eq8 unfortunately hinder to convert the system to a linear one.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by