1
$\begingroup$

I've got a mathematica notebook that uses RK4 to successively approximate solutions to a vector valued DE. The output is a nested list similar to:

solutions := {{a[t0],b[t0],c[t0],d[t0]},{a[t0+dt],b[t0+dt],c[t0+dt],d[t0+dt]},...}

The list that I want to use as the x-axis is simply {t0,t0+dt,t0+2dt,...}, and I want to plot it against the a column of my solutions matrix. Perhaps I'm missing it, but I couldn't get ParametricPlot to work with these lists.

Here's a link to my notebook: http://pastebin.com/ZK0pUBSz

Sample data:

t:={0,20,40,60,80,100,120,140,160,180,200}

y:={200.,177.847,162.025,150.607,142.306,136.239,131.788,128.518,126.111,124.341,123.04}

Desired output is a plot with y as the dependent variable and t as the independent variable.


(edit: Solved! I had previously defined solutions as a matrix without any argument, and when I tried to turn it into something that took an argument, the kernel threw up errors. Solved by running a simple Remove@solutions.)

An an extra aside, my end goal is to see the effect of step size on solution resolution. I tried to accomplish this using the following code:

Tlist[step_] := 
Transpose[{Range[0, tmax, step], Part[solutions[step], All, 1]}];
ListLinePlot[{TList[.1], Tlist[.5], Tlist[1]}]

I'm trying to create a plot with each step size using Tlist, but the evaluation fails with write protection. Maybe this isn't the right way to tackle this problem in Mathematica, and I'm taking too much of a procedural approach. Any guidance on this would be welcome!

Here's the code for solutions:

RungeKutta[func_List, yinit_List, y_List, step_] := 
 Module[{k1, k2, k3, k4}, 
  k1 = step N[func /. MapThread[Rule, {y, yinit}]];
  k2 = step N[func /. MapThread[Rule, {y, k1/2 + yinit}]];
  k3 = step N[func /. MapThread[Rule, {y, k2/2 + yinit}]];
  k4 = step N[func /. MapThread[Rule, {y, k3 + yinit}]];
  yinit + Total[{k1, 2 k2, 2 k3, k4}]/6]
solutions[step_] := 
 NestList[RungeKutta[func, #, y, step] &, N[yinit], Round[tmax/step]]

RungeKutta code from here: Solving a system of ODEs with the Runge-Kutta method

$\endgroup$
13
  • $\begingroup$ Welcome! It would be easier to help you if you included some actual data to plot with, but I suspect Thread or MapThread or Transpose may come in handy here. $\endgroup$
    – Yves Klett
    Commented Oct 6, 2015 at 17:05
  • $\begingroup$ I added a link to the notebook, sorry about that! I'll look at those functions! $\endgroup$ Commented Oct 6, 2015 at 17:08
  • $\begingroup$ No problem! If you post a small sample (or just bogus data with the same structure) here, you will get more answers. This will be quicker to browse, more convenient and secure for users to access, and permanent (your link may vanish). $\endgroup$
    – Yves Klett
    Commented Oct 6, 2015 at 17:12
  • $\begingroup$ Okay, I'll add some in. $\endgroup$ Commented Oct 6, 2015 at 17:12
  • 1
    $\begingroup$ @ijustlovemath It would be very nice to give a link to the author of the RungeKutta code. solving-a-system-of-odes-with-the-runge-kutta-method RunnyKine $\endgroup$
    – user31001
    Commented Oct 6, 2015 at 19:19

1 Answer 1

3
$\begingroup$

Suppose xColumn is the column of x-values and yColumn the y-values. Then - as pointed to by YvesKlett - you could use ListPlot[Transpose[{xColumn,yColumn}]], for instance.

In your case:

xColumn=Table[t0+i*dt,{i,0,n}];
yColumn=solutions[[All,1]];

Here, n clearly has to be adjusted to your code, depending on how long your time interval is. Then use for instance the ListPlot command from above with option Joined->True or use ListLinePlot instead.

$\endgroup$
3
  • $\begingroup$ I see. So here, we transpose the nested list to make it an appropriate list for listplot? Thanks so much for the clarification $\endgroup$ Commented Oct 6, 2015 at 17:28
  • $\begingroup$ Well, since you want to plot only the first column of your solution matrix you first extract it via solutions[[All,1]]. Then you essentially form the x-y tuples using Transpose so that it is in the required form for the plot commands. Glad I could help $\endgroup$
    – Lukas
    Commented Oct 6, 2015 at 17:31
  • $\begingroup$ Would it be possible to make that Transpose a function of step size, for plotting multiple plots over varying step sizes? $\endgroup$ Commented Oct 6, 2015 at 17:52

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