Clear Filters
Clear Filters

Different answers on different computers

2 views (last 30 days)
I am trying to solve a question my professor posed on a simulation for my Linear Algebra and Geometry course and I get different answers depending on the pc used, I tried using MATLAB online and I got the same result I had on my installed version, a friend gets a different answer on his own PC but always consistent in the specific PCs. I understand this might have something to do with the ill conditioning of a system but the code is so brief and short, and I think the point of the question posed is that fact in the first place. The problem in question:
Let M be the matrix generated by the command magic(432), I the identity matrix and A = M + 10I. Let b be the right-hand side such that the solution of the linear system Ax = b is a vector with all elements equal to 1. Solve the linear system with the MATLAB command \. Let Nr be the infinite norm of the residual b - Ax. Which is the order of Nr?"
The question is a multiple choice one and the choices are as follows:
  • 10^-3
  • 10^-8
  • 10^-6
  • 10^-1
  • 10^-5
with 10^-8 being marked as the correct answer.
The code I used is this:
M = magic(432);
I = eye(432);
A = M + 10 * I;
x = ones(432,1);
b = A*x;
x_computed = A\b;
norm(b - A*x_computed,inf)
ans = 1.1176e-07
I do not understand what process I have to go through to stabilize this problem nor if I need to do so in the first place. Is this question just problematic with the way it is posed or is there an obvious solution to it that I as a begineer can not see?

Accepted Answer

Sanju
Sanju on 27 May 2024
The issue you are experiencing with different results on different PCs is likely due to the numerical instability of the linear system. The matrix A in your problem is ill-conditioned, which means that small changes in the input can lead to large changes in the output. This can cause numerical errors and inconsistencies in the results.
To stabilize the problem and obtain more consistent results, you can use a technique called regularization. Regularization involves adding a small positive value to the diagonal elements of the matrix A to improve its condition number. This can help mitigate the effects of numerical instability.
Here's an example of how you can modify your code to apply regularization,
M = magic(432);
I = eye(432);
A = M + 10 * I;
x = ones(432, 1);
b = A * x;
% Apply regularization
lambda = 1e-6; % Regularization parameter
A_reg = A + lambda * I;
x_computed = A_reg \ b;
norm(b - A_reg * x_computed, inf)
ans = 9.6858e-08
By adding a small value (lambda) to the diagonal elements of A, you can stabilize the problem and obtain more consistent results. Adjust the value of lambda as needed to balance stability and accuracy.
Hope this helps!

More Answers (2)

Infinite_king
Infinite_king on 27 May 2024
Edited: Infinite_king on 27 May 2024
Hi Eyüp Ege,
The matrix values are stored in double-precision floating-point format. This behaviour is due to inherent problems with double-precision arithmetic. To demonstrate the problem, consider the following code snippet,
r1 = 0.51699042052531019031391679163789;
r2 = 0.55669463201210189762235813759617;
r3 = 0.15649521911404451479654653667239;
% adding r1,r2 and r3
a = (r1+r2)+r3;
b = r1+(r2+r3);
% expected error should be 0
error = a-b
error = -2.2204e-16
The expected error is 0, since . However, we can see that the value of 'a' does not exactly match the value of 'b'. Because of how floating-point operations are carried out, the order of operations does matter. MATLAB prefers to perform operations using parallelism as much as possible to maximize efficiency, which can be achieved using SIMD operations. Since the order of operations will determine the error, the same matrix operation on different machines may result in different errors. Please note that in most cases, these errors are small and insignificant.
To resolve the problem, you can follow the solution provided by @Sanju.
Hope this is helpful.

NAVNEET NAYAN
NAVNEET NAYAN on 27 May 2024
Edited: NAVNEET NAYAN on 27 May 2024
I executed the attached code line by line on two different systems and found that answers were different. Till the line
x_computed = A\b;
every data values were exactly matching on the two systems. But it is varying in the last line. In the last line too, I performed the following three lines independently
t=A*x_computed;
t1=b - A*x_computed;
norm(t1,inf)
There I found that on both systems, value of "t" was exactly same. While I found dissimilarities in the values of "t1". While doing this, I found that almost all the values of "t1" lie in the range of 10e-8 and 10e-9. From here comes the real problem that happened due to different systems. Both the systems were randomly making some of the values of "t1" to zeros or truncating them to some different specific values. So, due to different systems the values of "t1" or "b - A*x_computed" came out to be different. SInce these values were different, so norm value was also different.

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!