Error:index must be a positive integer or logical.

3 次查看(过去 30 天)
Anybody can help me whats wrong with my code please. I am constantly getting this error Attempted to access f(1.99); index must be a positive integer or logical.
Error in extreme (line 22) y2=f(x2);
  2 个评论
Shehzad Ahmed
Shehzad Ahmed 2017-4-16
Then please rectify my code (give it a range of -10,10) function extreme syms x; assume(x,'real') f=input('Enter function:','s'); fun=sym(f); df=diff(fun);
fprintf('\nDerivative of your function is:\n%s\n',char (df)) xmin=input('Enter lower limit of range of function;'); xmax=input('Enter upper limit of range of function;');
if (nargin<4) N=100; end dx=(xmax-xmin)/N; x2=xmin; y2=f(x2); y=[]; for i=1:N x1=x2; y1=y2; x2=xmin+i*dx; y2=f(x2); if (y1*y2<=0) y=fsolve(f,(x2*y1-x1*y2)/(y1-y2)); end end fprintf('\n%f',y)
r=input('\nHow many roots do you see above?'); n=r; disp ('Enter each point indivivually'); for i=1:n fprintf('\nInput point 0%d\n',i) r1=input(':'); y=r1; fprintf('\n Root%d=%f\n',i,y) end
navya sree
navya sree 2019-1-19
编辑:navya sree 2019-1-19
%back propogation network Program
clear all;
clc;
disp('Back Propogation Network');
v=[0.7 -0.4;-0.2 0.3];
x=[0 1];
t=[1];
w=[0.5;0.1];
t1=0;
wb=-0.3;
vb=[0.4 0.6];
alpha=0.25
e=1;
temp1=0;
while (e<=3)
e
for i=1:2
for j=1:2
temp1=temp1+(v(j,i)*x(j));
end
zin(i)=temp1+vb(i);
temp1=e (-zin(i));
fz(i)=(1/(1+temp1));
z(i)=fz(i);
fdz(i)=fz(i)*(1-fz(i));
temp1=0;
end
for k=1
for j=1:2
temp1=temp1+z(j)*w(j,k);
end
yin(k)=temp1+wb(k);
fy(k)=(1/(1+(e -yin(k))));
y(k)=fy(k);
temp1=0;
end
for k=1
fdy(k)=fy(k)*(1-fy(k));
delk(k)=(t(k)-y(k))*fdy(k);
end
for k=1
for j=1:2
dw(j,k)=alpha*delk(k)*z(j);
end
dwb(k)=alpha*delk(k);
end
for j=1:2
for k=1
delin(j)=delk(k)*w(j,k);
end
delj(j)=delin(j)*fdz(j);
end
for i=1:2
for j=1:2
dv(i,j)=alpha*delj(j)*x(i);
end
dvb(i)=alpha*delj(i);
end
for k=1
for j=1:2
w(j,k)=w(j,k)+dw(j,k);
end
wb(k)=wb(k)+dwb(k);
end
w,wb
for i=1:2
for j=1:2
v(i,j)=v(i,j)+dv(i,j);
end
vb(i)=vb(i)+dvb(i);
end
v,vb
te(e)=e;
e=e+1;
end
i am getting this error please help me out in solving thank you in advance.
Attempted to access e(-0.2); index must be a positive integer or logical.
Error in backpropogation (line 22)
temp1=e (-zin(i));
%Kohonen Self organizing feature maps Program
clear all;
clc;
disp('Kohonen Self organizing feature maps');
disp('The input patterns are');
x=[1 1 0 0; 0 0 0 1; 1 0 0 0;0 0 1 1]
t=1;
alpha(t)=0.6;
e=1;
disp('Since we have 4 input pattern and cluster unit to be formed is 2, the weight matrix is');
w=[0.2 0.8; 0.6 0.4; 0.5 0.7; 0.9 0.3];
disp('the learning rate of this epoch is');
alpha
while(e<=3)
i=1;
j=1;
k=1;
m=1;
disp('Epoch =');
e
while(i<=4)
for j=1:2
temp=0;
for k=1:4
temp= temp + ((w(k,j)-x(i,k) 2);
end
D(j)=temp
end
if(D(1)<D(2))
J=1;
else
J=2;
end
disp('The winning unit is');
J
disp('Weight updation');
for m=1:4
w(m,J)=w(m,J) + (alpha(e) * (x(i,m)-w(m,J)));
end
w
i=i+1;
end
temp=alpha(e);
e=e+1;
alpha(e)=(0.5*temp);
disp('First Epoch completed');
disp('Learning rate updated for second epoch');
alpha(e)
end
i am getting below error
Error: File: kohonenself.m Line: 24 Column: 42
Unexpected MATLAB expression.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2017-4-16
编辑:John D'Errico 2017-4-16
You have created (somewhere) a variable f. f is a scalar, a vector or an array.
Where is the 1.99'th element of that array? When did you create that element? The fact is, you cannot stuff something into the 1.99'th place in memory. There is no such place. Yet you have tried to access that memory element.
Since this is clearly impossible, MATLAB returns an error. It told you that your index must be a positive integer, or a logical (boolean) vector. However x2 is apparently the floating point number 1.99. How it got to be that is by your own design, since it was created by your coding.
If you are constantly getting that error, then you need not to try to index variables with real numbers. Perhaps you think of f as a function. It is not. It is a variable, at least as you have created it.
  2 个评论
Shehzad Ahmed
Shehzad Ahmed 2017-4-16
function extreme syms x; assume(x,'real') f=input('Enter function:','s'); fun=sym(f); df=diff(fun);
fprintf('\nDerivative of your function is:\n%s\n',char (df)) xmin=input('Enter lower limit of range of function;'); xmax=input('Enter upper limit of range of function;');
if (nargin<4) N=100; end dx=(xmax-xmin)/N; x2=xmin; y2=f(x2); y=[]; for i=1:N x1=x2; y1=y2; x2=xmin+i*dx; y2=f(x2); if (y1*y2<=0) y=fsolve(f,(x2*y1-x1*y2)/(y1-y2)); end end fprintf('\n%f',y)
r=input('\nHow many roots do you see above?'); n=r; disp ('Enter each point indivivually'); for i=1:n fprintf('\nInput point 0%d\n',i) r1=input(':'); y=r1; fprintf('\n Root%d=%f\n',i,y) end
Then please rectify my code (give it a range -10,10)
John D'Errico
John D'Errico 2017-4-16
You may THINK of the function as entered as a function. It is NOT so! f is just a string of characters.
Numbers and characters mean NOTHING unless you attribute some meaning to them.
f=input('Enter function:','s')
Enter function:s^2 + 2
f =
s^2 + 2
>> f(1.99)
Subscript indices must either be real positive integers or logicals.
As you can see, it generates the same error that you got.
You can use matlabfunction to convert a symbolic expression (like that in fun) into a function handle, something that one can evaluate.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numbers and Precision 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by