Hi guys.(adsbygoogle = window.adsbygoogle || []).push({});

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.

Here is the code:

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.Code (Text):

// 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;

}

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

I created a similiar 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.

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Runge-Kutta in C++

Loading...

Similar Threads for Runge Kutta |
---|

Fortran Runge - Kutta 4 understanding |

**Physics Forums | Science Articles, Homework Help, Discussion**