我需要使用带有方程组答案的循环来填充矩阵[i,j]。这应该很简单。
这是Python代码,我只想在R中而不是通过deSolve复制它
import numpy
M = numpy.zeros((2, 101)) # the matrix containing x,y values
t = 0.0
dt = 0.1
M[:,0]=[0,1] #initial values of [x,y]
for i in range(0,100):
t = t + dt #current value of t
M[0,i+1]=M[0,i]+dt*M[1,i] # dx/dt = y
M[1,i+1]=M[1,i]+dt*(-0.5*M[0,i]) # dy/dt = -0.5*x
last_val = M[:,100] #final values of [x y] at t=10
print ("At x(t=10):", last_val[0],", and at y(t=10):", last_val[1])