# Creating a linear combination in terms of the free variable in MATLAB

6 views (last 30 days)
Teoman on 6 Mar 2023
Answered: Rohit on 21 Apr 2023
In MATLAB I want to create a code that gets an Augmented matrix (Concatenating A and B) and produce the linear combination of the system in terms of the free variables. The free variables will be given the variables t1, t2, ..., tn for the nth free variables. I want to fix the code below so that the inputs match the outputs. The code should be able to output the linear combination of the system in terms of the free variables regardless of the size of the matrix, for example, a 1 x 10, 5 x 5, or 7 x 3. Look at the 2 test cases below and make sure that the code could output the linear combination as a whole with its column matrices and free variables.
Code:
A = [2 3 4 56 6 8; 1 2 4 5 6 7; 3 6 12 15 18 21; 4 6 8 112 12 16];
B = [9; 8; 24; 18];
Aug = [A B];
RREF = rref(Aug);
[m, n] = size(RREF);
free_variables = find(all(RREF(:,1:n-1)==0, 2));
for i=1:n-1
if ~any(RREF(:,i))
fprintf('x%d = ', i);
fprintf('%s', '0 ');
for j=1:length(free_variables)
fprintf('+ %dt%d ', RREF(free_variables(j), n), free_variables(j));
end
fprintf('\n');
else
[~, j] = max(RREF(:,i));
fprintf('x%d = ', i);
fprintf('%dt%d ', RREF(j, n), j);
for k=1:n-1
if k~=i && RREF(j,k)~=0
fprintf('%+dt%d ', -1*RREF(j,k), k);
end
end
for j=1:length(free_variables)
fprintf('%+dt%d ', RREF(free_variables(j), n), free_variables(j));
end
fprintf('\n');
end
end
Test Cases
Input:
Output:
x1= 4t1 - 97t2 +6t3 + 5t4 +6t5
x2= -4t1 + 46t2 - 6t3 - 6t4 - 7t5
x3= t1
x4= t2
x5= t3
x6= t4
x7= t5
Input:
Output:
x1= -4/3t1 - 5/3t2 - 1/3t3
x2= t1
x3= t2
##### 3 CommentsShow 1 older commentHide 1 older comment
Teoman on 8 Mar 2023
Im trying to get the row opertaions needed to get to the rref of a matrix. The code above does not give needed output
Dyuman Joshi on 8 Mar 2023
What is the output you want?

Rohit on 21 Apr 2023
I understand that given a system of equations, you want to convert them in matrix form and then represent it in terms of free variables.
Based on your expected output, it looks like matrix B is another matrix you are concatenating and not RHS of equations.
So, in your current code, there seems an issue in finding free variables and using them correctly to get relevant equations.
I have written a basic code which gives your expected output, and you can modify it as per your requirements.
A = [2 3 4 56 6 8; 1 2 4 5 6 7; 3 6 12 15 18 21; 4 6 8 112 12 16];
B = [9; 8; 24; 18];
% A= [3,4,5 ; 1.5,2,2.5];
% B= [1;1/2];
Aug = [A B];
%calculating pivot and free variables
[RREF,pivot_variables] = rref(Aug);
[m, n] = size(RREF);
free_variables =setdiff(1:size(Aug,2),pivot_variables);
for j = 1:n
if ismember(j, pivot_variables)
fprintf('x%d = ', j);
for k = 1:n
if k ~= j && ismember(k,free_variables) %writing pivot variable as combination of free variables
fprintf(' %+d*t%d', -RREF(find(RREF(:,j),1),k), k-length(pivot_variables));
end
end
else
% print the free variable
fprintf('x%d =', j);
for k = 1:length(free_variables)
if k == j-length(pivot_variables)
fprintf(' t%d', k);
end
end
end
fprintf('\n');
end
x1 =
+4*t1 -97*t2 +6*t3 +5*t4 +6*t5
x2 =
-4*t1 +46*t2 -6*t3 -6*t4 -7*t5
x3 =
t1
x4 =
t2
x5 =
t3
x6 =
t4
x7 =
t5

### Categories

Find more on Logical in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!