How to solve a simple Matrix equation?

Hello,
I have a small problem. I want to solve the multiplication of a given matrix.
For Example:
A = [-1,0,0,1;1,-1,0,0;0,1,-1,0;0,0,1,-1];
x = sym('x', [4,1]);
x(1,1)=1;
solve(A*x==[0;0;0;0])
Because I am a beginner at Matlab, I really don't know what im doing wrong. In this Example x should be equal to ones.
The goal is, to use it for a big matrix and calculate all unknown 'x'. If it is not possible to calculate them, I have to estimate the missing x, and solve it with something like a Netwon-Raphson method. But that will propably be in a later question.
Thank you for answering.

 Accepted Answer

Jon
Jon on 19 Feb 2020
Edited: Jon on 19 Feb 2020
It looks like you want to find all of the vectors satisfying Ax = 0 for some singular matrix , A. If so, you can do this using
X = null(A)
This returns a basis for the null space of a matrix A.
In your case MATLAB returns X = [0.5;0.5;0.5;0.5], any multiple of this will satisfy your equation,
so for example 2*[0.5;0.5;0.5,0.5] = [1;1;1;1] is a solution and you can verify that A*[1;1;1;1]=0
To find out more about the null function type doc null on the command line in MATLAB

7 Comments

Phillipp
Phillipp on 19 Feb 2020
Edited: Phillipp on 19 Feb 2020
Thank you very much. That is actually exactly what i needed for this problem.
This was only a small example, in the upscaled version there is another problem that arises. I actually have conditions that must be fulfilled, e.g.: B(51,1)=405;
With the B = null(Matrix) I have to check if the multiple of any solution of B is correct. Is there any way to implement these conditions in Matlab, that is not to check with hundreds of loop?
I'm not quite clear from your description what the conditions are that you have to check. Can you give a small example.
My base equation is A*x=0. The whole calculation refers to a network of pipes that are used to transport water.
"A" is a matrix with n-nodes x m-pipes. So it gives the information about which node is connected to which pipe.
"x" is the vector that has all the massflows of water in it. I do know some basic conditions e.g. no element of "x" can be negative, and when there is a measurement station, that value has to be fulfilled. So the Element x(10,1) has to be 500 l/h or anything else.
That is why I tried to solve it with A*x==0, because then I can change specific values of x to fulfill my boundary conditions.
So actually, now that I understand your problem, I think you can solve it quite directly using MATLAB's lsqlin function. Hopefully you have the optimization toolbox included in your MATLAB distribution.
You can type ver on the command line to see if you have the optimization toolbox.
If so you can solve your problem as shown in the attached simple example with 3 nodes and 2 pipes where I know the flow in pipe 1 and pipe 3 are 3 l/h and 8 l/h respectively.
% define tolerance for finding solution
tol = 1e-6;
% define piping network
Aeq = [1 1 -1 0;-1 -1 0 1;0 0 1 -1] % piping topology
% assign known measured values
iPipe = [1;3]; % pipe numbers where flow is measured (columns indices in Aeq)
xMeasured = [3;8] % measured flow rates
% find key dimensions
[numNodes,numPipes] = size(A);
numMeasured = length(xMeasured);
% assign constraints
beq = zeros(numNodes,1); % sum of flows into nodes must be zero
lb = zeros(numNodes,1); % lower bound, flows must be greater than zero
% assign objective function whose norm is minimized C*x - d
C = zeros(numMeasured,numPipes);
for k = 1:numMeasured
C(k,iPipe(k))=1; %C*x select desired flows
end
d = xMeasured; % so ideally C*x = d, or equivalently Cx - d = 0
% solve constrained least squares problem
[x,resnorm] = lsqlin(C,d,[],[],Aeq,beq,lb,[])
% check the size of resnorm to see if a solution was possible, it should be
% very small
if resnorm > tol
warning('exact solution could not be found')
end
Here are a couple of other approaches.
You could modify the above to use a linear objective function instead of a quadratic one and instead use linprog which I think is available without any special toolboxes.
Another partial idea is that you could move the columns in A (the incidence matrix) that correspond to the known flows over to the right hand side of the equation. So if the original system were Ax = 0, you would instead have Au*xu = -An*xn where Au,xu are the columns corresponding to the unknown flows, and the unknown flows An,xn are the columns corresponding to the known flows and xn are the known flows. Note that -An*xn is just a vector, call, it b. Then this puts equations in for Au*xu = b. Which can be solved using pinv, or Matlab \ . This at least enforces the known flow constraints, but I'm not sure how you would enforce the positivity conststraint (flows must be greater than zero)
If you have lsqlin then maybe just stick with that.
I have the optimization toolbox installed.Thank you! That really helped me.
You made a tiny mistake you have to change this:
lb = zeros(numNodes,1); % lower bound, flows must be greater than zero
to this.
lb = zeros(numPipes,1); % lower bound, flows must be greater than zero
But man, you really helped me out. Thanks a lot.
Jon
Jon on 20 Feb 2020
Edited: Jon on 20 Feb 2020
Good catch!
Glad it seem like this approach will help you. One benefit of the least squares minimization, is that it very naturally handles the situation where you have sensor measurements of the flows but they may be noisy/slightly in error. Imagine that you in fact have all of the flows measured with noisy measurements. The measured flows will not satisfy Ax = 0 but you can find a set of flows which are as close as possible to the measured values (in the least square sense) but that satisfy Ax = 0. This is in some sense best physically consistent estimate of the overall network flows.

Sign in to comment.

More Answers (0)

Asked:

on 19 Feb 2020

Edited:

Jon
on 20 Feb 2020

Community Treasure Hunt

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

Start Hunting!