Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Line 11 Error: Subscript indices must either be real positive integers or logicals.
2 ビュー (過去 30 日間)
古いコメントを表示
Hello! I am trying to code the attached image, but my code gets the error described in the question title. This is my code so far. Any help would be appreciated. Thank you!
data = webread("https://web.njit.edu/~goodman/Math222/matlab1A.html")
tFinal = 2;
N = 33;
h=tFinal/N;
t=linspace(0,tFinal,N+1); % type 'help linspace' to see how this works
y=zeros(1,N+1);
yExact=9./(3*t-1+10*exp(-3*t));
y(1) = 1; % setting the initial condition
for n=1:N
k1 = y(n)*(3-t(n)*y(n));
k2 = y(n+h*k1)*(3-t(n+1)*y(n+h*k1));
y(n+1) = y(n) + h/2 * (k1+k2);
end
plot(t,y,t,yExact,'--')
xlabel('t'); ylabel('y'); title('Look, ma! I solved another ODE in MATLAB!');
error200= abs(y(N+1)-yExact(N+1));
fprintf('The new error is %f.\n', error200);
1 件のコメント
Bob Thompson
2018 年 2 月 19 日
Assuming line 11 is:
k2 = y(n+h*k1)*(3-t(n+1)*y(n+h*k1));
Double check that your indices are turning up actual values, specifically k1, and if they are, then your issue may be in using * instead of .*, so it is trying to perform matrix multiplication rather than scalar multiplication.
回答 (1 件)
Roger Stafford
2018 年 2 月 19 日
In the line
k2 = y(n+h*k1)*(3-t(n+1)*y(n+h*k1));
the quantity n+h*k1 may not be a positive integer. If so, it is invalid as an index.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!