Skip to content

Instantly share code, notes, and snippets.

@carstenbauer
Created March 9, 2021 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carstenbauer/74d2add2e4358a22746ed9b54f2ee835 to your computer and use it in GitHub Desktop.
Save carstenbauer/74d2add2e4358a22746ed9b54f2ee835 to your computer and use it in GitHub Desktop.
"""
meshgrid(xvec) = meshgrid(xvec, xvec)
Produces a 2D meshgrid `X,X` by repeating xvec in y-dimension and xvec in x-dimension.
"""
meshgrid(v::AbstractVector{T}) where T<:Number = meshgrid(v, v)
"""
meshgrid(xvec, yvec)
Produces a 2D meshgrid `X,Y` by repeating xvec in y-dimension and yvec in x-dimension.
"""
function meshgrid(vx::AbstractVector{T}, vy::AbstractVector{S}) where T<:Number where S<:Number
m, n = length(vy), length(vx)
vx = reshape(vx, 1, n)
vy = reshape(vy, m, 1)
(repeat(vx, m, 1), repeat(vy, 1, n))
end
using PyPlot
#scalar
x = 0:0.1:1
y = 0:0.1:1
f(x,y) = x^2 + y^2
plot3D(x,y,f.(x,y)) # plot a 3D line
#meshgrid
X,Y = meshgrid(x,y)
Z = f.(X,Y)
surf(X,Y,Z) # plot a 2D surface
contour3D(X,Y,Z) # plot contour lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment