MATLAB: Actually, this code fails with an error: Error using zeros Size inputs must be integers. on this line: p=zeros(1,n); The reason is obvious because n is 2.1782 (approximately)

all

Actually, this code fails with an error: Error using zeros Size inputs must be integers. on this line: p=zeros(1,n); The reason is obvious because n is 2.1782 (approximately). to fixed how
h=0.01;
t0=0;
t1=2.2;
n=(t1-t0)/(h+1);
p=zeros(1,n);
T1=zeros(1,n);
x=t0:h:t1;
for i=1:n
t=(i-1)*h;
p(i)=sin(2.5*t.^2-5.*t);
T1(i)=(1/3).*sin(2.5*t.^2-5.*t+4.*pi);
end
P=con2seq(p);
T=con2seq(T1);
net=newlin([-1,1],1,0,0.1);
net.inputweights{1,1}.delays=[1,2,3,4,5];
net.inputweights{1,1}.initFcn='rands';
net.biases{1}.initFcn='rands';
net=init(net);
IW=net.IW{1,1};
b=net.b{1};
net.adaptParam.passes=50;
[net,y,E,pf,af]=adapt(net,P(6:n),T(6:n),P(1:5));
display(sqrt(mse(E)))
view(net)
figure
hold on;
plot(x,cell2mat(T),'b',x(6:end),cell2mat(y),'r'),grid
legend('Reference','output')
hold off
figure
hold on
plot(x(6:end),cell2mat(E),'r'),grid
legend('error')
hold off

Best Answer

Why not do it like most MATLABers would do it:
h = 0.01;
t0 = 0;
t1 = 2.2;
n = 221;
p = zeros(1,n);
T1 = zeros(1,n);
x = linspace(t0, t1, n);
Related Question