rk4.py
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
"""
|
3 |
Created on Thu Mar 23 02:05:44 2017
|
4 |
RK4 Method
|
5 |
University of Sao Paulo
|
6 |
@author: Gabriel
|
7 |
"""
|
8 |
|
9 |
def rk4(ini,end,step,x0,u0,P,deltap,u,j): |
10 |
|
11 |
from numpy import * |
12 |
import copy |
13 |
|
14 |
t = ini |
15 |
x = x0.T |
16 |
# print x
|
17 |
# x = array([[P[6]],[P[7]],[x[2,0]]])
|
18 |
# print x
|
19 |
p = copy.copy(P) |
20 |
|
21 |
if j != -1: |
22 |
p[j] += deltap[j] |
23 |
|
24 |
A,B,C,D = Matrix(p, x0,u0) |
25 |
|
26 |
y = dot(C,x) + dot(D,u) |
27 |
|
28 |
output = append(t, y.T) |
29 |
|
30 |
while t < end:
|
31 |
K1 = step*(dot(A,x) + dot(B,u)) |
32 |
K2 = step*(dot(A,x + K1) + dot(B,u)) #x.T é a transposta de x
|
33 |
K3 = step*(dot(A,x + K2) + dot(B,u)) |
34 |
K4 = step*(dot(A,x + K3) + dot(B,u)) |
35 |
|
36 |
t += step |
37 |
|
38 |
x = x + (K1 + 2*K2 + 2*K3 + K4)/6 |
39 |
y = dot(C,x) + dot(D,u) |
40 |
# if t == ini + step:
|
41 |
# print "\n\nC*x\n"
|
42 |
# print C, x
|
43 |
# print dot(C,x)
|
44 |
# print "\n\nD*u\n"
|
45 |
# print D, u
|
46 |
# print dot(D,u)
|
47 |
# print "\n\ny\n"
|
48 |
# print y
|
49 |
|
50 |
output = vstack((output, append(t, y.T))) |
51 |
|
52 |
return output
|