Solving a system of linear equations with a few known variables

I'm solving the following system of linear equations,
Ax = b, some of the x's are knowns.
For example,
A=
-12 12 0 0 0
0 -1 1 0 0
0 0 -0.5 0.5 0
0 0 0 -17 17
x = [x1 x2 x3 x4 x5]
b = [b1 0 0 0 b5]
When some of the variables are known, say x1 and x5 are known, the system can be reduced in terms of the known variables. However, when there are around 50 variables and 5 are known re-writing the matrix in terms of the known variables is difficult.
I would like ask for suggestions on alternate ways of solving these kind of linear systems in which the values of a few variables are known.

2 Comments

To solve b should be having a length equal to rows of A. Read about mldivide i,e \
Thanks for the reply. Yes, I am considering a square matrix to solve for b.

Sign in to comment.

 Accepted Answer

Assuming known is the logical index == TRUE for indexes of x that are known
% known = ismember(1:5,[1 5]) % in your example
x(known) = XValueYouKnow;
x(~known) = A(:,~known) \ (b-A(:,known)*x(known))

More Answers (1)

One way using solve():
syms x1 x2 x3 x4 x5 b1 b5
eqn=[ -12*x1+12*x2==b1;
-x2+x3==0;
-0.5*x3+0.5*x4==0;
-17*x4+17*x5==b5];
[x1,x2,x3,x4]=solve(eqn)
Second way using linsolve():
syms b1 b5
A=[ -12 12 0 0 0
0 -1 1 0 0
0 0 -0.5 0.5 0
0 0 0 -17 17];
b = [b1;0;0;b5];
[x,R]=linsolve(A,b)
Third way using mldivide():
syms b1 b5
A=[ -12 12 0 0 0
0 -1 1 0 0
0 0 -0.5 0.5 0
0 0 0 -17 17];
b = [b1;0;0;b5];
A\b % x5 has infinity number of solutions I guess

10 Comments

Sorry, if my question wasn't clear.I would like to know how to solve the above system when say the values of x1 and x5 are known.
THe same way...
syms x2 x3 x4 b1 b5
x1=4; %example values
x5=5;
eqn=[ -12*x1+12*x2==b1;
-x2+x3==0;
-0.5*x3+0.5*x4==0;
-17*x4+17*x5==b5];
[b1,x2,x3,x4]=solve(eqn)
Thanks you very much. Could you please give suggestions on how the above can be implemented in the matrix form? I'm afraid it would be difficult to write eqn=[] and use solve(eqn) for a large system with around 50 variables.
For simplicity, let us consider A to be a square matrix(I provided a rectangular matrix in my example).
just use mldivide() where matrix A is the coefficients of the equations and b is the constants of each equation
so size(A,1)==length(b) should be satisfied see the link below to know what I mean
Thank you, I will definetly check the mldivide function.
I just tried solving for x with square matrix, from the way suggested above. I think , I am making a mistake in the syntax. The output is empty.
syms x2 x3 x4
x1=8132;
x5=2666;
b1 = -16666;
b5 = 16666;
eqn=[ 190*x1-190*x2==b1;
-190*x1+381*x2-190*x3==0;
-190*x2+381*x3-190*x4==0;
-190*x3+381*x4-190*x5==0;
-190*x4+190*x5==b5];
[x2,x3,x4]=solve(eqn)
It clearly says that the solution does not exist then why rant about it??
syms x2 x3 x4
x1=8132;
x5=2666;
b1 = -16666;
b5 = 16666;
eqn=[ 190*x1-190*x2==b1;
-190*x1+381*x2-190*x3==0;
-190*x2+381*x3-190*x4==0;
-190*x3+381*x4-190*x5==0;
-190*x4+190*x5==b5];
solve(eqn,x2,x3,x4)
[A,b]=equationsToMatrix(eqn);
A\b
pinv(A)*b gives a solution with min norm
ok now what's your query? Alternatively you can use lsqminnorm() as mentioned https://in.mathworks.com/help/matlab/ref/lsqminnorm.html#d120e717236
lsqminnorm(double(A),double(b))
My query is,
eqn=[ 190*x1-190*x2==b1;
-190*x1+381*x2-190*x3==0;
-190*x2+381*x3-190*x4==0;
-190*x3+381*x4-190*x5==0;
-190*x4+190*x5==b5];
can be easily written for a small set of equations. When the size of matrix A is 50 x 50 , and 50 variables are present,it will be difficult to manually type all 50 equations in eqn=[]and use solve to reduce A matrix in terms of the unknowns.

Sign in to comment.

Categories

Find more on Linear Algebra 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!