Home » Programming » Function approximation using ANN ‎(with MATLAB GUI)

Function approximation using ANN ‎(with MATLAB GUI)

This code is a simple implementation of a function approximation using artificial neural networks (ANN). I developed this code some years ago as mini project for one of my master course. I posted here, maybe someone will get benefit from it. I developed the program using MATLAB GUI (see GUI tutorial here: http://www.mathworks.com/videos/creating-a-gui-with-guide-68979.html).  Also, in this code I just used available MATLAB functions to implement ANN. We can develop our own code to implement ANN in MATLAB.

 ANN

MATLAB CODE

%**************************************************************************

%                 Function Approximation Using NN

% ————————————————————————-

% By     : Sutrisno

% Contact: sutrisno_link@yahoo.com             Last update: March 10, 2011

%**************************************************************************

 

%**************************************************************************

% This program is developed to find approximation of a function.

% There are 5 basic steps ini this program:

% 1. Define the function (f(x))

% 2. Determine pairs of input (x) and output (y=f(x))

% 3. Create a network

% 4. Train the network (P=x,T=y)

% 5. Simulate the network (ynn= Sim(net,P))

% 6. Plot and compare the function and the network outputs.

%**************************************************************************

 

%**************************************************************************

%                   The Program Start Here

%*————————————————————————

function varargout = myann(varargin)

% MYANN M-file for myann.fig

% Begin initialization code – DO NOT EDIT

gui_Singleton = 1;

gui_State = struct(‘gui_Name’,       mfilename, …

‘gui_Singleton’,  gui_Singleton, …

‘gui_OpeningFcn’, @myann_OpeningFcn, …

‘gui_OutputFcn’,  @myann_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 myann is made visible.

function myann_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 myann (see VARARGIN)

 

% Choose default command line output for myann

handles.output = hObject;

 

% Update handles structure

guidata(hObject, handles);

 

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

% uiwait(handles.figure1);

 

 

% — Outputs from this function are returned to the command line.

function varargout = myann_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;

 

 

% — Executes on button press in PlotFunction.

%————————————————————————-

%    FUNCTION : Define the function (f(x)) that to be approximated

%————————————————————————-

function PlotFunction_Callback(hObject, eventdata, handles)

 

% constant

a=str2num(get(handles.edit1,’String’));

b=str2num(get(handles.edit2,’String’));

c=str2num(get(handles.edit4,’String’));

d=str2num(get(handles.edit3,’String’));

t=str2num(get(handles.edit5,’String’));

 

%  Polynomial

if (get(handles.radiobuttonpoly,’Value’)==1)

x=-2:0.1:2;

y=a*x.^3+b*x.^2+c*x+d;

end

 

% Sinusoidal

if (get(handles.radiobuttonsinus,’Value’)==1)

x = -2:(.4/t):2;

y = 1 + sin(t*pi*x/4);

end

 

% Random data

if (get(handles.radiobuttonld,’Value’)==1)

x = [-1:0.01:1];

y = sin(2*pi*x)+0.1*randn(size(x));

end

 

% Plot the function

plot(x,y,’o’,’color’,nnred), xlabel(‘x’),ylabel(‘f(x)’);

title(‘Function’);

 

% Update for global data

handles.x=x;

handles.y=y;

guidata(hObject,handles);

set(handles.text1,’String’,’Function plotting ok..’);

 

 

% — Executes on button press in pushbuttontrain.

%————————————————————————-

%                            TRAINING THE NETWORK

%————————————————————————-

function pushbuttontrain_Callback(hObject, eventdata, handles)

 

% Inputs training

P = handles.P;

T = handles.T;

net = handles.net;

% if use random data perform data dividing

if (get(handles.radiobuttonld,’Value’)==1)

[trainP,valP,testP,trainInd,valInd,testInd] = dividerand(P);

[trainT,valT,testT] = divideind(T,trainInd,valInd,testInd);

P = trainP;

T = trainT;

end

 

% Training

net = train(net,P,T);

 

% Update for global data

handles.net=net;

guidata(hObject,handles);

set(handles.text1,’String’,’Training ok..’);

 

% — Executes on button press in pushbuttonsimulate.

%————————————————————————-

%                          SIMULATION OF NETWORK

%————————————————————————-

function pushbuttonsimulate_Callback(hObject, eventdata, handles)

 

% Inputs for simulation

P = handles.P;

net = handles.net;

% if use random data perform data dividing

if (get(handles.radiobuttonld,’Value’)==1)

[trainP,valP,testP,trainInd,valInd,testInd] = dividerand(P);

P = testP;

end

 

% Simulate

ynn = sim(net,P);

 

% Plot the results

hold on

ann_plot =  plot(P,ynn),xlabel(‘input’),ylabel(‘target’);

title(‘Simulation Results’);

hold off

set(handles.text1,’String’,’Simulation ok..’);

 

% — Executes on button press in pushbuttoncreate.

%————————————————————————-

%                            CREATE NEW NETWORK

%————————————————————————-

function pushbuttoncreate_Callback(hObject, eventdata, handles)

%

P=handles.x;

T=handles.y;

 

S1 = str2num(get(handles.edit6,’String’));

traingd_show=50;

traingd_lr=str2double(get(handles.edit7,’String’));

traingd_epochs=str2num(get(handles.edit9,’String’));

traingd_goal=str2double(get(handles.edit8,’String’));

 

 

 

% Create the network object.

net = newff(P,T,S1);

% net.trainParam.show = traingd_show;

net.trainParam.lr = traingd_lr;

net.trainParam.epochs = traingd_epochs;

net.trainParam.goal = traingd_goal;

% net.trainParam.mc = 0.9;

 

handles.P=P;

handles.T=T;

handles.net=net;

guidata(hObject,handles);

set(handles.text1,’String’,’Creating new NN ok..’);

 

%————————————————————————-

%                            ENDING MAIN PROCEDURE

%————————————————————————-

 

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

function figure1_CreateFcn(hObject, eventdata, handles)

 

 

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 edit7_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of edit7 as a double

 

 

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

function edit7_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit7 (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 edit8_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of edit8 as a double

 

 

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

function edit8_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit8 (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 edit9_Callback(hObject, eventdata, handles)

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

%        str2double(get(hObject,’String’)) returns contents of edit9 as a double

 

 

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

function edit9_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit9 (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 edit1_Callback(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)

 

% Hints: get(hObject,’String’) returns contents of edit1 as text

%        str2double(get(hObject,’String’)) returns contents of edit1 as a double

 

 

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

function edit1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit1 (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 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 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 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

%**************************************************************************

%                   Ending of Program

%**************************************************************************

 

 

2 thoughts on “Function approximation using ANN ‎(with MATLAB GUI)

  1. % Basic code
    x = [-1:0.01:1];
    y = sin(2*pi*x)+0.1*randn(size(x)); % Function to be estimated, y=f(x)

    net = newff(x,y,5); % create ANN
    net = train(net,x,y); % training
    ynn = sim(net,x); % testing

    plot(x,ynn,x,y),xlabel(‘x’),ylabel(‘y’);
    title(‘Function approximation using ANN’);

  2. Pingback: Sutrisno W. Ibrahim

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s