-->

Numerical method : Solution of ordinary differential equation using RK4 method in C

Numerical method : Solution of ordinary differential equation using RK4 method in C

    Algorithm:

    1. Start
    2. Declare and Initialize necessary variable likes K1, K2, K3, K4 etc.
    3. Declare and define the function that returns the functional value.
    4. Get the initial value of x, initial value of y, no. of iteration and interval from user.
    5. for i = 0 to i < n go to step 6.
    6. Do the following calculation: and go to step 7 RK_4
    7. The new values of x and y are: x = x + interval, y = y + K;
    8. print the value of x and y;
    9. stop

    Flowchart:

    RK_4flow

     

    Source Code:

    #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


    RK4

    fardi zayden
    @مرسلة بواسطة
    كاتب ومحرر اخبار اعمل في موقع دراسات تقنية .

    إرسال تعليق