solving system of equations

2 visualizaciones (últimos 30 días)
Javeria
Javeria el 26 de Jun. de 2025
Comentada: Javeria el 28 de Jun. de 2025
I have the following system of equations let say
here the 1st two equations are linear but the third one has absolute term which make it non-linear. Now my question is:
we need to solve this system by considering iteratively i.e by putting =0 then our system converts to linear one. then use the guass elimination solve the system. so we will have andvalues. Then make use of this and solve the system again for the unknowns and in step B. Let define the tolerence is then find the difference between the previous and and the updated and if our difference is less than our defined tol then stop iteration otherwise the step B is repeated again. I am not sure whether our solution might converged or not but i want to make use of guass elimination method for one of my problem having these algorithm steps.
  1 comentario
Javeria
Javeria el 26 de Jun. de 2025
I am not sure my this one following code i write it correctly or not ?
% Tolerance for convergence check
tol = 1e-4;
max_iter = 100;
iter = 0;
% Initial previous solution (first iteration uses |x2| = 0)
x_prev = [0; 0; 0]; % These are just placeholders for the first iteration
% Iterative process
while iter < max_iter
% Step 1: Linearize the system by assuming |x2| = 0 for the first iteration
if iter == 0
% First iteration: Treat |x2| = 0
x2_abs = 0;
else
% For subsequent iterations, use the updated value of x2
x2_abs = abs(x_prev(2)); % |x2| as the updated value from the previous iteration
end
% Define the system of equations with updated |x2| term
A = [1, -2, -3; -3, 1, -4; 5, 2*x2_abs*x_prev(2), 0]; % Linear system with |x2| treated as constant
b = [4; -5; -6]; % The right-hand side of the system
% Step 2: Solve the system using Gaussian elimination (or MATLAB's backslash operator)
x = A \ b;
% Step 3: Compute the difference between the new and previous solution
diff = norm(x - x_prev);
% Step 4: Check for convergence
if diff < tol
disp('Converged');
break;
end
% Step 5: Update the previous solution
x_prev = x;
% Step 6: Update |x2| for the next iteration using the new value of x2
% We use the updated x2 from the current solution
x2 = x(2); % Update x2 for the next iteration (treat it as a constant for next round)
% Increment iteration count
iter = iter + 1;
end
% Display the results after convergence
disp(['Converged after ', num2str(iter), ' iterations']);
disp(['x1 = ', num2str(x(1))]);
disp(['x2 = ', num2str(x(2))]);
disp(['x3 = ', num2str(x(3))]);

Iniciar sesión para comentar.

Respuestas (3)

David Goodmanson
David Goodmanson el 26 de Jun. de 2025
Editada: David Goodmanson el 26 de Jun. de 2025
Hi Javeria,
If it's not required to solve this in the way you mention, there is a better way.
Assume that x2 is positive or zero. Then |x2| = x2, the abs goes away and if you let x3 keep its coefficient then you are solving the system
A = [1 -2 -3; 3 1 -4; -5 -2 1] % you have a sign error in the second row of A
b = [4;-5;-6];
x = m1\b
If the value of x2 is positive (or zero), then you are done. If not, assume x2 is negative. Then |x2| = -x2 and you are solving the system with a sign change for the x2 coefficent in the third equation, -2 --> +2
If you change A appropriately and solve Ax = b again, then if x2 comes out negative, you are done. Since either x2>=0 or x2<0, one of these should work. I will leave you to see what happens.
  5 comentarios
Javeria
Javeria el 26 de Jun. de 2025
@David Goodmanson Hello, what i mentioned in my steps i need algorithm in that way. So can you please confirm that my algorithm is okay or not?
Walter Roberson
Walter Roberson el 26 de Jun. de 2025
Except that sol1 is an incorrect answer
Ah, I did not notice that x2 is negative either way, so indeed sol1 is not a valid answer.

Iniciar sesión para comentar.


Torsten
Torsten el 26 de Jun. de 2025
Editada: Torsten el 26 de Jun. de 2025
This works, but I doubt it will work in all possible cases.
% Tolerance for convergence check
tol = 1e-8;
max_iter = 100;
iter = 0;
% Initial previous solution (first iteration uses |x2| = 0)
x_prev = [0; 0; 0]; % These are just placeholders for the first iteration
x2_abs = 0;
% Iterative process
while iter < max_iter
% Define the system of equations with updated |x2| term
A = [1, -2, -3; 3, 1, -4; -5, 0, 1]; % Linear system with |x2| treated as constant
b = [4; -5; 2*x2_abs-6]; % The right-hand side of the system
% Step 2: Solve the system using Gaussian elimination (or MATLAB's backslash operator)
x = A \ b;
% Step 3: Compute the difference between the new and previous solution
diff = norm(x - x_prev);
% Step 4: Check for convergence
if diff < tol
disp('Converged');
break;
end
% Step 5: Update the previous solution
x_prev = x;
x2_abs = abs(x(2));
% Increment iteration count
iter = iter + 1;
end
Converged
% Display the results after convergence
disp(['Converged after ', num2str(iter), ' iterations']);
Converged after 14 iterations
disp(['x1 = ', num2str(x(1))]);
x1 = 0.17241
disp(['x2 = ', num2str(x(2))]);
x2 = -2.8966
disp(['x3 = ', num2str(x(3))]);
x3 = 0.65517
x(1)-2*x(2)-3*x(3)-4
ans = 0
x(2)+3*x(1)-4*x(3)+5
ans = 0
x(3)-5*x(1)-2*abs(x(2))+6
ans = -2.0311e-09

John D'Errico
John D'Errico el 26 de Jun. de 2025
Editada: John D'Errico el 26 de Jun. de 2025
There is absolutely no need to use an iterative solver!!!!!!!!
The idea is to remove the absolute value from your problem, and then the solution becomes trivial, and what you are doing is hopelessly convoluted, and unnecessary. Sorry, but it is.
All the absolute value does is introduce a negative sign in there, when x2 is negative. If x2 is positive, then abs(x2) == x2. I'll write the equations first with the abs in there.
x1 - 2*x2 - 3*x3 = 4
3*x1 + x2 - 4*x3 = -5
5*x1 + 2*abs(x2) - x3 = 6
Now, reduce it to two distinct problems. When x2 is positve, we have this system:
x1 - 2*x2 - 3*x3 = 4
3*x1 + x2 - 4*x3 = -5
5*x1 + 2*x2 - x3 = 6
Solve it simply as:
Apos = [1 -2 -3;3 1 -4;5 2 -1];
rhs = [4;-5;6];
format long g
Apos\rhs
ans = 3×1
3.52631578947368 -4.42105263157895 2.78947368421053
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You can have zero, one, or two possible solutions to this problem.
Since the solution we have found here has x2 being negative, then it is NOT a solution to the orifginal problem.
If x2 is negative, then the absolute value introduces a negative sign in front of the x2 term.
x1 - 2*x2 - 3*x3 = 4
3*x1 + x2 - 4*x3 = -5
5*x1 - 2*x2 - x3 = 6
Aneg = [1 -2 -3;3 1 -4;5 -2 -1];
Aneg\rhs
ans = 3×1
0.172413793103448 -2.89655172413793 0.655172413793104
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
This one has x2 as being negative. And therefore, it is the solution, and the only solution.
There is NO iteration needed! That is the solution, and to within floating point trash, the only possible solution.
Is that the true solution? We can use syms to verify the result.
syms x1 x2 x3
x123 = solve( x1 - 2*x2 - 3*x3 == 4,3*x1 + x2 - 4*x3 == -5,5*x1 + 2*abs(x2) - x3 == 6, returnconditions = true)
x123 = struct with fields:
x1: 5/29 x2: -84/29 x3: 19/29 parameters: [1×0 sym] conditions: symtrue
  6 comentarios
Torsten
Torsten el 27 de Jun. de 2025
To me it looks as if it was one equation for an infinite number of coefficients e_p^l. Or am I mistaken ? It's hard for me to understand - even with your documentation - how you deduced the linear equations from the integral compatibility conditions.
Javeria
Javeria el 28 de Jun. de 2025
@Torsten By taking advantage of orthogonal functions i.e bessel function. Indeed this equation is non-linear so we can then solve by the above mentioned procedure

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by