I would like to plot a two variable function(s) (e_pos and e_neg in the code). Here, t and a are constants which I have given the value of 1.
My code to plot this function is the following:
t = 1
a = 1
kx = ky = range(3.14/a, step=0.1, 3.14/a)
# Doing a meshgrid for values of k
KX, KY = kx'.*ones(size(kx)[1]), ky'.*ones(size(ky)[1])
e_pos = +t.*sqrt.((3 .+ (4).*cos.((3)*KX*a/2).*cos.(sqrt(3).*KY.*a/2) .+ (2).*cos.(sqrt(3).*KY.*a)));
e_neg = -t.*sqrt.((3 .+ (4).*cos.((3)*KX*a/2).*cos.(sqrt(3).*KY.*a/2) .+ (2).*cos.(sqrt(3).*KY.*a)));
using Plots
plot(KX,KY,e_pos, st=:surface,cmap="inferno")
If I use Plots this way, sometimes I get an empty 3D plane without the surface. What am I doing wrong? I think it may have to do with the meshgrids I did for kx and ky, but I am unsure.
Edit: I also get the following error:
I changed some few things in my code.
First, I left the variables as ranges. Second, I simply computed the functions I needed without mapping the variables onto them. Here's the code:
t = 2.8
a = 1
kx = range(-pi/a,stop = pi/a, length=100)
ky = range(-pi/a,stop = pi/a, length=100)
#e_pos = +t*np.sqrt(3 + 4*np.cos(3*KX*a/2)*np.cos(np.sqrt(3)*KY*a/2) + 2*np.cos(np.sqrt(3)*KY*a))
e_pos(kx,ky) = t*sqrt(3+4cos(3*kx*a/2)*cos(sqrt(3)*ky*a/2) + 2*cos(sqrt(3)*ky*a))
e_neg(kx,ky) = -t*sqrt(3+4cos(3*kx*a/2)*cos(sqrt(3)*ky*a/2) + 2*cos(sqrt(3)*ky*a))
# Sort of broadcasting?
e_posfunc = e_pos.(kx,ky);
e_negfunc = e_neg.(kx,ky);
For the plotting I simply used the GR backend:
using Plots
gr()
plot(kx,ky,e_pos,st=:surface)
plot!(kx,ky,e_neg,st=:surface, xlabel="kx", ylabel="ky",zlabel="E(k)")
I got what I wanted!
Related
I am learning how to create plots with slider bars. Here is my code based off the first example of this tutorial
using Plots
gr()
using GLMakie
function plotLaneEmden(log_delta_xi=-4, n=3)
fig = Figure()
ax = Axis(fig[1, 1])
sl_x = Slider(fig[2, 1], range = 0:0.01:4.99, startvalue = 3)
sl_y = Slider(fig[1, 2], range = -6:0.01:0.1, horizontal = false, startvalue = -2)
point = lift(sl_x.value, sl_y.value) do n, log_delta_xi
Point2f(n, log_delta_xi)
end
plot(n, 1 .- log_delta_xi.^2/6, linecolor = :green, label="n = $n")
xlabel!("ξ")
ylabel!("θ")
end
plotLaneEmden()
When I run this, it gives UndefVarError: plot not defined. What am I missing here?
It looks like you are trying to mix and match Plots.jl and Makie.jl. Specifically, the example from your link is entirely for Makie (specifically, with the GLMakie backend), while the the plot function you are trying to add uses syntax specific to the Plots.jl version of plot (specifically including linecolor and label keyword arguments).
Plots.jl and Makie.jl are two separate and unrelated plotting libraries, so you have to pick one and stick with it. Since both libraries export some of the same function names, using both at once will lead to ambiguity and UndefVarErrors if not disambiguated.
The other potential problem is that it looks like you are trying to make a line plot with only a single x and y value (n and log_delta_xi are both single numbers in your code as written). If that's what you want, you'll need a scatter plot instead of a line plot; and if that's not what you want you'll need to make those variables vectors instead somehow.
Depending on what exactly you want, you might try something more along the lines of (in a new session, using only Makie and not Plots):
using GLMakie
function plotLaneEmden(log_delta_xi=-4, n=3)
fig = Figure()
ax = Axis(fig[1, 1], xlabel="ξ", ylabel="θ")
sl_x = Slider(fig[2, 1], range = 0:0.01:4.99, startvalue = n)
sl_y = Slider(fig[1, 2], range = -6:0.01:0.1, horizontal = false, startvalue = log_delta_xi)
point = lift(sl_x.value, sl_y.value) do n, log_delta_xi
Point2f(n, 1 - log_delta_xi^2/6)
end
sca = scatter!(point, color = :green, markersize = 20)
axislegend(ax, [sca], ["n = $n"])
fig
end
plotLaneEmden()
Or, below, a simple example for interactively plotting a line rather than a point:
using GLMakie
function quadraticsliders(x=-5:0.01:5)
fig = Figure()
ax = Axis(fig[1, 1], xlabel="X", ylabel="Y")
sl_a = Slider(fig[2, 1], range = -3:0.01:3, startvalue = 0.)
sl_b = Slider(fig[1, 2], range = -3:0.01:3, horizontal = false, startvalue = 0.)
points = lift(sl_a.value, sl_b.value) do a, b
Point2f.(x, a.*x.^2 .+ b.*x)
end
l = lines!(points, color = :blue)
onany((a,b)->axislegend(ax, [l], ["$(a)x² + $(b)x"]), sl_a.value, sl_b.value)
limits!(ax, minimum(x), maximum(x), -10, 10)
fig
end
quadraticsliders()
ETA: A couple examples closer to what you might be looking for
I am trying to plot histograms of different columns of a dataframe in subplots.
plt_count = 1
for i = names(abalone)[2:end]
p[plt_count]=histogram(abalone[:,i])
plt_count += 1
end
plot(p, layout=(3,3), legend=false)
This is what I tried. But I can't come up with the right definition for the array p. How do I define p?
Improvements to the code will also be helpful.
If you don't care about the type stability, you can make Any type array.
ps = Array{Any}(nothing, 3)
ps[1] = plot([2,3,4])
ps[2] = plot([1,5])
ps[3] = plot([10,5,1,0])
#show typeof(ps)
plot(ps..., layout=(3,1))
If you want to create an array of Plot type specifically, one approach is to initialize an array with a dummy plot, then replace later.
ps = repeat([plot(1)], 3)
ps[1] = plot([2,3,4])
ps[2] = plot([1,5])
ps[3] = plot([10,5,1,0])
#show typeof(ps)
plot(ps..., layout=(3,1))
In Julia, I want to solve a system of ODEs with external forcings g1(t), g2(t) like
dx1(t) / dt = f1(x1, t) + g1(t)
dx2(t) / dt = f2(x1, x2, t) + g2(t)
with the forcings read in from a file.
I am using this study to learn Julia and the package DifferentialEquations, but I am having difficulties finding the correct approach.
I could imagine that using a callback could work, but that seems pretty cumbersome.
Do you have an idea of how to implement such an external forcing?
You can use functions inside of the integration function. So you can use something like Interpolations.jl to build an interpolating polynomial from the data in your file, and then do something like:
g1 = interpolate(data1, options...)
g2 = interpolate(data2, options...)
p = (g1,g2) # Localize these as parameters to the model
function f(du,u,p,t)
g1,g2 = p
du[1] = ... + g1[t] # Interpolations.jl interpolates via []
du[2] = ... + g2[t]
end
# Define u0 and tspan
ODEProblem(f,u0,tspan,p)
Thanks for a nice question and nice answer by #Chris Rackauckas.
Below a complete reproducible example of such a problem. Note that Interpolations.jl has changed the indexing to g1(t).
using Interpolations
using DifferentialEquations
using Plots
time_forcing = -1.:9.
data_forcing = [1,0,0,1,1,0,2,0,1, 0, 1]
g1_cst = interpolate((time_forcing, ), data_forcing, Gridded(Constant()))
g1_lin = scale(interpolate(data_forcing, BSpline(Linear())), time_forcing)
p_cst = (g1_cst) # Localize these as parameters to the model
p_lin = (g1_lin) # Localize these as parameters to the model
function f(du,u,p,t)
g1 = p
du[1] = -0.5 + g1(t) # Interpolations.jl interpolates via ()
end
# Define u0 and tspan
u0 = [0.]
tspan = (-1.,9.) # Note, that we would need to extrapolate beyond
ode_cst = ODEProblem(f,u0,tspan,p_cst)
ode_lin = ODEProblem(f,u0,tspan,p_lin)
# Solve and plot
sol_cst = solve(ode_cst)
sol_lin = solve(ode_lin)
# Plot
time_dense = -1.:0.1:9.
scatter(time_forcing, data_forcing, label = "discrete forcing")
plot!(time_dense, g1_cst(time_dense), label = "forcing1", line = (:dot, :red))
plot!(sol_cst, label = "solution1", line = (:solid, :red))
plot!(time_dense, g1_lin(time_dense), label = "forcing2", line = (:dot, :blue))
plot!(sol_lin, label = "solution2", line = (:solid, :blue))
I'm having some problems with my code. Here it is:
lambdaz = 1.2;
n = 24;
mu = 0.00055e9;
lambda = sym('lambda','clear');
W = (((2.*mu)./n.^2)).*((lambda.^n)+(lambdaz.^n)+((lambda.^-n).*(lambdaz.^-n))-3);
dW_dlambda = diff(W, lambda);
W2=(((2.*mu)./n.^2).*(lambda.^n))+(((2.*mu)./n.^2).*(lambdaz.^n))+(((2.*mu)./n.^2).*((lambda.^-n).*(lambdaz.^-n)))-(3.*((2.*mu)./n.^2))
dW2_dlambda=diff(W2,lambda)
x=((((lambda.^2).*(lambdaz))-1).^-1).*(dW_dlambda);
x2=((((lambda.^2).*(lambdaz))-1).^-1).*(dW2_dlambda)
P2 = int(x2,lambda)
P=int(x,lambda);
P=(0:1000:26700)
plot(lambda,P)
When I try to plot lambda against P I get the "conversion to double from sym is not possible" error message. I'm not particularly fantastic at Matlab so any help would be gratefully received!
The plot function only works for numeric inputs. Both lambda and P are symbolic expressions (at least before overwrote P by setting it equal to a vector after the integration) that cannot be directly converted to to floating point. You get the same error if try to something like double(sym('exp(x)')). You have two options. The first is the ezplot function in the Symbolic Toolbox:
...
P = int(x,lambda);
ezplot(P,[-5 5]); % Plot's P from lambda = -5 to lambda = 5
Or you can use the subs function:
...
P = int(x,lambda);
lambda = -5:0.01:5;
plot(lambda,real(subs(P,'lambda',lambda)))
axis([lambda(1) lambda(end) -1e15 1e15])
I used real to suppress a warning for negative values of lambda.
I'm trying to draw sigmoid function using this code on scilab, but the result I got is not from the equation. what's wrong with my code?
x = -6:1:6;
y = 1/(1+%e^-x)
y =
0.0021340
0.0007884
0.0002934
0.0001113
0.0000443
0.0000196
0.0000106
0.0000072
0.0000060
0.0000055
0.0000054
0.0000053
0.0000053
http://en.wikipedia.org/wiki/Sigmoid_function
thank you so much
Try:
-->function [y] = f(x)
--> y = 1/(1+%e^-x)
-->endfunction
-->x = -6:1:6;
-->fplot2d(x,f)
which yields:
Your approach calculates the pseudoinverse of the (1+%e.^x) vector. You can verify by executing: (1+%e^-x)*y
Here are two things you could do:
x = -6:1:6; y = ones(x)./(1+%e.^-x)
This gives the result you need. This performs element-wise division as expected.
Another approach is:
x = -6:1:6
deff("z = f(x)", "z = 1/(1+%e^-x)")
// The above line is the same as defining a function-
// just as a one liner on the interpreter.
y = feval(x, f)
Both approaches will yield the same result.
With Scilab ≥ 6.1.1, simply
x = (-6:1:6)';
plot(x, 1./(1+exp(-x)))