using GLMakie,Symbolics
6-gradient-vector-fields
1. load pacages
2.define symbolics variables
@variables x, y
=Differential(x)
Dx=Differential(y) Dy
(::Differential) (generic function with 3 methods)
3. make data
使用Symbolics.jl
方法构建求导函数.
of
表示原函数fx
沿 x 轴的导函数fy
沿 y轴方向的导函数
=x^2+2y^2
f=5-x^2-2y^2
g=x+2y+3
h=[f,g,h]
funcs=[]
dffor fun in funcs
local of=build_function(fun, x,y)|>eval
local fx=fun|>Dx|>expand_derivatives|>expr->build_function(expr, x,y)|>eval
local fy=fun|>Dy|>expand_derivatives|>expr->build_function(expr, x,y)|>eval
push!(df,[of,fx,fy])
end
4. plot
=Figure(resolution=(900,600))
fig=ys=range(-3,3,10)
xsfor i in 1:3
local ax1=Axis(fig[1,i],title=L"%$(funcs[i])")
local ax2=Axis(fig[2,i])
local zs=[df[i][1](x,y) for x in xs, y in ys]
local us=[df[i][2](x,y) for x in xs, y in ys]
local vs=[df[i][3](x,y) for x in xs, y in ys]
contour!(ax1,xs,ys,zs)
arrows!( ax2,xs, ys, us, vs,lengthscale=i==3 ? 0.2 : 0.05,color=(:purple,0.8))
end
fig