MATLAB Answers

0

Index exceeds number of array elements (1)?

Riley Vargas さんによって質問されました 2018 年 11 月 6 日
最新アクティビティ Aquatris
さんによって コメントされました 2019 年 3 月 13 日
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?

  0 件のコメント

サインイン to comment.

タグ

1 件の回答

Aquatris
回答者: Aquatris
2018 年 11 月 6 日
編集済み: Aquatris
2018 年 11 月 6 日
 採用された回答

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.

  4 件のコメント

Lykha ahmed 2019 年 1 月 30 日
could you tell me how you added?
parag gupta 2019 年 3 月 11 日
how did u make the change?
Aquatris
2019 年 3 月 13 日
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

サインイン to comment.



Translated by