Solving linear system of equations

3 views (last 30 days)
Luis
Luis on 15 Aug 2025
Edited: Torsten on 16 Aug 2025
I want to solve a system of three equations. I'm using this code to calculate the eigenvectors and eigenvalues, then write the equations for S1, S2 and S3:
%Parâmetros
x=0;
del=0;
al1=0.707;
oc=0.3;
Gamma1=1;
Gamma3=1;
g1=1/100;
%Matriz a ser diagonalizada
sig23e = [-(1i*(del+x)+Gamma1+g1) 1i*al1*oc 0 ];
sig13e = [1i*al1*oc -(1i*x+g1/2) -1i*oc ];
sig14e = [0 -1i*oc 1i*(del-x)-Gamma1/2-g1/2];
A = [sig23e; sig13e; sig14e];
[V,D] = eig(A);
%Solução final
syms A1 A2 A3 sig13
S1 = V(:,1)*exp(D(1,1))
S2 = V(:,2)*exp(D(2,2))
S3 = V(:,3)*exp(D(3,3))
S = [S1,S2,S3];
eqn1 = S(1,:) == 0;
eqn2 = S(2,:) == 0;
eqn3 = S(3,:) == sig13;
sol = solve([eqn1,eqn2,eqn3],[A1,A2,A3])
The problem arises when calculating S1, S2, and S3, if I let them in the format above, the output are something like this:
S1 =
0.3700 - 0.0000i
-0.0000 - 0.0677i
0.0436 + 0.0000i
Which is fine. But when I do the following change to the last part to add the variables A1, A2 and A3:
syms A1 A2 A3 sig13
S1 = A1*V(:,1)*exp(D(1,1))
S2 = A2*V(:,2)*exp(D(2,2))
S3 = A3*V(:,3)*exp(D(3,3))
S = [S1,S2,S3];
eqn1 = S(1,:) == 0;
eqn2 = S(2,:) == 0;
eqn3 = S(3,:) == sig13;
sol = solve([eqn1,eqn2,eqn3],[A1,A2,A3])
The output becomes horrid and the solution using "sol.A1" gives me "Empty sym: 0-by-1"
  2 Comments
Torsten
Torsten on 15 Aug 2025
Edited: Torsten on 15 Aug 2025
Maybe you could describe in your own words what you try to do. It's hard to deduce it from your code.
Do you try to solve some system of ordinary differential equation using matrix exponentials ?
Luis
Luis on 15 Aug 2025
Yeah, sure. I'm trying to solve this problem x' = A*x. Where x is a column vector of the variables, x' is the derivative of x respect to time and A is the coefficient matrix. The general solution for this problem is x = exp(λ*t). Therefore, the solution is now found by finding the eigenvector and eigenvalues of A. I do this on this part of the code:
[V,D] = eig(A);
Now, the general form of the solution is given by x = A1*S1 + A2*S2 + A3*S3, where the S's are the eigenvector times the exponential of the associated eigenvalue and A's are constants to be determined.
Therefore, the last part:
syms A1 A2 A3 sig13
S1 = A1*V(:,1)*exp(D(1,1))
S2 = A2*V(:,2)*exp(D(2,2))
S3 = A3*V(:,3)*exp(D(3,3))
S = [S1,S2,S3];
eqn1 = S(1,:) == 0;
eqn2 = S(2,:) == 0;
eqn3 = S(3,:) == sig13;
sol = solve([eqn1,eqn2,eqn3],[A1,A2,A3])
Was supposed to give the values of A1, A2 and A3 from eqn1, eqn2 and eqn3.

Sign in to comment.

Accepted Answer

Torsten
Torsten on 15 Aug 2025
Edited: Torsten on 16 Aug 2025
I think you mean
%Parâmetros
x=0;
del=0;
al1=0.707;
oc=0.3;
Gamma1=1;
Gamma3=1;
g1=1/100;
%Matriz a ser diagonalizada
sig23e = [-(1i*(del+x)+Gamma1+g1) 1i*al1*oc 0 ];
sig13e = [1i*al1*oc -(1i*x+g1/2) -1i*oc ];
sig14e = [0 -1i*oc 1i*(del-x)-Gamma1/2-g1/2];
A = [sig23e; sig13e; sig14e];
[V,D] = eig(A);
syms t A1 A2 A3 sig13
% General solution of ODE system
S = V*[A1*exp(D(1,1)*t);A2*exp(D(2,2)*t);A3*exp(D(3,3)*t)];
% Determine A1, A2 and A3 such that x1(0) = 0, x2(0) = 0 and x3(0) = sig13
S0 = subs(S,t,0);
sol_coeff = solve(S0==[0;0;sig13],[A1 A2 A3]);
% Substitute the thus obtained values for A1, A2 and A3 in the general
% solution of the ODE system
sol = subs(S,[A1 A2 A3],[sol_coeff.A1,sol_coeff.A2,sol_coeff.A3]);
sol_to_plot = subs(sol(3),sig13,1);
figure(1)
hold on
fplot(real(sol_to_plot),[0 10])
fplot(imag(sol_to_plot),[0 10])
hold off
grid on
If you are allowed to use "dsolve" for the solution of x'=A*x, you could set
syms x1(t) x2(t) x3(t) sig13
x = [x1(t);x2(t);x3(t)];
eqns = diff(x,t) == A*x;
conds = [x1(0)==0,x2(0)==0,x3(0)==sig13];
sol = dsolve(eqns,conds,'MaxDegree',3);
sol_to_plot = subs(sol.x3,sig13,1);
figure(2)
hold on
fplot(real(sol_to_plot),[0 10])
fplot(imag(sol_to_plot),[0 10])
hold off
grid on
  1 Comment
Luis
Luis on 16 Aug 2025
I see, there was also a miss on the code I was writing.
This was truly helpful. Thank you very much.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 15 Aug 2025
Moved: Walter Roberson on 15 Aug 2025
format long g
%Parâmetros
x=0;
del=0;
al1=0.707;
oc=0.3;
Gamma1=1;
Gamma3=1;
g1=1/100;
%Matriz a ser diagonalizada
sig23e = [-(1i*(del+x)+Gamma1+g1) 1i*al1*oc 0 ];
sig13e = [1i*al1*oc -(1i*x+g1/2) -1i*oc ];
sig14e = [0 -1i*oc 1i*(del-x)-Gamma1/2-g1/2];
A = [sig23e; sig13e; sig14e];
[V,D] = eig(A);
%Solução final
syms A1 A2 A3 sig13
S1 = V(:,1)*exp(D(1,1))
S1 =
0.369975188379713 - 8.21509945358374e-17i -1.29955495866915e-17 - 0.0676920303661541i 0.0435604819119288 + 2.39741692879728e-17i
S2 = V(:,2)*exp(D(2,2))
S2 =
0.0101550277323791 + 0.148630850284153i 0.525554721308386 + 0.112301429960421i -0.260764335167304 - 0.445675984904963i
S3 = V(:,3)*exp(D(3,3))
S3 =
-0.0101550277323791 + 0.148630850284154i 0.525554721308386 - 0.112301429960421i 0.260764335167305 - 0.445675984904962i
S1, S2, and S3 are each 3 x 1 vectors.
S = [S1,S2,S3];
So S is a 3 x 3 vector
eqn1 = S(1,:) == 0;
eqn2 = S(2,:) == 0;
eqn3 = S(3,:) == sig13;
eqn1, eqn2, eqn3 are each 1 x 3 equations.
eqn1 and eq2 consists of comparing a set of 3 constants to 0. Unless the constants happen to be exactly 0, solve() involving eqn1 or eqn2 will not have a solution.
sol = solve([eqn1,eqn2,eqn3],[A1,A2,A3])
sol = struct with fields:
A1: [0×1 sym] A2: [0×1 sym] A3: [0×1 sym]
[eqn1, eqn2, eqn3] is a 3 x 3 set of equations.
You are trying to solve() a 3 x 3 set of equations with respect to exactly 3 variables. The system is overdetermined and does not happen to have a solution.
symvar([eqn1, eqn2, eqn3])
ans = 
The equations do not have any A1, A2, or A3 at all.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!