neural network code explanation - matlab

The following is an implementation of a simple Perceptron supplied in a blog.
input = [0 0; 0 1; 1 0; 1 1];
numIn = 4;
desired_out = [0;1;1;1];
bias = -1;
coeff = 0.7;
rand('state',sum(100*clock));
weights = -1*2.*rand(3,1);
iterations = 10;
for i = 1:iterations
out = zeros(4,1);
for j = 1:numIn
y = bias*weights(1,1)+...
input(j,1)*weights(2,1)+input(j,2)*weights(3,1);
out(j) = 1/(1+exp(-y));
delta = desired_out(j)-out(j);
weights(1,1) = weights(1,1)+coeff*bias*delta;
weights(2,1) = weights(2,1)+coeff*input(j,1)*delta;
weights(3,1) = weights(3,1)+coeff*input(j,2)*delta;
end
end
I have the following questions,
(1) which one is training data here?
(2) which one is test data here?
(3) which are the labels here?

training data is [0 0; 0 1; 1 0; 1 1] in the other view every row is one set of training data as follow
>> input
input =
0 0
0 1
1 0
1 1
and target is
desired_out =
0
1
1
1
please think about desired_out this is your labels .. every row in training data(input) have a specific output(label) in binary set{0,1}(because this example for implementation of OR logic circuit.
in matlab you can use or function as below for further understanding:
>> or(0,0)
ans =
0
>> or(1,0)
ans =
1
>> or(0,1)
ans =
1
>> or(1,1)
ans =
1
Note that your code has not any training test and this code just trying to get weights and other parameters of perceptron but you can add training test to your code by just little program
NumDataTest = 10 ;
test=randi( [0 , 1] , [ NumDataTest , 2]) ...
+(2*rand(NumDataTest,2)-1)/20;
so test data will be similar to below
test =
1.0048 1.0197
0.0417 0.9864
-0.0180 1.0358
1.0052 1.0168
1.0463 0.9881
0.9787 0.0367
0.9624 -0.0239
0.0065 0.0404
1.0085 -0.0109
-0.0264 0.0429
for test this data you can use your own program by below code:
for i=1:NumDataTest
y = bias*weights(1,1)+test(i,1)*weights(2,1)+test(i,2)*weights(3,1);
out(i) = 1/(1+exp(-y));
end
and finally:
table(test(:,1),test(:,2),out,'VariableNames',{'input1' 'input2' 'output'})
output is
input1 input2 output
_________ _________ ________
1.0048 1.0197 0.99994
0.041677 0.98637 0.97668
-0.017968 1.0358 0.97527
1.0052 1.0168 0.99994
1.0463 0.98814 0.99995
0.97875 0.036674 0.9741
0.96238 -0.023861 0.95926
0.0064527 0.040392 0.095577
1.0085 -0.010895 0.97118
-0.026367 0.042854 0.080808
Code section:
clc
clear
input = [0 0; 0 1; 1 0; 1 1];
numIn = 4;
desired_out = [0;1;1;1];
bias = -1;
coeff = 0.7;
rand('state',sum(100*clock));
weights = -1*2.*rand(3,1);
iterations = 100;
for i = 1:iterations
out = zeros(4,1);
for j = 1:numIn
y = bias*weights(1,1)+input(j,1)*weights(2,1)+input(j,2)*weights (3,1);
out(j) = 1/(1+exp(-y));
delta = desired_out(j)-out(j);
weights(1,1) = weights(1,1)+coeff*bias*delta;
weights(2,1) = weights(2,1)+coeff*input(j,1)*delta;
weights(3,1) = weights(3,1)+coeff*input(j,2)*delta;
end
end
%% Test Section
NumDataTest = 10 ;
test=randi( [0 , 1] , [ NumDataTest , 2]) ...
+(2*rand(NumDataTest,2)-1)/20;
for i=1:NumDataTest
y = bias*weights(1,1)+test(i,1)*weights(2,1)+test(i,2)*weights(3,1);
out(i) = 1/(1+exp(-y));
end
table(test(:,1),test(:,2),out,'VariableNames',{'input1' 'input2' 'output'})
I hope this helps and sorry for my English if it's bad

Related

Is there a way to derive implict ODE function from big symbolic expression?

I'm Davide and I have a problem with the derivation of a function that later should be given to ode15i in Matlab.
Basically I've derived a big symbolic expression that describe the motion of a spececraft with a flexible appendice (like a solar panel). My goal is to obtain a function handle that can be integrated using the built-in implicit solver in Matlab (ode15i).
The problem I've encounter is the slowness of the Symbolic computations, especially in the function "daeFunction" (I've run it and lost any hope for a responce after 3/4 hours had passed).
The system of equations, that is derived using the Lagrange's method is an implicit ode.
The complex nature of the system arise from the flexibility modelling of the solar panel.
I am open to any suggestions that would help me in:
running the code properly.
running it as efficiently as possible.
Thx in advance.
Here after I copy the code. Note: Matlab r2021a was used.
close all
clear
clc
syms t
syms r(t) [3 1]
syms angle(t) [3 1]
syms delta(t)
syms beta(t) [3 1]
mu = 3.986e14;
mc = 1600;
mi = 10;
k = 10;
kt = 10;
Ii = [1 0 0 % for the first link it is different thus I should do a functoin or something that writes everything into an array or a vector
0 5 0
0 0 5];
% Dimension of satellite
a = 1;
b = 1.3;
c = 1;
Ic = 1/12*mc* [b^2+c^2 0 0
0 c^2+a^2 0
0 0 a^2+b^2];
ra_c = [0 1 0]';
a = diff(r,t,t);
ddelta = diff(delta,t);
dbeta = diff(beta,t);
dddelta = diff(delta,t,t);
ddbeta = diff(beta,t,t);
R= [cos(angle1).*cos(angle3)-cos(angle2).*sin(angle1).*sin(angle3) sin(angle1).*cos(angle3)+cos(angle2).*cos(angle1).*sin(angle3) sin(angle2).*sin(angle3)
-cos(angle1).*sin(angle3)-cos(angle2).*sin(angle1).*cos(angle3) -sin(angle1).*sin(angle3)+cos(angle2).*cos(angle1).*cos(angle3) sin(angle2).*cos(angle3)
sin(angle2).*sin(angle3) -sin(angle2).*cos(angle1) cos(angle2)];
d_angle1 = diff(angle1,t);
d_angle2 = diff(angle2,t);
d_angle3 = diff(angle3,t);
dd_angle1 = diff(angle1,t,t);
dd_angle2 = diff(angle2,t,t);
dd_angle3 = diff(angle3,t,t);
d_angle = [d_angle1;d_angle2;d_angle3];
dd_angle = [dd_angle1;dd_angle2;dd_angle3];
omega = [d_angle2.*cos(angle1)+d_angle3.*sin(angle2).*sin(angle1);d_angle2.*sin(angle1)-d_angle3.*sin(angle2).*cos(angle1);d_angle1+d_angle3.*cos(angle2)]; % this should describe correctly omega_oc
d_omega = diff(omega,t);
v1 = diff(r1,t);
v2 = diff(r2,t);
v3 = diff(r3,t);
v = [v1; v2; v3];
[J,r_cgi,R_ci]= Jacobian_Rob(4,delta,beta);
% Perform matrix multiplication
for mm = 1:4
vel(:,mm) = J(:,:,mm)*[ddelta;dbeta];
end
vel = formula(vel);
dr_Ccgi = vel(1:3,:);
omega_ci = vel(4:6,:);
assumeAlso(angle(t),'real');
assumeAlso(d_angle(t),'real');
assumeAlso(dd_angle(t),'real');
assumeAlso(r(t),'real');
assumeAlso(a(t),'real');
assumeAlso(v(t),'real');
assumeAlso(beta(t),'real');
assumeAlso(delta(t),'real');
assumeAlso(dbeta(t),'real');
assumeAlso(ddelta(t),'real');
assumeAlso(ddbeta(t),'real');
assumeAlso(dddelta(t),'real');
omega = formula(omega);
Tc = 1/2*v'*mc*v+1/2*omega'*R*Ic*R'*omega;
% kinetic energy of all appendices
for h = 1:4
Ti(h) = 1/2*v'*mi*v+mi*v'*skew(omega)*R*ra_c+mi*v'*skew(omega)*R*r_cgi(:,h)+mi*v'*R*dr_Ccgi(:,h)+1/2*mi*ra_c'*R'*skew(omega)'*skew(omega)*R*ra_c ...
+ mi*ra_c'*R'*skew(omega)'*skew(omega)*R*r_cgi(:,h)+mi*ra_c'*R'*skew(omega)'*R*dr_Ccgi(:,h)+1/2*omega'*R*R_ci(:,:,h)*Ii*(R*R_ci(:,:,h))'*omega ...
+ omega'*R*R_ci(:,:,h)*Ii*R_ci(:,:,h)'*omega_ci(:,h)+1/2*omega_ci(:,h)'*R_ci(:,:,h)*Ii*R_ci(:,:,h)'*omega_ci(:,h)+1/2*mi*r_cgi(:,h)'*R'*skew(omega)'*skew(omega)*R*r_cgi(:,h)+mi*r_cgi(:,h)'*R'*skew(omega)'*R*dr_Ccgi(:,h)...
+ 1/2*mi*dr_Ccgi(:,h)'*dr_Ccgi(:,h);
Ugi(h) = -mu*mi/norm(r,2)+mu*mi*r'/(norm(r,2)^3)*(R*ra_c+R*R_ci(:,:,h)*r_cgi(:,h));
end
Ugc = -mu*mc/norm(r,2);
Ue = 1/2*kt*(delta)^2+sum(1/2*k*(beta).^2);
U = Ugc+sum(Ugi)+Ue;
L = Tc + sum(Ti) - U;
D = 1/2 *100* (ddelta^2+sum(dbeta.^2));
%% Equation of motion derivation
eq = [diff(jacobian(L,v),t)'-jacobian(L,r)';
diff(jacobian(L,d_angle),t)'-jacobian(L,angle)';
diff(jacobian(L,ddelta),t)'-jacobian(L,delta)'+jacobian(D,ddelta)';
diff(jacobian(L,dbeta),t)'-jacobian(L,beta)'+jacobian(D,dbeta)'];
%% Reduction to first order sys
[sys,newVars,R1]=reduceDifferentialOrder(eq,[r(t); angle(t); delta(t); beta(t)]);
DAEs = sys;
DAEvars = newVars;
%% ode15i implicit solver
pDAEs = symvar(DAEs);
pDAEvars = symvar(DAEvars);
extraParams = setdiff(pDAEs,pDAEvars);
f = daeFunction(DAEs,DAEvars,'File','ProvaSum');
y0est = [6778e3 0 0 0.01 0.1 0.3 0 0.12 0 0 0 7400 0 0 0 0 0 0 0 0]';
yp0est = zeros(20,1);
opt = odeset('RelTol', 10.0^(-7),'AbsTol',10.0^(-7),'Stats', 'on');
[y0,yp0] = decic(f,0,y0est,[],yp0est,[],opt);
% Integration
[tSol,ySol] = ode15i(f,[0 0.5],y0,yp0,opt);
%% Funcitons
function [J,p_cgi,R_ci]=Jacobian_Rob(N,delta,beta)
% Function to compute Jacobian see Robotics by Siciliano
% N total number of links
% delta [1x1] beta [N-1x1] variable that describe position of the solar
% panel elements
beta = formula(beta);
L_link = [1 1 1 1]'; % Length of each link elements in [m], later to be derived from file or as function input
for I = 1 : N
A1 = Homog_Matrix(I,delta,beta);
A1 = formula(A1);
R_ci(:,:,I) = A1(1:3,1:3);
if I ~= 1
p_cgi(:,I) = A1(1:3,4) + A1(1:3,1:3)*[1 0 0]'*L_link(I)/2;
else
p_cgi(:,I) = A1(1:3,4) + A1(1:3,1:3)*[0 0 1]'*L_link(I)/2;
end
for j = 1:I
A_j = formula(Homog_Matrix(j,delta,beta));
z_j = A_j(1:3,3);
Jp(:,j) = skew(z_j)*(p_cgi(:,I)-A_j(1:3,4));
Jo(:,j) = z_j;
end
if N-I > 0
Jp(:,I+1:N) = zeros(3,N-I);
Jo(:,I+1:N) = zeros(3,N-I);
end
J(:,:,I)= [Jp;Jo];
end
J = formula(J);
p_cgi = formula(p_cgi);
R_ci = formula(R_ci);
end
function [A_CJ]=Homog_Matrix(J,delta,beta)
% This function is made sopecifically for the solar panel
% define basic rotation matrices
Rx = #(angle) [1 0 0
0 cos(angle) -sin(angle)
0 sin(angle) cos(angle)];
Ry = #(angle) [ cos(angle) 0 sin(angle)
0 1 0
-sin(angle) 0 cos(angle)];
Rz = #(angle) [cos(angle) -sin(angle) 0
sin(angle) cos(angle) 0
0 0 1];
if isa(beta,"sym")
beta = formula(beta);
end
L_link = [1 1 1 1]'; % Length of each link elements in [m], later to be derived from file or as function input
% Rotation matrix how C sees B
R_CB = Rz(-pi/2)*Ry(-pi/2); % Clarify notation: R_CB represent the rotation matrix that describe the frame B how it is seen by C
% it is the same if it was wrtitten R_B2C
% becouse bring a vector written in B to C
% frame --> p_C = R_CB p_B
% same convention used in siciliano how C sees B frame
A_AB = [R_CB zeros(3,1)
zeros(1,3) 1];
A_B1 = [Rz(delta) zeros(3,1)
zeros(1,3) 1];
A_12 = [Ry(-pi/2)*Rx(-pi/2)*Rz(beta(1)) L_link(1)*[0 0 1]'
zeros(1,3) 1];
if J == 1
A_CJ = A_AB*A_B1;
elseif J == 0
A_CJ = A_AB;
else
A_CJ = A_AB*A_B1*A_12;
end
for j = 3:J
A_Jm1J = [Rz(beta(j-1)) L_link(j-1)*[1 0 0]'
zeros(1,3) 1];
A_CJ = A_CJ*A_Jm1J;
end
end
function [S]=skew(r)
S = [ 0 -r(3) r(2); r(3) 0 -r(1); -r(2) r(1) 0];
end
I found your question beautiful. My suggestion is to manipulate the problem numerically. symbolic manipulation in Matlab is good but is much slower than numerical calculation. you can define easily the ode into a system of first-order odes and solve them using numerical integration functions like ode45. Your code is very lengthy and I couldn't manage to follow its details.
All the Best.
Yasien

Addition of two integer in binary representation

I would like to implement code which will add two integers by their binary representation.
For instance:
a = [1 1 1 0];
and
b = [1 0 1 1];
So I implemented the following algorithm:
function s=add_binary(a,b)
m=length(a);
s=zeros(m+1,0);
n=length(a);
c=0;
for ii=n:-1:1
d=floor((a(ii)+b(ii)+c)/2);
s(ii)=a(ii)+b(ii)+c-2*d;
c=d;
end
s(1)=c;
end
However, it returns this as a result:
s=add_binary(a,b)
s =
1 0 0 1
Here, there should be an additional 1 on the left, such that it would read:
1 1 0 0 1
Where am I making a mistake?
Since you're dealing with binary numbers, why not use logical operators?
function s = add_binary(a, b)
a = [0 a];
b = [0 b];
c = a&b;
while any(c)
b = xor(a, b);
a = circshift(c, -1);
c = a&b;
end
s = a+b;
end
And a test:
>> a = randi([0 1], [1 6])
a =
0 1 0 1 0 1
>> b = randi([0 1], [1 6])
b =
1 1 0 0 0 1
>> s = add_binary(a, b)
s =
1 0 0 0 1 1 0
here is my solution
function s=add_binary(a,b)
m=length(a);
s=zeros(m+1,1);
n=length(a);
c=0;
for ii=n:-1:1
d=floor((a(ii)+b(ii)+c)/2);
s(ii+1)=a(ii)+b(ii)+c-2*d;
c=d;
end
s(1)=c;
s=s';
end
>> s=add_binary(a,b)
s =
1 1 0 0 1
This problem was discussed in a 2011 question on MATLAB Answers. I'd like to mention 2 more solutions:
If you have the Communications System Toolbox, you can use bi2de and de2bi:
de2bi(bi2de(a,'left-msb') + bi2de(b,'left-msb'),'left-msb')
If the amount of "digits" in the input is known (and reasonable), one solution could be to store all possible input combinations in some data-structure, which would facilitate rapid access. One such example is the MapN class (though it can just as easily be done with regular numeric arrays). I will provide a small example to show this idea (including a benchmark):
function allResults = q43095156(allResults)
if nargin == 0
% Let's store all combinations of 8 binary digits:
in = uint8(0:255);
out = uint16(in)+uint16(in.');
outB = logical(de2bi(out(:),'left-msb'));
[in1,in2] = meshgrid(in,in);
allResults = MapN(...
num2cell([num2cell(in1(:),2),num2cell(in2(:),2)],2),...
num2cell(outB,2));
end
benchmark(allResults);
end
function benchmark(allResults)
rng(43095156);
a = logical(randi([0 1],10,8,'uint8'));
b = logical(randi([0 1],10,8,'uint8'));
% Test:
R{5} = de2bi(bi2de(a,'left-msb') + bi2de(b,'left-msb'),'left-msb');
R{4} = add_a_bunch_gnovice(a,b);
R{3} = add_a_bunch_Sardar(a,b);
R{2} = add_a_bunch_dato(a,b);
R{1} = getFromMap(a, b, allResults);
assert(isequal(R{:}));
% Benchmark:
a = logical(randi([0 1],1000,8,'uint8'));
b = logical(randi([0 1],1000,8,'uint8'));
fprintf(1,'\nSardar''s method:\t%f',timeit(#() add_a_bunch_Sardar(a, b) ));
fprintf(1,'\nDev-iL''s method:\t%f',timeit(#() getFromMap(a, b, allResults) ));
fprintf(1,'\ngnovice''s method:\t%f',timeit(#() add_a_bunch_gnovice(a, b) ));
fprintf(1,'\ndato''s method:\t\t%f',timeit(#() add_a_bunch_dato(a, b) ));
fprintf(1,'\nbi2de method:\t\t%f',timeit(#() de2bi(bi2de(a,'left-msb') + bi2de(b,'left-msb'),'left-msb')));
end
function out = getFromMap(a,b,map)
out = cell2mat(values(map, num2cell([ num2cell(bi2de(a,'left-msb'),2),...
num2cell(bi2de(b,'left-msb'),2)],2)));
end
function out = add_a_bunch_gnovice(a,b)
out = zeros(size(a)+[0,1],'logical');
for ind1 = 1:size(a,1)
out(ind1,:) = add_binary_gnovice(a(ind1,:), b(ind1,:));
end
end
function out = add_a_bunch_Sardar(a,b)
out = zeros(size(a)+[0,1],'logical');
for ind1 = 1:size(a,1)
out(ind1,:) = add_binary_Sardar(a(ind1,:), b(ind1,:));
end
end
function out = add_a_bunch_dato(a,b)
out = zeros(size(a)+[0,1],'logical');
for ind1 = 1:size(a,1)
out(ind1,:) = add_binary_dato(a(ind1,:), b(ind1,:));
end
end
function s = add_binary_gnovice(a, b)
a = [0 a];
b = [0 b];
c = a&b;
while any(c)
b = xor(a, b);
a = circshift(c, -1);
c = a&b;
end
s = a+b;
end
function s = add_binary_Sardar(a,b)
s = logical(str2double(num2cell(dec2bin(bin2dec(num2str(a)) +...
bin2dec(num2str(b)), numel(a)+1))));
end
function s = add_binary_dato(a,b)
m = length(a);
s = zeros(m+1,1);
n = length(a);
c = 0;
for ii = n:-1:1
d = floor((a(ii)+b(ii)+c)/2);
s(ii+1) = a(ii)+b(ii)+c-2*d;
c = d;
end
s(1) = c;
s = logical(s.');
end
And the results with my x64 MATLAB R2017a # Win10 are:
Sardar's method: 0.336414
Dev-iL's method: 0.061656
gnovice's method: 0.022031
dato's method: 0.002123
bi2de method: 0.000356
So this should give us an idea which methods are faster (unless I messed the wrapper functions up greatly)...
The advantage of the MapN method would be noticeable if the computation of the sum was some other, costlier operation.
Use num2str to convert a and b to strings and use bin2dec to convert them to decimal. Then add them and convert the sum back to binary using dec2bin. Use num2cell to split the string and finally use str2double to get the desired result.
s=str2double(num2cell(dec2bin(bin2dec(num2str(a))+bin2dec(num2‌​str(b)))))
% Output for given a and b
%---------------------------
% s =
% 1 1 0 0 1

MatLab - Gaussian elimination with scaled row pivoting

I am trying to write a function which performs Gaussian elimination with scaled row pivoting. I almost have it right, but my answer is not quite correct, so something must be wrong in my code. I have written:
function [B,h] = factorization(A)
n = length(A);
p = zeros(1,n);
s = zeros(1,n);
for i = 1:n
p(i) = i;
s(i) = max(abs(A(i,1:n)));
end
for k = 1:(n-1)
m = abs(A(k:n,k));
q = length(m);
v = zeros(1,q);
w = s(k:n);
for j = 1:q
v(j) = m(j)/(w(j));
end
[pivot,pivot] = max(abs(v(1:end)));
if pivot ~= 1
var = p(k);
p(k) = p(pivot);
p(pivot) = var;
end
for i = (k+1):n
z = A(p(i),k)/A(p(k),k);
A(p(i),k) = z;
for j = (k+1):n
A(p(i),j) = A(p(i),j) - z*A(p(k),j);
end
end
end
B = A;
h = p;
Say then that I use the matrix A = [2 3 -6; 1 -6 8; 3 -2 1] as input. My code gives me the output: B = [0.6667 -0.8125 -0.4375; 0.3333 -5.3333 7.6667; 3 -2 1], h = [3 2 1]. The correct answer, however, should be: B = [0.0007 4.3333 -6.6667; 0.3333 -1.2308 -0.5385; 3 -2 1], h = [3 1 2]
I can't see where in the code I'm doing wrong, so if anyone could help me out, I would be very grateful!

Neural Network in matlab: How to specify input weights

I need help rectifying this code to implement XOR using Neural Network in matlab. But, I am unable to set the input weights from the input layer to the first layer. The network has input layer, hidden layer and output layer of 2,2 and 1 neurons respectively.
Can somebody help me with this?
net=network;
net.numInputs = 1;
net.inputs{1}.size = 2;
net.numLayers = 2;
net.layers{1}.size = 2;
net.layers{2}.size = 1;
net.inputConnect(1) = 1;
net.layerConnect(2, 1) = 1;
net.outputConnect(2) = 1;
net.targetConnect(2) = 1;
net.layers{1}.transferFcn = 'logsig';%>> net.layers{2}.transferFcn = 'purelin';
net.layers{2}.transferFcn = 'logsig';
net.biasConnect = [ 1 ; 1];
net.layers{1}.initFcn = 'initwb';
net.layers{2}.initFcn = 'initwb';
net.inputWeights={1 1;1 1};%ask this. error is not explanatory. probably syntax.
net.biases{1}={-1.5 -0.5};
net.biases{2}=-0.5;
net.layerWeights{2,1}={-2 1};
P=[0 1 0 1;0 0 1 1];
T=[0 1 1 0];
net.initFcn = 'initlay';
net = init(net);
net.adaptFcn = 'adaptwb';
net.inputWeights{1,1}.learnFcn = 'learnp';
net.biases{1}.learnFcn = 'learnp';
net.adaptParam.passes =3;
net.performFcn = 'mse';
y = sim(net,P)
doc network tells me:
If net.inputConnect(i,j) is 1, then net.inputWeights{i,j} is a structure
defining the weight to layer i from input j.
So instead of setting a cell array for the net.inputWeights you should be setting the elements of net.inputWeights for each combination of input and first-layer nodes like this:
net.inputWeights{1,1} = weight11; % input1 node 1
net.inputWeigtts{1,2} = weight12; % input1 node 2
...

how to repeat function with for loop?

I am trying to generate a pn sequence and it works. However, when I try I call the function with different inputs in a for-loop, it gives me the same results each time. As if it is not affected by using the for loop. Why?
This is my code:
%e.g. noof flip flops 4 ==>
function[op_seq]=pnseq(a,b,c)
a = 7;
%generator polynomial x4+x+1 ==>
b = [1 0 0 1 1 0 1 ]
%initial state [1 0 0 0] ==>
c = [1 0 0 0 1 0 1 ]
%refere figure to set a relation between tap function and initial state
%
for j= 1:50,
x = a;
tap_ff =b;
int_stat= c;
for i = 1:1: length(int_stat)
old_stat(i) = int_stat(i);
gen_pol(i) = tap_ff(i);
end
len = (2 ^x)-1;
gen_pol(i+1)= 1;
gen_l = length(gen_pol);
old_l = length(old_stat);
for i1 = 1: 1:len
% feed back input genration
t = 1;
for i2 = 1:old_l
if gen_pol(i2)==1
stat_str(t) = old_stat(gen_l - i2);
i2 = i2+1;
t = t+1;
else
i2 = i2+1;
end
end
stat_l = length(stat_str);
feed_ip = stat_str(1);
for i3 = 1: stat_l-1
feed_ip = bitxor(feed_ip,stat_str(i3 + 1));
feed_ipmag(i1) = feed_ip;
i3 = i3+1;
end
% shifting elements
new_stat = feed_ip;
for i4 = 1:1:old_l
new_stat(i4+1) = old_stat(i4);
old_stat(i4)= new_stat(i4);
end
op_seq(i1) = new_stat(old_l +1);
end
%op_seq;
end
I assume you're doing something like:
for n = 1:10
...
% set a,b,c for this n
...
op_seq =pnseq(a,b,c)
...
end
and that you see the same op_seq output for each case. This is because you have a,b,c as inputs, but you overwrite them at the start of your function. If I remove, or comment out the following lines in your function:
a = 7;
b = [1 0 0 1 1 0 1 ]
c = [1 0 0 0 1 0 1 ]
Then I get different results for calling the function with different a,b,c. There is nothing random in your function, so the same inputs give the same outputs.