# how to efficiently simulate a sine function with neural networks toolbox?

20 views (last 30 days)
Darwin Brochero on 4 Jul 2012
Hello
I use matlab2010a. At this moment I'm trying to simulate a simple sinusoidal function with domain between 0 and 100 with neural networks toolbox, but the results are very poor, it seems that the network only learns of the initial and final data but I made sure to train and validate with data well distributed over the entire problem domain.
Surprisingly when I work with a function of a smaller domain (between 0 and 20) the results are very good, it seems a problem of scale or the number of training data. It is worth noting that I include pre-and post-processing of information with the "mapstd" function.
I would like to know if there is anything I can fit within the parameters of the network to avoid this problem or or is this just a lack of concentration because before I used the toolbox in much more complex problems with great success.
Here I attached the code to display results according to the upper boundary of the domain (20 or 100 for example).
Best Regards,
DB
%% code
clear,clc, close all
% upper boundary of the domain
n = 100;
%% Synthetic data
nnhl = 5;
x = 0:0.1:n;
y = sin(x)+0.1*rand(1,length(x));
%% Datasets configuration
Train_idx = 1:3:length(x);
Valid_idx = 2:3:length(x);
Test_idx = 3:3:length(x);
figure, plot(x,y,'-k'), hold on, plot(x(Train_idx),y(Train_idx),'.b'), hold on, plot(x(Valid_idx),y(Valid_idx),'.g'), hold on, plot(x(Test_idx), y(Test_idx),'.r'), legend('model','train','validation','test')
%% Neural Netwok configuration
TFi = {'tansig' 'purelin'}; BTF = 'trainlm';
BLF = 'learngdm'; PF = 'mse';
IPF = {'mapstd'}; OPF = {'mapstd'};
% net creation
net = newff(x, y,nnhl,TFi,BTF,BLF,PF,IPF,OPF);
% net.divideFcn = 'divideint';
net.divideFcn = 'divideind';
net.divideParam.trainInd = Train_idx;
net.divideParam.valInd = Valid_idx;
net.divideParam.testInd = Test_idx;
% network initialization
bas_stnet = init(net);
% NN train
NN_conf = train(bas_stnet,x,y);
% Model output
% Train
out.train = sim(NN_conf, x(:,Train_idx));
% Validation
out.val = sim(NN_conf, x(:,Valid_idx));
% Test
out.test = sim(NN_conf, x(:,Test_idx));
% figures
figure, subplot(3,1,1), plot(x(Train_idx),y(Train_idx),'.b'), hold on, plot(x(Train_idx),out.train,'.r'), title('train'),legend('target','neural net model') subplot(3,1,2), plot(x(Valid_idx),y(Valid_idx),'.b'), hold on, plot(x(Valid_idx),out.val,'.r'), title('validation'), legend('target','neural net model') subplot(3,1,3), plot(x(Test_idx), y(Test_idx),'.b'), hold on, plot(x(Test_idx),out.test,'.r'), title('test'), legend('target','neural net model') end
Greg Heath on 7 Jul 2012
Consider:
1. You can approximate a sine function over P periods using at least 8 evenly spaced points per period. However, 16 is much better. 2. How many linear combinations of shifted tanh functions does it take to approximate a sine wave over one period? 3. How many periods of sin(x) are in [0,20] ? What about [0,100] ? ================================================================== 1. What 2nd order difference equation does sin(x) satisfy? 2. Is it possible to simulate the solution of that equation using a feedback neural network?
Hope this helps.
Greg

Greg Heath on 7 Jul 2012
Consider:
1. You can approximate a sine function over P periods using at least 8 evenly spaced points per period. However, 16 is much better.
2. How many linear combinations of shifted tanh functions does it take to approximate a sine wave over one period?
3. How many periods of sin(x) are in [0,20] ? What about [0,100] ? ==================================================================
1. What 2nd order difference equation does sin(x) satisfy?
2. Is it possible to simulate the solution of that equation using a feedback neural network?
Hope this helps.
Greg
Darwin Brochero on 9 Jul 2012
Perfect, as I thought from the beginning, it was a bad problem formulation; excellent example to show the importance of sampling.
Thank you very much for your help