Effacer les filtres
Effacer les filtres

Solving multiple equations with constants like c_i

1 vue (au cours des 30 derniers jours)
Ismita
Ismita le 14 Mar 2024
Commenté : Ismita le 18 Mar 2024
I request for help/ any suggestions. I need to solve for X1, X2, X3, X4, X5, X6, X7, X8
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);
"
I have written the following code but it is not running
% Define symbolic variables
syms X1 X2 X3 X4 X5 X6 X7 X8;
% Define symbolic parameters and constants
syms p_11 p_12 p_13 p_14 p_15 p_16 p_17 p_21 p_22 p_23 p_24 p_25 p_26 p_27 ...
p_31 p_32 p_33 p_34 p_35 p_36 p_37 p_41 p_42 p_43 p_44 p_45 p_46 p_47 ...
p_51 p_52 p_53 p_54 p_55 p_56 p_57 p_61 p_62 p_63 p_64 p_65 p_66 p_67 ...
p_71 p_72 p_73 p_74 p_75 p_76 p_77 p_81 p_82 p_83 p_84 p_85 p_86 p_87 ...
C_1 C_2 C_3 C_4 C_5 C_6 C_7 C_8;
p_11_val = 1;
p_12_val = 2;
p_13_val = 3;
p_14_val = 4;
p_15_val = 5;
p_16_val = 6;
p_17_val = 7;
p_21_val = 8;
p_22_val = 9;
p_23_val = 10;
p_24_val = 11;
p_25_val = 12;
p_26_val = 13;
p_27_val = 14;
p_31_val = 15;
p_32_val = 16;
p_33_val = 17;
p_34_val = 18;
p_35_val = 19;
p_36_val = 20;
p_37_val = 21;
p_41_val = 22;
p_42_val = 23;
p_43_val = 24;
p_44_val = 25;
p_45_val = 26;
p_46_val = 27;
p_47_val = 28;
p_51_val = 29;
p_52_val = 30;
p_53_val = 31;
p_54_val = 32;
p_55_val = 33;
p_56_val = 34;
p_57_val = 35;
p_61_val = 36;
p_62_val = 37;
p_63_val = 38;
p_64_val = 39;
p_65_val = 40;
p_66_val = 41;
p_67_val = 42;
p_71_val = 43;
p_72_val = 44;
p_73_val = 45;
p_74_val = 46;
p_75_val = 47;
p_76_val = 48;
p_77_val = 49;
p_81_val = 50;
p_82_val = 51;
p_83_val = 52;
p_84_val = 53;
p_85_val = 54;
p_86_val = 55;
p_87_val = 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];
% Define arrays to store equations
eq1 = sym(zeros(1, length(C_1)));
eq2 = sym(zeros(1, length(C_1)));
eq3 = sym(zeros(1, length(C_1)));
eq4 = sym(zeros(1, length(C_1)));
eq5 = sym(zeros(1, length(C_1)));
eq6 = sym(zeros(1, length(C_1)));
eq7 = sym(zeros(1, length(C_1)));
eq8 = sym(zeros(1, length(C_1)));
% Populate the arrays with equations for each value of C_i
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 for each value of C_i
solutions = cell(length(C_1), 1);
for i = 1:length(C_1)
solutions{i} = solve([eq1(i), eq2(i), eq3(i), eq4(i), eq5(i), eq6(i), eq7(i), eq8(i)], [X1, X2, X3, X4, X5, X6, X7, X8]);
end
% Display solutions for each value of C_i
for i = 1:length(C_1)
fprintf('Solutions for C_1(%d):\n', i);
disp(solutions{i});
end

Réponse acceptée

Shubham
Shubham le 18 Mar 2024
Modifié(e) : Shubham le 18 Mar 2024
Hey Ismita,
The symbolic variables and the coefficients in the equations are different. You can rename your variables by removing "_val". For e.g. rename "p_11_val" to "p_11" while initializing the variables.
Even after making the necessary changes, "solve()" method is returning empty object. Your system of equations does not have a solution for the provided constants. Please refer to the following documentation of "solve": https://www.mathworks.com/help/symbolic/sym.solve.html
I would suggest you to use matrices for creating the system of equations to make the code more modular and readable. You can create a matrix of symbolic variables as well. Please have a look at the following: https://www.mathworks.com/help/symbolic/syms.html#mw_0d627db4-8cb4-4366-b0f6-3ddce83d90b0
Have a look at the following code snippet:
% Define symbolic variables
syms X C [2 1];
syms P [2 2];
% Define the knowns in matrices
P = [2 3; 4 -1];
C = [5; 3];
% Obtain the system of eqns and solve
eqns = P * X == C;
solution = solve(eqns, X)
solution = struct with fields:
X1: 1 X2: 1
You can similarly populate the coefficient and constant matrices and obtain the system of equations and pass them into the "solve" function to get results.
I hope this helps!

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by