solving a system of ODEs in 3x3 matrix form

3 views (last 30 days)
The following allowed me to solve wit 2x2 matrix , how about 3x3 matrix ? what the code. thanks

Answers (1)

Star Strider
Star Strider on 17 Sep 2021
The easiest way would be to use the expm function.
The expression changes slightly:
A = rand(3);
b = rand(3,1);
t = 0:0.1:10;
for k = 1:numel(t)
y(:,k) = expm(A*t(k))*b;
end
figure
plot(t, y)
grid
Experiment with your own ‘A’ and ‘b’ arrays.
.
  3 Comments
chen tianle
chen tianle on 17 Sep 2021
example what your code ?
if my A is [ 3 1 1 ; 0 3 -1 ; 0 - 1 3]
and my B= 0 ?
what the Sx, Sy, Sz , value ?
Star Strider
Star Strider on 17 Sep 2021
The code works. The problem is that ‘b’ is 0 so the output is 0, and there was an error in ‘A’ (spaces are delimiters, so the dimensions did not originally match) —
A = [ 3 1 1 ; 0 3 -1 ; 0 -1 3] ;
b = ones(3,1)*eps; % 'b': Small, Non-Zero Value
t = 0:0.1:1;
for k = 1:numel(t)
y(:,k) = expm(A*t(k))*b;
end
figure
plot(t, y)
grid
This would also be the situation doing symbolic integration:
syms t x1(t) x2(t) x3(t) x10 x20 x30
x = [x1(t); x2(t); x3(t)]
x = 
eqn = diff(x) == A*x + b
eqn = 
Y = dsolve(eqn, x1(0)==x10, x2(0)==x20, x3(0)==x30)
Y = struct with fields:
x2: [1×1 sym] x1: [1×1 sym] x3: [1×1 sym]
Y.x1
ans = 
Y.x2
ans = 
Y.x3
ans = 
sympref('AbbreviateOutput',false);
Y = vpa(simplify([Y.x1; Y.x2; Y.x3], 500), 3)
Y = 
Setting the intital conditions to 0:
Y = dsolve(eqn, x1(0)==0, x2(0)==0, x3(0)==0)
Y = struct with fields:
x2: [1×1 sym] x1: [1×1 sym] x3: [1×1 sym]
% sympref('AbbreviateOutput',false);
Y = vpa(simplify([Y.x1; Y.x2; Y.x3], 500), 3)
Y = 
.

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!