function varargout = newton(varargin)
% NEWTON M-file for newton.fig
%      NEWTON, by itself, creates a new NEWTON or raises the existing
%      singleton*.
%
%      H = NEWTON returns the handle to a new NEWTON or the handle to
%      the existing singleton*.
%
%      NEWTON('Property','Value',...) creates a new NEWTON using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to newton_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      NEWTON('CALLBACK') and NEWTON('CALLBACK',hObject,...) call the
%      local function named CALLBACK in NEWTON.M with the given input
%      arguments.
%
%      *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 newton

% Last Modified by GUIDE v2.5 12-Aug-2002 20:18:20

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @newton_OpeningFcn, ...
                   'gui_OutputFcn',  @newton_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
if nargin & isstr(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 newton is made visible.
function newton_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = newton_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 on button press in newfunction.
function newfunction_Callback(hObject, eventdata, handles)
% hObject    handle to newfunction (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hold off
x = -5:0.1:5;
y = newtonfun(x);
h = plot(x,y);
set(h, 'LineWidth', 3)
axis([-5 5 min(y)-2 max(y)+2 ])
hold on
plot(x, zeros(size(x)), 'k-')

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

% THIS CALLBACK IS NULL


% --- Executes on button press in findroot.
function findroot_Callback(hObject, eventdata, handles)
% hObject    handle to findroot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hx0 = handles.x0;
x0 = str2double(get(hx0,'String'));
tol = 0.1;
diff = tol+1;
xvals = x0;
yvals = [];
x = x0;
global p

while diff > tol
    
    y = p(1) + ...
      p(2)*cos((2*pi/10)*x) + ...
      p(3)*sin((2*pi/10)*x) + ...
      p(4)*cos((4*pi/10)*x) + ...
      p(5)*sin((4*pi/10)*x);
  
    y1 = -p(2)*(2*pi/10)*sin((2*pi/10)*x) + ...
      p(3)*(2*pi/10)*cos((2*pi/10)*x) - ...
      p(4)*(4*pi/10)*sin((4*pi/10)*x) + ...
      p(5)*(4*pi/10)*cos((4*pi/10)*x);
    
    xnew = x - y/y1;  % Newton Evaluation
    xvals = [xvals, xnew];
    yvals = [yvals, y];
    x = xnew;
    diff = abs(y);    
end

xvals = [xvals; xvals];
z = zeros(size(yvals));
yvals = [z; yvals];
root = xvals(end);
set(handles.root, 'String', num2str(root))

global hsearchplot
hsearchplot = plot(xvals(1:end-2), yvals(:), 'ro-');
hold on
global hx0plot
hx0plot = plot(x0,0,'ko','MarkerFaceColor','c');
global hrootplot
hrootplot = plot(root,0,'ko','MarkerFaceColor','m');


% --- Executes during object creation, after setting all properties.
function root_CreateFcn(hObject, eventdata, handles)
% hObject    handle to root (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
%     set(hObject,'BackgroundColor','white');
% else
%     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
% end


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

% THIS CALLBACK IS NULL


% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
% hObject    handle to clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.root, 'String', ' ')
global hsearchplot
set(hsearchplot, 'Visible', 'off')
global hx0plot
set(hx0plot, 'Visible', 'off')
global hrootplot
set(hrootplot, 'Visible', 'off')