% Practice exam

% Problem 1
syms y(t) Dy
Dy=diff(y)
dsolve(diff(Dy)+4*Dy+4*y==sin(2*t),y(0)==0, Dy(0)==0)
simplify(ans)
%%
% Problem 2
syms y(t) 
dsolve((t*diff(y)-y)*cos(y/t)==t)
%%
% Problem 4
syms y(x) w v a k
dsolve(x*diff(y)-y==(w/v)*sqrt(1+(y/x)^2), y(a)==0)
solve(y/x+sqrt(1+(y/x)^2)==k*exp(w/(v*x)),y)

%%
% Problem 6
A=[2  -1  3;
   1  -3 -1;
   0  5   5];
null(A)
A=[2  -1  3;
   1  -3 -1];
b=[2 -4]';
A\b

%%
% Problem 8
k1=20; k2=30; M1=10; M2=7;

A=[0         1    0   0;
  (-k1-k2)/M1 0 k2/M1 0;
    0        0    0   1;
  k2/M2     0 -k2/M2  0];
[V D]=eig(A)

% Part d -- Initial values
C=V\[V(1,2)/V(3,2); 0; 1; 0];
State=C(1)*V(:,1)*exp(D(1,1)*t)+C(2)*V(:,2)*exp(D(2,2)*t)...
     +C(3)*V(:,3)*exp(D(3,3)*t)+C(4)*V(:,4)*exp(D(4,4)*t);
figure(2)
ezplot(State(1),[0 10])
hold on
ezplot(State(2),[0 10])

%%

%Problem 9
syms y(t) Dy
Dy=diff(y)
% impulse response
dsolve(2*diff(Dy)+8*Dy+6*y==0, y(0)==0, Dy(0)==1/2)
% the response for 0<t<7
dsolve(2*diff(Dy)+8*Dy+6*y==heaviside(t), y(0)==0, Dy(0)==0)
% Which is exp(-3*t)/12 - exp(-t)/4 + 1/6
% the response for 0<t<9
dsolve(2*diff(Dy)+8*Dy+6*y==heaviside(t)-2*heaviside(t-7), y(0)==0, Dy(0)==0)
% Which from 7<t<9 is 
% 
%            / exp(3 t)                            \ 
%  exp(-3 t) | -------- - 1843838036102911/8388608 | - 
%            \    12                               / 
%   
%             / exp(t)                                  \ 
%     exp(-t) | ------ - 2410422306565845/4398046511104 | 
%             \   4                                     /

% the response for all t
dsolve(2*diff(Dy)+8*Dy+6*y==heaviside(t)-2*heaviside(t-7)+heaviside(t-9), y(0)==0, Dy(0)==0)
figure(1)
ezplot(ans,[9 13])
hold on
ezplot((1110254517766234499*exp(-3*t)/25165824)-6499012692648109*exp(-t)/4398046511104,[9 13])
% which after 9 sec is
% 1110254517766234499 exp(-3 t)   6499012692648109 exp(-t) 
%  ----------------------------- - ------------------------ 
%            25165824                   4398046511104
% 
% or expressed another way
% (-1/12)*exp(-3*(t-7))+(1/4)*exp(-(t-7))+(1/12)*exp(-3*t)-(1/4)*exp(-t)+(1/12)*exp(-3*(t-9))-(1/4)*exp(-(t-9))-(1/12)*exp(-3*(t-7))+(1/4)*exp(-(t-7))
% or expressed another way
% exp(-3*t)/12 - exp(-t)/4 + exp(7 - t)/2 - exp(9 - t)/4 - exp(21 - 3*t)/6 + exp(27 - 3*t)/12
