I want to use solve() to solve a large system of linear equations. The solve() function needs equations and variables. I use a loop to generate the equations and my variables are contained in a large array. This is a simple code of what I am trying to do:
x = sym('x',[1 3])
eqn = sym('eqn', [1,3])
eqn1 = 2*x1 + x2 + x3 - 2 == 0
eqn2 = 2*x2 -x2 -x1- x3 == 3
eqn3 = 2*x2+ x1 + 3*x3 == -10
Y = solve(eqn, x)
MATLAB does not recognize my variable x1. I have solved the same system using the following code:
syms x1 x2 x3
eqn1 = 2*x1 + x2 + x3 == 2
eqn2 = 2*x2 -x2 -x1 - x3 == 3
eqn3 = 2*x2+ x1 + 3*x3 == -10
X = solve([eqn1, eqn2, eqn3, eqn4], [x1 x2 x3])
structfun(#subs,X)
But this is useless for a very large number of equations. What am I doing wrong?
You don't need symbolic (syms) for that. This is a standard linear system of equations that can be represented as:
Ax = b where A = [2 1 1; -1 1 -1; 1 2 3], x = [x1; x2; x3] and b = [0; 3; -10]
To solve for x, you would first define
A = [2 1 1; -1 1 -1; 1 2 3]
and
b = [0; 3; -10]
and then solve using
x = A\b
PS. There are some odd things in your question, eg. in eq.2 eqn2 = 2*x2 -x2 -x1- x3 == 3 I assume you omitted simplying this to -x1 +x2 -x3 ==3.
PS2. This is pretty standard Matlab, you can find a lot of info under the standard mldivide page in the documentation along with a lot similar questions here on SO.
Related
For the below given input and output, the matrix A can be found out by pseudoinverse or mrdivision in MATLAB. Similarly, I would now like to know, how to determine A, if my output signal Y matrix contains additive zero mean, uncorrelated, Gaussian noise?
x1 = [1 1 1]';
x2 = [0 1 1]';
x3 = [0 0 1]';
x4 = [1 0 1]';
y1 = [1 2 0]';
y2 = [-1 0 3]';
y3 = [3 1 1]';
y4 = [5 3 -2]';
X = [x1 x2 x3 x4];
Y = [y1 y2 y3 y4];
A = Y/X
Also, I have modelled the unknown noisy output as below:
y1_n = y1 + sqrt(var(y1))*randn(size(y1));
y2_n = y2 + sqrt(var(y2))*randn(size(y2));
y3_n = y3 + sqrt(var(y3))*randn(size(y3));
y4_n = y4 + sqrt(var(y4))*randn(size(y4));
Y = [y1_n y2_n y3_n y4_n];
The statement A = Y/X solves the linear system of equations A*X = Y. If the system is overdetermined, as in your case, the solution given is the least squares solution. Thus, if you have additive, zero mean, uncorrelated, Gaussian noise on Y, then A = Y/X will give you the best possible, unbiased, estimate of A.
Note that the noise you add to your Y matrix is quite large, hence the estimate of A is far away from the ideal. If you add less noise, the estimate will be closer:
x1 = [1 1 1]';
x2 = [0 1 1]';
x3 = [0 0 1]';
x4 = [1 0 1]';
X = [x1 x2 x3 x4];
y1 = [1 2 0]';
y2 = [-1 0 3]';
y3 = [3 1 1]';
y4 = [5 3 -2]';
Y = [y1 y2 y3 y4];
for n = [1,0.1,0.01,0]
Y_n = Y + n*randn(size(Y));
A = Y_n/X;
fprintf('n = %f, A = \n',n)
disp(A)
end
Output:
n = 1.000000, A =
2.9728 -5.5407 2.8011
2.6563 -1.3166 0.6596
-3.3366 1.1349 1.5342
n = 0.100000, A =
2.0011 -4.0256 2.9402
1.9223 -1.0029 1.0921
-3.1383 1.9874 1.0913
n = 0.010000, A =
1.9903 -3.9912 2.9987
1.9941 -1.0001 1.0108
-3.0015 2.0001 1.0032
n = 0.000000, A =
2.0000 -4.0000 3.0000
2.0000 -1.0000 1.0000
-3.0000 2.0000 1.0000
Of course if you make X and Y larger by adding more vectors you'll get a better estimate too, and will be able to compensate more noisy data.
I'm confused about MATLAB's symbolic operation precedence. My code does not produce the desired output.
syms x1 x2 x3 x4
aa=((x1 == 0 & x2 == 0 & x3 == 0 & x4 == 0) + ((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000));
bb=eval(subs(aa, [x1 x2 x3 x4], [0.2 0.2 0.2 0.2]))
I expect the output of
bb=
0.1051
but the actual output is
bb =
logical
0
Why does MATLAB do this?
I use MATLAB R2018b
Case 1: x == y
syms x y z
aa = (x == y) + z
Meaning
aa = [x or y] + z = x + z = y + z
Wherever you have x, you can replace it by y, interchangeably.
It does not check if x and y are the same
Case 2: isequal(x, y)
syms x y z
aa = (x == y) + z
aa = isequal(x, y) + z
Meaning
aa = [check if x and y are the same] + z = 0 + z = z
Modify the given code to this using isequal()
syms x1 x2 x3 x4
aa=((isequal(x1, 0)& isequal(x2, 0) & isequal(x3, 0) & isequal(x4, 0)) + ...
((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000));
bb=eval(subs(aa, [x1 x2 x3 x4], [0.2 0.2 0.2 0.2]));
Result
bb = 0.1051
I think MATLAB tries to reduce the number of brackets necessary to produce the simplest output. Note that if a is equal to b, a+c is also equal to b+c, so the statement is not wrong. If you call simplify(aa) you even get a simple a == b, which is indeed the simplest form as one can cancel the c on both sides:
syms a b c
aa=(a==b)+c
aa =
a + c == b + c
pretty(aa)
a + c == b + c
simplify(aa)
ans =
a == b
With respect to your edited question: why are you using symbolic variables at all? They are slow and cumbersome (not to speak about the unspeakable evil that is eval). Using numeric computation does yield the correct result:
syms x1 x2 x3 x4
aa=((x1 == 0 & x2 == 0 & x3 == 0 & x4 == 0) + ((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000));
bb=eval(subs(aa, [x1 x2 x3 x4], [0.2 0.2 0.2 0.2]))
bb =
logical
0
x1=0.2;x2=0.2;x3=0.2;x4=0.2;
(x1 == 0 & x2 == 0 & x3 == 0 & x4 == 0)
ans =
logical
0
((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000)
ans =
0.1051
aa=((x1 == 0 & x2 == 0 & x3 == 0 & x4 == 0) + ((9*x1)/50 + (327*x2)/2000 + (1841*x3)/20000 + (1799*x4)/20000))
aa =
0.1051
I suspect the problem is somewhere in the belly of subs and/or eval.
It might seem a simple question. I need it, though. Let's assume we have two equations:
2 * y + x + 1 = 0
and
y - 2 * x = 0
I would like to find their bisection which can be calculated from this equation:
|x + 2 * y + 1| |-2 *x + y |
------------------- = -----------------
(sqrt(2^2 + 1^2)) (sqrt(1^2 + 2^2))
To make the long story short, we only need to solve this below system of equation:
2 * y + x + 1 = -2 *x + y
and
2 * y + x + 1 = 2 *x - y
However, using solve function of MATLAB:
syms x y
eqn1 = 2 * y + x + 1 == -2 *x + y ;
eqn2 = 2 * y + x + 1 == 2 *x - y ;
[x, y] = solve (eqn1 , eqn2, x, y) ;
Will give me:
x = -1/5 and y = -2/5
But, I am looking for the result equations, which is:
y = -3 * x - 1 and 3 * y = 2 * x - 1
So, does anyone know how I can get the above line equation instead of the result point? Thanks,
The following should solve both equations with y on the left-hand-side:
y1 = solve(eqn1,y)
y2 = solve(eqn2,y)
Result:
y1 =
- 3*x - 1
y2 =
x/3 - 1/3
As an aside, it would be much faster to solve this system by thinking of it it as a matrix inversion problem Ax=b rather than using MATLAB's symbolic tools:
A = [1 2; -2 1];
b = [-1; 0];
x = A\b
Result:
x =
-0.2000
-0.4000
I have a 2 column vector with different lengths y1 and y2. I want to mixed them together in one matrix.
This matrix should have y1 and y2 as a rows, but they have different lengths. Is it possible to add 0 to shorter vector that it has the same length as longer one?
Many ways to do this, here is one:
>> y1 = [1;2;3;4;5];
>> y2 = [7;8;9];
>> z = zeros(2, max(length(y1), length(y2)));
>> z(1, 1:length(y1)) = y1;
>> z(2, 1:length(y2)) = y2
z =
1 2 3 4 5
7 8 9 0 0
vec2mat does that easily:
vec2mat([y1; y2], max(numel(y1), numel(y2)))
I am trying to plot this system:
x1 - x2 + 3x3 = 8
2x1 - x2 + 4x3 = 11
- x1 + 2x2 -4x3 = -11
I tried with ezsurf and meshgrid, but I wasn't able to do it.
clc
clear all
close all
A = [1 -1 3; 2 -1 4; -1 2 -4];
B = [8 11 -11]';
C = [A B];
R = rref(C);
% R =
% 1 0 0 1
% 0 1 0 -1
% 0 0 1 2
D = R(:,4); % salvo la 4 colonna che contiene le soluzioni
disp('Le soluzioni del sistema proposto sono:');
disp(D);
figure(1);
hold on
grid on
syms x y z
eq = x + y + 3*z - 8;
Z = solve(eq,z)
ezsurf('8/3 - y/3 - x/3');
scatter3(D(1),D(2),D(3));
How can I plot this system of equations?
Maybe I'm missing something, but you have 3 unknown x1, x2 and x3 for 3 equations, therefore there is a unique solution (provided the determinant of the matrix is not zero):
>> A = [1 -1 3; 2 -1 4; -1 2 -4];
>> B = [8 11 -11]';
>> x = A\B
x =
1
-1
2
So there is nothing to plot other than a single point?