## Error: Array indices must be positive integers or logical values

### Laura Zichella (view profile)

on 30 Oct 2019
Latest activity Edited by Sai Bhargav Avula

### Sai Bhargav Avula (view profile)

on 30 Oct 2019
I am trying to find y(t) of a differential equation dy/dt= (1/RC)y(t) +(1/C)x(t) for t to compare my Fourier Approximated y(t) to see how resonable it is. I am getting an error and don't know why
t=[0:0.0001:1.8];
for i=1:length(t)
if i<=length(x)
x_t(i)=Io.*sin((pi.*t(i))./Ts); % Io and Ts are constants
else
x_t(i)=0;
end
y = sym('y',[1 length(t)])
ode=diff(y,t(i))==(-1/(R*C))*y + (1/C)*x_t(t(i))
end
y_tSol(t)=dsolve(ode)
is giving me error:
Array indices must be positive integers or logical values.
Error in project2 (line 162)
ode=diff(y,t(i))==(-1/(R*C))*y + (1/C)*x_t(t(i))

### Sai Bhargav Avula (view profile)

on 30 Oct 2019
Edited by Sai Bhargav Avula

### Sai Bhargav Avula (view profile)

on 30 Oct 2019

Hi,
MATLAB indices start from 1. In your code I think error is in x_t(t(i)) of the mentioned line. When i=1, t(i) = 0 which is the reason for the error.
Hope this helps ### Walter Roberson (view profile)

on 30 Oct 2019

In
y = sym('y',[1 length(t)])
then y will become a vector of variable names, y1, y2, y3, and so on up to y18001
In
ode=diff(y,t(i))==(-1/(R*C))*y + (1/C)*x_t(t(i))
you try to differentiate those 18001 different individual independent variables, each with respect to the numeric value from t(i) such as 0.0178 . That is not a permitted operation: you have to differentiate with respect to a variable, or else you have to specify the differentation order (number of times to differentiate) as a non-negative integer. If you try to interpret the t(i) as a fractional differentiation order, then in that case you would be differentiating each independent variable with respect to itself, which would have a result of 1 except for order 0.
On the right hand side of the == then y will be the entire vector of 18001 variables. After that, t(i) is one individual numeric value, and x_t(t(i)) would be a request to index x_t() at the location given by the numeric value in y(t) -- an indexing that could only work for the one location where y(t) is 1 exactly.
If you could manage to get that equation to work (such as by more selective indexing) you would have the bug that you are overwriting tge variable ode each time through the loop.