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!

 採用された回答

Matt J
Matt J 2023 年 9 月 10 日
編集済み: Matt J 2023 年 9 月 10 日

1 投票

You can use trustregprob from this FEX download,
to obtain a numerical least squares solution.
A = [-1, 1, 1; 1,-4,2; 1, 2,-4]
A = 3×3
-1 1 1 1 -4 2 1 2 -4
C = [0;0;0];
B1=trustregprob(A.'*A,A.'*C,1)
B1 = 3×1
0.8165 0.4082 0.4082

3 件のコメント

THOMAS DEGAETANO
THOMAS DEGAETANO 2023 年 9 月 10 日
Wow! The function worked! Thank you!
Bruno Luong
Bruno Luong 2023 年 9 月 10 日
B1=trustregprob(A.'*A,A*C,1)
Are you sure it's not A'*C ? ( assumsing C is not 0)
Matt J
Matt J 2023 年 9 月 10 日
Fixed.

サインインしてコメントする。

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 10 日

2 投票

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])
L = 
m = 
n = 

15 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 10 日
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])
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
y = 3×1
0.8165 0.4082 0.4082
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
THOMAS DEGAETANO 2023 年 9 月 10 日
Thank you! i keep getting the following error when using Live Script Editor:
CAT arguments dimensions not consistent.
[resz, ranges] = checkDimensions(sz,dim);
Error in sym/cat (line 25)
ySym = catMany(dim, args);
Error in sym/horzcat (line 19)
ySym = cat(2,args{:});
Any suggestions?
My code below:
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])
Dyuman Joshi
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
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
THOMAS DEGAETANO 2023 年 9 月 10 日
it worked! Thank you again.
Dyuman Joshi
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
Bruno Luong 2023 年 9 月 10 日
編集済み: Bruno Luong 2023 年 9 月 10 日
  1. fsolve returns wrong solution
  2. Usualy when people talking about "linear system with constraints" they mean least-square on the linear system and strict verification on the constraints.
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 11 日
"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])
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
No solution found. fsolve stopped because the last step was ineffective. However, the vector of function values is not near zero, as measured by the value of the function tolerance.
y = 3×1
1.0e-08 * 0.6833 -0.6223 0.6048
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
THOMAS DEGAETANO 2023 年 9 月 14 日
Ok, so back at this again:
When using Dyuman's suggestion, i keep getting the errors shown below. Any suggestions?
syms L1 L2 L3 m1 m2 m3 n1 n2 n3 x
A = [5-x,0,3;0,0-x,2;3,2,0-x]
A = 
b = det(A)
b = 
p = [-1,5,13,-20];
r = roots(p)
r = 3×1
6.5229 -2.6709 1.1480
sig1 = 6.53
sig1 = 6.5300
sig2 = -2.67
sig2 = -2.6700
sig3 = 1.15
sig3 = 1.1500
A1 = [5-sig1,0,3;0,-sig1,2;3,2,-sig1]
A1 = 3×3
-1.5300 0 3.0000 0 -6.5300 2.0000 3.0000 2.0000 -6.5300
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 = Empty sym: 0-by-1 m1 = Empty sym: 0-by-1 n1 = Empty sym: 0-by-1
Bruno Luong
Bruno Luong 2023 年 9 月 14 日
@THOMAS DEGAETANO I already said why "Careful this method won't work correctly with A a full rank matrix"
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 14 日
syms L1 m1 n1 x
A = [5-x,0,3;0,0-x,2;3,2,0-x]
A = 
b = det(A)
b = 
%Get the roots of the polynomial
r = solve(b==0,x)
r = 
vpa(r)
ans = 
%Substitute the appropriate root
A1 = subs(A,x,r(3))
A1 = 
B1 = [L1; m1; n1];
C = [0;0;0];
eqn1 = A1*B1 == C;
eqn1 = 
eqn2 = L1^2+m1^2+n1^2 == 1;
eqn2 = 
[L1,m1,n1] = solve([eqn1; eqn2], [L1,m1,n1])
L1 = 
m1 = 
n1 = 
L1 = vpa(L1)
L1 = 
m1 = vpa(m1)
m1 = 
n1 = vpa(n1)
n1 = 
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 14 日
@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))
A1 = 
rank(vpa(A1,100))
ans = 2
Bruno Luong
Bruno Luong 2023 年 9 月 14 日
I see, it is not full rank symbolically since the root is comuted numerically.
THOMAS DEGAETANO
THOMAS DEGAETANO 2023 年 9 月 14 日
編集済み: THOMAS DEGAETANO 2023 年 9 月 14 日
Thank you both for your efforts! @Bruno Luong, i honestly didnt understand what a full rank matrix was :)
@Dyuman Joshi, thanks for your help. The solution seems a bit more intense (for a lay person - level 1 mind you). My goals was to compare @Matt J's solution versus the solution presented by @Dyuman Joshi.
Based on @Dyuman Joshi's method presented, the values are practically the same between the two methods.
Thank you all for your support and knowledge!
Dyuman Joshi
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.

サインインしてコメントする。

製品

リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by