Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Index exceeds dimensions error
1 回表示 (過去 30 日間)
古いコメントを表示
I'm trying to code the predictor-corrector method, but I keep getting an "Index exceeds dimensions", and I don't see where the error is.
%function[t y]=abmpc4(f,a,b,y0,h)
f=@(t,y)((y/t)-(y/t)^2);
h=0.1;
a=1;
b=2;
y0=1;
n=(b-a)/h;
t(1)=a;
y(1)=y0;
w=[];
s=[];
s(1)=a;
w(1)=y0;
%RK4 method to get initial values
for i=1:3
k1=h*f(t,y);
k2=h*f(t+(h/2),y+(k1/2));
k3=h*f(t+(h/2),y+(k2/2));
k4=h*f(t+h,y+k3);
y=y+(k1+(2*k2)+(2*k3)+k4)/6;
t=t+h;
w=[w,y];
s=[s,t];
end
%truevals(1,1)=y0;
%Adams-Bashforth 4-Step
% i=4;
% y=y+(55/24)*h*f(s(i,1),w(i,1))-(59/24)*h*f(s(i-1,1),w(i-1,1))+(37/24)*h*f(s(i-2,1),w(i-2,1))-(9/24)*h*f(s(i-3,1),w(i-3,1)); %predictor
% %t=t+h;
% w(i+1,1)=y;
% s(i+1,1)=t;
for i=4:n
%truevals=g;
t=t+h;
y=y+(h/24)*(55*f(t(i),y(i))-59*f(t(i-1),y(i-1))+37*f(t(i-2),y(i-2))-9*f(t(i-3),y(i-3))); %predictor
y=y+(h/24)*(9*f(t(i+1,1),y(i+1,1))+19*f(t(i,1),y(i,1))-5*f(t(i-1,1),y(i-1,1))+f(t(i-2,1),y(i-2,1))); %corrector
w=[w,y];
s=[s,t];
end
1 件のコメント
回答 (1 件)
Star Strider
2017 年 2 月 13 日
The error:
Index exceeds matrix dimensions.
Error in Elipses (line ###)
y=y+(h/24)*(55*f(t(i),y(i))-59*f(t(i-1),y(i-1))+37*f(t(i-2),y(i-2))-9*f(t(i-3),y(i-3)));
%predictor
The problem is that here ‘t’ is a (1x1) double and ‘i’ is 4. That condition with throw the error you see.
This situation:
q = pi
r = q(4)
will throw the same error.
4 件のコメント
Star Strider
2017 年 2 月 13 日
You need to update the vectors in your loops by subscripting them.
For example:
for i=4:n-1
. . .
y(n+1) = y(n) + ...
. . .
end
Then reference them appropriately in your other equations that use them, similar to what I outlined here. You will have to experiment.
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!