Anda di halaman 1dari 5

function varargout = GUI_fun(varargin)

% GUI_FUN MATLAB code for GUI_fun.fig


% GUI_FUN, by itself, creates a new GUI_FUN or raises the existing
% singleton*.
%
% H = GUI_FUN returns the handle to a new GUI_FUN or the handle to
% the existing singleton*.
%
% GUI_FUN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_FUN.M with the given input arguments.
%
% GUI_FUN('Property','Value',...) creates a new GUI_FUN or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_fun_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_fun_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 GUI_fun

% Last Modified by GUIDE v2.5 21-May-2014 13:49:42

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_fun_OpeningFcn, ...
'gui_OutputFcn', @GUI_fun_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

function [Sol,x] = SolveEquation(handles)

%Recieve functions f and g and the Sol limits x


f = inline(get(handles.function_f,'string'));
g = inline(get(handles.function_g,'string'));
x = str2num(get(handles.x_res,'string'));

g = vectorize(g);
f = vectorize(f);
tol=1e-9;
tic()
S=sign(g(x)-f(x));
h=find(diff(S)~=0); % Find where two lines cross over
Sol=zeros(size(h));
Y_point=zeros(size(h));
Err=zeros(size(h));
root = zeros(size(h));
Y_root=zeros(size(h));

if ~isempty(h) % There are some cross-over points


for i=1:length(h) % For each point, improve the approximation
xN=x(h(i):h(i)+1);
err=1;
while(abs(err)>tol) % Iteratively improve aproximation
S=sign(g(xN)-f(xN));
hF=find(diff(S)~=0);
xN=xN(hF:hF+1);
[~,I]=min(abs(f(xN)-g(xN)));
xG=xN(I);
err=f(xG)-g(xG);
xN=linspace(xN(1),xN(2),15);
end
Sol(i)=xG;
Y_point(i) = f(xG);
Err(i)=f(xG)-g(xG);
end
else % No crossover points - lines could meet at tangents
[h,I]=findpeaks(-abs(g(x)-f(x)));
Sol=x(I(abs(f(x(I))-g(x(I)))<1e-5));
Err=f(Sol)-g(Sol);
end
% We also have to check each endpoint
if abs(f(x(end))-g(x(end)))<tol && abs(Sol(end)-x(end))>1e-12
Sol=[Sol x(end)];
Err=[Err g(x(end))-f(x(end))];
end
if abs(f(x(1))-g(x(1)))<tol && abs(Sol(1)-x(1))>1e-12
Sol = [x(1) Sol];
Err = [g(x(1))-f(x(1)) Err];
end

%Outputs the equation roots


if isempty(Sol)
set(handles.roots_display,'String','There are no intersection points or the
roots are complex')
else
Solutions = cell(numel(Sol),1);
for l = 1:(numel(Sol)-1)
Solutions(l) = {['x',num2str(l),' = ' ,num2str(Sol(l)),'
','y',num2str(l),' = ',num2str(Y_point(l))]};
end
set(handles.roots_display,'String',Solutions)
end;

Plot_Functions(handles,Sol,f,g,x); %Calls to the function that plots f(x) and g(x)


function Plot_Functions(handles,Sol,f,g,x)

cla %Clears the axes before drawing on them


if isempty(Sol)
axes(handles.functions_graph);
plot(x,f(x),x,g(x));
title('Functions f(x) and g(x)'); xlabel('X'); ylabel('Y'); grid;
else
axes(handles.functions_graph);
plot(x,f(x),'b',Sol,f(Sol),'ro',x,g(x),'cyan',Sol,g(Sol),'ro');
title('Functions f(x) and g(x)'); xlabel('X'); ylabel('Y'); grid;
axis normal
end;

% --- Executes just before GUI_fun is made visible.


function GUI_fun_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 GUI_fun (see VARARGIN)

% Choose default command line output for GUI_fun


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes GUI_fun wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = GUI_fun_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;

% --- Executes during object creation, after setting all properties.

% --- Executes on button press in Solve_Button.


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

%Present equation on text for user


fun_f = get(handles.function_f,'string');
fun_g = get(handles.function_g,'string');
Eq = strcat(fun_f,' = ',fun_g);
set(handles.Sol,'string',Eq);

SolveEquation(handles); %Call for the function solving the equation


function function_f_Callback(hObject, eventdata, handles)
% hObject handle to function_f (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 function_f as text


% str2double(get(hObject,'String')) returns contents of function_f as a double

% --- Executes during object creation, after setting all properties.


function function_f_CreateFcn(hObject, eventdata, handles)
% hObject handle to function_f (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 function_g_Callback(hObject, eventdata, handles)


% hObject handle to function_g (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 function_g as text


% str2double(get(hObject,'String')) returns contents of function_g as a double

% --- Executes during object creation, after setting all properties.


function function_g_CreateFcn(hObject, eventdata, handles)
% hObject handle to function_g (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 x_res_Callback(hObject, eventdata, handles)


% hObject handle to x_res (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 x_res as text


% str2double(get(hObject,'String')) returns contents of x_res as a double

% --- Executes during object creation, after setting all properties.


function x_res_CreateFcn(hObject, eventdata, handles)
% hObject handle to x_res (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

[]

: Sol

.1

Anda mungkin juga menyukai