fractional-order,Index exceeds the number of array elements (1)

Hi
Good time
I wrote this code but it gives an error
Please help me
thank you
%%
n=100;
u1=[0,0]';
X1=[-4,-2,0]';
a=0.9;
h=0.9;
cp1=1;cp2=1;cp3=1;
for j=1:n
c1(j)=(1-(1+a)/j)*cp1;
c2(j)=(1-(1+a)/j)*cp2;
c3(j)=(1-(1+a)/j)*cp3;
cp1=c1(j); cp2=c2(j); cp3=c3(j);
end
% initial conditions setting:
v1(1)=u1(1);
w1(1)=u1(2);
x1(1)=X1(1); y1(1)=X1(2); z1(1)=X1(3);
% calculation of phase portraits /numerical solution/:
for i=2:n
x1(i)=h*cos(z1(i-1))*v1(i-1) - memo(x1, c1, i);
y1(i)=h*sin(z1(i-1))*v1(i-1)-memo(y1, c2, i);
z1(i)=h*w1(i-1)-memo(z1, c3, i) ;
end
%%
function [yo] = memo(r, c, k)
%
temp = 0;
for j=1:k-1
temp = temp + c(j)*r(k-j);
end
yo = temp;
%
%%%%% error
Index exceeds the number of array elements (1).
Error in exocstrstateFcnCT1 (line 28)
x1(i)=h*cos(z1(i-1))*v1(i-1) - memo(x1, c1, i);

Risposte (1)

You only initialize
v1(1)=u1(1);
and you do not assign to any other entry in v1, so when i becomes 3, v1(i-1) becomes v1(2) which does not exist.

11 Commenti

What should I change in the code?Please
I have no idea; you have not shown your code.
However, I was dealing with exactly the same type of problem for someone else at very nearly the same time, including the same variable name v1, so it just might be the case that you are working on the same problem. So on that slight chance, see https://www.mathworks.com/matlabcentral/answers/700617-index-exceeds-the-number-of-array-elements-1#answer_582392
I have sent the complete code
When I call this as a function shows the following problem
Output argument "Z1" (and maybe others) not assigned during call to "exocstrstateFcnCT1".
I was able to change the code with the following definition, but an error occurred while reading the error I wrote above
I changed the code as follows
v1=zreos(1,n)
w1=zreos(1,n)
As Walter also pointed out, v1 is scalar not a vector. Now that you've changed it to a vector (you misspelled zeros though), your snippet works (whether the output fits your expectations is another question).
You need to share your code if you seek for help; what is exocstrstateFcnCT1?
What I should have said is that you have not shown your equations.
function Z1=exocstrstateFcnCT1(X1,U1,k)
% U1=[0;0];
v(1)=U1(1);
w1(1)=U1(2);
% X1=[-4;-2;0];
% p=data.PredictionHorizon;
a=0.9;h=0.9;
cp1=1;cp2=1;cp3=1;
% c1=0;c2=0;c3=0;
for j=1:k
c1(j)=(1-(1+a)/j)*cp1;
c2(j)=(1-(1+a)/j)*cp2;
c3(j)=(1-(1+a)/j)*cp3;
cp1=c1(j); cp2=c2(j); cp3=c3(j);
end
% initial conditions setting:
% v1(1)=u1(1);
% w1(1)=u1(2);
% v1=zeros(2,k);
% w1=zeros(2,k);
x1(1)=X1(1); y1(1)=X1(2); z1(1)=X1(3);
% calculation of phase portraits /numerical solution/:
x1(k+1)=h*cos(z1(k-1))*v1(k-1) - memo(x1, c1, k);
y1(k+1)=h*sin(z1(k-1))*v1(k-1)-memo(y1, c2, k);
z1(k+1)=h*w1(k-1)-memo(z1, c3, k) ;
% function [x0] = memo1(r, c, k)
% temp = 0;
% for j=1:k-1
% temp = temp + c(j)*r(k-j);
%
% end
% x0 = temp;
%
Z1(1)=x1(k+1);
Z1(2)=y1(k+1);
Z1(3)=z1(k+1);
Start of Error Report
------------------------------------------------------
Array indices must be positive integers or logical values.
Error in exocstrstateFcnCT1 (line 30)
x1(k+1)=h*cos(z1(k-1))*v1(k-1) - memo(x1, c1, k);
Error in nlmpc/validateFcns (line 134)
xk1 = data.hStateFcn(xk, uk, data.Parameters{:});
Error in mpc1 (line 69)
validateFcns(nlobj1,X1,U1,[],{Ts});
------------------------------------------------------
End of Error Report
------------------------------------------------------
Error using nlmpc/validateFcns (line 155)
Error occurred when calling "Model.StateFcn". See the error report displayed above.
Error in mpc1 (line 69)
validateFcns(nlobj1,X1,U1,[],{Ts});
Unfortunately that part of the equations does not define what v(k) or omega(k) are.
By the way: for the alpha times alpha-1 times etc part, see https://www.mathworks.com/help/symbolic/pochhammer.html or use factorial(alpha)/factorial(alpha-i)/factorial(i) * eye(3) ... which works out as nchoosek(alpha, i) * eye(3) by the way.
For the second part, how to code it, please help
function z=myStateFunction1(x,u,X2G,X3G,k)
a=0.9;h=0.9;
cp1=1;cp2=1;cp3=1;
c1= nchoosek(a,k);
c2=nchoosek(a,k);
c3=nchoosek(a,k);
cp1=c1; cp2=c2; cp3=c3;
z(1)=x(1); z(2)=x(2); z(3)=x(3);
z(1)=h*cos(x(3))*u(1) - memo(z(1), c1, k);
z(2)=h*sin(x(3))*u(1)-memo(z(2), c2, k);
z(3)=h*u(2)-memo(z(3), c3, k) ;
%%
function [x0] = memo1(r, c, k)
temp = 0;
for j=1:k-1
temp = temp + c(j)*r(k-j);
end
x0 = temp;
Start of Error Report
------------------------------------------------------
Array indices must be positive integers or logical values.

Accedi per commentare.

Richiesto:

il 22 Dic 2020

Commentato:

il 23 Dic 2020

Community Treasure Hunt

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

Start Hunting!

Translated by