Clear Filters
Clear Filters

How can I solve A*X + X*B = C equation for X? where A, B&C are 2*2 matrixes.

3 views (last 30 days)
A = [1 1;2 3] B= [5 6;7 8] C = [11 12;13 14] Syms X; Eqn = A*X + X*B == C; solve(Eqn,X); % I'm facing difficulties to define matrix X as syms

Accepted Answer

Steven Lord
Steven Lord on 1 Jun 2017
If you're using release R2014a or later, use the sylvester function in MATLAB.
If you're using an older release but have access to Control System Toolbox, use the lyap function.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 1 Jun 2017
>> A = [1 1;2 3];
B= [5 6;7 8];
C = [11 12;13 14];
X = reshape(sym('x',[4,1],'real'),2,[]);
v = A*X + X*B - C;
v = v(:);
f = matlabFunction(v,'Vars',{X});
x = fsolve(f,[1;1;1;1])
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
x =
2.1809
-0.1489
-0.2766
1.4043
>> X = reshape(x,2,[]);
>> v = A*X + X*B - C
v =
1.0e-14 *
-0.3553 -0.3553
-0.1776 -0.5329
>>
  5 Comments
Andrei Bobrov
Andrei Bobrov on 1 Jun 2017
Edited: Andrei Bobrov on 1 Jun 2017
Same
>> A = [1 1;2 3] ;
B= [5 6;7 8] ;
C = [11 12;13 14] ;
syms x1 y1 x2 y2
X = [x1 y1 ; x2 y2] ;
eqn = A*X+X*B == C ;
D = solve(eqn);
X = reshape(sym('x',[4,1],'real'),2,[]);
v = A*X + X*B - C;
v = v(:);
f = matlabFunction(v,'Vars',{X});
x = fsolve(f,[1;1;1;1]);
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
>> structfun(@double,D) - x
ans =
1.0e-14 *
0.6217
-0.4469
-0.4219
0.3775
>>

Sign in to comment.


Karan Gill
Karan Gill on 5 Jun 2017
You can solve this with the symbolic toolbox if you declare "X" as a matrix:
>> X = sym('x%d',[2 2])
X =
[ x11, x12]
[ x21, x22]
Then solve as usual:
>> A = [1 1;2 3];
B= [5 6;7 8];
C = [11 12;13 14];
>> eqn = A*X + X*B == C
eqn =
[ 6*x11 + 7*x12 + x21 == 11, 6*x11 + 9*x12 + x22 == 12]
[ 2*x11 + 8*x21 + 7*x22 == 13, 2*x12 + 6*x21 + 11*x22 == 14]
>> sol = solve(eqn,X)
sol =
struct with fields:
x11: [1×1 sym]
x21: [1×1 sym]
x12: [1×1 sym]
x22: [1×1 sym]
>> solX = [sol.x11 sol.x12; sol.x21 sol.x22]
solX =
[ 205/94, -13/47]
[ -7/47, 66/47]
>> vpa(solX)
ans =
[ 2.1808510638297872340425531914894, -0.27659574468085106382978723404255]
[ -0.1489361702127659574468085106383, 1.4042553191489361702127659574468]

Products

Community Treasure Hunt

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

Start Hunting!