I’m trying to plot points that I’ve created in a table in mathematica but for some reason one component of my points seems to have double braces around it while the other only has one as below:
{{x},y},{{x1},y1}....{{xn},yn}
and list plot will not recognize these as points and will not plot them.
Here is my mathematica code:
Remove["Global`*"]
b = .1;
w = 1;
Period = 1;
tstep = 2 Pi/Period;
s = NDSolve[{x''[t] + b x'[t] - x[t] + x[t]^3 - .5 Cos[w t] == 0,
x'[0] == 0, x[0] == 0}, x[t], {t, 0, 1000}, MaxSteps -> Infinity];
x[t_] = x[t] /. s
data = Table[Evaluate[{x'[t], .5}], {t, 0, 1000, tstep}]
ListPlot[data]
I’ve also tried using the command
ListPlot[Flatten[Table[Evaluate[{x'[t], .5}], {t, 0, 1000, tstep}]]]
to no avail as well as
ListPlot[Table[Evaluate[{Flatten[x'[t]], .5}], {t, 0, 1000, tstep}]]]
How can I remove the {}?
My colleagues are correct, but I think there is more that can be said. First, to your actual question. The output of
NDSolveis a list of the formwhere the second and subsequent replacement rules are only there if more than one solution is present. I have never encountered a case using
NDSolvewhere that is true, but it makes the answer consistent withSolve, where multiple solutions is not uncommon. Therefor, with only one solution, you have a double list, i.e.As per Mr. Wizard, you can use
First, or you can usePart, i.e.which is my preferred method, although it is slightly more difficult to read and may obscure your intent. You should be aware that the
InterpolatingFunctionthatNDSolvereturns is a function, and it will accept variables directly. So, the variables on the left hand side of the declarationsand from Belisarius
are superfluous at best, and the second one requires the replacement to occur every time
xris used. Instead, you can declareand then writing
x[t]afterwards will returnIntepolatingFunction[t], exactly like you want. Then, as Belisarius points out, you can use it, or its derivative, inPlotdirectly, instead of first building a table of values and feeding them intoListPlot.Edit: when I first posted this, I didn’t notice a quirk with
NDSolve. If you explicitly solve forx[t]notx, thenNDSolvereturnsInterpolatingFunction[...][t], but if you just solve forxyou get what I posted. This quirk allows both the OP’s and Belisarius’s solutions to function, otherwise the replacement shouldn’t occur.