使用来自链接的相同思路,并通过一些改进来控制箭头和网格点的大小:
SlopeField = function(FUN,xi = -5,xs = 5,yi = -5,ys = 5, radius = 0.1, grid.by = 0.25){
# FUN - given function ODE i.e:
# xi,xs - lower and upper bound - x - plot
# yi,ys - lower and upper bound - y - plot
# grid points
seqx = seq(xi,xs,grid.by)
seqy = seq(yi,ys,grid.by)
# plot
f = c(xi,xs)
h = c(yi,ys)
plot(f,h,main="Slope field", ylab = "Dependet variable", xlab = "Independet variable", pch = ".")
# arrows
for(x in seqx){
for(y in seqy){
ym = y
xm = x
slope = unlist(FUN(x,y))
if(is.na(slope)){
cor = "black"
} else if(slope > 0){
cor = "blue"
}else if (slope < 0) {
cor = "red"
}else if(slope == 0) {
cor = "green"
}
arrows(radius*cos(atan(slope)+pi)+xm,
radius*sin(atan(slope)+pi)+ym,
radius*cos(atan(slope))+xm,
radius*sin(atan(slope))+ym,
length = 0.2*radius, col= cor)
}
}
}
ode = function(t, y){
dydt <- y^2-t
list(dydt)
}
函数结果:
SlopeField(ode, xi = -2, xs = 5, yi = -2, ys = 2,radius = 0.1, grid.by = 0.25)