Why is lscov giving me a very different solution to a simple system with an analytical solution?
조회 수: 6 (최근 30일)
이전 댓글 표시
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
Bjorn Gustavsson
2022년 8월 12일
Because your transfer-matrix doesn't correspond to the "traditional method" relation between vel and your X Y Z and e. Have you checked that it does what it should with simple test-cases that you can interpret manually?
답변 (1개)
John D'Errico
2022년 8월 12일
편집: John D'Errico
2022년 8월 12일
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]
Now, what you wrote, as the "traditional solution" was just
syms b1 b2 b3 b4
t_matrix*[b1;b2;b3;b4]
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))])
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?
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!