Did mldivide Implementation Change in R2022b?

2 visualizzazioni (ultimi 30 giorni)
Paul
Paul il 17 Mar 2023
Commentato: Bruno Luong il 18 Mar 2023
When I run this code here on 2022b (actually, 2023a now, but I saw the same effect on 2022b) I get:
load('mldivdivideex')
format long e
clsys.A\clsys.B
ans = 5×2
-1.000000000000000e+00 2.047143402298295e-17 -7.194341130875050e-12 7.868810611894586e-13 -1.000000000000001e+00 1.999999999991291e-02 -9.738334588696467e-13 8.679999999981491e+01 -2.428571428833528e-03 3.004408164584185e-04
But when I run on my local installation using 2022a I get
>> clsys.A\clsys.B
ans =
-1.000000000000000e+00 2.168317634010083e-17
-6.812536955189428e-12 1.287988200135117e-13
-1.000000000000000e+00 1.999999999991291e-02
-9.670661834891370e-13 8.679999999981482e+01
-2.428571428833527e-03 3.004408164584182e-04
The second row is obviously different, as are the (4,1) and (1,2) elements. Even the "large" elements can be different in the last digit.
The 2022b release notes do mention "mldivide and pagemldivide Functions: Improved performance with small matrices." But the release notes don't say anything about expecting different answers.
  9 Commenti
Paul
Paul il 17 Mar 2023
Maybe this an example that motivates your desire to rehabilitate inv.
Assuming the sym/vpa solution is as good as can be done ...
load(websave('mldivideexample.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1328525/mldivideexample.mat'));
format long e
C1 = double(inv(sym(A))*sym(B))
C1 = 5×2
-1.000000000000000e+00 0 0 0 -1.000000000000000e+00 1.999999999991288e-02 -4.041939973822555e-13 8.679999999981486e+01 -2.428571428833525e-03 3.004408164584182e-04
ans = logical
1
The exact -1's and 0's are appealing, and I believe theoretically expected for this problem.
C2 = inv(A)*B
C2 = 5×2
-1.000000000000000e+00 0 0 0 -1.000000000000000e+00 1.999999999991291e-02 -9.738334588696467e-13 8.679999999981491e+01 -2.428571428833528e-03 3.004408164584187e-04
The exact -1's and 0's are appealing
C3 = A\B
C3 = 5×2
-1.000000000000000e+00 2.047143402298295e-17 -7.194341130875050e-12 7.868810611894586e-13 -1.000000000000001e+00 1.999999999991291e-02 -9.738334588696467e-13 8.679999999981491e+01 -2.428571428833528e-03 3.004408164584185e-04
norm(C1 - C2)
ans =
5.724687657581802e-13
norm(C1 - C3)
ans =
7.259849696053097e-12

Accedi per commentare.

Risposte (1)

Matt J
Matt J il 17 Mar 2023
Modificato: Matt J il 17 Mar 2023
The condition number of clsys.A is very poor, so you should not expect any consistency in the result among different implementations of the mldivide operation nor among different CPUs.
load('mldivdivideex')
size(clsys.A)
ans = 1×2
5 5
cond(clsys.A)
ans = 1.9600e+09
  5 Commenti
Steven Lord
Steven Lord il 17 Mar 2023
Let's take a sample matrix with a condition number of 1e9.
format longg
A = [1 0; 0 1e9]
A = 2×2
1.0e+00 * 1 0 0 1000000000
cond(A)
ans =
1000000000
Now let's multiply A by two vectors that are very close to one another.
x1 = [1; 1]
x1 = 2×1
1 1
y1 = A*x1
y1 = 2×1
1.0e+00 * 1 1000000000
x2 = [1; 1.1]
x2 = 2×1
1 1.1
y2 = A*x2
y2 = 2×1
1.0e+00 * 1 1100000000
How different are y1 and y2? The first elements are the same. The second elements:
difference = y2(2)-y1(2)
difference =
100000000
The differences in the second element of the x vectors was only 0.1. The differences in the second elements of the y vectors was much, much larger. Granted this was a contrived example, but hopefully you can see that badly conditioned matrices can magnify differences or errors quite substantially.
From the Wikipedia entry on condition number: "As a rule of thumb, if the condition number , then you may lose up to k digits of accuracy on top of what would be lost to the numerical method due to loss of precision from arithmetic methods."
Bruno Luong
Bruno Luong il 17 Mar 2023
Modificato: Bruno Luong il 17 Mar 2023
"As a rule of thumb, if the condition number is 10^k, then you may lose up to k digits of accuracy on top of what would be lost to the numerical method due to loss of precision from arithmetic methods."
Hmmm I admit I don't understand this sentence from Wikipedia. Or at least I'm not sure how to interpret it correctly.

Accedi per commentare.

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by