So, do we always need a grid? I mean, for a region that cannot be represented by a tensor product (or direct product) of X
and Y
, is it not possible to create a surface plot?
This looks good. Thank you!
using Plots
using Interpolations
#original data
xs = 0.1:0.05:2.0
ys = 0.2:0.1:2.0
X = [x for x = xs for _ = ys]
Y = [y for _ = xs for y = ys]
Z = [1/x + y*x^2 for x in xs for y in ys]
#interpolations
ZIp = [1/x + y*x^2 for x in xs, y in ys] #make a length(xs)×length(ys) matrix
interp_linear = linear_interpolation((xs, ys), ZIp) # linear interpolation
interp_cubic = cubic_spline_interpolation((xs, ys), ZIp) # cubic spline interpolation
#define a interpolation function
ZIp1(x,y) = interp_linear(x,y)
ZIp2(x,y) = interp_cubic(x,y)
#interpolated region
xsIp = 0.1:0.01:2.0
ysIp = 0.2:0.05:2.0
XIp = [x for x = xsIp for _ = ysIp]
YIp = [y for _ = xsIp for y = ysIp]
plot(XIp,YIp,ZIp1.(XIp,YIp),st=:scatter,markercolor=:blue,markerstrokecolor=:blue,markerstrokewidth=0,markersize=3,label="linear interpolation")
plot!(X,Y,Z,st=:scatter,markercolor=:pink,markerstrokecolor=:pink,markerstrokewidth=0,markersize=2,label="original data")
fig1 = plot!(xlabel="x",ylabel="y",zlabel="z",foreground_color_legend=nothing)
plot(XIp,YIp,ZIp2.(XIp,YIp),st=:scatter,markercolor=:blue,markerstrokecolor=:blue,markerstrokewidth=0,markersize=3,label="cubic spline interpolation")
plot!(X,Y,Z,st=:scatter,markercolor=:pink,markerstrokecolor=:pink,markerstrokewidth=0,markersize=2,label="original data")
fig2 = plot!(xlabel="x",ylabel="y",zlabel="z",foreground_color_legend=nothing)
plot(fig1,fig2,layout=(1,2))