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

  • Thread starter Thread starter Pauly Man
  • Start date Start date
  • Tags Tags
    C++ Runge-kutta
AI Thread Summary
The discussion revolves around a user's attempt to implement the Runge-Kutta method for solving differential equations in C++. The user successfully created a similar program in Visual Basic and shared their C++ code, which prints a table of x and y values. Initially, the program produced incorrect results, displaying y values as zero. The issue was identified as a mistake in the final line of the "runge" function, where the calculation needed proper parentheses. After correcting this, the user expressed satisfaction with the solution. Additionally, there was a mention of a FORTRAN 90 subroutine for the Runge-Kutta method, showcasing the user's familiarity with different programming languages. The thread also included a request for Visual Basic code, but it was noted that the original contributors were no longer active.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
15
Views
3K
Replies
1
Views
3K
Replies
5
Views
2K
Replies
5
Views
3K
Replies
4
Views
8K
Replies
2
Views
3K
Replies
23
Views
2K
Replies
10
Views
13K
Back
Top