Info
此问题已关闭。 请重新打开它进行编辑或回答。
Line 11 Error: Subscript indices must either be real positive integers or logicals.
1 次查看(过去 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!