Finding "all solutions" of x in A.x = b in the finite field domain - matlab

Matlab provides a way to find a particular solution to Ax=b in GF(2^m). Here is the link http://www.mathworks.in/help/comm/ref/gflineq.html But it gives only one solution. How can I find the rest of the solutions?
Eg: A=[1 0 2 0 0 1] in GF(4), b=[0] in GF(4). x=A\b gives [0 0 0 0 0 0]' as the solution. I also know [0 1 2 3 2 3]' to be one other solution. But I cannot get any other solution except all zero.How to find all solutions in Matlab?

In General,
The solutioin to the matrix equation Ax=b need not exist and even if it does, need not be unique. In your case, you know that multiple solutions exist. In such a situation, you have a "particular solution" which is basically, any x that solves Ax=b and now, to get multiple solutions, you can keep adding vectors from the Nullspace of A.
Proof:
Let x be a particular solution and y be in the Nullspace of A (any vector in Nullspace is OK). We know Ax=b. We also know Ay=0. Add them up, A(x+y)=b.
TL;DR Solution:
Find the Nullspace of the Matrix in GF and add any vector of the Nullspace to the particular solution to generate more solutions.
I have not used it but there seems to be a code on MATLAB Central which finds the Nullspace of a Matrix in GF.

Related

Binary constraint in non linear optimization in R (NLOPTR - COBYLA)

I am trying to solve a non linear optimization problem using NLOPTR library in R. Within NLOPTR, I am using COBYLA algorithm. Some of my constraints are binary and I am not able to specify it in NLOPTR.
Consider two values x, y. My requirement is as follows,
x can be 0 or 1.
y can be 0 or 1
x + y = 1 #so that one of x/y is 0 and the other one is 1.
If any of you has specified similar problem in Non-Linear Optimization, please help. If there is a better library to use, kindly suggest it.
Thanks / Sandeep

How do I find Multiple Roots for a Polynomial in Matlab?

I'm trying to create two matlab .m files. "f.m" contains a function of the polynomial I want to use.
function y = f(x)
y = x.^3 - 7*x + 6;
Compute.m calls fzero with that function returning the polynomial and a for loop of values from -10 to 10.
clc
fun = #f;
answerArray= [];
for x0 = -10:10
z = fzero(fun,x0);
answerArray=[answerArray z];
end
answerArrayUnique=unique(answerArray)
The problem is my unique method is not working for some of the negative values. I am getting an answer of:
answerArrayUnique =
-3.0000 -3.0000 -3.0000 1.0000 2.0000
What is strange, is that if unique was failing every time on negative numbers there would be many more -3.0000's. Anyone have any idea what is causing this?
Note: Using the unique method call on that again does not fix the issue, which leads me to believe it thinks the numbers as they go further out in to the ten-thousandth spot are different.. maybe?
You should see Mendis answer, for how to properly do it. However, the problem you have is because those -3.000 are not equal.
unique work by sorting and then checking if consecutive numbers are equal. However, as you have used a numerical method to find the zeros, the solutions are approximate. Try to subtract two of the equal solutions, the difference will be small, but not zero.
To avoid this you can use uniquetol which allows you to specify a tolerance, for which within, you think two numbers are equal. e.g. uniquetol(answerArray,1e-4)
In matlab the best way to represent a polynomialy is thru coefficient vector. for your example:
p = [1 0 -7 +6];
To calculate the value at x=0.8 for example you use:
polyval(p,0.8)
to find the roots you use:
r = roots(p) %output: -3 2 1
Use 'fzero' only for non linear function and pray to find all solutions.

For what value of k are these 3 vectors linearly dependent

So I have these three vectors:
And I have to find out for what value of k these three vectors are linearly dependent. I have tried using rref and linsolve with syms for this but that did not work out. I'm relatively new to MatLab and matrices so please keep that in mind.
I know in order to check if vectors are linearly dependent that c1...cn have to be non-zero.
I also want to know how you can use variables in general when solving these types of equations in MatLab.
A set of vectors (at least if you have n vectors in n dimensions) is linearly dependent if the matrix constructed from them is singular, i.e. if its determinant is 0. If you have the Symbolic Math Toolbox, you can construct a symbolic matrix:
syms k;
M = [1 k 0; -1 1 2; 0 0 3];
det(M)
This will tell you that det(M)==3*k+3, which you can solve by hand. But generally, you can ask matlab to solve it:
solve(det(M)==0,k);
which will tell you the answer is -1. So unless k==-1, these vectors are linearly independent (i.e. they comprise a basis of the Euclidean space R^3).
Update: If you don't have the Symbolic Math Toolbox, you could still try to find a numerical solution. First define a function
detfun=#(k) det([1 k 0; -1 1 2; 0 0 3]);
that for any value of k will give you the determinant of your matrix, for instance detfun(3) gives 12. Then you can use fsolve to find a numerical solution to the equation detfun(k)==0, by calling
fsolve(detfun,0)
in which the second argument, 0, refers to the starting point of the search performed by fsolve. This will tell you that the answer is k==-1, but a single call to fsolve will only give you a single solution. If your function has multiple roots, you have to play around with the starting points to find more of them. In this case, you can know that your function (i.e. det(M(k)) is linear in k, so it has a unique root.

Matlab left - division in vectors?

x=[1;2;3]
x =
1
2
3
y=[4;5;6]
y =
4
5
6
x\y
ans =
2.2857
How did Matlab find that result ? (I searched many forums but I did not understand what they told.I would like to know the algorithm which gave this result.)
From MATLAB documentation of \:
If A is an M-by-N matrix with M < or > N and B is a column vector with M components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations A*X = B.
Here your system is not under/over-determined. Since both have 3 rows. So you can visualize your equation as:
xM=y
M=inv(x)*y
Now, since your matrix is not square, it will calculate the pseudo-inverse using SVD. Therefore,
M=pinv(x)*y;
You will get value of M as 2.2857.
Another explanation can be: It will give you the solution of xM=y in the sense of least squares. You can verify this as follows:
M=lsqr(x,y)
This will give you the value of M = 2.2857.
You can always do help \ in MATLAB command window to get more information.
You are encouraged to check more details about the least squares and pseudo-inverse.
This documentation should explain it
http://www.mathworks.com/help/matlab/ref/mrdivide.html
Here is a link to the algorithm
http://www.maths.lth.se/na/courses/NUM115/NUM115-11/backslash.html
You can see the source inside matlab much more easily though. (I don't have it locally so I can't check but the source of a lot of matlab functions is available inside matlab)

RBF and pseudoinverse XOR

The problem i am trying to understand is easy but i cant seem to get the correct result in matlab. The actual problem is that i want to get the weight vectors of a 2 hidden layer input RBF using just the plain distance as a function, i.e. no Baysian or Gaussian function as my φ. I will use the function with 2 centres let's say 0,0 and 1,1. So this will give me a Matrix φ of:
[0 sqrt(2) ; 1 1; 1 1; sqrt(2) 0] *[w1; w2] = [0;1;1;0] As defined my the XOR function.
When i apply the pseudoinverse of the Φ in matlab * [0;1;1;0] though i get [0.33 ; 0.33] which is not the correct value which would allow me to get the correct output values [0;1;1;0].
i.e. .33 * sqrt(2) != 0 .
Can someone explain to me why this is the case?
I'll take a swag at this. The matrix, I'll call A, A = [0 sqrt(2) ; 1 1; 1 1; sqrt(2) 0] has full column rank, but not full row rank, i.e. rank(A) = 2. Then you essentially solve the system Ax = b, where x is your weighting vector. You could also just do x = A\b in Matlab, which is supposedly a much more accurate answer. I get the same answer as you. This is a very rough explanation, when your system can not be solved for a certain solution vector, it means that there exists no such vector x that can be solved for Ax = b. What Matlab does is try to estimate the answer as close as possible. I'm guessing you used pinv, if you look at the Matlab help it says:
If A has more rows than columns and is not of full rank, then the overdetermined least squares problem
minimize norm(A*x-b)
does not have a unique solution. Two of the infinitely many solutions are
x = pinv(A)*b
and
y = A\b
So, this appears to be your problem. I would recommend looking at your φ matrix if possible to come up with a more robust system. Hope this is useful.