1
$\begingroup$

My question is trivial but I don't seem to be able to get the answer.

Suppose that I do a numerical computation (division of some numbers that come from the numerical solution of an NDSolve or something else) and I want to assign them in a variable so that I can use it latter for plotting, e.g. p1=Abs[...]. The output of the p1 is p1={0.005} with brackets something that it makes it impossible to do the following plotting: ListPlot[{1,p1}] any idea what is happening and why it is happening?

Cheers

$\endgroup$

closed as off-topic by Johu, dr.blochwave, rhermans, MarcoB, Bob Hanlon Oct 3 '18 at 21:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Johu, dr.blochwave, rhermans, MarcoB, Bob Hanlon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Perhaps ListPlot[{1,p1[[1]]}] is what you are looking for. $\endgroup$ – Ulrich Neumann Sep 21 '18 at 12:42
  • $\begingroup$ Yes, that's true, this way it is correctly plotted, but I don't get why this happens (or it has to happen that way). Could you help? $\endgroup$ – hal Sep 21 '18 at 12:46
  • 2
    $\begingroup$ It would be helpful, if you show your code. Probably you don't want to use ListPlot for plotting only one point? $\endgroup$ – Ulrich Neumann Sep 21 '18 at 12:49
  • $\begingroup$ No of course not. I will try to sketch what it does because it is a long one. NDSolve[...,as] which as is the variable in the differential equation. Now I do the following assignement: p1=as[30] and the output is {0.005}. Then I do it for a different value p2=as[40] and the output is {0.004}. And this is the point that I get confused. Why the p1,p2,..etc come with brackets. Also this makes the ListPlot impossible unless I use p1[[1]]. Firstly, why? Secondly, is there a way to avoid it? Thirdly, what do the [[]] mean? Thank you very much! $\endgroup$ – hal Sep 21 '18 at 13:01
  • $\begingroup$ See First and Part in the documentation. The reason the list appears is because many Solve functions can acquire multiple solutions. In this case, the number of solutions is 1, so the length of the list is 1. You can instruct Mathematica to only use the first (and in this case only) solution by taking the appropriate part. $\endgroup$ – eyorble Sep 21 '18 at 17:33
1
$\begingroup$

Using NDSolveValue is an easy possibility to solve your problem:

sol = NDSolveValue [{x'[t] + x[t] == 0, x[0] == 1}, x, {t, 0, 5}]
pi = Table[{t, sol[t]}, {t, 0, 5, .25 }]
ListPlot[pi]

enter image description here

Of course you can also plot continuously

Plot[sol[t],{t,0,5}]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.