Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Line 11 Error: Subscript indices must either be real positive integers or logicals.

1 vue (au cours des 30 derniers jours)
et77794
et77794 le 19 Fév 2018
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaire
Bob Thompson
Bob Thompson le 19 Fév 2018
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.

Réponses (1)

Roger Stafford
Roger Stafford le 19 Fév 2018
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.

Community Treasure Hunt

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

Start Hunting!

Translated by