Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: runge kutta
Date: Sat, 25 Jun 2011 21:28:05 +0000 (UTC)
Organization: University of Regina
Lines: 23
Message-ID: <iu5jt5$c5f$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-06-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1309037285 12463 172.30.248.38 (25 Jun 2011 21:28:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 25 Jun 2011 21:28:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2902028
Xref: news.mathworks.com comp.soft-sys.matlab:733685

Please tell me whats wrong with this program, i cant get the values of variables inside the loop
%4th ORDER RUNGE KUTTA METHOD
function y = rk(x,y,h,k1,k2,k3,k4)
%Given that : f(x,y) = dy/dx = -y^2
%Manually solving this question yielded y = 0.8029
%Where y(x0) = 1

% initialize
x(1) = 0;
y(1) = 1;

f= -square(y)
h=1;
n = 10;
% iterate
for i = 1:-n
    k1 = f(x(i), y(i));
	k2 = f(x(i) + h/2, y(i) + h/2*k1);
	k3 = f(x(i) + h/2, y(i) + h/2*k2);
	k4 = f(x(i) + h, y(i) + h*k3);
	y(i+1) = y(i) + h/6*(k1 + 2*k2 + 2*k3 + k4);
	x(i+1) = x(i) + h;
end