%% Optimal Fit of a Non-linear Function
% This is a demonstration of the optimal fitting of a non-linear function to a
% set of data.  It uses FMINSEARCH, an implementation of the Nelder-Mead simplex% (direct search) algorithm, to minimize a nonlinear function of several
% variables.
%
% Modified by VdS from program fitdemo by
% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 5.15.4.2 $ $Date: 2004/08/16 01:37:29 $

%%
% First, create some sample data and plot it.

t = [0 0; 0 1; 1 0; 1 1];
y = [0 1 1 0]'

%%
% The goal is to fit a neural network with 7 non-linear parameters
% to the XOR data:
%
%
% To fit this function, we've create a function MYFITFUNSLOW.  Given the
% , FITFUN calculates the error in the
% fit for this equation and updates the line (h).

type neuralfitfun

%%
% Make a guess for initial estimate of w41 w43 w31 w32 b2 wv2 b4 
% and invoke FMINSEARCH.  It
% minimizes the error returned from FITFUN by adjusting them.  It returns the
% final value of the params.

% random initial small weights
% start = [1; -2; 1; 1; -2; 1; 0]; good initial conditions
% start=[.01; -.01; .005;-.04; .02; .01; -.03]; not good 
 start=[-1; 1 ; 2; 3; 1; -1; 1]
 start=[1; 1; 1; 1; 1; 1; 1; 1]
 start=[1; -1; 1; -1; 1; -1; 1]
 start=[-1; -1; -1; -1; -1; -1; -1]
%options = optimset('TolFun',0.1);  % termination tolerance on error 
[estimated_d,pval,exitflag] = fminsearch(@(x) neuralfitfun(x,t,y),start)

