0% found this document useful (0 votes)
6 views1 page

Rungkutta

Uploaded by

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

Rungkutta

Uploaded by

hasunbriren
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

// Define the function for dy/dx = x + y


double F(double x, double y) {
return x + y;
}

// Function to apply the Runge-Kutta method


double rungaKuttaMethod(double x, double x0, double y0, double h) {
double k1, k2, k3, k4, k, y;

// Calculating k1, k2, k3, and k4


k1 = h * F(x0, y0);
k2 = h * F(x0 + (h / 2), y0 + (k1 / 2));
k3 = h * F(x0 + (h / 2), y0 + (k2 / 2));
k4 = h * F(x0 + h, y0 + k3);

// Final value of y after one step


k = (1.0 / 6) * (k1 + 2 * k2 + 2 * k3 + k4);
y = y0 + k;

return y;
}

int main() {
cout << "Hello! This is a program to solve an ordinary differential equation by
the Runge-Kutta method in C++\n";

double x0, y0, x, h;


cout << "Enter the initial value of x0: ";
cin >> x0;
cout << "Enter the initial value of y0: ";
cin >> y0;
cout << "Enter the value of x where you want to calculate y: ";
cin >> x;
cout << "Enter the step size (h): ";
cin >> h;

double ans = rungaKuttaMethod(x, x0, y0, h);


cout << "The value of y at x = " << x << " is " << ans << endl;

cout << "************************************\n";


cout << "Name: SAKSHAM PRATAP\n";
cout << "Enrollment No.: 20820802823\n";
cout << "Course & Branch: BTECH ECE\n";
cout << "Group: 2\n";

return 0;
}

You might also like