-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRK_method
46 lines (37 loc) · 1.08 KB
/
RK_method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
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 = (int)((x - x0) / h);
float k1, k2, k3, k4, k5;
// Iterate for number of iterations
float y = y0;
for (int i = 0; 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;
printf("k1 = %f, k2 = %f, k3 = %f, k4 = %f\n", k1, k2, k3, k4);
printf("Updated values of x and y are %f, %f\n\n", x0, y);
}
return y;
}
void main()
{
float x0, y, x, h;
printf("Enter the values of x0, y, x, h: ");
scanf("%f %f %f %f", &x0, &y, &x, &h);
printf("The value of y at x is : %f\n", rungeKutta(x0, y, x, h));
return;
}