Matlab GUI - TPBVP

There will be  files - A script File Tpbvp_3.m for the code.  The MinFun.m , state.m and BC.m file from before and another script file TP_Control.m  which will be used to create the GUI elements.
 

The User Interface

The User Interface should appear as shown below

Tpbvp_3.m

% Solution to TBBVP ( Two Point Boundary value Problem) for ODE
%
% Dr. P.Venkataraman, RIT Mechanical Engineering
%  Jun 1998
%
% Using ODE45 and FMINS (non-stiff differential equations)
% Problem must be described in state space as a
% System of first order equations in a function M-file - state.m
%-----------
%  STATE.m  |
%-----------
% [ STATE.m is similar to Function F(T,Y) described in ODE45]
% except the differential system must be normalized with respect
% to final time - i.e. the final time is one (1)
% function sysstate = state(x, y, flag, tf)
% where x: independent variable
%   y: dependent vector - state derivatives
%   flag: ignore
%   tf:  final time
% ----   all derivatives must be multipled by tf ---
% This is useful for unknown final time problems
%
% Boundary Conditions are described in a function M-file  - BC.m
%--------
% BC.m  |
%-------
%  function BC(T0,Tf, Y(T0), Y(Tf))
%      Y(T0) = Vector of initial conditions of state
%       Y(Tf) = Vector of Final conditions of state after call to ODE45
% The function should return returns an vector of
%  Boundary Conditions set up with the
%  Right Hand Side to be 0 (ZERO) when satisfied
%  This is important in trajctory problems where
%  BC's may not be explicit - or they could be
%   a function of intial and final values
%
% FMINS is used with Initial conditions Y(T0) (and final time )
% As Design Variables
% The Objectve function is the LSE of the Boundary Conditions returned
% from Boundry condition M-file
%
 

% input the starting design vector - intial conditions fo rthe problem
clc  % clear the work space and return prompt to the top
global NSYS;  % variable to be available to defferent functions
global x y

Prompt ={'Enter the order of the Differential System'};
% the above is required to be an array (1 X 1) in this case

Title='Number of State Variables';

Lineno = 1;
ok = 1;
while (ok)  % this is a loop to ensure you do not enter non 0 number
   answer = inputdlg(Prompt,Title,Lineno);% input dialog box
 NSYS = answer{1,1};   % a string is returned
 nsys = str2num(NSYS);  % converted to number
 if (NSYS > 0)
      break;
   end
end

TPBVP_Control  % calls the GUI Script
 

ftm = FINAL;
if (FTIME == 1) % final time is free
   y0 = [INITVEC FINAL];
else
   y0 = INITVEC;
end
%nsys = str2num(NSYS);
OPT(14) = NMAX;  % number of iterations
 

hw = waitbar(0,'Computing...');% puts up a waitbar window

% call the fmins function with additional parameters
[YD, OPT] = fmins('MinFun',y0,[0 , 1.0e-08],[], nsys,FINAL);

close(hw)  % close waitbar

%fprintf('\nStrike any key to continue \n')
%pause

z = [ x y];
disp (z);
fprintf('\nNumber of iterations : %i5 ',OPT(10));
fprintf('\n\n')

fprintf('The final values for the design vector: \n')
disp(YD)
fprintf('The last element above is the final time \n')
 

TP_Control.m

%TPBVP_Control
% sets parametrs for Program Control
% default values are shown at startup

global NSYS  % NSYS is transferred from the main SCRIPT

% The following are used for default values
NMAX = 10000;  START = 0.0;  FINAL = 1.0;
FTIME = 1;
 
 

% create a default vector of zeros
%for the initial conditions
NumEqn = NSYS;
Num = str2num(NSYS);
DumArray = zeros(Num);
DumVec = DumArray(1,:);
clear DumArray;
clear Num;
 

Hfig_1 = figure;  % handle to figure
set(Hfig_1,'Color', [0.3, 0.6, 0.5], ...  % RGB spec for color
      'NumberTitle','Off', ...
            'Name','TPBVP Default/Parameter Selection', ...
            'Units','inches', ...  % units for the figure/controls
            'Menubar','none', ...  % removing standard menubar
            'Position',[4,2,5,5]);
 
 % remember - position is [left, bottom,width,height]
 Hc1_txt1 = uicontrol(Hfig_1, ...
        'Style','text', ...  % this is like a label in VB
               'Units','inches', ...
              'Position',[0.5,4.7,4.0,0.2], ... % relative to figure window
               'String','Type in new values to change default'); % label string

 
% installing the custom menubar with three items

Hm_fi = uimenu(gcf,'Label','File','ForeGroundColor','b');
Hm_ex = uimenu(gcf,'Label','Example','ForeGroundColor','b');
Hm_figs = uimenu(gcf,'Label','Figure','ForeGroundColor','b');

% apparantly foreground/background color doesn't work
% installing sub-menu items
Hm_fiCl = uimenu(Hm_fi,'Label','Close Figure','CallBack','Close', ...
     'BackgroundColor','b','ForeGroundColor','y');
   % note that the submenu uses the handle for the parent
   Hm_exlam = uimenu(Hm_ex,'Label','Laminar_flow','CallBack', ...
      'NumEqn = 3;');
   Hm_exlswl = uimenu(Hm_ex,'Label','Swirl_flow','CallBack', ...
      'NumEqn = 8;');
 Hm_exfigv = uimenu(Hm_figs,'Label','Velocity','CallBack', ...
      'PTyp = 1;');
 Hm_exfigp = uimenu(Hm_figs,'Label','Pressure','CallBack', ...
      'PTyp = 2;');
   Hm_exfigg = uimenu(Hm_figs,'Label','Grid','CallBack', ...
      'PTyp = 3;');
 
%-----------------------------------------------------------------------------
% the maximum number of iterations
Hc_nmax = uicontrol(Hfig_1, ...
        'Style','text', ...
               'Units','inches', ...
              'Position',[0.5,4.2,1.0,0.2], ...
                'String','NMAX');
 
Hc_tnmax = uicontrol(Hfig_1, ...
        'Style','edit', ...  % the textbox in VB - user inputs string
               'Units','inches', ...
               'Position',[1.6,4.2,1.0,0.2], ...
               'BackgroundColor','white', ...  % white background
               'String',num2str(NMAX), ... % what it displays
              'Callback','NMAX  = str2num(get(Hc_tnmax,''String'')) ;');
             % the callback is the most important featute of UI
             % The callback string is passed to Matlab eval function
Hc_nmaxdes = uicontrol(Hfig_1, ...
    'Style','text', ...
    'Units', 'inches', 'Position',[2.8,4.2,2.0,0.4], ...
    'String', 'NMAX is the Maximum number of iterations for FMINS');
 
 
 %-----------------------------------------------------------------------------------
 %---------------------------------------------------------------------------------
Hc_neql = uicontrol(Hfig_1, ...
        'Style','text','Units','inches', ...
              'Position',[0.5,3.5,1.5,0.2], ...
               'String','No. of Equations');

Hc_neqv = uicontrol(Hfig_1, ...
        'Style','edit','Units','inches', ...
              'Position',[2.1,3.5,0.5,0.2], ...
                'String',num2str(NumEqn), ...
                'BackgroundColor','white', ...
                'Callback',['NumEqn  = str2num(get(Hc_neqv,''String'')); ']);
 
 

%----------------------------------------------------------------------------------
% get the start time/end time/ and final time free/fixed
Hc_ts = uicontrol(Hfig_1,'Style','text', ...
               'Units','inches', 'Position',[0.5,3.0,1.5,0.2], ...
                'String','Start Time');

Hc_tsv = uicontrol(Hfig_1,'Style','edit', ...
               'Units','inches','Position',[2.1,3.0,0.5,0.2], ...
               'String',num2str(START), ...
               'BackgroundColor','white', ...
               'Callback','START  = str2num(get(Hc_tsv,''String'')); ');
 
Hc_tf = uicontrol(Hfig_1,'Style','text', ...
               'Units','inches', 'Position',[2.7,3.0,1.5,0.2], ...
                'String','Final Time');

Hc_tsf = uicontrol(Hfig_1,'Style','edit', ...
               'Units','inches','Position',[4.3,3.0,0.5,0.2], ...
               'String',num2str(FINAL), ...
               'BackgroundColor','white', ...
               'Callback','FINAL  = str2num(get(Hc_tsf,''String'')); ');
 
%----------------------------------------------  

Hc_ftn = uicontrol(Hfig_1,'Style','radio', ...
               'Units','inches','Position',[0.5,2.5,1.0,0.2], ...
               'String','Fixed final time', ...
               'BackgroundColor','white', ...
               'Value',0, ...
               'Callback', [ ...
                  'FTIME = 0,', ...
                  'set(Hc_ftn,''Value'',1),' ...
                  'set(Hc_fty,''Value'',0)' ]);
Hc_fty = uicontrol(Hfig_1,'Style','radio', ...
               'Units','inches','Position',[1.6,2.5,1.0,0.2], ...
               'String','Free final time', ...
               'BackgroundColor','white', ...
               'Value',1, ...
               'Callback', [ ...
                  'FTIME = 1,', ...
                  'set(Hc_ftn,''Value'',0),' ...
                  'set(Hc_fty,''Value'',1)' ]);
 
 

%-------------------------------------------------------------------------------
% pick up the initial state variables
%--------------------------------------------------------------------------------------------

Hc_stl = uicontrol(Hfig_1, ...
        'Style','text', ...
               'Units','inches', ...
               'Position',[0.5,2.0,2.0,0.2], ...
               'BackgroundColor','y', ...
               'String','Set up the initial state vector');
 
Hc_st2 = uicontrol(Hfig_1, ...
        'Style','edit', ...
               'Units','inches', ...
              'Position',[0.5,1.8,1.5,0.2], ...
               'String',num2str(DumVec), ...
               'BackgroundColor','white', ...
               'Callback','INITVEC  = str2num(get(Hc_st2,''String'')); ');
 

Hc_labt = uicontrol(Hfig_1, ...
        'Style','text', ...
               'Units','inches', ...
               'Position',[0.5,0.5,2.0,0.75], ...
               'BackgroundColor','g', ...
               'ForegroundColor','k', ...
               'String','A program for Solving Two-Point Boundary Value Problem.  It can handle mixed BC at both points.  It uses MATLAB''s ode45 and fmins');

 
% push button
 Hc_push1 = uicontrol(Hfig_1,'Style','push', ...
      'Units', 'inches','Position',[3.0,0.5,1.0,0.3], ...
            'String', 'update/exit', ...
            'Callback', 'close');
 
 
%---------------------------------------------------------------------------
uiwait  % the UI figure will be open until you close it
 

MinFun.m  (same as before)

function ReturnVal = MinFun(yd, NS, ftime)
% The function which will organize the optimal adjustment
%  of the boundary conditions and final time
global x y
n= length(yd);

if n > NS
 for ivar = 1:1:n-1
    ys(ivar) = yd(ivar);
   end
else
   for ivar = 1:1:n;
      ys(ivar) = yd(ivar);
   end
end

if n > NS
   tf = yd(n);
else
   tf = ftime;
end

tspan = [0, 1];
 

[x y] = ode45('state', tspan,ys,[],tf);

nd = length(x);

for ivar = 1:1:n-1
   yf(ivar) = y(nd,ivar);
end

bvec = feval('Bc',0,tf,ys,yf);

NBC = length(bvec);
sum = 0.0;
for ivar = 1:NBC
   sum = sum + bvec(ivar)*bvec(ivar);
end

ReturnVal = sum;
 
 

state.m (same as before)

function stst = state(t,y)
% set up the state equations as a col vector
% the previous state is known in y
% the first order derivatives is returned
% in the variable name used to the left
% of the equal sign
stst = [ y(2) , y(3) , -0.5*y(1)*y(3)]';

BC.m  (same as before)

function bvector = BC(t0, tf, yi, yf)
%
% This is where the boundary conditions are specified
% The conditions are specified such that the right hand sides are zero
%
one = yi(1) ;
two = yi(2) ;
three = yf(2) -1.0;

bvector = [one two three];
 

This completes our MATLAB segment of the course

HW: Build a UI for the Bezier Curve Generator