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
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.
Camille Godin
Camille Godin 2023년 3월 10일
Hi Jan, I think I did it properly but feel free to let me know if you want me to add it differently/add something else.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
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
Torsten
Torsten 2023년 3월 10일
편집: Torsten 2023년 3월 10일
x = 0:0.1:1
x = 1×11
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
y = x.^2
y = 1×11
0 0.0100 0.0400 0.0900 0.1600 0.2500 0.3600 0.4900 0.6400 0.8100 1.0000
plot(x,y)
Here, y(i) is the function f(x) = x^2 at x = x(i). y(i) is not defined as y(x(i)), but simply as y(i).
Do the same in your code to avoid errors.
Jan
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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by