Solve Linear Equation with Constraints on Variables
Mostrar comentarios más antiguos
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!
Respuesta aceptada
Más respuestas (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 comentarios
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
el 10 de Sept. de 2023
Dyuman Joshi
el 10 de Sept. de 2023
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
el 10 de Sept. de 2023
Editada: Bruno Luong
el 10 de Sept. de 2023
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
el 10 de Sept. de 2023
Dyuman Joshi
el 10 de Sept. de 2023
"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
el 10 de Sept. de 2023
Editada: Bruno Luong
el 10 de Sept. de 2023
- 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
el 14 de Sept. de 2023
@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
el 14 de Sept. de 2023
I see, it is not full rank symbolically since the root is comuted numerically.
THOMAS DEGAETANO
el 14 de Sept. de 2023
Editada: THOMAS DEGAETANO
el 14 de Sept. de 2023
Dyuman Joshi
el 15 de Sept. de 2023
"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.
Categorías
Más información sobre Linear Algebra en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!














