How to plot solution from ODEProblem

I am trying to plot the solution vector sol from the solver of an ODEProblem which is a 1432 element vector of 31x1 vectors. To do this, I attempted

 y= sol.t 
 z= [abs(sol.u[i][j])^2  for j=1:N₁, i=1:size(sol)[2]]
 surface(x, y, z)

where N₁=31

However, this never finishes compiling. What is the proper way of plotting my solution? Below is the full MWE.

using DifferentialEquations, LinearAlgebra, Plots, SparseArrays
N₁=31  # Number of waveguides
γ=-1  # Nonlinear term strength parameter
h=0.6 # Grid spacing 
centerGrid     = (N₁-1)/2;

x = -centerGrid:centerGrid;

# Coefficient matrix of second-order centered-difference operator (δ²u)ₙ
M           = spdiagm(-1 => fill(1,N₁-1), 0 => fill(-2,N₁), 1 => fill(1,N₁-1))
M[N₁,1]     = 1; # Periodic boundary conditions
M[1,N₁]     = 1;

# RHS of DNLS. The solution vector u is a N₁x1 complex vector
g₁(u,p,t)   = 1*im*(p[1]*Tridiagonal(fill(1,N₁-1), fill(-2,N₁), fill(1,N₁-1))*u + γ*u.^3)

u0  = sech.(x);
u0  = map(Complex,u0)
tspan = (0.0,100)

prob = ODEProblem(g₁,u0,tspan, [h])

sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)

 y= sol.t 
 z= [abs(sol.u[i][j])^2  for j=1:N₁, i=1:size(sol)[2]]
 surface(x, y, z)

Your z is a 31x1432 Matrix(Float64) . Hence to get the surface plotted, y must be a vector of length 31, and x of length 1432, but your length(x) and length(y) are permuted.

surface(sol.t, x, z)

is the right call of surface.

1 Like

This does not compile for me in a notebook environment. In fact, I cannot interrupt the compile and must close the IDE (VScode).

Strange… without plotlyjs() it compiles. but I want plotlyjs()… Why does it not compile with this backend?

I used plotlyjs(), and the colormap matter:
surface(sol.t, x, z, color=:matter)

The issue seems to be using a notebook environment. Using plotlyjs() as you wrote works as intended.