MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# How can I solve this problem?

Asked by Parwaz Ali on 13 Nov 2013
Latest activity Commented on by Parwaz Ali on 14 Nov 2013

Why does the outcome of this code:

function Ks = nn_ks(v, f, t)

for i=1:27

v=[80;80;80;80;80;80;80;80;80;150;150;150;150;150;150;150;150;150;220;220;220;220;220;220;220;220;220;]

f=[0.05;0.05;0.05;0.1;0.1;0.1;0.15;0.15;0.15;0.05;0.05;0.05;0.1;0.1;0.1;0.15;0.15;0.15;0.05;0.05;0.05;0.1;0.1;0.1;0.15;0.15;0.15;]

t=[5;10;15;5;10;15;5;10;15;5;10;15;5;10;15;5;10;15;5;10;15;5;10;15;5;10;15;]

v(i,1)=(v(i,1)-80)./(220-80)

f(i,1)=(f(i,1)-0.05)./(0.15-0.05)

t(i,1)=(t(i,1)-5)./(15-5)

X=[v(i,1); f(i,1); t(i,1)]

W1 = [0.83953 1.416 4.3197;

0.70603 -4.1921 -0.30888;

-3.1168 2.7796 0.75788]

B1=[-3.1521;

-0.45802;

-2.9756]

W2 =[0.70956 1.1833 -0.059955]

B2 =[-0.98502];

Y1 = logsig(W1*X + B1*ones(1,size(X,2)));

Y2 = purelin(W2*Y1 + B2*ones(1,size(Y1,2)));

Ks(i,1) =Y2.'

end

is very different from the outcome (outputs) of NN Toolbox when the same weights, biases, learning functions and algorithms are used?

## Products

No products are associated with this question.

Answer by Greg Heath on 14 Nov 2013

% How can I solve this problem?

% Latest activity Commented on by Walter Roberson about 6 hours ago

% Why does the outcome of this code:

% function Ks = nn_ks(v, f, t)

% is very different from the outcome (outputs) of NN Toolbox when the same weights, biases, learning functions and algorithms are used?

Create column vectors w/o semicolons: columnvec = rowvec'

Avoid using the same variable name on both sides of an equation

Avoid unnecessary loops

` close all, clear all, clc`
``` V = [  80   80   80   80   80   80  80     80   80 ...
150 150 150 150 150 150 150  150 150 ...
220 220 220 220 220 220 220 220 220 ];```
``` F = [ 0.05 0.05 0.05 0.1 0.1 0.1 0.15 0.15 0.15 ...
0.05 0.05 0.05 0.1 0.1 0.1 0.15 0.15 0.15 ...
0.05 0.05 0.05 0.1 0.1 0.1 0.15 0.15 0.15 ] ;```
``` T = [ 5 10 15 5 10 15 5 10 15 ...
5 10 15 5 10 15 5 10 15 ...
5 10 15 5 10 15 5 10 15 ] ;```
``` X       = [ V; F ; T ];
[ I N ] = size(X)
maxX    = max(X')' * ones(1,N);
minX    = min(X')' * ones(1,N);
x       = ( X-minX)./(maxX-minX);```
` whos`
``` W1 = [ 0.83953   1.416    4.3197;
0.70603  -4.1921 -0.30888;
-3.1168     2.7796  0.75788 ];```
``` [ H I ] = size(W1)
b1      = [ -3.1521 -0.45802 -2.9756 ]'
B1      = b1*ones( 1,N);
W2      = [ 0.70956 1.1833 -0.059955 ];
[ O H ] = size(W2)
b2     =  -0.98502 ;```
``` h = logsig( W1*x + B1);
y = W2*h + b2;```

No learning functions or algorithms were used here

MATLAB uses [-1,1] scaling, not [0,1]

Hope this helps,

Thank you for formally accepting my answer

Greg

Answer by Walter Roberson on 13 Nov 2013

Most of the NN routines initialize weights randomly. It is also possible to set particular weights, but I do not recall how to do that (Greg Heath has shown how to do it more than once.)

Parwaz Ali on 13 Nov 2013

Thank you Walter! I sent him a link of my problem. Hopefully, he will answer, thank you again!

Walter Roberson on 13 Nov 2013

You could search... I would guess using the terms

```NN random weight contributor:"greg heath"
```

might find relevant posts.

Parwaz Ali on 14 Nov 2013

Hi Walther,

thank you for your nice suggestion!

All the best!

Answer by Parwaz Ali on 14 Nov 2013

Dear Mr. Gerg,

Thank you very much for your wonderful contribution. I could obtain the same results as those of nntool, using another code and your hint of [-1 1] data normalization.

I had no idea that MATLAB uses default scaling of [-1 1]. Thank you again!

All the best!