When using equationsToMatrix you solve a set of linear equations as in the example (the solution is included)
syms x y z;
[A, b] = equationsToMatrix([x + y - 2*z == 0, x + y + z == 1, 2*y - z + 5 == 0], [x, y, z])
%solution of the equation set
A =
[ 1, 1, -2]
[ 1, 1, 1]
[ 0, 2, -1]
b =
0
1
-5
The vector b returns the values of the variables at issue: x,y, and z. However if I type x then MATLAB returns x and not 0, which is the solution of the equation in this case. This also occurs without adding the syms option.
The other problem is that if I type b(1) or b(2), I don't get any value: I would expect b to contain the values of x,y and z.
What I would need is to get something like this in the end
b(1) = 0
or
x = 0
What should I do to get the values of x,y,z by just typing x,y,z?
What you have is a way of converting symbolic linear equations into a numeric system by extracting the coefficient matrices. To solve the system you need to do
sol = A\b;
and now you can use the values in another expression with
subst(expr, {x,y,z}, {sol(1),sol(2),sol(3));
for example
A =
1 1 -2
1 1 1
0 2 -1
b =
0
1
-5
>> A\b
ans =
3.0000
-2.3333
0.3333
Related
The code is like this:
syms x y z
eqns = [x + y == 1, x + z == 2, z - y == 0.5];
vars = [x y z];
[solx, soly, solz] = solve(eqns,vars)
But the result is like this:
solx =
Empty sym: 0-by-1
soly =
Empty sym: 0-by-1
solz =
Empty sym: 0-by-1
Why is this not working?
Let's recast your problem as a numerical linear algebra problem:
A = [1 1 0; 1 0 1; 0 -1 1];
b = [1; 2; 0.5];
result = A\b
result =
1.00000
0.16667
0.83333
Aside from the result, you get a warning:
warning: matrix singular to machine precision
That should tell you the answer: your system is not solvable. You can determine the same thing manually by subtracting the first equation from the second:
x + z == 2
- x + y == 1
-------------
z - y == 1
This contradicts z - y == 0.5, so your system does not have a valid solution.
I have a list of formula stored in a cell array, and I solve the unknowns within the matrix.
For example, consider a 2*2 matrix:
[2x+y, 4q+z; 3x+0.5y, 2q+12z ]
How to solve q,x,y,z by setting each cell equals 20? (i.e., q= 4, x =5, y = 10, z=1)
You're asking to solve a linear system. The canonical way to write a linear system is as A*x = b where A is a matrix, x is the vector to solve for, and b is also a vector. Writing your problem (in math) using matrices, the system is:
[0 2 1 0 [q [20
4 0 0 1 * x = 20
0 3 .5 0 y 20
2 0 0 12] z] 20]
To solve the system numerically in MATLAB:
A = [0, 2, 1, 0; 4, 0, 0, 1;, 0, 3, .5, 0; 2, 0, 0, 12];
b = [20; 20; 20; 20];
xsol = linsolve(A, b);
You could also do xsol = A \ b. A point of caution: both linsolve and \ will solve the system in the least squares sense if the system is overdetermined (typically, system is overdetermined if A is m by n where m > n).
xsol(1) will give the value for q, xsol(2) will give value for x, etc...
Solution is [4.7826; 5.0000; 10.0000; 0.8696]
One way to achieve what you are looking for is to use the Symbolic Toolbox. Here is an example code to solve for q, x, y, and z.
syms q x y z
A = [2*x+y==20, 4*q+z==20; 3*x+0.5*y==20, 2*q+12*z==20];
S = solve(A,[q x y z]);
disp([S.q S.x S.y S.z]);
Output:
[110/23, 5, 10, 20/23]
I want to ask Matlab to tell me, for example, the greatest common divisor of polynomials of x^4+x^3+2x+2 and x^3+x^2+x+1 over fields like Z_3[x] (where an answer is x+1) and Z_5[x] (where an answer is x^2-x+2).
Any ideas how I would implement this?
Here's a simple implementation. The polynomials are encoded as arrays of coefficients, starting from the lowest degree: so, x^4+x^3+2x+2 is [2 2 0 1 1]. The function takes two polynomials p, q and the modulus k (which should be prime for the algorithm to work property).
Examples:
gcdpolyff([2 2 0 1 1], [1 1 1 1], 3) returns [1 1] meaning 1+x.
gcdpolyff([2 2 0 1 1], [1 1 1 1], 5) returns [1 3 2] meaning 1+3x+2x^2; this disagrees with your answer but I hand-checked and it seems that yours is wrong.
The function first pads arrays to be of the same length. As long as they are not equal, is identifies the higher-degree polynomial and subtracts from it the lower-degree polynomial multiplied by an appropriate power of x. That's all.
function g = gcdpolyff(p, q, k)
p = [p, zeros(1, numel(q)-numel(p))];
q = [q, zeros(1, numel(p)-numel(q))];
while nnz(mod(p-q,k))>0
dp = find(p,1,'last');
dq = find(q,1,'last');
if (dp>=dq)
p(dp-dq+1:dp) = mod(p(1+dp-dq:dp) - q(1:dq), k);
else
q(dq-dp+1:dq) = mod(q(dq-dp+1:dq) - p(1:dp), k);
end
end
g = p(1:find(p,1,'last'));
end
The names of the variables dp and dq are slightly misleading: they are not degrees of p and q, but rather degrees + 1.
I have a discrete signal
x = [ 1 2 3 4 5 6 ]
with
n = [ -2 -1 0 1 2 3 ]
How can i plot y[n] = x[n-1] + x[n-2] + x[n] ?
Thanks.
You can do the following:
y = x(1:end-2) + x(2:end-1) + x(3:end);
plot(n(3:end), y)
This looks like a filter... You should consider using the filter function to calculate y:
x = [...whatever...];
% Filter coefficients from your difference equation.
b = [1 1 1];
a = 1;
y = filter(b, a, x);
plot(n, y);
This will handle initial conditions more appropriately than naive approaches, so you will get a 6-element vector out with your given input (although note that your data is liable to be partly garbage for the first three samples).
I have to solve a simple problem using function linprog in matlab math toolbox. The problem is that I don't know how to format my equations so this function solves the problem.
This is the function I am trying to minimize (a_i are some given coefficients, x is in R^5):
x = argmax min{a1*x1 + a2*x2, a2*x2 + a3*x3 + a4*x4, a4*x4 + a5*x5}
subject to:
sum(x_i) = 3000
all x_i >= 0
This could be rephrased as:
(x, lambda) = argmin(-lambda)
subject to:
a1*x1 + a2*x2 >= lambda
a2*x2 + a3*x3 + a4*x4 >= lambda
a4*x4 + a5*x5 >= lambda
sum(x_i) = 3000
all x_i >= 0
I could only find examples of minimization of simple linear functions without min/max arguments in it. Could you give me a hint how to make my structures as arguments for linprog function?
Let's try the following
your x vector is now
[x1 x2 x3 x4 x5 lambda]
the objective vector
f = [0 0 0 0 0 -1]
equality constraint:
Aeq = [1 1 1 1 1 0] beq = 3000
Inequality constraint:
A = [-a1 -a2 0 0 0 1; 0 -a2 -a3 -a4 0 1; 0 0 0 -a4 -a5 1] b = [0;0;0]
lower bound:
lb = [0 0 0 0 0 -inf]
now try
linprog( f, A, b, Aeq, beq, lb )
up to some transposing of arguments should do the trick.
I don't believe you can pose the question as you phrased it as a linprog problem. The "MIN" operation is the problem. Since the objective function can't be phrased as
y = f'x.
Even though your constraints are linear, your objective function isn't.
Maybe with some trickery you can linearize it. But if so, that's a math problem. See: https://math.stackexchange.com/