Question
Hey

Can someone write me a c++ pogramm using 4th order runge kutta method? h=0.1

y = 3y, y(0) = 1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Runge Kutta Method- With this method one can solve approximate value of 'y' of diffrential equation where one bounty conditions is also given.

Formula-

K1= h*f(xn, yn )

K2= h*f( xn + (h/2) , yn + (K1/2))

K3= h*f( xn + (h/2), yn + (K2/2) )

K4= h*f( xn + h, yn + K3)

K= (1/6)*(K1 + 2*K2 + 2*K3 + K4)

Yn+1 = yn + K

Here the value of 'n' are 1,2,3,4,5,.....,n

Also here 'h' is a step height and xn+1 = x0+h

Question- y' = 3*y, y(0)=1 , h=0.1

So initially when x0=0   y0 = 1

                             x1=x0+h=0+0.1=0.1    y1 = ?

                             x2=x1+h=0.1+0.1=0.2     y2 = ?

program-

#include<iostream>
using namespace std;
float equation(float,float);
int main()
{
    float x0= 0 , y =1 ,h = 0.1; // here the y value is y0
    int n=1;// no of iteration
    float k1,k2,k3,k4,k5;
    for(int i=1; i<=n ; i++)
    {
        k1=h* equation(x0,y);
        k2=h* equation(x0 + 0.5*h, y + 0.5*k1);
        k3=h* equation(x0 + 0.5*h, y + 0.5*k2);
        k4=h* equation(x0 + h, y + k3);

        k5= ((1.0/6.0)*(k1 + 2*k2 + 2*k3 + k4));

        y=y + k5;

        x0=x0+h;
    }
   cout<<"The value of y is"<<y;


}

float equation(float x , float y)
{
    return 3*y;
}

{ 1 #include<iostream> 2 using namespace std; float equation(float, float); 4 int main() 5 - { 6 float xe= 0, y =1 ,h = 0.1;

When we run the loop 2 times the output is -

The value of y is1.82206 ... Program finished with exit code 0 Press ENTER to exit console. I

When we run the loop 3 times the output is -

The value of y is2.45949 ... Program finished with exit code 0 Press ENTER to exit console.

So, depending upon the no of iteration the approximate value of y changes.


  

Add a comment
Know the answer?
Add Answer to:
Hey Can someone write me a c++ pogramm using 4th order runge kutta method? h=0.1 y'...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT