% graph of a solution to a second-order linear constant
% coefficient equation
%
% by Gavin LaRose, 2/1/2013
% 
% (c)2013 Regents of the University of Michigan
% This work is licensed under the
% Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
% To view a copy of this license, visit
% http://creativecommons.org/licenses/by-nc-sa/3.0/
% or send a letter to
% Creative Commons,
% 444 Castro Street, Suite 900,
% Mountain View, CA, 94041, USA.
%
function Soln_Graph()
%-------------------------------------------------------------------------------
% configuration variables

% our equation is 
%    m x'' + c x' + k x = 0,
% with initial conditions
%    x(0) = x0, x'(0) = xp0
    m = 5;
    c = 15;
    k = 50;
    x0 = 0;
    xp0 = 2;

% t values on which to graph the solutions
    t_range = [0 5];

% background color
    background_grey = [ 0.6 0.6 0.6 ];

%-------------------------------------------------------------------------------
% figure out the solution
    t = t_range(1):.01:t_range(2);

    char_roots = roots( [m c k] );
    if char_roots(1) == char_roots(2)
        c1 = x0;
        c2 = xp0 - x0*char_roots(1);
        y = c1*exp(char_roots(1)*t) + c2*t.*exp(char_roots(1)*t);
    elseif imag(char_roots(1)) == 0
        c2 = (xp0 - char_roots(1)*x0)/(char_roots(2) - char_roots(1));
        c1 = (xp0 - char_roots(2)*x0)/(char_roots(1) - char_roots(2));
        y = c1*exp(char_roots(1)*t) + c2*exp(char_roots(2)*t);
    else
        a = real(char_roots(1));
        b = abs(imag(char_roots(1)));
        c1 = x0;
        c2 = (xp0 - a*x0)/b;
        y = c1*exp(a*t).*cos(b*t) + c2*exp(a*t).*sin(b*t);
    end

    pfig = figure();
    pfaxes = axes( 'Parent', pfig, 'Color', background_grey );
    hold( pfaxes );
    plot( pfaxes, t,y, '-b', 'LineWidth', 2 );
    plot( pfaxes, t,0*t, '--k' );
    title( [ 'Solution to ' num2str(m) ' y'''' + ' num2str(c) ' y'' + ' ...
             num2str(k) ' y = 0, y(0) = ' num2str(x0) ', y''(0) = ' ...
             num2str(xp0) ], 'FontSize', 14 );
    xlabel( 't', 'FontSize', 14 );
    ylabel( 'y', 'FontSize', 14 );
end

% all done
%-------------------------------------------------------------------------------
