Solving equations using Newton-Raphson method for 7 equations with 7 variables

18 visualizaciones (últimos 30 días)
I need to solve "PX_i = C_i" (i = 1,2,...7) using Newton-Raphson method. I have written the code in two files. One is unction and using another one I am trying to find the solution. But it is giving me a lot of errors and Nan value. I request for help/any suggestion regarding this problem. where
% Coefficients matrix P
P = [p11, p12, p13, p14, p15, p16, p17;
p21, p22, p23, p24, p25, p26, p27;
p31, p32, p33, p34, p35, p36, p37;
p41, p42, p43, p44, p45, p46, p47;
p51, p52, p53, p54, p55, p56, p57;
p61, p62, p63, p64, p65, p66, p67;
p71, p72, p73, p74, p75, p76, p77];
% Constants vector C
C = [C1; C2; C3; C4; C5; C6; C7];
How I tried:
I have a file "newtons_method.m" for function as
function X = newtons_method(a, b, X0, tol, max_iter)
% Function to solve a system of linear equations using Newton's method
% Define the function F(X) and its Jacobian J(X)
F = @(X) (a * X - b);
J = @(X) a;
% Initialize X
X = X0;
% Perform Newton's method iterations
for iter = 1:max_iter
% Evaluate F(X) and J(X)
F_val = F(X);
J_val = J(X);
% Compute the update using matrix division (avoiding explicit inversion)
delta_X = - J_val\F_val;
% Update X
X = X + delta_X;
% Check convergence
if norm(F_val) < tol
disp(['Converged at iteration ', num2str(iter)]);
return;
end
end
% If max_iter is reached without convergence
warning('Did not converge within maximum iterations');
end
Using that function I am trying to run it for the equations as follows
% Coefficients and constants
a_11 = 1; a_12 = 2; a_13 = 3; a_14 = 4; a_15 = 5; a_16 = 6; a_17 = 7;
a_21 = 2; a_22 = 3; a_23 = 4; a_24 = 5; a_25 = 6; a_26 = 7; a_27 = 8;
a_31 = 3; a_32 = 4; a_33 = 5; a_34 = 6; a_35 = 7; a_36 = 8; a_37 = 9;
a_41 = 4; a_42 = 5; a_43 = 6; a_44 = 7; a_45 = 8; a_46 = 9; a_47 = 10;
a_51 = 5; a_52 = 6; a_53 = 7; a_54 = 8; a_55 = 9; a_56 = 10; a_57 = 11;
a_61 = 6; a_62 = 7; a_63 = 8; a_64 = 9; a_65 = 10; a_66 = 11; a_67 = 12;
a_71 = 7; a_72 = 8; a_73 = 9; a_74 = 10; a_75 = 11; a_76 = 12; a_77 = 13;
b_1 = 14; b_2 = 15; b_3 = 16; b_4 = 17; b_5 = 18; b_6 = 19; b_7 = 20;
a = [a_11, a_12, a_13, a_14, a_15, a_16, a_17;
a_21, a_22, a_23, a_24, a_25, a_26, a_27;
a_31, a_32, a_33, a_34, a_35, a_36, a_37;
a_41, a_42, a_43, a_44, a_45, a_46, a_47;
a_51, a_52, a_53, a_54, a_55, a_56, a_57;
a_61, a_62, a_63, a_64, a_65, a_66, a_67;
a_71, a_72, a_73, a_74, a_75, a_76, a_77];
b = [b_1; b_2; b_3; b_4; b_5; b_6; b_7];
% Initial guess
x0 = zeros(7, 1);
% Tolerance and maximum number of iterations
tol = 1e-6;
max_iter = 100;
% Call the Newton's method function
X = newtons_method_7eq(a, b, x0, tol, max_iter);
disp('Solution:');
disp(X);
  2 comentarios
Ismita
Ismita el 4 de Mzo. de 2024
Movida: John D'Errico el 5 de Mzo. de 2024
Could you please help/suggest me to solve the following equations (C_i, i = 1,2....8) are the constants/measured values. Thanks a lot
% Given equation
C_1 = (delta_rho_m / rho_0) + sin(psi) * (delta_u_mx / U_0) + cos(psi) * (delta_u_mz / U_0);
C_2 = sin(psi) * (delta_rho_m / rho_0) + (1 + sin(psi)^2) * (delta_u_mx / U_0) + ...
sin(psi) * cos(psi) * (delta_u_mz / U_0) + (sin(psi) / (M_0^2)) * (delta_p_m / (rho_0 * a_0^2));
C_3 = delta_u_my / U_0;
% Given equation
C_4 = cos(psi) * (delta_rho_m / rho_0) + sin(psi) * cos(psi) * (delta_u_mx / U_0) + ...
(1 + cos(psi)^2) * (delta_u_mz / U_0) + (cos(psi) / (M_0^2)) * (delta_p_m / (rho_0 * a_0^2));
C_5 = sin(psi) * (delta_u_mx / U_0) + cos(psi) * (delta_u_mz / U_0) + ...
(a_0^2 / (U_0^2 * (gamma - 1))) * ((delta_p_m / P_0) - (delta_rho_m / rho_0)) + ...
(epsilon_0 / (U_0^2)) * (delta_rho_m / rho_0 + sin(psi) * (delta_u_mx / U_0) + cos(psi) * (delta_u_mz / U_0));
C_6 = delta_u_mx / U_0;
C_7 = delta_u_mz / U_0;
C_8 = delta_rho_m / rho_0;
% C_9 = delta_p_m / (rho_0 * a_0^2);
% Define the equations
C1 = 'p11*X1 + p12*X2 + p13*X3 + p14*X4 + p15*X5 + p16*X6 + p17*X7*sin(X8)';
C2 = 'p21*X1 + p22*X2 + p23*X3 + p24*X4 + p25*X5 + p26*X6 + p27*X7*sin(X8)';
C3 = 'p31*X1 + p32*X2 + p33*X3 + p34*X4 + p35*X5 + p36*X6 + p37*X7*cos(X8)';
C4 = 'p41*X1 + p42*X2 + p43*X3 + p44*X4 + p45*X5 + p46*X6 + p47*X7*sin(X8)';
C5 = 'p51*X1 + p52*X2 + p53*X3 + p54*X4 + p55*X5 + p56*X6 + p57*X7*sin(X8)';
C6 = 'p61*X1 + p62*X2 + p63*X3 + p64*X4 + p65*X5 + p66*X6 + p67*X7*sin(X8)';
C7 = 'p71*X1 + p72*X2 + p73*X3 + p74*X4 + p75*X5 + p76*X6 + p77*X7*sin(X8)';
C8 = 'p81*X1 + p82*X2 + p83*X3 + p84*X4 + p85*X5 + p86*X6 + p87*X7';
John D'Errico
John D'Errico el 5 de Mzo. de 2024
Editada: John D'Errico el 5 de Mzo. de 2024
This is a completely different question. Please do not keep on posting new questions as answers to your last question. I've changed your answer to a comment on your own question, but if you actually want an answer, then post it as a separate quesiton, NOT an answer to a question.

Iniciar sesión para comentar.

Respuestas (2)

John D'Errico
John D'Errico el 3 de Mzo. de 2024
Editada: John D'Errico el 3 de Mzo. de 2024
As @Torsten said, this is a LINEAR system of equations. Nothing nonlinear needed. In fact, you can use many tools in MATLAB to try to solve the problem, but the problem in the end has rank 2.
This is just a basic question of linear algebra, covered in depth by any undergraduate text in linear algebra.
a_11 = 1; a_12 = 2; a_13 = 3; a_14 = 4; a_15 = 5; a_16 = 6; a_17 = 7;
a_21 = 2; a_22 = 3; a_23 = 4; a_24 = 5; a_25 = 6; a_26 = 7; a_27 = 8;
a_31 = 3; a_32 = 4; a_33 = 5; a_34 = 6; a_35 = 7; a_36 = 8; a_37 = 9;
a_41 = 4; a_42 = 5; a_43 = 6; a_44 = 7; a_45 = 8; a_46 = 9; a_47 = 10;
a_51 = 5; a_52 = 6; a_53 = 7; a_54 = 8; a_55 = 9; a_56 = 10; a_57 = 11;
a_61 = 6; a_62 = 7; a_63 = 8; a_64 = 9; a_65 = 10; a_66 = 11; a_67 = 12;
a_71 = 7; a_72 = 8; a_73 = 9; a_74 = 10; a_75 = 11; a_76 = 12; a_77 = 13;
b_1 = 14; b_2 = 15; b_3 = 16; b_4 = 17; b_5 = 18; b_6 = 19; b_7 = 20;
a = [a_11, a_12, a_13, a_14, a_15, a_16, a_17;
a_21, a_22, a_23, a_24, a_25, a_26, a_27;
a_31, a_32, a_33, a_34, a_35, a_36, a_37;
a_41, a_42, a_43, a_44, a_45, a_46, a_47;
a_51, a_52, a_53, a_54, a_55, a_56, a_57;
a_61, a_62, a_63, a_64, a_65, a_66, a_67;
a_71, a_72, a_73, a_74, a_75, a_76, a_77]
a = 7×7
1 2 3 4 5 6 7 2 3 4 5 6 7 8 3 4 5 6 7 8 9 4 5 6 7 8 9 10 5 6 7 8 9 10 11 6 7 8 9 10 11 12 7 8 9 10 11 12 13
rank(a)
ans = 2
So a VERY simple looking system. What does it mean to solve a linear system of equations?
b_1 = 14; b_2 = 15; b_3 = 16; b_4 = 17; b_5 = 18; b_6 = 19; b_7 = 20;
b = [b_1; b_2; b_3; b_4; b_5; b_6; b_7]
b = 7×1
14 15 16 17 18 19 20
Does there exist a solution of the form
a*x == b
where x is a vector of unknowns? Can we solve that?
What does this mean? When you multiply the matrix a by a vector, you form a LINEAR combination of the columns of a.
If the vector b can be represented by such a linear combination, then
rank([a,b])
ans = 2
we must have that rank equal also to 2.
And that means n exact solution does exist, BUT, there are infinitely many ways to solve the problem. One of them will be given by:
format long g
X0 = lsqminnorm(a,b)
X0 = 7×1
-0.928571428571431 -0.571428571428574 -0.214285714285715 0.142857142857145 0.500000000000001 0.857142857142858 1.21428571428572
See that lsqminnorm can survive having a singular system of equations.
a*X0 - b
ans = 7×1
1.0e+00 * 1.24344978758018e-14 1.06581410364015e-14 1.06581410364015e-14 1.06581410364015e-14 7.105427357601e-15 7.105427357601e-15 7.105427357601e-15
And the result is indeed zero to within floating point trash. If you want to see AN EXACT solution, we can use symbolic tools.
X_sym = sym(a)\b
Warning: Solution is not unique because the system is rank-deficient.
X_sym = 
a*X_sym - b
ans = 
Either solution is equally valid. As I said, there are infinitely many solutions.
What does it mean to say the matrix has rank 2? That merely means we can write the entire matrix as a linear combinations of two column vectors. We can see two possible vectors that can generate the entire matrix, since the first two columns of a are linearly independent.
Again, this is all just basic linear algebra. Using a Newton method to solve it is the mathematical equivalent of using a Mack truck to carry a pea to Boston, so wild overkill. And worse, the Newton method will have numerical problems due to the singular nature of the matrix a.

Torsten
Torsten el 3 de Mzo. de 2024
Movida: Torsten el 3 de Mzo. de 2024
You have a linear system of equations. No Newton method is required in this case.
But since a only has rank 2, no solution exists.
% Coefficients and constants
a_11 = 1; a_12 = 2; a_13 = 3; a_14 = 4; a_15 = 5; a_16 = 6; a_17 = 7;
a_21 = 2; a_22 = 3; a_23 = 4; a_24 = 5; a_25 = 6; a_26 = 7; a_27 = 8;
a_31 = 3; a_32 = 4; a_33 = 5; a_34 = 6; a_35 = 7; a_36 = 8; a_37 = 9;
a_41 = 4; a_42 = 5; a_43 = 6; a_44 = 7; a_45 = 8; a_46 = 9; a_47 = 10;
a_51 = 5; a_52 = 6; a_53 = 7; a_54 = 8; a_55 = 9; a_56 = 10; a_57 = 11;
a_61 = 6; a_62 = 7; a_63 = 8; a_64 = 9; a_65 = 10; a_66 = 11; a_67 = 12;
a_71 = 7; a_72 = 8; a_73 = 9; a_74 = 10; a_75 = 11; a_76 = 12; a_77 = 13;
b_1 = 14; b_2 = 15; b_3 = 16; b_4 = 17; b_5 = 18; b_6 = 19; b_7 = 20;
a = [a_11, a_12, a_13, a_14, a_15, a_16, a_17;
a_21, a_22, a_23, a_24, a_25, a_26, a_27;
a_31, a_32, a_33, a_34, a_35, a_36, a_37;
a_41, a_42, a_43, a_44, a_45, a_46, a_47;
a_51, a_52, a_53, a_54, a_55, a_56, a_57;
a_61, a_62, a_63, a_64, a_65, a_66, a_67;
a_71, a_72, a_73, a_74, a_75, a_76, a_77];
b = [b_1; b_2; b_3; b_4; b_5; b_6; b_7];
rank(a)
ans = 2
X = a\b
Warning: Matrix is singular to working precision.
X = 7×1
NaN NaN NaN NaN NaN NaN -Inf
  1 comentario
Ismita
Ismita el 3 de Mzo. de 2024
Thank you! Could you please explain "But since a only has rank 2, no solution exists." I am a beginner.
I request any of your suggestion to solve the matrix. Thanks in advance

Iniciar sesión para comentar.

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by