5 Replies - 2142 Views - Last Post: 24 June 2012 - 07:49 AM

#1 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Runge-Kutta Fourth-Order Numerical Integrator

Posted 14 November 2007 - 12:47 AM

Description: Double-precision function for integrating ODEs. Accepts a vector of y-values, the current x-value, and a pointer to function that returns a vector of the derivatives at (x,y).

Requires the STL <vector> library, as well as a function to calculate the derivatives at each point in the integration. Simple example usage is provided.

Very few optimizations have been made for this code, in an effort to make it as clear as possible. If you're looking for high-performance, post a comment or PM me and I can suggest a few things to try.Function for numerical integration of ordinary differential equations.
/******************************************************************************/
/* rungeKutta4()
 * 
 * jjhaag AT dreamincode.net
 * 14 November 2007
 * 
 * 
 * 4-step (fourth order) Runge-Kutta numerical integrator for ordinary differential equations.
 * 
 * A common and powerful numerical integrator.  No adaptive step size; such 
 * functionality can be controlled using an external function.
 * 
 * Arguments:
 * 		x	-	the initial x-value
 * 		yi	-	vector of the initial y-values
 * 		dx	-	the step size for the integration
 * 		derivatives		-	a pointer to a function that returns the derivative 
 * 				of a function at a point (x,y), where y is an STL vector of 
 * 				elements.  Returns a vector of the same size as y.
 * 
 * If you're looking for details on the Runge-Kutta method(s), do a google 
 * search - that would take up pages here, and that's probably a little more 
 * than belongs in the header comments.
 * 
 * Dependencies: requires <vector> (plus whatever is required by the derivatives
 * function and the rest of your program).
 * 
 * 
 * 
 * Example usage - numerical integration dy/dt = cos(x) :
 * #include <vector>
 * #include <cmath>
 * std::vector<double> dydtcos(double x, vector<double> y) {
 *     std::vector<double> dydt(y)
 *     dydt.at(i)=cos(x);
 *     return dydt;
 * }
 * --------------
 * vector<double> yi(1,0.0);
 * double x=0.0, dx=0.10;
 * 
 * while (x<10.0) {
 *     yi=rungeKutta4(yi,x,dx,(*dydtcos));
 *     x+=dx;
 * }
 * 
 */

std::vector<double> rungeKutta4(
		double xi,
		std::vector<double> yi,
		double dx,
		std::vector<double> (*derivatives)(double x, std::vector<double> y) ) {
	
	//	total number of elements in the vector
	int n=yi.size();
	
	//	first step
	std::vector<double> k1;
	k1=derivatives(xi, yi);
	for (int i=0; i<n; ++i) {
		k1.at(i)*=dx;
	}
	
	//	second step
	std::vector<double> k2(yi);
	for (int i=0; i<n; ++i) {
		k2.at(i)+=k1.at(i)/2.0;
	}
	k2=derivatives(xi+dx/2.0,k2);
	for (int i=0; i<n; ++i) {
		k2.at(i)*=dx;
	}
	
	//	third step
	std::vector<double> k3(yi);
	for (int i=0; i<n; ++i) {
		k3.at(i)+=k2.at(i)/2.0;
	}
	k3=derivatives(xi+dx/2.0,k3);
	for (int i=0; i<n; ++i) {
		k3.at(i)*=dx;
	}
	
	
	//	fourth step
	std::vector<double> k4(yi);
	for (int i=0; i<n; ++i) {
		k4.at(i)+=k3.at(i);
	}
	k4=derivatives(xi+dx,k4);
	for (int i=0; i<n; ++i) {
		k4.at(i)*=dx;
	}
	
	
	//	sum the weighted steps into yf and return the final y values
	std::vector<double> yf(yi);
	for (int i=0; i<n; ++i) {
		yf.at(i)+=(k1.at(i)/6.0)+(k2.at(i)/3.0)+(k3.at(i)/3.0)+(k4.at(i)/6.0);
	}
	
	return yf;
}

This post has been edited by jimblumberg: 10 January 2015 - 11:29 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Runge-Kutta Fourth-Order Numerical Integrator

#2 leejay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 22-April 08

Re: Runge-Kutta Fourth-Order Numerical Integrator

Posted 22 April 2008 - 04:35 PM

wow~ thanks~§Ľ§Ľ
Was This Post Helpful? 0
  • +
  • -

#3 zaqnafein   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 15-June 08

Re: Runge-Kutta Fourth-Order Numerical Integrator

Posted 15 June 2008 - 01:43 AM

Thanks, the code is very clear.
Was This Post Helpful? 0
  • +
  • -

#4 iris_sam2002   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 31-December 08

Re: Runge-Kutta Fourth-Order Numerical Integrator

Posted 03 March 2009 - 11:26 PM

how can I introduce a pause, I tried but still did not pause. thanks
Was This Post Helpful? 0
  • +
  • -

#5 neeraj213   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 24-May 09

Re: Runge-Kutta Fourth-Order Numerical Integrator

Posted 23 May 2009 - 11:21 PM

thanks a lot dear
Was This Post Helpful? 0
  • +
  • -

#6 Xandros   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 24-June 12

Re: Runge-Kutta Fourth-Order Numerical Integrator

Posted 24 June 2012 - 07:49 AM

I really would need to speed up this code ... any suggestions on how to do that? Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1