Solve Linear Equation with Constraints on Variables

16 views (last 30 days)
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!

Accepted Answer

Matt J
Matt J on 10 Sep 2023
Edited: Matt J on 10 Sep 2023
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

More Answers (1)

Dyuman Joshi
Dyuman Joshi on 10 Sep 2023
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 Comments
THOMAS DEGAETANO
THOMAS DEGAETANO on 14 Sep 2023
Edited: THOMAS DEGAETANO on 14 Sep 2023
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 on 15 Sep 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.

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!