Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
runge kutta

Subject: runge kutta

From: Anna Qureshi

Date: 25 Jun, 2011 21:28:05

Message: 1 of 2

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

Subject: runge kutta

From: Roger Stafford

Date: 25 Jun, 2011 21:44:05

Message: 2 of 2

"Anna Qureshi" wrote in message <iu5jt5$c5f$1@newscl01ah.mathworks.com>...
> 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
- - - - - - - - - -
  See reply in thread 309732. It is best not to make duplicate copies of the same request.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us