//
// pendulum_demo.sce
//
// Scilab demonstration script that uses the vector field
// defined in pendulum.sci
//
// This file was generated by the program VFGEN, version: 2.5.0-dev
// Generated on 14-May-2014 at 21:04
//

// Load the vector field definition and the jacobian.
exec('pendulum.sci');

Pi = %pi;

// Create data for an x_mdialog.
tstr = 'Enter initial conditions, parameters, stop time, and number of samples:';
field_names = ['theta';'v';'g';'b';'L';'m';'Stop Time';'Num Samples'];
default_field_values = ['-0.01+Pi';'0.0';'9.81';'0.0';'1.0';'1.0';'10.0';'201'];
t0 = 0.0;
field_values = x_mdialog(tstr,field_names, default_field_values);
while (field_values ~= [])
    // Pull the data from the x_mdialog values. 
    real_values = evstr(field_values);
    x0 = real_values(1:2);
    params = real_values(3:6);
    tfinal = real_values(7);
    nsamples = real_values(8);
    tsamples = linspace(t0,tfinal,nsamples);

    // Call the ODE solver.
    sol = ode(x0,t0,tsamples,list(pendulum_vf,params),list(pendulum_jac,params));

    // Plot the solution.
    n = size(sol,2);
    clf;
    subplot(2,1,1);
    plot(tsamples(1:n),sol(1,:));
    ax = gca();
    ax.x_label.text = 't';
    ax.y_label.text = 'theta';
    subplot(2,1,2);
    plot(tsamples(1:n),sol(2,:));
    ax = gca();
    ax.x_label.text = 't';
    ax.y_label.text = 'v';
    // Get another set of data from the user.
    field_values = x_mdialog(tstr,field_names, field_values);
end;
