julia vector differential equation: plot only the first n components of the vector - plot

I am integrating a vector differential equation in Julia (the number of components is of the order of magnitude of 50). When I plot this solution, obviously this plot will be very crowded if I plot all the components, so I want to plot only let's say the first 10 components. I can't find how to do this. Also, instead of plotting the output for each component, I would like to plot not the output of the integration at each time step, but the hyperbolic tangent of this. I also don't find how to do this manipulation. I think the key problem is that I don't understand/find what kind of object the solution of a differential equation is in julia.
Here is a minimal working example that integrates the differential equation and plots the whole solution.
using DifferentialEquations
using Plots
using LinearAlgebra
N=50
J=0.18*randn(Float64,N,N)
g=1
function hDerivative(timederiv,h,p,t)
for i=1:length(h)
timederiv[i] = -h[i]
for j=1:length(h)
timederiv[i]=+timederiv[i]+J[i,j]*tanh(g*h[j])
end
end
end
function pltTimeVolution()
hinit=rand(Float64,N)
tspan=(0.0,50)
prob = ODEProblem(hDerivative,hinit,tspan)
sol=solve(prob)
plot(sol)
print(sol)
end
pltTimeVolution()

For help, take a look at the solution handling page: http://diffeq.sciml.ai/latest/basics/solution.html . The solution is both an abstract array and a continuous function. So you can for example get a continuous solution at 10000 evenly spaced time points like t=range(tspan[1],stop=tspan[2],length=10000), do A = sol(t) and that will give a 50x10000 timeseries of each solution at evenly spaced time points, and then sol[i,:] would be the timeseries of the ith variable at the time points t. You can use that to build the arrays you want an plot them. Full code like this:
t=range(tspan[1],stop=tspan[2],length=10000)
A = sol(t)
ts10 = sol[10,:]
Another way to do this is to solve with something like saveat=t, in which case the solution is an array which saves at those time points. This looks like:
t=range(tspan[1],stop=tspan[2],length=10000)
sol=solve(prob,saveat=t)
ts10 = sol[10,:]
Or you can make use of the plot recipe which does this kind of handling internally. Take a look at http://diffeq.sciml.ai/latest/basics/plot.html#Choosing-Variables-1 . You can do things like plot the first variable and then lay over the plot of the 10th variable like:
plot(sol,vars=(0,1))
plot!(sol,vars=(0,10))
The ! means its the mutating plot function, i.e. modify the previous plot by adding a new series. Then as noted in the documentation there are shortcuts, like
plot(sol,vars = [1, 3, 4])
will plot the 1st, 3rd, and 4th variables as functions of time. In this form, you can also specify what kind of transformation you want as a function. That's mentioned in the same place in the documentation. For example, the two-dimensional plot of time vs tanh would be the transformation function f=(t,y)->(t,tanh(y)), i.e. how you take in the 2D variable and spit out the 2D variable to plot is just pass through the first and tanh the second. Then using this, you'd use the plot command:
f=(t,y)->(t,tanh(y))
plot(sol,vars = (f,0,5))
to plot the tanh of the 5th variable against time.
These are a few different ways to manipulate the solution argument and generate the plots you need. Pick the one that works best for you.

Related

How can i plot an array of numbers in WxMaxima?

I need to plot all the elements of a[n] 0<=n<=30. Is that possible in maxima?
These are the numbers i need to plot
by the way, you can help others help you by typing your work so far into your question. That way people can see it immediately instead of having to go look for it.
About plotting the points, remember that a[n] as you have defined it is a so-called array function instead of a list or array. The plotting functions can work with lists, I believe. So you need to evaluate your function at a number of points and then plot the points.
Something like: mypoints: makelist([n, a[n]], n, 1, 30); to generate the points. Then with the points in hand, there are different ways to plot them. You can try: plot2d([discrete, mypoints]); There are other ways; see wxdraw2d for example.

Value for mixture distributions crossing using 'mixdist' in R

I have a plot (below) generated using the package "mixdist" and would like to know the exact value at which the two distributions cross one another rather than just estimating from the plot. I haven't come across this in any of the output information. Can this be obtained through mixdist?
Thanks for any help
use locator() function to click on the point in the graph, followed by 'esc' key to give the values

Cross-Recurrence plots in R (with or without ggplot)

I have different time-series corresponding to different individuals and their location within a building (a categorical variable -- more like a room name).
I would like to study the similarity in movement of different individuals by something like cross-recurrence plots, where the two time-series correspond to the two axes and the actual points correspond to the presence/absence of individuals in the same room.
Has anyone tried doing such plots in R or while using ggplot? Any help would be great!
I haven't used this routine. I used only d2 dimension and Lyapunov exponent for EEG but this package Tisean (RTisean for your case) has a routine ['recurr'] that returns the specific plot.
This link has a nice wrap up of tutorials and links
Edited:
In this link you can find a nice example of application of recurrence plot.
The return variables of function recur(and similar functions of other packages) you can access after putting $ after the dataset (like database)
and you can access them inside in ggplot function and applying the appropriate aes.

How to easily visualize a matrix?

When doing matrix operations, I would like to be able to see what the results of my calculations are, at least to get a rough idea of the nature of the matrices going in and coming out of the operation.
How can I plot a matrix of real numbers, so that the x axis represents columns, the y represents rows, and the color or size of a point represents the cell value?
Ultimately, I would like to display multiple plots, e.g. the right and left hand sides of an equation.
Here is some example code:
a <- matrix(rnorm(100), ncol = 10)
b <- diag(1,10)
c <- a*b
par(mfrow = c(1,3))
plot.matrix.fn <- function(m) {
#enter answer to this question here
}
lapply(list(a,b,c), plot.matrix.fn)
update: since posting this question, I found that there are some great examples here: What techniques exists in R to visualize a "distance matrix"?
You could try something like (adjusting the parameters to your particular needs)
image(t(m[nrow(m):1,] ), axes=FALSE, zlim=c(-4,4), col=rainbow(21))
producing something like
See ?image for a single plot (note that row 1 will be at the bottom) and ?rasterImage for adding 1 or more representations to an existing plot. You may want to do some scaling or other transformation on the matrix first.
Not an answer but a longer comment.
I've been working on a package to plot matrices using grid.raster, but it's not quite ready for release yet. Your example would read,
library(gridplot)
row_layout(a, b, c)
I found that writing custom functions was probably easier than tweaking 10s of parameters in lattice or base graphics, and ggplot2 lacks some control over the axes.
However, writing graphics functions from scratch also means reinventing non-trivial things like layout and positioning; hopefully Hadley's scales and guides packages can make this easier. I'll add the functions to gridExtra when the overall design seems sound and more stable.

R: update plot [xy]lims with new points() or lines() additions?

Background:
I'm running a Monte Carlo simulation to show that a particular process (a cumulative mean) does not converge over time, and often diverges wildly in simulation (the expectation of the random variable = infinity). I want to plot about 10 of these simulations on a line chart, where the x axis has the iteration number, and the y axis has the cumulative mean up to that point.
Here's my problem:
I'll run the first simulation (each sim. having 10,000 iterations), and build the main plot based on its current range. But often one of the simulations will have a range a few orders of magnitude large than the first one, so the plot flies outside of the original range. So, is there any way to dynamically update the ylim or xlim of a plot upon adding a new set of points or lines?
I can think of two workarounds for this: 1. store each simulation, then pick the one with the largest range, and build the base graph off of that (not elegant, and I'd have to store a lot of data in memory, but would probably be laptop-friendly [[EDIT: as Marek points out, this is not a memory-intense example, but if you know of a nice solution that'd support far more iterations such that it becomes an issue (think high dimensional walks that require much, much larger MC samples for convergence) then jump right in]]) 2. find a seed that appears to build a nice looking version of it, and set the ylim manually, which would make the demonstration reproducible.
Naturally I'm holding out for something more elegant than my workarounds. Hoping this isn't too pedestrian a problem, since I imagine it's not uncommon with simulations in R. Any ideas?
I'm not sure if this is possible using base graphics, if someone has a solution I'd love to see it. However graphics systems based on grid (lattice and ggplot2) allow the graphics object to be saved and updated. It's insanely easy in ggplot2.
require(ggplot2)
make some data and get the range:
foo <- as.data.frame(cbind(data=rnorm(100), numb=seq_len(100)))
make an initial ggplot object and plot it:
p <- ggplot(as.data.frame(foo), aes(numb, data)) + layer(geom='line')
p
make some more data and add it to the plot
foo <- as.data.frame(cbind(data=rnorm(200), numb=seq_len(200)))
p <- p + geom_line(aes(numb, data, colour="red"), data=as.data.frame(foo))
plot the new object
p
I think (1) is the best option. I actually don't think this isn't elegant. I think it would be more computationally intensive to redraw every time you hit a point greater than xlim or ylim.
Also, I saw in Peter Hoff's book about Bayesian statistics a cool use of ts() instead of lines() for cumulative sums/means. It looks pretty spiffy:

Resources