skip to primary navigationskip to content
 

ODE Inerface.m

Objective-C source code icon ODE_Interface.m — Objective-C source code, 39 KB (40700 bytes)

File contents

function varargout = ODE_Interface(varargin)
% ODE_Interface MATLAB code for ODE_Interface.fig
%      ODE_Interface, by itself, creates a new ODE_Interface or raises the existing
%      singleton*.
%
%      H = ODE_Interface returns the handle to a new ODE_Interface or the handle to
%      the existing singleton*.
%
%      ODE_Interface('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ODE_Interface.M with the given input arguments.
%
%      ODE_Interface('Property','Value',...) creates a new ODE_Interface or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ODE_Interface_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ODE_Interface_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ODE_Interface

% Last Modified by GUIDE v2.5 22-Jun-2011 14:39:34

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ODE_Interface_OpeningFcn, ...
                   'gui_OutputFcn',  @ODE_Interface_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ODE_Interface is made visible.
function ODE_Interface_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ODE_Interface (see VARARGIN)

% Choose default command line output for ODE_Interface
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ODE_Interface wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = ODE_Interface_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

function f_x_y_Callback(hObject, eventdata, handles)
% hObject    handle to f_x_y (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of f_x_y as text
%        str2double(get(hObject,'String')) returns contents of f_x_y as a double


% --- Executes during object creation, after setting all properties.
function f_x_y_CreateFcn(hObject, eventdata, handles)
% hObject    handle to f_x_y (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function g_x_y_Callback(hObject, eventdata, handles)
% hObject    handle to g_x_y (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of g_x_y as text
%        str2double(get(hObject,'String')) returns contents of g_x_y as a double

% --- Executes during object creation, after setting all properties.
function g_x_y_CreateFcn(hObject, eventdata, handles)
% hObject    handle to g_x_y (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Xmin_Callback(hObject, eventdata, handles)
% hObject    handle to Xmin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Xmin as text
%        str2double(get(hObject,'String')) returns contents of Xmin as a double


% --- Executes during object creation, after setting all properties.
function Xmin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Xmin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function Xmax_Callback(hObject, eventdata, handles)
% hObject    handle to Xmax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Xmax as text
%        str2double(get(hObject,'String')) returns contents of Xmax as a double


% --- Executes during object creation, after setting all properties.
function Xmax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Xmax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Ymin_Callback(hObject, eventdata, handles)
% hObject    handle to Ymin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Ymin as text
%        str2double(get(hObject,'String')) returns contents of Ymin as a double


% --- Executes during object creation, after setting all properties.
function Ymin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Ymin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function Ymax_Callback(hObject, eventdata, handles)
% hObject    handle to Ymax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Ymax as text
%        str2double(get(hObject,'String')) returns contents of Ymax as a double


% --- Executes during object creation, after setting all properties.
function Ymax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Ymax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function Tfinal_Callback(hObject, eventdata, handles)
% hObject    handle to Tfinal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Tfinal as text
%        str2double(get(hObject,'String')) returns contents of Tfinal as a double


% --- Executes during object creation, after setting all properties.
function Tfinal_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Tfinal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function IC_Callback(hObject, eventdata, handles)
% hObject    handle to IC (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of IC as text
%        str2double(get(hObject,'String')) returns contents of IC as a double


% --- Executes during object creation, after setting all properties.
function IC_CreateFcn(hObject, eventdata, handles)
% hObject    handle to IC (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function nsteps_Callback(hObject, eventdata, handles)
% hObject    handle to nsteps (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nsteps as text
%        str2double(get(hObject,'String')) returns contents of nsteps as a double


% --- Executes during object creation, after setting all properties.
function nsteps_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nsteps (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function JP1_Callback(hObject, eventdata, handles)
% hObject    handle to JP1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of JP1 as text
%        str2double(get(hObject,'String')) returns contents of JP1 as a double


% --- Executes during object creation, after setting all properties.
function JP1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to JP1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function JP2_Callback(hObject, eventdata, handles)
% hObject    handle to JP2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of JP2 as text
%        str2double(get(hObject,'String')) returns contents of JP2 as a double


% --- Executes during object creation, after setting all properties.
function JP2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to JP2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit21_Callback(hObject, eventdata, handles)
% hObject    handle to edit21 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit21 as text
%        str2double(get(hObject,'String')) returns contents of edit21 as a double


% --- Executes during object creation, after setting all properties.
function edit21_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit21 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global Handles;
% Data Cursor object

try
dcm_obj = datacursormode(handles.figure1);
set(dcm_obj,'UpdateFcn',@ODE_Interfaceupdatefcn);
handles.dcm_obj = dcm_obj;
guidata(hObject, handles);

% Read in the values
syms x y
uicontrol(handles.f_x_y);
tfinal = str2double(get(handles.Tfinal,'String'));
nsteps = str2double(get(handles.nsteps,'String'));
Xmin = str2double(get(handles.Xmin,'String'));
Xmax = str2double(get(handles.Xmax,'String'));
Ymin = str2double(get(handles.Ymin,'String'));
Ymax = str2double(get(handles.Ymax,'String'));
f_x_y= get(handles.f_x_y,'String');
g_x_y= get(handles.g_x_y,'String');
if isempty(regexp(f_x_y,'x')) & isempty(regexp(f_x_y,'y'))
    f_x_y = [f_x_y ];
end
if isempty(regexp(g_x_y,'x')) & isempty(regexp(g_x_y,'y'))
    g_x_y = [g_x_y ];
end

zzz = get(handles.InitStates,'Data'); zz =[];
[mrows,ncols]=size(zzz);
for i=1:mrows
    if (~isnan(zzz(i,1)) & ~isnan(zzz(i,2)))
        zz = [zz ; zzz(i,:)];
    end
end

if isempty(zz)
    errordlg('Data Invalid','No initial states','Modal');
    return
end


set(handles.InitStates,'Data',zz);

init = zz'; initSize = size(init);
PopUpStr='1';
for indexVal=2:initSize(2)
    PopUpStr = [PopUpStr '|' num2str(indexVal)];
end

set(handles.popupmenu2,'String',PopUpStr);

eval(['feq = ' f_x_y  ';']);
eval(['geq = ' g_x_y  ';']);
% Use Symbolic Maths Toolbox to obtain the numerical version of equations
% eqm is the function generated by Symbolic Maths Toolbox
% fxy is the function necessary to be called by the ODE solver       
if isnumeric(feq)
    eqm = matlabFunction([[num2str(feq) '+1e-12*x']; geq]);   
elseif isnumeric(geq)
    eqm = matlabFunction([feq; [num2str(geq) '+1e-12*x']]);
else
    eqm = matlabFunction([feq; geq]);
end


fxy = @(t,x) eqm(x(1),x(2));

% Compute the nullclines

Lims = [Xmin, Xmax, Ymin, Ymax];
handles.Lims = Lims;
guidata(hObject, handles);
[Dx1, Dy1, xx, yy] = nullcline(feq,Lims);
[Dx2, Dy2, xx, yy] = nullcline(geq,Lims);

% Work out size of the plotting grid
% Use the limits and the number to build the x,y grid for plot command
xl = linspace(Xmin,Xmax,nsteps);
yl = linspace(Ymin,Ymax,nsteps);
[X1,X2] = meshgrid(xl,yl);

% Evaluate the derivative functions for the grid values
dx = eqm(X1,X2);

% Choose the main axis
delete(get(handles.axes1,'Children'));
axes(handles.axes1);

% Compute the magnitudes and normalize the plot to get direction field only
lx = Xmax-Xmin; ly = Ymax-Ymin; r = ly/lx; 
DL = sqrt((r*dx(1:end/2,:)).^2 + dx(end/2+1:end,:).^2);
h=quiver(X1,X2,dx(1:end/2,:)./DL,dx(end/2+1:end,:)./DL,0.5,'Color',[0.8 0.7 1]);

% set(h,'ShowArrowHead','off');
axis equal
hold on

if ~isempty(Dx1), h1=plot(Dx1,yy,'r'); end;
if ~isempty(Dy1), h2=plot(xx,Dy1,'r'); end;
if ~isempty(Dx2), h3=plot(Dx2,yy,'b'); end;
if ~isempty(Dy2), h4=plot(xx,Dy2,'b'); end;

% Set tight limits of the axes for the phase plane 
xlim([Xmin Xmax]); ylim([Ymin Ymax]); grid on;
title('Phase Plane - y against x: {{\color{red} --- dx/dt = 0} {\color{blue} --- dy/dt=0}}'); 
xlabel('x'); ylabel('y');

MarkCol = colormap('Lines');      

handles.MarkCol = MarkCol;
guidata(hObject, handles);

for i=1:initSize(2)
    axes(handles.axes1)
    % Solve the ODE
    options = odeset('RelTol',1e-6);
    [t,xx] = ode45(fxy,[0 tfinal],init(:,i)',options);
    global Vals;
    Vals(i).t = t; Vals(i).xx = xx;
    assignin('base', ['t' num2str(i)],t);
    assignin('base', ['x' num2str(i)],xx(:,1));
    assignin('base', ['y' num2str(i)],xx(:,2));
    
    % Plot using scatter plot
    ha(i) = plot(xx(:,1),xx(:,2),'Color',MarkCol(i,:),'LineWidth',2,...
            'XDatasource',['x' num2str(i)],'YDataSource',['y' num2str(i)]); 
    % linkdata on;
    plot(xx(1,1),xx(1,2),'o','MarkerEdgeColor', MarkCol(i,:), 'MarkerSize', 10, 'LineWidth', 2);
    % brushobj=brush(handles.axes1);
    
    % Store the data
end
handles.ha = ha;
guidata(hObject, handles);
plotaxis2(Vals,handles)

% Find the Stationary points (the intersection of the nullclines)
% Solve the combined equation and identify the relevant points
% Compute the Jacobian Matrix at the stationary points
Sx = solve([feq; geq],'x,y');
if isempty(Sx)
    errordlg('No Solution','No stationary points','Modal');
    set(handles.popupmenu3,'String', ' ');
    handles.jmeq = [];
    handles.jm = [];
    handles.valSx1 = [];
    handles.valSx2 = [];
    guidata(hObject, handles);
    CalcTraceDet(handles);
else

i=sqrt(-1); 
valSx1 = eval(Sx.x); 
valSx1=real(valSx1(abs(imag(valSx1))<=1e-6));
valSx2 = eval(Sx.y); 
valSx2=real(valSx2(abs(imag(valSx2))<=1e-6));

[jmeq, jm] = deriveJacobian([feq; geq], x,y);
% Print the stationary points, the value of the Jacobian matrix, the trace and the determinant 
% For each of the computed stationary points workout the display value

    l = length(valSx1); clc;
    mitem=[''];
        for i=1:l
            mitem = [mitem  num2str(i) ':(' num2str(valSx1(i),3) ',' num2str(valSx2(i),3) ')'];
            if i<l,
               mitem = [mitem '|'];
            end
        end
    set(handles.popupmenu3,'String', mitem);
    
    axes(handles.axes1);
    offset = 0.05*(Lims(2)-Lims(1));
    for i=1:l
        text(valSx1(i)-offset,valSx2(i),['S' num2str(i)]);
    end
% Store values in handles guidata for the future
    handles.jmeq = jmeq;
    handles.jm = jm;
    handles.valSx1 = valSx1;
    handles.valSx2 = valSx2;
    guidata(hObject, handles);
    CalcTraceDet(handles);
end    
    Handles = handles;
catch err
    errordlg('An Error has occurred','Error Occurred','modal');
end
return


function [Dx, Dy, xx, yy] = nullcline(f,Lims)

xx = linspace(Lims(1),Lims(2),201); yy = linspace(Lims(3), Lims(4), 201); Dx = []; Dy =[];
try
if ~isnumeric(f) 
    dbydtzeroy = solve(f,'y');  % Solve the equation d/dt = 0 for y 
    dbydtzerox = solve(f,'x');  % Solve the equation d/dt = 0 for x
else
    return
end
ndx = size(dbydtzerox); ndx=ndx(1); nullcline_dx=[];

if ~isempty(dbydtzerox)
    for i=1:ndx
        nullcline_dx{i} = matlabFunction(dbydtzerox(i)); zz = char(nullcline_dx{i});
        if strcmp(zz(3),')'),
            fval = nullcline_dx{i}().*ones(size(yy)); fval(abs(imag(fval))>1e-3)=NaN;
        else
            fval = nullcline_dx{i}(yy); fval(abs(imag(fval))>1e-3)=NaN;
        end
        Dx = [Dx fval'];
    end
end
ndy = size(dbydtzeroy); ndy=ndy(1); nullcline_dy=[]; Dy_y = [];
if ~isempty(dbydtzeroy)
    for i=1:ndy
        nullcline_dy{i} = matlabFunction(dbydtzeroy(i)); zz = char(nullcline_dy{i});
        if strcmp(zz(3),')'),
            fval = nullcline_dy{i}().*ones(size(xx)); fval(abs(imag(fval))>1e-3)=NaN;
        else
            fval = nullcline_dy{i}(xx); fval(abs(imag(fval))>1e-3)=NaN;
        end
        Dy = [Dy fval'];
    end
end
catch
    Dx=[]; Dy=[];
end
return


function plotaxis2(Vals,handles)

Lims = handles.Lims;
XorY = get(handles.popupmenu1,'Value');
Index = get(handles.popupmenu2,'Value');
delete(get(handles.axes2,'Children'));
axes(handles.axes2);

switch XorY
    case 1
        tStr = ['t' num2str(Index)];
        yStr = ['x' num2str(Index)];
        plot(Vals(Index).t, Vals(Index).xx(:,XorY),'LineWidth',2,'Color',handles.MarkCol(Index,:),...
        'XDataSource',tStr,'YDataSource',yStr);
        ylim([Lims(2*XorY-1) Lims(2*XorY)]);
        xlabel('Time'); ylab='xy'; ylabel(ylab(XorY)); title('Time Response');
        grid on;
    case 2
        tStr = ['t' num2str(Index)];
        yStr = ['y' num2str(Index)];
        plot(Vals(Index).t, Vals(Index).xx(:,XorY),'LineWidth',2,'Color',handles.MarkCol(Index,:),...
        'XDataSource',tStr,'YDataSource',yStr);
        ylim([Lims(2*XorY-1) Lims(2*XorY)]);
        xlabel('Time'); ylab='xy'; ylabel(ylab(XorY)); title('Time Response');
        grid on;
    case 3
        n = num2str(Index);
        xstr=['x' n]; ystr = ['y' n]; tstr = ['t' n];
        plot3(Vals(Index).xx(:,1), Vals(Index).xx(:,2), Vals(Index).t,'LineWidth', 3, ...
        'Color',handles.MarkCol(Index,:),'XDataSource',xstr);
        hold on;
        grid on
        % Plot initial & end markers
        plot3(Vals(Index).xx(1,1),Vals(Index).xx(1,2),Vals(Index).t(1),'Marker','o','MarkerEdgeColor','red','LineWidth',2)
        plot3(Vals(Index).xx(end,1), Vals(Index).xx(end,2),Vals(Index).t(end),'Marker','pentagram','MarkerEdgeColor','red','LineWidth',2);
        xlim(Lims(1:2)); ylim(Lims(3:4));
        title('3D plot x & y vs time');
        xlabel('x'); ylabel('y'); zlabel('Time');
        hold off;        
    case 4
        plot(Vals(Index).t, Vals(Index).xx(:,1), Vals(Index).t, Vals(Index).xx(:,2),'--',...
            'LineWidth',2,'Color',handles.MarkCol(Index,:));
        %'XDataSource',tStr,'YDataSource',yStr);
        ylim([min(Lims(1),Lims(3)) max(Lims(2),Lims(3))]);
        xlabel('Time'); ylab='xy'; ylabel('x(t) & y(t)'); title('Time Response');
        grid on; legend('x(t)','y(t)');
    otherwise
end


return

% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
global Vals;
plotaxis2(Vals, handles);

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2
global Vals;
plotaxis2(Vals, handles);

% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3
CalcTraceDet(handles);


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function Trace_Callback(hObject, eventdata, handles)
% hObject    handle to Trace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Trace as text
%        str2double(get(hObject,'String')) returns contents of Trace as a double


% --- Executes during object creation, after setting all properties.
function Trace_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Trace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','blue');
end



function Det_Callback(hObject, eventdata, handles)
% hObject    handle to Det (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Det as text
%        str2double(get(hObject,'String')) returns contents of Det as a double


% --- Executes during object creation, after setting all properties.
function Det_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Det (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function CalcTraceDet(handles)
% Call the function that computes the Jacobian for the relevant stationary
% point
        jmeq = handles.jmeq;
        valSx1 = handles.valSx1;
        valSx2 = handles.valSx2;
        if ~isempty(valSx1)
            i=get(handles.popupmenu3,'Value');
        
% Get the numerical value of the jacobian
            J = jmeq(valSx1(i), valSx2(i));
% Compute Trace and determinant
            tJ= trace(J); 
            dJ = det(J);
            jm = handles.jm;
            D11 = ['D_{11} = ' char(jm(1))];
            D12 = ['D_{12} = ' char(jm(3))];
            D21 = ['D_{21} = ' char(jm(2))];
            D22 = ['D_{22} = ' char(jm(4))];
            axes(handles.axes4);
            delete(get(handles.axes4,'Children'));
            axes('Visible','off');
            text('Parent',handles.axes4,'Interpreter','latex',...
            'String', ['$\begin{array}{ll}'  D11 ' &'  D12 ...
            ' \\ ' D21 ' &' D22 ' \end{array} $'],...
            'Position',[0.0 0.5 0],...
            'FontSize',14, 'FontWeight','bold'); 
        
        
            D11 = ['D_{11} = ' num2str(J(1,1),3)];
            D12 = ['D_{12} = ' num2str(J(1,2),3)];
            D21 = ['D_{21} = ' num2str(J(2,1),3)];
            D22 = ['D_{22} = ' num2str(J(2,2),3)];
            T = ['T = ' num2str(tJ,3)];
            Delta = ['\Delta = ' num2str(dJ,3)];
            
            t2JmdJ = tJ^2-4*dJ; 
            if dJ < 0,
                StationaryPointType = 'Saddle';
            else
                if tJ > 0,
                    if t2JmdJ > 0,
                        StationaryPointType = 'Unstable Node';
                    else
                        StationaryPointType = 'Unstable Focus';
                    end
                else
                    if t2JmdJ > 0,
                        StationaryPointType = 'Stable Node';
                    else
                        StationaryPointType = 'Stable Focus';
                    end                
                end
            end
           
            axes(handles.axes5);
            delete(get(handles.axes5,'Children'));
            axes('Visible','off');
            text('Parent',handles.axes5,'Interpreter','latex',...
            'String', ['$\begin{array}{llll}' D11 ' &'  D12 ...
            ' &' T '& ' Delta  ' \\ ' ...
            D21 ' &' D22 ' &' StationaryPointType ' &' ... 
            '\end{array} $'],...
            'Position',[0.0 0.4 0],...
            'FontSize',12, 'FontWeight','bold');
         
        else
            D11 = ['D_{11} = '];
            D12 = ['D_{12} = '];
            D21 = ['D_{21} = '];
            D22 = ['D_{22} = '];
            axes(handles.axes4);
            delete(get(handles.axes4,'Children'));
            axes('Visible','off');
            text('Parent',handles.axes4,'Interpreter','latex',...
            'String', ['$\begin{array}{ll}'  D11 ' &'  D12 ...
            ' \\ ' D21 ' &' D22 ' \end{array} $'],...
            'Position',[0.0 0.5 0],...
            'FontSize',14, 'FontWeight','bold'); 
        
        
            D11 = ['D_{11} = ' ];
            D12 = ['D_{12} = ' ];
            D21 = ['D_{21} = ' ];
            D22 = ['D_{22} = ' ];
            T = ['T = ' ];
            Delta = ['\Delta = ' ];            
            axes(handles.axes5);
            delete(get(handles.axes5,'Children'));
            axes('Visible','off');
            text('Parent',handles.axes5,'Interpreter','latex',...
            'String', 'No Stationary Points',...
            'Position',[0.0 0.0 0],...
            'FontSize',11, 'FontWeight','bold'); 
        end
    
        % Plot stationary point against the standard quadratic
        % plotJacobian(handles, tJ, dJ);
return


function [jmeq, jm] = deriveJacobian(eq,x,y)

jm = [jacobian(eq,x) jacobian(eq,y)];
jmeq = matlabFunction(jm); 
jmeqstr = func2str(jmeq);
switch jmeqstr(3:4)
    case 'x)'
        jmeqstr = [jmeqstr(1:3) ',y' jmeqstr(4:end)];
    case 'y)'
        jmeqstr = [jmeqstr(1:2) 'x,' jmeqstr(3:end)];
    case 'x,';    
    otherwise
        jmeqstr = [jmeqstr(1:2) 'x,y' jmeqstr(3:end)];
end
jmeq= str2func(jmeqstr);
return

% --- Executes on key press with focus on pushbutton1 and none of its controls.
function pushbutton1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)



function sJ2_Callback(hObject, eventdata, handles)
% hObject    handle to sJ2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of sJ2 as text
%        str2double(get(hObject,'String')) returns contents of sJ2 as a double


% --- Executes during object creation, after setting all properties.
function sJ2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sJ2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function sJ1_Callback(hObject, eventdata, handles)
% hObject    handle to sJ1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of sJ1 as text
%        str2double(get(hObject,'String')) returns contents of sJ1 as a double


% --- Executes during object creation, after setting all properties.
function sJ1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sJ1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function txt = ODE_Interfaceupdatefcn(empt,event_obj)
% Customizes text of data tips

persistent plothandle;
global Handles;

p2 = get(Handles.popupmenu2,'Value');
if p2==4
    return
end
dataIndex = get(event_obj,'DataIndex');
Target = get(event_obj,'Target');
Par = get(Target,'Parent'); 
Ax = get(Par,'Tag');
dcm_obj = Handles.dcm_obj;

X = get(Target,'XDataSource');

Xval = evalin('base', ['x' X(2) '(' num2str(dataIndex) ')']);
Yval = evalin('base', ['y' X(2) '(' num2str(dataIndex) ')']);
Tval = evalin('base', ['t' X(2) '(' num2str(dataIndex) ')']);
txt = {['X: ',num2str(Xval)],...
	   ['Y: ',num2str(Yval)],...
       ['Time: ',num2str(Tval)]};

if (strcmp(Ax,'axes1'))
    axes(Handles.axes2); hold on;
    p1 = get(Handles.popupmenu1,'Value');
    p2 = get(Handles.popupmenu2,'Value');
    n = str2double(X(2));
    if p2 == n,
        if ishghandle(plothandle)
            set(plothandle,'Visible','off');
        end
        if p1 == 1,
            plothandle = scatter(Tval,Xval,'rs','MarkerFaceColor','red');
        elseif p1 == 2,
            plothandle = scatter(Tval,Yval,'rs','MarkerFaceColor','red');
        else
            plothandle = plot3(Xval,Yval,Tval,'rs','MarkerFaceColor','red');
        end
    end
    hold off;
else
    axes(Handles.axes1);
    hold on;
    if ishghandle(plothandle)
       set(plothandle,'Visible','off');
    end
    plothandle = scatter(Xval,Yval,'rs','MarkerFaceColor','red');
    hold off;
end
set(dcm_obj,'Enable','off')
    

return


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axes1);

nsteps = str2double(get(handles.nsteps,'String'));
Xmin = str2double(get(handles.Xmin,'String'));
Xmax = str2double(get(handles.Xmax,'String'));
Ymin = str2double(get(handles.Ymin,'String'));
Ymax = str2double(get(handles.Ymax,'String'));
dx = 0.1*(Xmax-Xmin)/nsteps;
dy = 0.1*(Ymax-Ymin)/nsteps;
Z=ginput(1);
Z(1) = round(Z(1)/dx)*dx; Z(2) = round(Z(2)/dy)*dy;
zzz = get(handles.InitStates,'Data'); zz =[];
[mrows,ncols]= size(zzz);
for i=1:mrows
    if (~isnan(zzz(i,1)) & ~isnan(zzz(i,2)))
        zz = [zz ; zzz(i,:)];
    end
end
[mrows,ncols] = size(zz);
zz = [zz; Z(1) Z(2)];
set(handles.InitStates,'Data',zz);
pushbutton1_Callback(hObject, eventdata, handles);


return