How Can I Fix My C++ Runge-Kutta Method to Correctly Update Y Values?

  • Context: C/C++ 
  • Thread starter Thread starter Pauly Man
  • Start date Start date
  • Tags Tags
    C++ Runge-kutta
Click For Summary

Discussion Overview

The discussion revolves around a C++ implementation of the Runge-Kutta method for solving differential equations. Participants explore issues related to code functionality, specifically the correct updating of y values in the numerical method.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their C++ code for a Runge-Kutta method and describes the intended output as a table of x and y values.
  • The participant notes that the current implementation results in y values being printed as zero, indicating a potential error in the code.
  • Another participant identifies and corrects an issue in the Runge-Kutta function, specifically regarding the placement of brackets in the calculation of the new y value.
  • A different participant shares a FORTRAN 90 subroutine that performs a similar Runge-Kutta integration, providing insight into the structure and calculations involved.
  • One participant suggests that the original poster should consider overloading some functions in their C++ code.
  • Another participant requests the original poster's Visual Basic code for a similar project they are working on.
  • A later reply points out that the thread is over five years old, implying that the original contributors may no longer be available to assist.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to the implementation, and multiple viewpoints regarding code structure and functionality are presented.

Contextual Notes

There are unresolved issues related to the initial implementation of the Runge-Kutta method, particularly concerning the correct calculation of y values. The discussion also highlights differences in programming languages and approaches to numerical methods.

Who May Find This Useful

Individuals interested in numerical methods, particularly the Runge-Kutta method, as well as those working with C++ and FORTRAN in the context of solving differential equations may find this discussion relevant.

Pauly Man
Messages
129
Reaction score
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
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));
 
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
 
you're all set, i think you should overload some functions though.
 
hey dude nice codes there but can i get ur codes in vb am working on something similar thnx
 
This thread is over five years old!

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

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
14
Views
5K
  • · Replies 10 ·
Replies
10
Views
13K