What part of this for loop does not want to work with anything else than positive integers?
2 次查看(过去 30 天)
显示 更早的评论
Hello there,
I'm really struggling to fix this for loop. I have provided all the variables that are in the code, and the for loop as well. So far, I managed to run it properly when dt = a positive integer, which means that t ranges from 0 to Lt without decimals. However, I need my loop to work on decimals! In other words, when t = [1,2,3,4,5,6,7,8,9,10], it works. When t = [0, 0.1, 0.3, ... ,10] it does not work.
I thought that wouldve been fixed by creating the i loop that is made of positive integers only, and using i to index in t which is made of decimals. But that is not working.
This is the error code I get when I run the for loop step by step:
Array indices must be positive integers or logical values.
Error in WC_sim (line 35)
drE = (-(LE)*rE(t(i)) + F(wEE .* rE(t(i)) - wEI .* rI(t(i)) + I_ext_E(t(i))));
*Note that the F function is:
function drE = F(x) %fonction Sin
drE = 1 ./ exp(-x)
end
Here is the code:
%Weight values
wEE = 8;
wII = 10;
wEI = 1;
wIE = 1;
%Time range on axis
Lt = 10;
dt = 0.1;
t = 0.1:dt:Lt;
%External stim
I_ext_E = 3;
I_ext_I = 1;
I_ones = ones(1,length(t));
I_ext_E = I_ext_E .* I_ones;
I_ext_I = I_ext_E .* I_ones;
LE = 0.5;
n = length(t);
rE = zeros(1,n);
rI = zeros(1,n);
for i = 1:length(t);
t = 0:dt:Lt;
% Calculate the derivative of the E population
drE = (-rE(t(i)) + F(wEE .* rE(t(i)) - wEI .* rI(t(i)) + I_ext_E(t(i))));
i
% Calculate the derivative of the I population
drI = (-rI(t(i)) + F(wIE .* rE(t(i)) - wII .* rI(t(i)) + I_ext_I(t(i))));
% Update using Euler's method
rE(i + 1) = rE(t(i)) + (dt .* drE);
% rI(i + 1) = rI(t(i)) + (dt .* drI);
end
2 个评论
Jan
2023-3-10
Please use the tools to format code and add a copy of the complete error message. Then the reader do not have to search, inwhich line the error occurs.
回答(1 个)
Jan
2023-3-10
编辑:Jan
2023-3-10
rE is a vector:
rE = zeros(1,n);
You cann address its elements with positive integers as indices: rE(1), rE(2), ...
t contains nonintegers and zero also:
t = 0:dt:Lt;
Here indices are valid also: t(1), t(2), ... t(1) is 0 in this case.
Now you try to use t(i) as index:
drE = (-rE(t(i)) + F(wEE .* rE(t(i)) - wEI .* rI(t(i)) + I_ext_E(t(i))));
% ^^^^^^^^^ ^^^^^^^ ^^^^^^^^^ ^^^^^^^^^^^^^^
For i=1 this is rE(0). For i=2: rE(dt). This is not valid.
Why do you use t at all?
%Weight values
wEE = 8;
wII = 10;
wEI = 1;
wIE = 1;
%Time range on axis
Lt = 10;
dt = 0.1;
t = 0:dt:Lt;
n = length(t);
%External stim
I_ext_E = 3;
I_ext_I = 1;
I_ones = ones(1, n);
I_ext_E = I_ext_E .* I_ones;
I_ext_I = I_ext_E .* I_ones;
rE = zeros(1,n);
rI = zeros(1,n);
for i = 1:n - 1 % Not length(t)
drE = -rE(i) + F(wEE .* rE(i) - wEI .* rI(i) + I_ext_E(i));
% Not used: drI = -rI(i) + F(wIE .* rE(i) - wII .* rI(i) + I_ext_I(i));
rE(i + 1) = rE(i) + dt .* drE;
end
function drE = F(x) %fonction Sin
drE = 1 ./ exp(-x);
end
rE contains NaNs after a valid start, but this is another problem.
3 个评论
Jan
2023-3-10
编辑:Jan
2023-3-10
For i=1 this is rE(0). For i=2: rE(dt). This is not valid. Answer:
Why is this not valid?
Because 0 is no valid index in Matlab. Indices must be > 0.
"t is my x axis. I need it to be divided with decimals (so not only positive integers such as 1)."
You are consuing the values of the vector with the indices. See this example:
t = 0:0.01:2*pi;
x = zeros(size(t));
for k = 1:numel(t)
x(k) = sin(t(k));
end
plot(t, x);
Do you see it? Your t can have any value, but the indices are strictly from 1 to numel(t). Assigning x(k) is working, but x(t(k)) is not. Of course, mathematically you construct "x(t)", but this is not the correct notation in Matlab, but the meaning of the contents.
The for loop does work with fractional steps:
t = zeros(1, 629);
x = zeros(1, 629);
k = 0;
for T = 0:0.01:2*pi
k = k + 1; % Integers > 0
t(k) = T;
x(k) = sin(T);
end
With this approach you have to care for another counter to create the indices.
Clearer now?
By the way, the clean Matlab way omits the loop:
t = 0:0.01:2*pi;
x = sin(t);
Less troubles, because no indices appear anywhere.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!