Solve special system of linear equations in Matlab (GNU Octave) - matlab

I have a matrix, let's say 5x5 looking like this:
0 0 0 1 0
0 0 0 4/5 1/5
3/5 1/5 1/5 0 0
1/5 2/5 1/5 1/5 0
1/10 1/10 2/5 1/5 1/5
I need it to solve it like a system of linear equations looking like this (I can transpose it myself, but then multiplying it with the symbolic variables gets me into troubles):
0 * a + 0 * b + 3/5 * c + 1/5 * d + 1/10 + e = a
0 * a + 0 * b + 1/5 * c + 2/5 * d + 1/10 + e = b
0 * a + 0 * b + 1/5 * c + 1/5 * d + 2/5 + e = c
1 * a + 4/5 * b + 0 * c + 1/5 * d + 1/5 + e = d
0 * a + 1/5 * b + 0 * c + 0 * d + 1/5 + e = e
a + b + c + d + e = 1
I can easily solve this in wxMaxima, but I have to manually write all the values there, which is increasingly tedious with bigger matrices.
Is there a way to get the results after some steps using matlab operator \ for solving system of linear equations?

You can solve the equation set no?
>>[A-eye(5);ones(1,5)]\[0,0,0,0,0,1]'
ans =
0.1729
0.2061
0.1345
0.4350
0.0515
>> sum(ans)
ans =
1.0000

And a symbolic solution:
M=sym(A);
v=sym('[a;b;c;d;e]');
sol=solve(M*v==v,sum(v)==1);
returns solutions in the form sol.a, sol.b, ...

Related

How do you factor over a Z field?

I have to factorize a polynomial e.g.
over the field of Z5 using Matlab or Mupad.
And i tried everything read a lot of Matlab and Mupad documentation and still can't find it, so i am guessing it is the math i don't know that's going to help me factor it.
Don't kill a mosquito with a cannon!
You only need to find a root between 0, 1, 2, -2, -1.
Also, given that x5 = x, the problem reduces to finding x such that
2x + 2x^4 + x^3 + 2x^2 - 3 = 0
and since x ≠ 0, x^4 = 1 hence
2x + x^3 + 2x^2 - 1 = 0
Well, let's try!
1: 2 + 1 + 2 - 1 -> -1
2: -1 + 3 - 2 - 1 -> -1
-2: 1 - 3 + 3 - 1 -> 0 -> root!
Then the polynomial is divisible by (x - 3), and you can repeat the procedure with the quotient until there are no roots left.
Addendum
After dividing by (x - 3) we get
x4 + x2 + 1
which we can expressed as
(x2 + 1)2 - x2
or
((x2 + 1) - x)((x2 + 1) + x)
To find the factors of degree 2 programmatically, just try with x2 + ax + b for a and b between 0 and 4.
I found a mupad command to do what i needed.
Still thanks for exaplaining the math behind it.

Dividing two polynomials in MATLAB [duplicate]

This question already has answers here:
Divide two polynomials using MATLAB
(2 answers)
Closed 6 years ago.
I'm currently working on a Cyclic Code program for a class in MATLAB and I'm trying to figure out the best way to divide two polynomials, the generator P(X) and the user input, shifted by 3 (x^3 * D(X)) in order to get the Quotient Q(X) and Remainder C(X), which would allow me to get the transmitted data T(X) = X^3*D(X) + C(X)
The code I have for my program so far takes in the users 4-bit input in binary, i.e.
Insert 4-bit input: 1001
Then it checks it to make sure its valid, and shifts it giving:
0 0 0 1 0 0 1
which stands for the polynomial
X^3 + X^6
I then need to divide that by the generator polynomial
P(X) = 1 + X + X^3
Working it out on paper,
x^6 + X^3
___________
x^3 + x + 1
Gives: Q(X) = X^3 + X
R(X) = X^2 + X
So, T(X) = X^6 + X^3 + X^2 + X, which is 0111001 for the Codeword
What would be the best way to do this?
I have tried the following:
% Prompt for User input
b4_in = input('Insert 4-bit input: ' ,'s'); %Input 1001
%% CHECK FOR VAILD INPUT %%
dec_in = bin2dec(b4_in)
bin_in = fliplr(de2bi(dec_in)) %User input in Binary
d = [0000000]; %Calculating X^3 * D(X)
d = bin_in;
d(7)=0;
d = fliplr(d); %Gives 0 0 0 1 0 0 1
d
gen_pol = [1 1 0 1] %P(X) = 1 + X + X^3
[q, c] = deconv(bin_in, gen_pol)
When I go this, I get:
q =
1
c =
0 -1 0 0
What do I need to do differently to get the following?
q = 0 1 0 1
c = 0 1 1
Thank you!
In MATLAB, polynomials read in a binary vector from left to right. For example, x^3+x is [1 0 1 0], x^2+x is [1 1 0]. The Quotient Q(X) should be x^3-x instead of x^3+x. Make sure your inputs are in the right format, and you should get the following result as expected,
q =
1 0 -1 0
c =
0 0 0 0 1 1 0

How to write a varying matrix in matlab?

I have this equation system a set of 1 ≤ n ≤ 30
−(2 + α)x1 + x2 = b1,
xj−1 − (2 + α)xj + xj+1 = bj , for 2 ≤ j ≤ 29,
x29 − (2 + α)x30 = b30.
α = 1
We assume that the membrane is held at the end points (i.e x0 = 0 and x31 = 0). There is no weight on the membrane so all bj = 0 for j = 1 . . . 30 except for j = 6 where a load is applied: b6 = 2.
I want to calculate LU factorization of the system .
I do not know how to implement the left side of the system in matlab.
The right side I made it like this :
b=[0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]';
How to do the left side?
Thanks
It's unclear why you have included the entire linear system if you are only interested in the LU factorization of A? Regardless, here is some code which generates your A matrix as described above, and solves the linear system and shows the LU factorization.
% equation A*X = b
b = zeros(30,1);
b(6) = 2;
alpha = 1;
A = zeros(30, 30);
A(1, 1) = -(2 + alpha);
A(1, 2) = 1;
for i = 2:29
A(i, i-1) = 1;
A(i, i) = -(2 + alpha);
A(i, i+1) = 1;
end
A(30, 29) = 1;
A(30, 30) = -(2 + alpha);
You can then get the LU factorization using lu(A) or solve the linear system of equations using linsolve(A,b).

Matlab, linear programming

I want to solve this linear programming (simplex) problem using MATLAB 7, but it returns
Exiting: the problem is unbounded.
This function
f = 2(15 s0 + 8s1 + 2576s2 + 744s3 + 427s4 + 8s5)
Should be minimized in such a way that two constraints for each observation are
satisfied
0.1s0 + 0.1s1 + 14.5s2 + 4s3 + 2.4s4 – a0 − a1 − 145a2 − 40a3 − 24a4 ≥ −2.2
0.1s0 + 0.1s1 + 14.5s2 + 4s3 + 2.4s4 + a0 + a1 + 145a2 + 40a3 + 24a4 ≥ 2.2
S5 and a5 are 0. I used
f = [15 8 2576 744 427 8 15 8 2576 744 427 8];
b = [-2.2; 2.2];
a = [0.1 0.1 14.5 0.4 2.4 0 -1 -1 -145 -40 -24 0 ; 0.1 0.1 14.5 4 2.4 0 1 1 145 40 24 0];
[x, fval, exitflag, output, lambda] = linprog(f, a, b)
What is the right way to solve this problem?
You didn't constrain your s5 and a5 to actually be zero, since you set the corresponding coefficients in the a matrix to zero. Thus, they can take on any value, and the LP is unbounded.
To fix, add an equality constraint:
beq = [0; 0];
aeq = [0 0 0 0 0 1 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 1];
[x,fval,exitflag,output,lambda] = linprog(f,a,b,aeq,beq)
Or, just drop s5 and a5 from the LP since they don't contribute at all.

Matlab Vectorization : How to avoid this "for" loop?

I have following matrices :
X=1 2 3
Y=4 5 6
A=1 2 3
4 5 6
7 8 9
I Want to do
for each (i,j) in A
v = A(i,j)*X - Y
B(i,j) = v * v'
i.e. each element of A is multiplied by vector X, then resultant vector subtracts Y from itself and finally we take inner product of that vector to bring a single number.
Can it be done without for loop ?
One thing often forgotten in Matlab: The operator ' takes the conjugate transposed (.' is the ordinary transposed). In other words, A' == conj(trans(A)), whereas A.' == trans(A), which makes a difference if A is a complex matrix.
Ok, let's apply some mathematics to your equations. We have
v = A(i,j)*X - Y
B(i,j) = v * v'
= (A(i,j)*X - Y) * (A(i,j)*X - Y)'
= A(i,j)*X * conj(A(i,j))*X' - Y * conj(A(i,j))*X'
- A(i,j)*X * Y' + Y * Y'
= A(i,j)*conj(A(i,j)) * X*X' - conj(A(i,j)) * Y*X' - A(i,j) * X*Y' + Y*Y'
So a first result would be
B = A.*conj(A) * (X*X') - conj(A) * (Y*X') - A * (X*Y') + Y*Y'
In the case of real matrices/vectors, one has the identities
X*Y' == Y*X'
A == conj(A)
which means, you can reduce the expression to
B = A.*A * (X*X') - 2*A * (X*Y') + Y*Y'
= A.^2 * (X*X') - 2*A * (X*Y') + Y*Y'
An alternative method:
X = [1 2 3]
Y = [4 5 6]
A = [1 2 3; 4 5 6; 7 8 9]
V = bsxfun(#minus, A(:)*X, [4 5 6])
b = sum((V.^2)')
B = reshape(b , 3, 3)
I get the result:
B = 27 5 11
45 107 197
315 461 635