05-30-2015, 06:03 PM

The Runge Kutta 4th Order is a method for solving differential equations involving the form: dy/dx = f(x,y), where:

x_n+1 = x_n + h

y_n+1 = y_n + (k1 + 2*k2 + 2*k3 + k4)/6

Where:

k1 = h * f(x_n, y_n)

k2 = h * f(x_n + h/2, y_n + k1/2)

k3 = h * f(x_n + h/2, y_n + k2/2)

k4 = h * f(x_n + h, y_n + k3)

Variables used:

A = x_n

B = y_n

C = x_n+1

D = y_n+1

H = step

K = k1

L = k2

M = k3

N = k4

Results are stored in lists L1 and L2. L1 represents the x coordinates, L2 represents the y coordinates.

Both DIFFTBL and RK4 return the same output. The difference is that DIFFTBL uses an Input box and no-pass through arguments and RK4 uses five pass-through arguments. This way, RK4 could be used as a subroutine.

Both return the results in a matrix, M1.

DIFFTBL:

RK4:

x_n+1 = x_n + h

y_n+1 = y_n + (k1 + 2*k2 + 2*k3 + k4)/6

Where:

k1 = h * f(x_n, y_n)

k2 = h * f(x_n + h/2, y_n + k1/2)

k3 = h * f(x_n + h/2, y_n + k2/2)

k4 = h * f(x_n + h, y_n + k3)

Variables used:

A = x_n

B = y_n

C = x_n+1

D = y_n+1

H = step

K = k1

L = k2

M = k3

N = k4

Results are stored in lists L1 and L2. L1 represents the x coordinates, L2 represents the y coordinates.

Both DIFFTBL and RK4 return the same output. The difference is that DIFFTBL uses an Input box and no-pass through arguments and RK4 uses five pass-through arguments. This way, RK4 could be used as a subroutine.

Both return the results in a matrix, M1.

DIFFTBL:

Code:

`EXPORT DIFFTBL()`

BEGIN

// EWS 2015-05-29

// Radian

HAngle:=0;

// Input

LOCAL f;

INPUT({{f,[8]},A,B,H,S},"Data Input",

{"dy/dx=","x0=","y0=","Step=",

"# Steps="});

L1:={A}; L2:={B};

// Main Loop

FOR I FROM 1 TO S DO

// k1

X:=A;

Y:=B;

K:=H*EVAL(f);

// k2

X:=A+H/2;

Y:=B+K/2;

L:=H*EVAL(f);

// k3

Y:=B+L/2;

M:=H*EVAL(f);

// k4

X:=A+H;

Y:=B+M;

N:=H*EVAL(f);

// point

A:=A+H;

B:=B+(K+2*L+2*M+N)/6;

L1:=CONCAT(L1,{A});

L2:=CONCAT(L2,{B});

END;

M1:=list2mat(CONCAT(L1,L2),

SIZE(L1));

M1:=TRN(M1);

RETURN M1;

END;

RK4:

Code:

`EXPORT RK4(f,A,B,H,S)`

BEGIN

// EWS 2015-05-29

// Radian

HAngle:=0;

// Input

L1:={A}; L2:={B};

// Main Loop

FOR I FROM 1 TO S DO

// k1

X:=A;

Y:=B;

K:=H*EVAL(f);

// k2

X:=A+H/2;

Y:=B+K/2;

L:=H*EVAL(f);

// k3

Y:=B+L/2;

M:=H*EVAL(f);

// k4

X:=A+H;

Y:=B+M;

N:=H*EVAL(f);

// point

A:=A+H;

B:=B+(K+2*L+2*M+N)/6;

L1:=CONCAT(L1,{A});

L2:=CONCAT(L2,{B});

END;

M1:=list2mat(CONCAT(L1,L2),

SIZE(L1));

M1:=TRN(M1);

RETURN M1;

END;