-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicEqSolver.m
54 lines (37 loc) · 1004 Bytes
/
DynamicEqSolver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function [SS, xx] = DynamicEqSolver(Eq, q, Dq, ParamList, ParamVal, tspan, InitCnd)
% Author: Mansour Torabi
% Email: [email protected]
%% [1.1]: Convert Eq To State-Space Form:
N = length(Eq);
DDq = sym(zeros(1, N));
for ii = 1:N
DDq(ii) = sym(['DD', char(q(ii))]);
end
%
% AA * X = BB;
AA = jacobian(Eq, DDq);
BB = -simplify(Eq - AA*DDq.');
DDQQ = sym(zeros(N, 1));
DET_AA = det(AA);
for ii = 1:N
AAn = AA;
AAn(:,ii) = BB;
DDQQ(ii) = simplify(det(AAn) / DET_AA);
end
%% [1.2]: State Space formation - Final Step
SS = sym(zeros(N, 1));
for ii = 1:N
SS (ii) = Dq(ii);
SS (ii + N) = DDQQ(ii);
end
%% [1.3]: Change variables from q to x
Q = [q, Dq];
X = sym('x_',[1 2*N]);
SS = subs(SS, Q, X);
%% [2.1] Solving ODEs
syms t
% Preparation of SS Eq for ODE Solver: Creating Anonymous Fcn
SS_0 = subs(SS, ParamList,ParamVal);
SS_ode0 = matlabFunction(SS_0,'vars',{X, t});
SS_ode = @(t, x)SS_ode0(x(1:2*N)',t);
[ts, xx] = ode45(SS_ode, tspan, InitCnd);