Skip to content

Instantly share code, notes, and snippets.

@mbeltagy
Last active February 14, 2019 18:03
Show Gist options
  • Save mbeltagy/dd3e1edbdd5278b392ca98eef871445e to your computer and use it in GitHub Desktop.
Save mbeltagy/dd3e1edbdd5278b392ca98eef871445e to your computer and use it in GitHub Desktop.
Interpolations Example
using Interpolations, Plots
# Demonstrating interpolations
x=0:0.5:2π
y=sin.(x);
itp=LinearInterpolation(x,y);
y2=[itp(x) for x in 0:0.1:2];
x2=0:0.1:6;
y2=[itp(x) for x in x2];
itpC=CubicSplineInterpolation(x,y);
y3=[itpC(x) for x in x2];
gg(x)=Interpolations.gradient(itpC,x)[1]
itpCC=CubicSplineInterpolation(x,gg.(x));
ggg(x)=Interpolations.gradient(itpCC,x)[1]
plot(x,y, label="orig")
plot!(x2,y2, label="linear") # overwrites the first plot
plot!(x2,y3, label="cubic") # smoother plot
plot!(x2,gg.(x2),label="gradient based on cubic") # looks like a cosine
plot!(x2,ggg.(x2),label="double gradient based on cubic") # looks like a -sin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment