3D Vector Plot in Julia - plot

I am trying to plot an EM wave (propagating in the z-direction) vector field in Julia. I looked around and it looks like quiver is what I need to use and I have tried that with unsuccessful results. As far as I understand (x, y, z) are the origins of the vectors and (u, v, w) are the vectors themselves originating at the (x, y, z) points. Here is what I have so far but this doesn't seem to produce the correct plot. How can I get this to work? I'm open to try other plotting libs as well. Thanks in advance.
using Plots; gr()
t = 0; n = 100; k = 1; ω = 1; φ = π/4
x = y = w = zeros(n)
z = range(0, stop=10, length=n)
u = #. cos(k*z - ω*t)
v = #. sin(k*z - ω*t)
quiver(x, y, z, quiver=(u, v, w), projection="3d")

I'm not exactly sure this is the result you want but I've managed to make your code work in Julia v1.1 :
using PyPlot
pygui(true)
fig = figure()
ax = fig.gca(projection="3d")
t = 0; n = 100; k = 1; ω = 1; φ = π/4
x = y = w = zeros(n)
z = range(0, stop=10, length=n)
u = cos.(k*z .- ω*t)
v = sin.(k*z .- ω*t)
ax.quiver(x,y,z, u,v,w)
Or, with colors :
using PyPlot
using Random
function main()
pygui(true)
fig = figure()
ax = fig.gca(projection="3d")
t = 0; n = 100; k = 1; ω = 1; φ = π/4
x = y = w = zeros(n)
z = range(0, stop=10, length=n)
u = cos.(k*z .- ω*t)
v = sin.(k*z .- ω*t)
a = ((u[1], 0.8, 0.5), (u[2], 0.8, 0.5))
for i in 3:length(u)-2
a = (a..., (abs(u[i]), 0.8, 0.5))
end
c = ((0.4, 0.5, 0.4), (0.4, 0.9, 0.4), (0.1, 0.1, 0.1))
q = ax.quiver(x,y,z, u,v,w, color = a)
end
main()

Related

How to generate B-spline of degree zero using R

I am currently working with B-splines using R's function bs from the package splines and as a graphic example I would like to provide a figure showing the differences between set of splines with different degrees.
The problem is that bs only supports degrees bigger than 0.
A spline of degree zero, is nothing more than an indicator function for the given region defined by the knots, but I don't really know how to generate it.
This is what I've done so far
x<-seq(0,1,length.out =1000)
par(mfrow=c(3,1))
B1<-bs(x,knots = seq(0,1,length.out = 11)[-c(1,11)],Boundary.knots = c(0,1),intercept = T,degree = 1)
matplot(x,B1,type="l",lty=1,ylim = c(-0.1,1.2),xlab = "",ylab = "")
abline(v=seq(0,1,length.out = 11),lty=2)
legend("top", legend ="B-splines of order 2")
B2<-bs(x,knots = seq(0,1,length.out = 11)[-c(1,11)],Boundary.knots = c(0,1),intercept = T,degree = 2)
matplot(x,B2,type="l",lty=1,ylim = c(-0.1,1.2),xlab = "",ylab = "")
abline(v=seq(0,1,length.out = 11),lty=2)
legend("top", legend ="B-splines of order 3")
B3<-bs(x,knots = seq(0,1,length.out = 11)[-c(1,11)],Boundary.knots = c(0,1),intercept = T,degree = 3)
matplot(x,B3,type="l",lty=1,ylim = c(-0.1,1.2),xlab = "",ylab = "")
abline(v=seq(0,1,length.out = 11),lty=2)
legend("top", legend ="B-splines of order 4")
This image taken from Hastie et.al (2017) is basically what I am missing.
Thanks in advance
As I understand from the comments, you want a function that given an input vector x of n points returns a series of n-1 "splines"; where the ith spline is defined as having the value 1 in the range x[i] < x < x[i+1] or 0 elsewhere.
We can do this so:
x <- seq(0,1,length.out =10)
zero_spline = function(x, xout, n=1000) {
if (missing(xout)) xout = seq(min(x), max(x), length.out = n)
zs = data.frame()
y = numeric(length(xout))
for (i in 1:(length(x)-1L)) {
yi = y
yi[(xout > x[i]) & (xout < x[i+1])] = 1
zs = rbind(zs, data.frame(xout, yi, interval=i))
}
zs
}
zs = zero_spline(x, n=100)
library(ggplot2)
ggplot(zs, aes(xout, yi, color=factor(interval))) +
geom_line()

Can't plot the complete Cobweb diagram in Scilab

I was trying to produce the cobweb diagram of the following equation
xn+1 = xn * e ^ a(1−xn/1000), x0 = 200 and a is a constant
But somehow I couldn't plot the original function and y = (x)
The following is my code in scilab
x0 = 200
n = 20
a = 2
deff("y = F(x)", "y = x*%e^(a*(1-x/1000))")
cw = [x0 0];
x = x0;
for i = 1:n
newx = F(x);
cw = [cw;x newx; newx newx];
x = newx
end
for x = x0;
y1 = x*%e^(a*(1-x/1000))
y2 = x
end
plot2d(cw(:,1),cw(:,2),4)
plot2d(x, y1, 5)
plot2d(x, y2, 6)
title("Cobweb Diagram")
xlabel("x(n)")
ylabel("x(n+1)")

How to animate changing histogram in Plots.jl?

I'm working from the following example and failing miserably
# initialize the attractor
n = 1500
dt = 0.02
σ, ρ, β = 10., 28., 8/3
x, y, z = 1., 1., 1.
# initialize a 3D plot with 1 empty series
plt = path3d(1, xlim=(-25,25), ylim=(-25,25), zlim=(0,50),
xlab = "x", ylab = "y", zlab = "z",
title = "Lorenz Attractor", marker = 1)
# build an animated gif, saving every 10th frame
#gif for i=1:n
dx = σ*(y - x) ; x += dt * dx
dy = x*(ρ - z) - y ; y += dt * dy
dz = x*y - β*z ; z += dt * dz
push!(plt, x, y, z)
end every 10
I have simulations in which I want to calculate and plot a histogram every so often, and then animate all those histograms. This is what I have so far as a toy example:
using Distributions
using Plots
Plots.gr()
p = rand(Normal(0,1), 10000)
myplot = Plots. histogram(p)
savefig("initial.png")
anim = #animate for i=1:10
p = rand(Normal(0,1), 10000)
push!(myplot, Plots.histogram(p))
end
gif(anim, "mygif.gif", fps = 1)
As you can probably tell, I don't really get how this #animate thing works and am just guessing at the syntax here. I know that I could save a bunch of PNGs and later animate, but I want to try it this way.
anim = #animate for i=1:10
p = rand(Normal(0,1), 10000)
histogram(p)
end
gif(anim, "mygif.gif", fps = 1)
should work.

Binomial Tree Plot in R

I have a little issue with the binomial tree plot in R; I'm using the package fOptions. Given St=39, K=40, T1=0.5, r=0.02, sigma=0.2, n=2, I use the following code:
CRRTree<- BinomialTreeOption(TypeFlag='ce',39,40,0.5,0.02,0.02,0.2,2)
BinomialTreePlot(CRRTree)
and the corresponding plot is
I have two problems.
First: I want that the x axis starts from zero and goes to 2
Second: I don't undestand why the upper value of the tree is not showed in the picture; how can I fix it?
Thank you very much.
EDIT: I solved the second problem in the easiest way, I think. It was sufficient to code the plot in this way:
BinomialTreePlot(CRRTree,ylim=c(-2,2.5))
There is an easy way to solve also the problem of making the tree starts from 0?
You will have to modify the code for the BinomialTreePlot function. For example, you could try something like that:
my_BinomialTreePlot<-function (BinomialTreeValues, dx = -0.025, dy = 0.4, cex = 1,
digits = 2, ...)
{
Tree = round(BinomialTreeValues, digits = digits)
depth = ncol(Tree)
plot(x = c(0, depth-1), y = c(-depth + 1, depth - 1), type = "n",
col = 0, ...)
points(x = 0, y = 0)
text(0 + dx, 0 + dy, deparse(Tree[1, 1]), cex = cex)
for (i in 1:(depth - 1)) {
y = seq(from = -i, by = 2, length = i + 1)
x = rep(i, times = length(y)) + 0
points(x, y, col = 1)
for (j in 1:length(x)) text(x[j] + dx, y[j] + dy, deparse(Tree[length(x) +
1 - j, i + 1]), cex = cex)
y = (-i):i
x = rep(c(i, i-1), times = 2 * i)[1:length(y)]
lines(x, y, col = 2)
}
invisible()
}
Then use it like this:
CRRTree<- BinomialTreeOption(TypeFlag='ce',39,40,0.5,0.02,0.02,0.2,2)
my_BinomialTreePlot(CRRTree,xlim=c(-0.1,2), ylim=c(-2.5,2.5))

R: Converting from Continuous 2D Points to Continuous 2D Vectors

I have a fairly large dataframe (df) with pathing information in the form of continuous x,y coordinates:
df$x
df$y
With these data, I would like to:
1. Calculate a set of continuous vectors
2. Determine the angle between each of these vectors (in degrees)
3. Count the number of angles in the dataframe that meet a certain threshold (i.e. <90°)
Thank you!
Please see the post here for reference
require("ggplot2")
Hypocycloid <- function(num_points) {
r = 1
k = 3
theta = seq(from = 0, to = 2*pi, length.out = num_points)
x = r*(k - 1)*cos(theta) + r*cos((k - 1)*theta)
y = r*(k - 1)*sin(theta) - r*sin((k - 1)*theta)
df = data.frame(x = x, y = y)
gg1 = ggplot(df,
aes(x = x, y = y),
size = 1) +
geom_path()
print(gg1)
return(df)
}
ComputeUnitVectors <- function(points_df) {
npoints = nrow(points_df)
vx = points_df$x[2:npoints] - points_df$x[1:(npoints-1)]
vy = points_df$y[2:npoints] - points_df$y[1:(npoints-1)]
length = sqrt(vx^2 + vy^2)
return(data.frame(vx = vx/length, vy = vy/length))
}
ComputeAngles <- function(vectors_df) {
Angle <- function(v1, v2) {
return(acos(as.numeric(v1) %*% as.numeric(v2))*180/pi)
}
nvectors = nrow(vectors_df)
v1 = vectors_df[1:(nvectors-1),]
v2 = vectors_df[2:nvectors,]
v_df = cbind(v1, v2)
angle = apply(v_df, 1, function(row) {Angle(row[1:2], row[3:4])})
return(data.frame(angle))
}
points.df = Hypocycloid(20)
vectors.df = ComputeUnitVectors(points.df)
print(vectors.df)
angles.df = ComputeAngles(vectors.df)
print(angles.df)

Resources