Tja! jag försöker lösa diffekvationen
y' = -xy
mha. RK2 i matlab. Det går inte riktigt som jag vill. Detta är min kod:
Kod:
clear all
x(1)=0;
y(1)=1;
%[a,b] is the interval at which we are looking
a = 0;
b = 1;
h = 0.01;
%N is the total number of steps in our interval, and h is the step size
N = (b-a)/h;
%2nd order RK, with coefficient values assigned according Heun's method:
for n=1:(N-1);
k1 = -x(n)*y(n)*h;
k2 = -(x(n)+h)*(y(n)+h);
y(n+1) = y(n) + (1/2*k1 + 1/2*k2)*h;
end
plot(x,y,x,exp(-x.^2./2).*1);
Felet jag får är:
Kod:
??? Attempted to access x(2); index out of bounds because numel(x)=1.
Error in ==> E12 at 21
k1 = -x(n)*y(n)*h;
Kan någon reda ut begreppen? numel(x) ska tydligen ha att göra med antalet element i x?
Tack!