ddhr.org
Code - rungekutta Thursday, Jan 22, 2009 10:55 am

This is a Matlab function that uses the Runge-Kutta method of numerical approximation to predict values of differential equations.  I wrote it for a point mass simulation, which is a simplified model of the motion of a spherical object. 

function output = rungekutta(x,dt,f)
%RUNGEKUTTA   Numerical integration (prediction).
%   RUNGEKUTTA(X,DT,F) uses a fourth-order Runge-Kutta
%   prediction routine to determine the next value of a
%   function based on the current value.  X can be any
%   number or array of numbers, DT is the time step for
%   integration, and F is the name of the function to
%   integrate (with single quotes).
%
%   Example:
%      position2 = rungekutta(position1,0.01,'equations')

k1 = dt*feval(f,x);
k2 = dt*feval(f,x+k1./2);
k3 = dt*feval(f,x+k2./2);
k4 = dt*feval(f,x+k3);
   
output = x+(k1+2.*k2+2.*k3+k4)./6;

Download

Linked:  Code

Hello
Hi, my name is Dave Hosier, and this website is where I write my unfounded opinions on trivial matters. Feel free to look around, but please refrain from reading anything.

about | contact | feed

Comments (feed)
Dave on Cubeicals wine rack: I ended up cutting a...
Harry on Cubeicals wine rack: So Dave, how did you...
Emily on Constant vs. average speed: I can give you sun...
Dave on Accents that annoy me: I'm pretty much saying I...
Wendy on Accents that annoy me: Are you passively aggressively saying...
JSW on Accents that annoy me: KInda like I can't tell...
Dave on Accents that annoy me: Understood, but my untrained ears...
Dave on Constant vs. average speed: I don't think so.  There...
JSW on Accents that annoy me: I realize with you being...
Emily on Constant vs. average speed: You don't think the beaches...