image thumbnail
from GUI solver for nonlinear algebraic equation systems using fsolve by Tingyue Gu
GUI application using MATLAB fsolve to solve up to 99 nonlinear algebraic equations simultaneously.

fsolveGUI(varargin)
%This was coded by Dr. Tingyue Gu (gu@ohio.edu) of Ohio Univ. (2009)
function varargout = fsolveGUI(varargin)
% FSOLVEGUI M-File for fsolveGUI.fig
%      FSOLVEGUI, by itself, creates a new FSOLVEGUI or raises the existing
%      singleton*.
%
%      H = FSOLVEGUI returns the handle to a new FSOLVEGUI or the handle to
%      the existing singleton*.
%
%      FSOLVEGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FSOLVEGUI.M with the given input arguments.
%
%      FSOLVEGUI('Property','Value',...) creates a new FSOLVEGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before fsolveGUI_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to fsolveGUI_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 fsolveGUI

% Last Modified by GUIDE v2.5 24-Apr-2009 12:35:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @fsolveGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @fsolveGUI_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 fsolveGUI is made visible.
function fsolveGUI_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 fsolveGUI (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = fsolveGUI_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 edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 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 = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- 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



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
line{1}='This software uses MATLAB fsolve to solve a nonlinear equation set.';
line{2}='It allows up to 99 equations in a set.';
line{4}='This free software is for academic teaching & learning only.';
line{5}='Commercial users must contact Dr. Tingyue Gu (gu@ohio.edu)';
line{6}='to develop a free-standing executable commercial version.';
line{8}='Copyright 2009';
msgbox(line,'About','none');


% --- 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 eqexpression neq
global x feval
set(handles.edit3,'Visible','off')  %hide previous results
x0input=get(handles.edit1,'String');
x0arraystring=['[' x0input ']'];
x0=eval(x0arraystring);
neq=length(x0);
eqexpression=get(handles.edit2,'String');
eqexpression=cellstr(eqexpression); %cell array of strings from character array
eqexpression=regexprep(eqexpression,'x(\d\d)','x($1)'); %x12 -> x(12)
eqexpression=regexprep(eqexpression,'x(\d)','x($1)');  %x1 -> x(1)

format compact
[x feval exitflag]=fsolve(@obj,x0)
set(handles.edit3,'Visible','on', 'String', [x feval])
if exitflag~=1
    line{1}='There may be a problem with the solution.';
    line{2}='Check the residual values. Are they close to zero?';
    line{4}='Trying different initial values may help!';
    uiwait(msgbox(line, 'Solution problem','warn'));
end

function residual=obj(x)     
global eqexpression neq 
for n=1:neq
residual(n)=eval(eqexpression{n});   %eval is needed to calcualte string expression.
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over edit2.
function edit2_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit2,'String',clipboard('paste'));

% --------------------------------------------------------------------
function loadeqfile_Callback(hObject, eventdata, handles)
% hObject    handle to loadeqfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global eqexpression neq
titlestring='Import User Supplied .txt File Containing Equations'
[filename, pathname] =uigetfile('*.txt',titlestring);
if isequal(filename,0) | isequal(pathname,0); return; end %Pressed Cancel
userEqFile=fullfile(pathname,filename); 
eqexpression=textread(userEqFile,'%s');
set(handles.edit2,'String',eqexpression)

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


% --------------------------------------------------------------------
function SaveResults_Callback(hObject, eventdata, handles)
% hObject    handle to SaveResults (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global eqexpression neq
global x feval
[filename,pathname] = uiputfile('.txt','Save Results');
if isequal(filename,0) | isequal(pathname,0); return; end %Pressed Cancel
fid=fullfile(pathname,filename);
fid=fopen(fid, 'wt'); %using w will not wrap text for notepad
fprintf(fid,'The %d Roots Are: \n',neq);
fprintf(fid,'%g\n',x);
fprintf(fid,'\nThe %d Residuals Are: \n',neq);
fprintf(fid,'%g\n',feval);
fprintf(fid,'\nThe %d Initial Values Are: \n',neq);
x0input=get(handles.edit1,'String');
fprintf(fid,'%s', x0input);
fprintf(fid,'\n\nThe %d Equation Expressions (=0 omitted) Are: \n',neq);
eqs=get(handles.edit2,'String');
for n=1:neq; fprintf(fid,'%s\n', eqs{n}); end
%fprintf(fid,'%s\n', eqs);  %No good even though fprintf is vectorized!
fclose(fid);

% --------------------------------------------------------------------
function openResults_Callback(hObject, eventdata, handles)
% hObject    handle to openResults (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] =uigetfile('*.txt','Open a .txt file to view');
if isequal(filename,0) | isequal(pathname,0); return; end %Pressed Cancel
resultFile=fullfile(pathname,filename); 
open(resultFile)

% --------------------------------------------------------------------
function mfile_Callback(hObject, eventdata, handles)
% hObject    handle to mfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x0input=get(handles.edit1,'String');
x0arraystring=['[' x0input ']'];
x0=eval(x0arraystring);
neq=length(x0);
eqexpression=get(handles.edit2,'String');
eqexpression=regexprep(eqexpression,'x(\d\d)','x($1)'); %x12 -> x(12)
eqexpression=regexprep(eqexpression,'x(\d)','x($1)');  %x1 -> x(1)
eqexpression=regexprep(eqexpression,'%',';%');  %put no output before %
[filename,pathname] = uiputfile('.m','Specify a file name (e.g., test.m)');
if isequal(filename,0) | isequal(pathname,0); return; end %Pressed Cancel
fid=fullfile(pathname,filename);
fid=fopen(fid, 'wt'); %using w will not wrap text for notepad
fprintf(fid,'%Run this m-file in MATLAB command console');
fprintf(fid,'function main\n');
fprintf(fid,'x0=[%s]; %%Initial guess\n',x0input);
fprintf(fid,'[x feval exitflag]=fsolve(@obj,x0)\n\n');
fprintf(fid,'function residual=obj(x)\n');
for n=1:neq; fprintf(fid,'residual(%d)=%s;\n',n,eqexpression{n}); end
fclose(fid);



% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over edit1.
function edit1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=clipboard('paste');  %this may be multiline that is not allowed here
str=regexprep(str,'\n',';'); %replace carriage return with ;
set(handles.edit1,'String',str);

% --------------------------------------------------------------------
function pastefrom_Callback(hObject, eventdata, handles)
% hObject    handle to pastefrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
line{1}='Right click on an edit field (white cell).';
line{2}='This will put clipboard text to the field';
line{3}='You cannot copy more than one line of text';
line{4}='to a single field. An error will occur!';
msgbox(line,'How','help');


% --------------------------------------------------------------------
function copyto_Callback(hObject, eventdata, handles)
% hObject    handle to copyto (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
line{1}='Right click on the results.';
line{2}='This will copy the text to clipboard.';
msgbox(line,'How','help');

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

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over edit3.
function edit3_ButtonDownFcn(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)
global x feval  
str=['x=', num2str(x), '; feval=' num2str(feval)];  %no multi-line allowed by clipboard here
h=msgbox('       Done!','Paste to clipboard','Help');
pause(1); close(h);
clipboard('copy',str)

Contact us at files@mathworks.com