Numerical method : Solution of ordinary differential equation using RK4 method in C
destination source:https://www.programming-techniques.com/2011/09/numerical-method-solution-of-ordinary-differential-equation-using-rk4-method-in-c.html
Algorithm:

- Start
- Declare and Initialize necessary variable likes K1, K2, K3, K4 etc.
- Declare and define the function that returns the functional value.
- Get the initial value of x, initial value of y, no. of iteration and interval from user.
- for i = 0 to i < n go to step 6.
- Do the following calculation: and go to step 7
- The new values of x and y are: x = x + interval, y = y + K;
- print the value of x and y;
- stop
Flowchart:
Source Code:
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 | #include <stdio.h> /****************************************************** Program: solution of ordinary differential equation Language : C Author: Bibek Subedi Tribhuvan University, Nepal ********************************************************/ float func(float x, float y){ return (y*y-x*x)/(y*y+x*x); } int main(){ float K, K1, K2, K3, K4; float x0 , y0, x, y; int j, n; float i, H; printf("Enter initial value of x: "); scanf("%f", &x0); printf("Enter initial value of y: "); scanf("%f", &y0); printf("Enter no iteration: "); scanf("%d", &n); printf("Enter the interval: "); scanf("%f", &H); x = x0; y = y0; for(i = x+H, j = 0; j < n; i += H, j++){ K1 = H * func(x , y); K2 = H * func(x+H/2, y+K1/2); K3 = H * func(x+H/2, y+K2/2); K4 = H * func(x+H, y+K3); K = (K1 + 2*K2 + 2*K3 + K4)/6; x = i; y = y + K; printf("At x = %.2f, y = %.4f ", x, y); printf("\n"); } return 0; } |
Output

Related Article
destination source:https://www.programming-techniques.com/2011/09/numerical-method-solution-of-ordinary-differential-equation-using-rk4-method-in-c.html