Runge-Kutta in C++ | Help Needed

In summary, a user was asking for help with creating a program in C++ using the Runge-Kutta method. They shared their code and mentioned that it was supposed to print out a table with x and y values, but the y values were all zero. They later realized that there was an error in the final line of their "runge" function. Another user shared a similar program they had written in FORTRAN 90 and suggested overloading some functions. The conversation ended with someone asking for code in VB, but it was mentioned that the thread was over five years old and no one from the original conversation would be able to provide it.
  • #1
Pauly Man
129
0
Hi guys.

I was wondering if anyone could help me with this code. I have sucessfully created a program in visual basic that can run a runge-kutta method. However I want to create one in c++, maybe eventually turn it into a .dll when i work out how to create and use them. :smile:

Here is the code:

Code:
// Runge-Kutta.cpp
//--------------------------------------------------
//A Runge-Kutta Method for solving Differential Equations
//of the form y'=f(x,y) ; y(x0)=y0
//--------------------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;

//Define constants
#define X0 0 
#define Y0 0
#define H  0.2
#define N  5

//Define Functions
double f(double x, double y);
double runge(double x, double y);


//Main Function
int main(double x, double y)
{
	cout<<"\t*** Euler Method ***"
		<<"\n\n";
	cout<<"     "
		<<setw(12)<<"x"<<setw(12)<<"\ty"
		<<"\n"
		<<"\t------------------------------"
		<<"\n";
	y=Y0;
	for(int i=0;i<=5;i++)
	{
		x=X0+(i*H);
		y=runge(x,y);
		cout<<left<<setw(6)<<i<<"|"
			<<setprecision(4)<<left<<setw(8)<<"\t"<<x
			<<setprecision(4)<<left<<setw(8)<<"\t"<<y;
		cout<<"\n\n";
	}
	cout<<"\n\n";
	return 0;
}

double runge(double x, double y)
{
    double K1    = (H * f(x,y));
    double K2    = (H * f((x + 1 / 2 * H), (y + 1 / 2 * K1)));
    double K3    = (H * f((x + 1 / 2 * H), (y + 1 / 2 * K2)));
    double K4    = (H * f((x + H), (y + K3)));
	double runge = (y + (1 / 6) * (K1 + 2 * K2 + 2 * K3 + K4));
	return runge;
}

double f(double x, double y)
{
	double f = x+y;
	return f;
}

It is supposed to print out a table, with the x values in one column, and y values in the other. Each successive iteration is in a new row of the table.

So far so good, the code creates the table fine.

I created a similar program to run a Euler method, and it works just fine. But in the Runge-Kutta it just prints the y values as zero. I must have done something wrong somewhere.
 
Technology news on Phys.org
  • #2
I've worked it out now.

It was the final line of the "runge" function.

I had the brackets wrong, it should read:

double runge = (y + ((K1 + 2 * K2 + 2 * K3 + K4)/6));
 
  • #3
i wrote a very similar program in FORTRAN 90 a couple of years ago.

just out of interest, here's the subroutine for the program it was used in:

SUBROUTINE RUNKUT(H,F,X,N,Y)
! Performs one step of a fourth order Runge-Kutta integration
IMPLICIT NONE
INTEGER, PARAMETER :: DOUB_PREC=SELECTED_REAL_KIND(P=10,R=30)
INTEGER :: N ! number of y's
REAL(DOUB_PREC) :: H,X,Y(N) ! x step size, x, y's
EXTERNAL F ! subroutine of the form F(X,Y,YPRIME)
! The subroutine F must return derivatives of Y's wrt X in YPRIME
! Both the independent variable X and the dependent variables Y are
! updated on each call.
! Local automatic arrays for workspace
REAL(DOUB_PREC) :: F1(N),F2(N),F3(N),F4(N)
REAL(DOUB_PREC) :: Y1(N),Y2(N),Y3(N),Y4(N)
REAL(DOUB_PREC) :: K1(N),K2(N),K3(N),K4(N)
REAL(DOUB_PREC) :: X1,X2,X3,X4
! Define steps for 4th order terms
X1=X
X2=X1+H*0.5
X3=X2
X4=X1+H
! Calculate 4th order terms
Y1=Y
CALL F(X1,Y1,F1)
K1=H*F1
Y2=Y1+K1*0.5
CALL F(X2,Y2,F2)
K2=H*F2
Y3=Y1+K2*0.5
CALL F(X3,Y3,F3)
K3=H*F3
Y4=Y1+K3
CALL F(X4,Y4,F4)
K4=H*F4
! Perform 4th order step
Y=Y1+(K1+2.0D0*K2+2.0D0*K3+K4)/6.0D0
! Update independent variable
X=X4
END SUBROUTINE RUNKUT
 
  • #4
you're all set, i think you should overload some functions though.
 
  • #5
hey dude nice codes there but can i get ur codes in vb am working on something similar thnx
 
  • #6
This thread is over five years old!

None of the people who posted in this thread are around to give you their code.
 

What is Runge-Kutta method in C++?

The Runge-Kutta method is a numerical technique used for solving ordinary differential equations (ODEs). It is named after the German mathematicians Carl Runge and Wilhelm Kutta. In C++, the Runge-Kutta method is implemented as a class template in the <cmath> library.

How does Runge-Kutta method work?

The Runge-Kutta method works by approximating the solution of an ODE at discrete points. It uses a weighted average of several intermediate results to estimate the next point, making it more accurate than other numerical methods like Euler's method.

What are the advantages of using Runge-Kutta method in C++?

The Runge-Kutta method has several advantages, including:

  • It can handle a wide range of ODEs, including stiff equations.
  • It is a higher-order method, meaning it can achieve better accuracy with fewer steps compared to lower-order methods.
  • It is easy to implement in C++ using the <cmath> library.

What are the limitations of Runge-Kutta method in C++?

Despite its advantages, the Runge-Kutta method also has some limitations, such as:

  • It can be computationally expensive for complex ODEs.
  • It may not provide accurate results if the step size is too large.
  • It may not be suitable for solving higher-order ODEs.

How can I use Runge-Kutta method in my C++ program?

To use the Runge-Kutta method in your C++ program, you will need to:

  1. Include the <cmath> library.
  2. Define the ODE you want to solve as a function.
  3. Instantiate the Runge-Kutta class template with the necessary parameters.
  4. Call the integrate() function to solve the ODE and obtain the results.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
4
Views
69
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
1
Views
724
  • Programming and Computer Science
3
Replies
89
Views
4K
Replies
10
Views
947
Back
Top