luizaspan icon

RK4 Method

luizaspan | PRO | 11/11/15 11:43:47 PM UTC | 0 ⭐ | 765 👁️ | Never ⏰ | []
C |

570 B

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <math.h>
 
#define h 0.1 // "salto"
 
#define x0 1 // parâmetros iniciais
#define y0 2
 
#define N 100 // número de iterações
 
double f(double x, double y) // dy/dx = f(x,y)
{
    return 3*x*x*y; // função a ser resolvida
}
 
int main(void)
{
    int i;
    double K1, K2, K3, K4, x, y, yn;
 
    x=x0;
    y=y0;
 
    for (i=0; i<N; i++)
    {
        K1 = f(x,y);
        K2 = f(x+h/2,y+(h/2)*K1);
        K3 = f(x+h/2,y+(h/2)*K2);
        K4 = f(x+h,y+h*K3);
 
        yn = y + (h/6)*(K1 + 2*K2 + 2*K3 + K4);
 
        x+=h;
 
        printf("%lf\t%lf\n",yn,x);
    }
 
    return 0;
}

Comments