solving system of equations
Show older comments
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
and
values. 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 Comment
Javeria
on 26 Jun 2025
Answers (3)
David Goodmanson
on 26 Jun 2025
Edited: David Goodmanson
on 26 Jun 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 Comments
syms x [1 3] real
eqns1 = [x1 == 2*x2 + 3*x3 + 4;
x2 == -3*x1 + 4*x3 - 5;
x3 == 5*x1 + 2*x2 - 6];
sol1 = solve(eqns1)
eqns2 = [x1 == 2*x2 + 3*x3 + 4;
x2 == -3*x1 + 4*x3 - 5;
x3 == 5*x1 + 2*(-x2) - 6];
sol2 = solve(eqns2)
eqns3 = [x1 == 2*x2 + 3*x3 + 4;
x2 == -3*x1 + 4*x3 - 5;
x3 == 5*x1 + 2*abs(x2) - 6];
sol3 = solve(eqns3)
Interesting that solve() only produces one of the two answers.
David Goodmanson
on 26 Jun 2025
Except that sol1 is an incorrect answer, so the sol3 answer is the only real one.
Javeria
on 26 Jun 2025
Javeria
on 26 Jun 2025
Walter Roberson
on 26 Jun 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.
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
% 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))]);
x(1)-2*x(2)-3*x(3)-4
x(2)+3*x(1)-4*x(3)+5
x(3)-5*x(1)-2*abs(x(2))+6
John D'Errico
on 26 Jun 2025
Edited: John D'Errico
on 26 Jun 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
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
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)
6 Comments
The "real" problem is much more complicated. It is a system of linear equations with only one additional nonlinear variable that @Javeria wants to solve for iteratively:
Javeria
on 27 Jun 2025
Torsten
on 27 Jun 2025
What is the (single) variable you want to fix by the integral condition (given in the first line of your comment) ?
Javeria
on 27 Jun 2025
Torsten
on 27 Jun 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
on 28 Jun 2025
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








