Cubic spline interpolation in Julia with irregular grids - r

I'm porting some code from R to Julia, and struggling with translating R's spline function. I need a Julia function:
function spline_j(x,y,xout)
that yields the same return as calling the R function:
spline(x,y,,"fmm",,,xout)
i.e. using the method of Forsyth, Malcolm and Moler, which is the default method in R.
My x and y are always 1-dimensional, but the points of x are not regularly spaced. That non-regularity seems to rule out using the pure-Julia Interpolations package as the documentation states "presently only LinearInterpolation supports irregular grids".
The Dierckx package supports irregular x, so a candidate for spline_j is:
using Dierckx
function spline_j(x, y, xout)
spl = Dierckx.Spline1D(x, y)
spl(xout)
end
which matches R's spline function if method is "natural".
Is it possible to replicate R's "fmm" method in Julia?

Following this PR https://github.com/JuliaMath/Interpolations.jl/pull/238 (merged as #243), Interpolations.jl actually has a number of excellent monotonic spline interpolation algorithms that support irregular grids, including Fritsch-Carlson (1980), Fritsch-Butland (1984), and Steffen (1990).
It doesn't seem to be reflected in the docs yet, but the options are visible in the following git diff
For example:
using Interpolations, Plots
x = sort(2*rand(10))
y = x.^2 .+ rand.()
itp = interpolate(x,y,FritschCarlsonMonotonicInterpolation())
xq = minimum(x):0.01:maximum(x)
plot(x,y, seriestype=:scatter, label="Data", legend=:topleft, framestyle=:box)
plot!(xq, itp.(xq), label="Interpolation")
If you prefer different interpolation methods, you can also substitute
FiniteDifferenceMonotonicInterpolation,FritschButlandMonotonicInterpolation, or SteffenMonotonicInterpolation for FritschCarlsonMonotonicInterpolation.

Related

Polar Plots in Julia

I came across a Rose plot obtained with Plots.jl package in Julia:
https://goropikari.github.io/PlotsGallery.jl/src/rose.html
Two things are not clear to me. The first one is what is Julia doing on the line:
θ = 0:2pi/n:2pi
It seems that the output is (lower limit):(bin size):(upper limit) but I haven't seen this type of arithmetics previously where two ranges are divided. The second thing is that I would like to obtain a histogram polar plot as it was done with R (Making a polar histogram in ggplot2), but I haven't found the documentation for line styles or how to do it in Plots.jl. Thanks.
Note that start:step:end is a common syntax in creating ranges. Let's dissect the line:
# `pi` is a reserved variable name in Julia
julia> pi
π = 3.1415926535897...
# A simple division
julia> 2pi/1
6.283185307179586
# Simple multiplication
julia> 2pi
6.283185307179586
So the 0:2pi/n:2pi creates an object of type StepRange that starts from 0 up to 2pi with steps of size 2pi/n.
In the case of desired plot, you can use the PlotlyJS.jl package. As they provided an example here. (Scroll down until you see "Polar Bar Chart")
I tested the code myself, and it's reproducible expectedly. Unfortunately, I don't know anything about the R language.
julia> using RDatasets, DataFrames, PlotlyJS
julia> df = RDatasets.dataset("datasets", "iris");
julia> sepal = df.SepalWidth;
julia> plot(
barpolar(
r=sepal
)
)
Results in:

What is the equivalent of Matlab's mesh function in Julia Plots.jl

In Matlab, we would first use [x, y] = meshgrid to generate the grid, then use mesh(x, y, z) to plot the 3D plot. I want to use the same funtionality in Julia Plots.jl, which API should I use? And how can I achieve that?
Thanks a lot in advance!!!
use surface
using Plots
xs = range(-2, stop=2, length=100)\
ys = range(-pi, stop=pi, length=100)
f(x,y) = x*sin(y)
surface(xs, ys, f)
In modern Julia, v1.17, the approach is to create x and y ranges. Julia has changed over the years, and used to have linspace - it doesn't anymore.
There are three ways to create a range:
x = start:step:end
x = range(start,end,step=step)
x = range(start,end,length=npts)
You will also need Plots. If you precompile it, it takes less time to load.
]
pkg > add Plots
pkg > precompile
pkg > Ctrl-C
You need to select your backend for Plots. Choices are:
pyplot() to select PyPlot (also requires Python's MatPlotLib)
plotly() to select Plotly (displays in web browser)
gr() to select GR, the default
Finally, you need to use surface to draw the surface. The function surface can take either a function or a matrix of z values. The function takes two parameters, x and y. Either the function is supplied directly, or it is applied to the ranges:
z = f.(x',y);
One of the ranges is transposed with ', and output suppressed with ;
Surface also takes optional parameters:
fill = :fillname
legend = true | false
size = (width,height)
clims = (lowlimit,highlimit)
An example:
using Plots
plotly()
x=range(-5,5,length=101)
y=range(-5,5,length=101)
function f(x,y)
r = sqrt(x^2+y^2)
sinc(r)
end
z = f.(x',y);
surface(x,y,z,size=(1600,1000),fill=:greens,legend=false)

How do I write a piecewise Differential Equation in Julia?

I am new to Julia, I would like to solve this system:
where k1 and k2 are constant parameters. However, I=0 when y,0 or Ky otherwise, where k is a constant value.
I followed the tutorial about ODE. The question is, how to solve this piecewise differential equation in DifferentialEquations.jl?
Answered on the OP's cross post on Julia Discourse; copied here for completeness.
Here is a (mildly) interesting example $x''+x'+x=\pm p_1$ where the sign of $p_1$ changes when a switching manifold is encountered at $x=p_2$. To make things more interesting, consider hysteresis in the switching manifold such that $p_2\mapsto -p_2$ whenever the switching manifold is crossed.
The code is relatively straightforward; the StaticArrays/SVector/MVector can be ignored, they are only for speed.
using OrdinaryDiffEq
using StaticArrays
f(x, p, t) = SVector(x[2], -x[2]-x[1]+p[1]) # x'' + x' + x = ±p₁
h(u, t, integrator) = u[1]-integrator.p[2] # switching surface x = ±p₂;
g(integrator) = (integrator.p .= -integrator.p) # impact map (p₁, p₂) = -(p₁, p₂)
prob = ODEProblem(f, # RHS
SVector(0.0, 1.0), # initial value
(0.0, 100.0), # time interval
MVector(1.0, 1.0)) # parameters
cb = ContinuousCallback(h, g)
sol = solve(prob, Vern6(), callback=cb, dtmax=0.1)
Then plot sol[2,:] against sol[1,:] to see the phase plane - a nice non-smooth limit cycle in this case.
Note that if you try to use interpolation of the resulting solution (i.e., sol(t)) you need to be very careful around the points that have a discontinuous derivative as the interpolant goes a little awry. That's why I've used dtmax=0.1 to get a smoother solution output in this case. (I'm probably not using the most appropriate integrator either but it's the one that I was using in a previous piece of code that I copied-and-pasted.)

Solve or plot the reverse of an equation in R

I plotted an expression curve, i.e.curve(-log((1-x)/0.9999)/x,ylim=c(0,4)).
However, I want to see the reverse relationship, i.e. y changes over x instead of x changes over y. Are there any R function can plot it automatically? Or a function that can solve the equation?
There are two obvious choices:
(i) derive the inverse function algebraically (trivial in this case),
That is, take y=-log((1-x)/0.9999) and make x the subject of the equation (which would require straightforward algebraic manipulation suitable for a question on math.SE if it's not obvious how to proceed)...
... and then use curve on the result of that, or
(ii) use plot rather than curve to plot a set of (x,y) pairs (set type="l" to get a curve), and simply interchange which is x and which is y in the call to plot.

How to make topographic map from sparse sampling data?

I need to make a topographic map of a terrain for which I have only fairly sparse samples of (x, y, altitude) data. Obviously I can't make a completely accurate map, but I would like one that is in some sense "smooth". I need to quantify "smoothness" (probably the reciprocal the average of the square of the surface curvature) and I want to minimize an objective function that is the sum of two quantities:
The roughness of the surface
The mean square distance between the altitude of the surface at the sample point and the actual measured altitude at that point
Since what I actually want is a topographic map, I am really looking for a way to construct contour lines of constant altitude, and there may be some clever geometric way to do that without ever having to talk about surfaces. Of course I want contour lines also to be smooth.
Any and all suggestions welcome. I'm hoping this is a well-known numerical problem. I am quite comfortable in C and have a working knowledge of FORTRAN. About Matlab and R I'm fairly clueless.
Regarding where our samples are located: we're planning on roughly even spacing, but we'll take more samples where the topography is more interesting. So for example we'll sample mountainous regions more densely than a plain. But we definitely have some choices about sampling, and could take even samples if that simplifies matters. The only issues are
We don't know how much terrain we'll need to map in order to find features that we are looking for.
Taking a sample is moderately expensive, on the order of 10 minutes. So sampling a 100x100 grid could take a long time.
Kriging interpolation may be of some use for smoothly interpolating your sparse samples.
R has many different relevant tools. In particular, have a look at the spatial view. A similar question was asked in R-Help before, so you may want to look at that.
Look at the contour functions. Here's some data:
x <- seq(-3,3)
y <- seq(-3,3)
z <- outer(x,y, function(x,y,...) x^2 + y^2 )
An initial plot is somewhat rough:
contour(x,y,z, lty=1)
Bill Dunlap suggested an improvement: "It often works better to fit a smooth surface to the data, evaluate that surface on a finer grid, and pass the result to contour. This ensures that contour lines don't cross one another and tends to avoid the spurious loops that you might get from smoothing the contour lines themselves. Thin plate splines (Tps from library("fields")) and loess (among others) can fit the surface."
library("fields")
contour(predict.surface(Tps(as.matrix(expand.grid(x=x,y=y)),as.vector(z))))
This results in a very smooth plot, because it uses Tps() to fit the data first, then calls contour. It ends up looking like this (you can also use filled.contour if you want it to be shaded):
For the plot, you can use either lattice (as in the above example) or the ggplot2 package. Use the geom_contour() function in that case. An example can be found here (ht Thierry):
ds <- matrix(rnorm(100), nrow = 10)
library(reshape)
molten <- melt(data = ds)
library(ggplot2)
ggplot(molten, aes(x = X1, y = X2, z = value)) + geom_contour()
Excellent review of contouring algorithm, you might need to mesh the surface first to interpolate onto a grid.
maybe you can use:
GEOMap
geomapdata
gtm
with
Matrix
SparseM
slam
in R

Resources