0% found this document useful (0 votes)
284 views2 pages

C Program To Implement Runge Kutta Method1

This C++ program implements the Runge-Kutta method to numerically solve differential equations. It defines a function to calculate the derivative at any given x and y value. The rungeKutta function then iteratively applies the Runge-Kutta formulas using a step size h to calculate successive y values from the initial value up to the target x value. The main function calls rungeKutta to solve the sample equation and outputs the result.

Uploaded by

Amit Kohli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
284 views2 pages

C Program To Implement Runge Kutta Method1

This C++ program implements the Runge-Kutta method to numerically solve differential equations. It defines a function to calculate the derivative at any given x and y value. The rungeKutta function then iteratively applies the Runge-Kutta formulas using a step size h to calculate successive y values from the initial value up to the target x value. The main function calls rungeKutta to solve the sample equation and outputs the result.

Uploaded by

Amit Kohli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// C program to implement Runge Kutta method

#include<iostream>

using namespace std;

// A sample differential equation "dy/dx = (x - y)/2"

float dydx(float x, float y)

return((x - y)/2);

// Finds value of y for a given x using step size h

// and initial value y0 at x0.

float rungeKutta(float x0, float y0, float x, float h)

// Count number of iterations using step size or

// step height h

int n =((x - x0) / h);

float k1, k2, k3, k4, k5;

// Iterate for number of iterations

float y = y0;

for (int i=1; i<=n; i++)

// Apply Runge Kutta Formulas to find

// next value of y
k1 = h*dydx(x0, y);

k2 = h*dydx(x0 + 0.5*h, y + 0.5*k1);

k3 = h*dydx(x0 + 0.5*h, y + 0.5*k2);

k4 = h*dydx(x0 + h, y + k3);

// Update next value of y

y = y + (1.0/6.0)*(k1 + 2*k2 + 2*k3 + k4);;

// Update next value of x

x0 = x0 + h;

return y;

// Driver method

int main()

float x0 = 0, y = 1, x = 2, h = 0.2;

cout<<rungeKutta(x0, y, x, h);

return 0;

You might also like