x’(t)=[0,-1;1,-2]x(t)+[0;2],x(0)=[1;2],t属于[0,1),在python中用欧拉法求解,T取0.1。
我写的代码
import numpy as np
from matplotlib.pyplot import *
Euler's method with integration step size T=0.1
T = 0.1 # step size
t = np.arange(0.,1,0.1) # time instants that the solution will be evaluated
u = np.array([[0];[1]])
x0[0]=np.array([[1];[2]])
def f(x0,u):
x=np.zeros(shape=(2,1))
x[0]=-1*x0[1]
x[1]=1*x0[0]-2*x0[1]+u
return np.squeeze(x)
If k in range(1,9):
x0[k]=x0[k-1]+f(x0[k-1])*T
我无论怎么改都会说维度有问题或者未命名函数,我实在是不知道怎么改了,求解答