Index exceeds number of array elements (1)?

Asked by Riley Vargas

Riley Vargas (view profile)

on 6 Nov 2018
Latest activity Commented on by Aquatris

Aquatris (view profile)

on 13 Mar 2019
Accepted Answer by Aquatris

Aquatris (view profile)

dt = 0.005; N = 200; %(s)
P3s = 2; P3d = 23.5; %(mmHg)
V2(1) = .475; %(L)
V0 = .06; %(L)
C2 = 0.05; %(L/mmHg)
R1 = 30; R2 = 30; %(mmHg*s/L)
P1 = 11; %(mmHg)
P3(1:120) = P3s;
P3(121:200) = P3d;
t = 0:dt:(N-1)*dt;
plot(t, P3)
xlabel('Time (s)'), ylabel('Pressure (mmHg)')
for i = 1:N
P2(i) = (V2(i) - V0) / C2;
if P2(i) > P3(i); Q3(i) = (P2(i) - P3(i)) / R2;
else Q3(i) = 0;
end
Q1(i) = (P1 - P2(i)) / R1;
Q2(i) = Q1(i) - Q3(i);
P2(i+1) = P2(i) + Q2(i)*dt;
end
tp = 0:dt:N*dt;
figure
plot(tp, P2)
xlabel('Time (s)'), ylabel('P2 (mmHg)')
Error is in line 12 " P2(i) = (V2(i) - V0) / C2;"
What is strange about this problem is that the code runs perfectly on my computer, but when transferred to another and ran, it gives the error message. Any ideas?

Aquatris (view profile)

on 6 Nov 2018
Edited by Aquatris

Aquatris (view profile)

on 6 Nov 2018

You only define V2(1) in your code and do not increase the size of the V2 variable. So I think you are missing a code in your for loop which assigns values to V2(i+1).
It probably runs in your computer because your workspace includes a V2 variable with correct size from another script so the code does not yell at you.

Show 1 older comment
Lykha ahmed

Lykha ahmed (view profile)

on 30 Jan 2019
could you tell me how you added?
parag gupta

parag gupta (view profile)

on 11 Mar 2019
how did u make the change?
Aquatris

Aquatris (view profile)

on 13 Mar 2019
In his code he never defined V2(2) so he added a line in his for loop, something like;
for i = 1:N
...
V2(i+1) = *equation*
end