How to perform mathematical operation of arrays inside loop?

I know it is a very basic question, but I have not been able to solve it. I have an array X(28*1). Using values of X for the first time, I would calculate E=B-A*X where E=(352*1), A(352*28) and B=(352*1). Then if the sum of E is larger than 10^-40, The follwoing operations are done:
C=diag(E*E');
W=diag(C)^-1;
X=(A'*W*A)^-1*A'*W*B
Then the updated value of X will be used for E=B-A*X operation. I understand whole thing is required to be inside a loop. But I can not find the exact process to write the code. If anyone has any idea, It would be much appriciated. I have attached the X for 1st iteration, A, and B as .mat file.

6 comentarios

It is inadvisable to implement this equation directly.
X=(A'*W*A)^-1*A'*W*B
You should use lscov (see my answer below).
Hi. Thank you for the response. I was trying to use the weighted matrix to reach closer to the actual values. I used lescov command but so far the results are not close. The values I get are not equal or close to equal to the exact ones. I have attached the actual X matrix that I am supposed to get. If you can help me on this with any suggesstions, it is highly appreciated. Thank you again for your time and effort.
If instead of using lscov, you implement the operation directly,
X=(A'*W*A)^-1*A'*W*B
is the result better or worse?
Thank you for the response. There are 28 values in X. After using the lscov, the results are mix of better and worse. Some values are closer to the actual value whereas some are far way than the results of the backslash. As far as I know, by using the weighted matrix, the results which are found are really close to the actual ones. That is why I don't understand how to use the lescov or any other ways to find the values that are close to the actual ones.
If you want to do robust linear fit with outliers, I recommend you do L1 fitting
minimize norm(A*X - B, 1)
rather than weighted 2-norm (more robust than l2, but still not very robust).
This requires linear programing linprog of optimization toolbox.
Matt J
Matt J el 19 de Jul. de 2023
Editada: Matt J el 19 de Jul. de 2023
I recommend you do L1 fitting...This requires linear programing linprog of optimization toolbox.
But, I'd like to add that there would be no need to implement it from scratch. You could instead use minL1lin() from this FEX download,

Iniciar sesión para comentar.

Respuestas (3)

Matt J
Matt J el 18 de Jul. de 2023
Editada: Matt J el 18 de Jul. de 2023
for i=1:N
E=B-A*X;
if norm(E,1)<1e-40, continue; end
C=E.^2;
X=lscov(A,B,1./C);
end

3 comentarios

I didn't know lscov. Thanks.
Matt J
Matt J el 20 de Jul. de 2023
Editada: Matt J el 20 de Jul. de 2023
@ANANTA BIJOY BHADRA I notice that the second column of your A matrix is zero, which iof course makes A ill-conditioned.
A=load('A').A_primary_A;
[l,h]=bounds(A(:,2))
l = 0
h = 0
cond(A)
ans = 2.5955e+16
cond(A(:, [1,3:end])) %skip second column
ans = 1.4142
Good catch. But I beleive the solution of "\" or lscov, both based on QR decomposition would not be affected by 0 column and remain accurate, despite the warning. The solution corresponds to 0-column is simply nil.
A=rand(10,3);
b=rand(10,1);
As=A; As(:,end+1)=0;
w = 0.1+rand(size(b));
format long
A\b
ans = 3×1
0.305726431573425 0.655213115202102 -0.126807101744095
As\b
Warning: Rank deficient, rank = 3, tol = 4.365185e-15.
ans = 4×1
0.305726431573425 0.655213115202102 -0.126807101744095 0
lscov(A,b,w)
ans = 3×1
0.365558371139105 0.511882753808717 -0.133776410641447
lscov(As,b,w)
Warning: A is rank deficient to within machine precision.
ans = 4×1
0.365558371139105 0.511882753808717 -0.133776410641447 0

Iniciar sesión para comentar.

E = B-A*X;
while(sum(E(:))> 10^-40)
C=diag(E*E');
W=diag(C)^-1;
X=(A'*W*A)^-1*A'*W*B;
end
E = B-A*X;
while sum(E,'all') > 1e-40
C = diag(E*E');
W = diag(C)^-1;
X = (A'*W*A)^-1*A'*W*B;
E = B-A*X;
end

Categorías

Más información sobre Linear Programming and Mixed-Integer Linear Programming en Centro de ayuda y File Exchange.

Productos

Versión

R2023a

Preguntada:

el 18 de Jul. de 2023

Editada:

el 20 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by