%
% pendulum_demo.m
%
% MATLAB demo function for the vector field: pendulum
%
% This file was generated by the program VFGEN, version: 2.5.0-dev
% Generated on 14-May-2014 at 21:03
%
%

function pendulum_demo
    figure;
    clf
    set(gcf,'Position',[0 0 250 200]);
    v = [];
    uicontrol('Style','text','Position',[10 180 100 18],'String','theta(0)');
    ui = uicontrol('Style','edit','Position',[115 180 100 18],'String','-0.01+pi');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 160 100 18],'String','v(0)');
    ui = uicontrol('Style','edit','Position',[115 160 100 18],'String','0.0');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 140 100 18],'String','g');
    ui = uicontrol('Style','edit','Position',[115 140 100 18],'String','9.81');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 120 100 18],'String','b');
    ui = uicontrol('Style','edit','Position',[115 120 100 18],'String','0.0');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 100 100 18],'String','L');
    ui = uicontrol('Style','edit','Position',[115 100 100 18],'String','1.0');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 80 100 18],'String','m');
    ui = uicontrol('Style','edit','Position',[115 80 100 18],'String','1.0');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 60 100 18],'String','Stop Time');
    ui = uicontrol('Style','edit','Position',[115 60 100 18],'String','10');
    v = [v; ui];
    ui = uicontrol('Style','checkbox','Position',[10 40 200 18],'String','Separate Axes');
    v = [v; ui];
    uicontrol('Style','pushbutton','Position',[10 20 40 18],'String','Go','Callback',@go_cb,'UserData',v);
end

function go_cb(arg1,arg2)
    v = get(arg1,'UserData');
    ic = zeros(size(v,1)-2,1);
    for k = 1:size(v,1)-2,
        ic(k) = eval(get(v(k),'String'));
        if (isnan(ic(k)))
            ic(k) = 0.0;
        end;
    end;
    stoptime = str2double(get(v(end-1),'String'));
    if (isnan(stoptime))
        stoptime = 0.0;
    end;
    abstol = 1e-9;
    reltol = 1e-7;
    opts = odeset('AbsTol',abstol,'RelTol',reltol,'Jacobian',@pendulum_jac);
    % Change ode45 to ode15s for stiff differential equations.
    [t,z_] = ode45(@pendulum_vf,[0 stoptime],ic(1:2),opts,ic(3:end));
    figure;
    clf;
    a = get(v(end),'Value');
    if (a == 0),
        plot(t,z_);
        xlabel('t');
        legend('theta','v');
    grid on
    else
        subplot(2,1,1);
        plot(t,z_(:,1))
        xlabel('t');
        ylabel('theta')
        grid on
        subplot(2,1,2);
        plot(t,z_(:,2))
        xlabel('t');
        ylabel('v')
        grid on
    end
end
