%
% pendulum_demo.m
%
% MATLAB demo function for the vector field: pendulum
%
% This file was generated by the program VFGEN (Version:2.1.0)
% Generated on  3-Jun-2007 at 12:24
%
%

function pendulum_demo
    figure;
    clf
    set(gcf,'Position',[0 0 250 400]);
    v = [];
    uicontrol('Style','text','Position',[10 375 100 20],'String','theta(0)');
    ui = uicontrol('Style','edit','Position',[115 375 100 20],'String','-0.01+pi');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 350 100 20],'String','v(0)');
    ui = uicontrol('Style','edit','Position',[115 350 100 20],'String','0.0');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 325 100 20],'String','g');
    ui = uicontrol('Style','edit','Position',[115 325 100 20],'String','9.81');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 300 100 20],'String','b');
    ui = uicontrol('Style','edit','Position',[115 300 100 20],'String','0.0');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 275 100 20],'String','L');
    ui = uicontrol('Style','edit','Position',[115 275 100 20],'String','1.0');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 250 100 20],'String','m');
    ui = uicontrol('Style','edit','Position',[115 250 100 20],'String','1.0');
    v = [v; ui];
    uicontrol('Style','text','Position',[10 225 100 20],'String','Stop Time');
    ui = uicontrol('Style','edit','Position',[115 225 100 20],'String','10');
    v = [v; ui];
    ui = uicontrol('Style','checkbox','Position',[10 200 200 20],'String','Separate Axes');
    v = [v; ui];
    uicontrol('Style','pushbutton','Position',[10 175 40 20],'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);
    [t,z_] = ode15s(@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
