Modify a C code to find the solution of the differential equ

Modify a C code to find the solution of the differential equation below by using forth-order Runge-Kutta method. The range of x is 2 to 3 and the step size is 0.1. dx/dy = 1 - y^2 + 0.3 * x^2 With y=3 at x=2. List all the y values within the range of x-2 to x=3.

Solution

// C program to implement Runge Kutta method
#include<stdio.h>

// A differential equation \"dy/dx = 1/1-y*y*y+0.3*x*x\"
float dydx(float x, float y)
{
    return(1/(1-y*y*y+0.3*x*x));
}

// 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=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);;
        printf(\"\ \ y(%.4lf) = %.3lf \",x0+h,y);

        // Update next value of x
        x0 = x0 + h;
    }

    return y;
}

// Driver method
int main()
{
    float x0 = 2, y = 3, x = 3, h = 0.1;
    printf(\"\ The value of y at x is : %f\",
            rungeKutta(x0, y, x, h));
    return 0;
}
OUTPUT:

                                                                                                                                                                 

  y(2.1000) = 2.996                                                                                                                                                

                                                                                                                                                                   

  y(2.2000) = 2.992                                                                                                                                                

                                                                                                                                                                   

  y(2.3000) = 2.988                                                                                                                                                

                                                                                                                                                                   

  y(2.4000) = 2.984                                                                                                                                                

                                                                                                                                                                   

  y(2.5000) = 2.979                                                                                                                                                

                                                                                                                                                                   

  y(2.6000) = 2.975                                                                                                                                                

                                                                                                                                                                   

  y(2.7000) = 2.971                                                                                                                                                

                                                                                                                                                                   

  y(2.8000) = 2.966                                                                                                                                                

                                                                                                                                                                   

  y(2.9000) = 2.962                                                                                                                                                

                                                                                                                                                                   

  y(3.0000) = 2.957                                                                                                                                                

The value of y at x is : 2.957478sh-4.3$

 Modify a C code to find the solution of the differential equation below by using forth-order Runge-Kutta method. The range of x is 2 to 3 and the step size is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site