%% solve_sys.m Solving a System of Equations Symbollically
% BJ Furman
% 07SEP2015
%% System 1 - We do the substitution for t==0
% The system of equations is:
%   Xnat = C1*exp(P1*t) + C2*exp(P2*t)              (1)
%   Xnat_dot = C1*P1*exp(P1*t) + C2*P2*exp(P2*t)    (2)
%
clear all
syms X0 Xdot0 P1 P2 C1 C2   % define the symbolic variables
eqn1 = X0 == C1 + C2        % substitute t==0 into (1)
eqn2 = Xdot0 == C1*P1 + C2*P2 % substitute t==0 into (2)
Sol = solve([eqn1,eqn2],[C1,C2])  % solve eqn1 and eqn2 for C1 and C2
C1 = Sol.C1
C2 = Sol.C2
%% System 2 - Let the symbolic solver do everything
% The system of equations is:
%   Xnat = C1*exp(P1*t) + C2*exp(P2*t)              (1)
%   Xnat_dot = C1*P1*exp(P1*t) + C2*P2*exp(P2*t)    (2)
%
clear all
syms X C1 C2 P1 P2 X0 Xdot0 t   % define the symbolic variables
X(t)=C1*exp(P1*t)+C2*exp(P2*t)  % define the time function Xnat(t)
Xdot(t)=diff(X)                 % find the derivative of Xnat(t)
Sol = solve([X(0)==X0,Xdot(0)==Xdot0],[C1,C2]) % solve for C1 and C2 at t==0
C1 = Sol.C1     % put the expression for C1 into C1
C2 = Sol.C2     % put the expression for C2 into C2
% Assign some numbers to the symbolic variables
X0 = 2;
Xdot0 = 1;
P1 = -1;
P2 = -2;
t = 0:0.1:5;
Xnat = eval(X(t));  % evaluate X(t) over th
plot(t,eval(Xnat))
%% System 3 - Let the symbolic solver do everything with values for P1 and P2
% The system of equations is:
%   Xnat = C1*exp(P1*t) + C2*exp(P2*t)              (1)
%   Xnat_dot = C1*P1*exp(P1*t) + C2*P2*exp(P2*t)    (2)
%   P1 = -z*wn + wn*sqrt(z^2 - 1)                   (3)
%   P2 = -z*wn - wn*sqrt(z^2 - 1)                   (4)
%   
clear all
syms X C1 C2 z wn X0 Xdot0 t   % define the symbolic variables
P1 = -z*wn + wn*sqrt(z^2 - 1)
P2 = -z*wn - wn*sqrt(z^2 - 1)
X(t)=C1*exp(P1*t)+C2*exp(P2*t)  % define the time function Xnat(t)
Xdot(t)=diff(X)                 % find the derivative of Xnat(t)
Sol = solve([X(0)==X0,Xdot(0)==Xdot0],[C1,C2]) % solve for C1 and C2 at t==0
C1 = Sol.C1     % put the expression for C1 into C1
C2 = Sol.C2     % put the expression for C2 into C2
% Assign some numbers to the symbolic variables
X0 = 2;
Xdot0 = 0;
z = 5;
wn = 2;
t = 0:0.1:10;
Xnat = eval(X(t));  % evaluate X(t) over th
plot(t,eval(Xnat)) 
%% System 4 - Critical Damping
% The system of equations is:
%   Xnat = (C1+C2*t)*exp(-wn*t)                    (1)
%   Xnat_dot = [C2 - wn*(C1 + C2*t)]*exp(-t*wn)    (2)
%   P1 = -wn                                       (3)
%   P2 = -wn                                       (4)
%   
clear all
syms X C1 C2 z wn X0 Xdot0 t   % define the symbolic variables
P1 = -wn
P2 = P1
X(t)=(C1+C2*t)*exp(-wn*t)  % define the time function Xnat(t)
Xdot(t)=diff(X)                 % find the derivative of Xnat(t)

Sol = solve([X(0)==X0,Xdot(0)==Xdot0],[C1,C2]) % solve for C1 and C2 at t==0
C1 = Sol.C1     % put the expression for C1 into C1
C2 = Sol.C2     % put the expression for C2 into C2

% Assign some numbers to the symbolic variables
X0 = 2;
Xdot0 = 0;
wn = 2;
t = 0:0.1:10;
Xnat = eval(X(t));  % evaluate X(t) over th
plot(t,eval(Xnat)) 
%% System 5 - No damping (undamped)
% The system of equations is:
%   Xnat = C1*exp(P1*t) + C2*exp(P2*t)              (1)
%   Xnat_dot = C1*P1*exp(P1*t) + C2*P2*exp(P2*t)    (2)
%   P1 = jwn                   (3)
%   P2 = -jwn                   (4)
%   
clear all
syms X C1 C2 z wn X0 Xdot0 t   % define the symbolic variables
P1 = -z*wn + wn*sqrt(z^2 - 1)
P2 = -z*wn - wn*sqrt(z^2 - 1)
X(t)=C1*exp(P1*t)+C2*exp(P2*t)  % define the time function Xnat(t)
Xdot(t)=diff(X)                 % find the derivative of Xnat(t)
Sol = solve([X(0)==X0,Xdot(0)==Xdot0],[C1,C2]) % solve for C1 and C2 at t==0
C1 = Sol.C1     % put the expression for C1 into C1
C2 = Sol.C2     % put the expression for C2 into C2
% Assign some numbers to the symbolic variables
X0 = 2;
Xdot0 = 0;
z = 0;
wn = 2;
t = 0:0.1:10;
Xnat = eval(X(t));  % evaluate X(t) over th
plot(t,eval(Xnat)) 
 