Clear Filters
Clear Filters

linear system of equations

7 views (last 30 days)
antlhem
antlhem on 29 Aug 2018
Edited: John D'Errico on 29 Aug 2018
I have two arrays and both of them has variables, I am trying to use
linsolve
but no luck, since both have the variables and also some of the rows are just numbers. Is there a function in MATLAB R2017a that could solve this considering that both arrays have the variables to solve and numbers. My code and resutls are as follows:
x=sym('x', [1 4]);
%states = [ 1, 0, x1, x2, x1 + 1, x1 + x2, x2 + 1, 1, 0]
states = [ 1 0 x1 x2 x1 + 1 x1 + x2 x2 + 1 1 0];
%patt=[ 1, 0, x1, x2, x3, 1, 1, 1, x4]
patt=[ 1 0 x1 x2 x3 1 1 1 x4];
A=equationsToMatrix(states,x) %matrix of coefficients
B=transpose(patt) %constant terms
X=linsolve(A,B)%the solution
I know that the solution exist an is as follows, the question is how get this solution with MATLAB? I have much more arrays like this and I would like to optimise this process.
  5 Comments
Star Strider
Star Strider on 29 Aug 2018
If you do:
RankA = rank(A)
RankA =
2
antlhem
antlhem on 29 Aug 2018
ok, this will give me the linearly independent columns but, doesn't give the values of my variables. Is there a function in MATLAB that joins 2 arrays in the way that the resulted array could have a more suitable form to be solved by linsolve? I am thinking something like:

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 29 Aug 2018
Edited: John D'Errico on 29 Aug 2018
It is not clear what you are trying to do, since the code you have written produces this:
A =
[ 0, 0, 0, 0]
[ 0, 0, 0, 0]
[ 1, 0, 0, 0]
[ 0, 1, 0, 0]
[ 1, 0, 0, 0]
[ 1, 1, 0, 0]
[ 0, 1, 0, 0]
[ 0, 0, 0, 0]
[ 0, 0, 0, 0]
B =
1
0
x1
x2
x3
1
1
1
x4
Here, A has two columns that are entirely zero.
My guess is that your problem is to solve for the variables x1,x2,x3,x4, such that:
A*[x1;x2;x3;x4] = B
where B also has those variables embedded in it.
Now, one might simply think to just throw it at solve. That will fail, of course, but it will be instructive to show you why it fails.
solve(A*[x1;x2;x3;x4] == B)
ans =
struct with fields:
x1: [0×1 sym]
x2: [0×1 sym]
x3: [0×1 sym]
x4: [0×1 sym]
Effectively, you now have a linear system of 9 equations in 4 unknowns, so in general, no exact solution. Solve only works when an exact solution does exist.
Anyway, the system is linear. So you can just move the unknowns in B to the left hand side. That is, re-write B as:
B = B0 + B1*[x1;x2;x3;x4]
where
B0 = [1;0;0;0;0;1;1;1;0];
B1 = zeros(9,4);
B1([3 4 5 9],:) = eye(4);
As you see, I constructed them properly.
B0 + B1*[x1;x2;x3;x4]
ans =
1
0
x1
x2
x3
1
1
1
x4
Now, your problem reduces to solving for the vector of unknowns, such that:
(A-B1)*[x1;x2;x3;x4] == B0
NOT GONNA HAPPEN.
There is no exact solution. So you cannot use symbolic tools to solve it. Yeah, you can try.
(double(A)-B1)\B0
ans =
3.92523114670944e-16
1
7.69185074553425e-16
0
Ignoring the floating point trash, does the vector [0;1;0;0] actually solve that problem? No.
Compare the result to B0.
[(A-B1)*[0;1;0;0],B0]
ans =
[ 0, 1]
[ 0, 0]
[ 0, 0]
[ 0, 0]
[ 0, 0]
[ 1, 1]
[ 1, 1]
[ 0, 1]
[ 0, 0]
So you can see the solution is not indeed exact! In fact, as I said before, there is no exact solution to your problem.
Lets look at your equations.
[A-B1,B0]
ans =
[ 0, 0, 0, 0, 1]
[ 0, 0, 0, 0, 0]
[ 0, 0, 0, 0, 0]
[ 0, 0, 0, 0, 0]
[ 1, 0, -1, 0, 0]
[ 1, 1, 0, 0, 1]
[ 0, 1, 0, 0, 1]
[ 0, 0, 0, 0, 1]
[ 0, 0, 0, -1, 0]
Look at the first row. (Row 8 is the same.) That corresponds to the equation
0*x1 + 0*x2 + 0*x3 + 0*x4 = 1
Any guesses what values of x1,x2,x3,x4 might allow you to have any success at all here? I'll give you two guesses, and your first three guesses won't even count. It is simply not gonna happen, at least not unless your name is Harry Potter, and you can freely suspend the laws of mathematics with the wave of a wand.
So in theory, you can solve the problem, IF you are willing to accept an inexact solution, simply by moving the unknowns to the left hand side as they should be in the first place. Don't expect an exact solution when trying to solve an over-determined linear system of equations though.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!