% 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')
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
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;
bvector = [one
two three];
This completes our MATLAB segment of the course
HW: Build a UI
for the Bezier Curve Generator