Why is lscov giving me a very different solution to a simple system with an analytical solution?

5 views (last 30 days)
I am making a simple comparison for the solution of a linear system that I'd normally just solve by hand to the solution given by lscov. I thought this would be very quick and straightforward but I'm getting completely different results and I can't seem to find why. I'm sure it's something silly but I'm not finding it. I'm using MATLAB 2016a. Many thanks in advance!!!
Here is the code:
vel is a 212400 x 4 matrix.
% Least-square solution
bangle=25;
c=+1;
a=1./(2*sind(bangle));
b=1./(4*cosd(bangle));
d=a./sqrt(2);
zr=zeros(size(a));
t_matrix = [c.*a -c.*a zr zr;...
zr zr -c.*a c.*a;...
b b b b;...
d d -d -d];
[vel_sol] = lscov(t_matrix,vel'); % in vel_sol the first row is X, than Y, Z and e
% Traditional method
b1 = vel(:,1);
b2 = vel(:,2);
b3 = vel(:,3);
b4 = vel(:,4);
X_traditional = (b1 - b2)./(2*sind(bangle));
Y_traditional = (b4 - b3)./(2*sind(bangle));
Z_traditional = (b1 + b2 + b3 + b4)./(4*cosd(bangle));
e_traditional = (b1 + b2 - b3 - b4)./(2*sqrt(2)*sind(bangle));
% Plot both
plot(X_traditional)
hold on
plot(vel_sol(1,:))
  2 Comments
Larissa Perez
Larissa Perez on 16 Aug 2022
Of course.. That was a silly mistake, I was feeding the function with the wrong matrix. It is working now. Thanks for taking the time to answer and be kind.
You put it as a comment rather than an answer, I don't think I can actually accept the answer though. If you'd like to, move to an answer and I'll accept it.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 12 Aug 2022
Edited: John D'Errico on 12 Aug 2022
This is not the fault of lscov. As you have written it, t_matrix is quite well conditioned. There is only one solution to the problem you have formulated. lscov will find it, or you could have used any other tool. Therfore the problem must lie in your claims. Should the two solutions be the same? We can verify that.
First, a is a scalar. So zr is just 0. I'll leave a and b in a symbolic form so it is easier to trace through the equations, easier to read. Only at the end will I replace a and b
zr = 0;
c = 1;
syms a b
d = a/sqrt(sym(2));
t_matrix = [c.*a -c.*a zr zr;...
zr zr -c.*a c.*a;...
b b b b;...
d d -d -d]
t_matrix = 
Now, what you wrote, as the "traditional solution" was just
syms b1 b2 b3 b4
t_matrix*[b1;b2;b3;b4]
ans = 
All you did was to multiply t_matrix by the vector [b1;b2;b3;b4].
We can now substitute in a and b. I'll still leave bangle as symbolic though.
syms bangle
subs(t_matrix*[b1;b2;b3;b4],[a,b],[1/(2*sind(bangle)),1/(4*cosd(bangle))])
ans = 
Your "traditional solution" was nothing of the sort. You never solved ANY system of equations here. But then you used lscov, which DOES solve a linear system of equations. So of course you got a different esult. What should you expect?

Tags

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by