Solve Linear Equation with Constraints on Variables
이전 댓글 표시
I am trying to solve a system of linear equations with the following expressions:
A*B1 = C; where:
syms L1 L2 L3 m1 m2 m3 n1 n2 n3
A = [-1, 1, 1; 1,-4,2; 1, 2,-4]
B1 = [L1; m1; n1]
C = [0;0;0]
with the constraint that: L1^2 + m1^2 + n1^2 == 1. I keep getting an error with linsolve to solve the variables L1, m1, and n1.
Any help would be greatly appreacted!
채택된 답변
추가 답변 (1개)
syms L1 L2 L3 m1 m2 m3 n1 n2 n3
A = [-1, 1, 1; 1,-4,2; 1, 2,-4];
B1 = [L1; m1; n1];
C = [0;0;0];
%Equations to be solved
eqn1 = A*B1 == C;
eqn2 = L1^2+m1^2+n1^2 == 1;
[L,m,n] = solve([eqn1; eqn2], [L1,m1,n1])
댓글 수: 15
Using fsolve -
A = [-1, 1, 1; 1,-4,2; 1, 2,-4];
C = [0;0;0];
fun = @(x) nonlin(x,A,C);
y=fsolve(fun,[0.5;0.5;0.5])
function F = nonlin(x,A,C)
F= [A*[x(1);x(2);x(3)]-C; x(1)^2+x(2)^2+x(3)^2-1];
end
THOMAS DEGAETANO
2023년 9월 10일
Dyuman Joshi
2023년 9월 10일
You have to vertically concatenate the equations, using semi-colon, as I have done in my answer -
% v
[L,m,n] = solve([eqn1;eqn2],[L1,m1,n1])
Bruno Luong
2023년 9월 10일
편집: Bruno Luong
2023년 9월 10일
Careful this method won't work correctly with A a full rank matrix
A = 100*rand(3);
C = [0;0;0];
fun = @(x) nonlin(x,A,C);
y=fsolve(fun,[0.5;0.5;0.5])
y(1)^2+y(2)^2+y(3)^2-1 % this is NOT 0
function F = nonlin(x,A,C)
F= [A*[x(1);x(2);x(3)]-C; x(1)^2+x(2)^2+x(3)^2-1];
end
THOMAS DEGAETANO
2023년 9월 10일
Dyuman Joshi
2023년 9월 10일
"Careful this method won't work correctly with A a full rank matrix"
It will work and return no solution which is consistent as for a full rank matrix both conditions will not be satisfied.
Bruno Luong
2023년 9월 10일
편집: Bruno Luong
2023년 9월 10일
- fsolve returns wrong solution
- Usualy when people talking about "linear system with constraints" they mean least-square on the linear system and strict verification on the constraints.
"Usualy when people talking about "linear system with constraints" they mean least-square on the linear system and strict verification on the constraints. "
Noted.
"fsolve returns wrong solution"
No, fsolve does not return a solution. See, "No solution found".
A = 100*rand(3);
C = [0;0;0];
fun = @(x) nonlin(x,A,C);
y=fsolve(fun,[0.5;0.5;0.5])
function F = nonlin(x,A,C)
F= [A*[x(1);x(2);x(3)]-C; x(1)^2+x(2)^2+x(3)^2-1];
end
Bruno Luong
2023년 9월 14일
@THOMAS DEGAETANO I already said why "Careful this method won't work correctly with A a full rank matrix"
syms L1 m1 n1 x
A = [5-x,0,3;0,0-x,2;3,2,0-x]
b = det(A)
%Get the roots of the polynomial
r = solve(b==0,x)
vpa(r)
%Substitute the appropriate root
A1 = subs(A,x,r(3))
B1 = [L1; m1; n1];
C = [0;0;0];
eqn1 = A1*B1 == C;
eqn2 = L1^2+m1^2+n1^2 == 1;
[L1,m1,n1] = solve([eqn1; eqn2], [L1,m1,n1])
L1 = vpa(L1)
m1 = vpa(m1)
n1 = vpa(n1)
@Bruno Luong, A1 is not a full rank matrix
syms L1 m1 n1 x
A = [5-x,0,3;0,0-x,2;3,2,0-x];
b = det(A);
%Get the roots of the polynomial
r = solve(b==0,x);
%Substitute the appropriate root
A1 = subs(A,x,r(3))
rank(vpa(A1,100))
Bruno Luong
2023년 9월 14일
I see, it is not full rank symbolically since the root is comuted numerically.
THOMAS DEGAETANO
2023년 9월 14일
편집: THOMAS DEGAETANO
2023년 9월 14일
Dyuman Joshi
2023년 9월 15일
"i honestly didnt understand what a full rank matrix was"
If you are going to work and write code in MATLAB, I strongly recommend you understand the basics of Matrix algebra, because MATLAB is literally based on it.
카테고리
도움말 센터 및 File Exchange에서 Lengths and Angles에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!














